From dbc7283a73cc1eeea06cc02eedcd54a232dec541 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Tue, 11 Sep 2018 17:56:54 +0300
Subject: [PATCH 0001/1031] feat: avoid unnecessary parens around lookup nodes
(#598)
---
src/needs-parens.js | 4 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 149 +++++++++++++++++-
tests/parens/lookups.php | 69 ++++++++
.../variable/__snapshots__/jsfmt.spec.js.snap | 6 +-
4 files changed, 222 insertions(+), 6 deletions(-)
create mode 100644 tests/parens/lookups.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 0a760a005..773af4ef0 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -149,6 +149,10 @@ function needsParens(path) {
return true;
}
+ return false;
+ case "propertylookup":
+ case "staticlookup":
+ case "offsetlookup":
return false;
case "bin": {
if (["pre", "post"].includes(parent.kind)) {
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index c9eed8723..5a12e74d3 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -682,7 +682,7 @@ $var = (object) array('1' => 'foo');
$var = (object) ['1' => 'foo'];
$var = ((object) 'ciao')->scalar;
$var = ((object) array('test'))->{'0'};
-$var = (((object) array('test'))->{'0'});
+$var = ((object) array('test'))->{'0'};
$var = (array) new B();
$var = (array) new B();
$var = (array) new B();
@@ -913,6 +913,149 @@ $a->c();
`;
+exports[`lookups.php 1`] = `
+bar;
+($var->bar);
+
+$var->bar();
+($var->bar());
+
+$var::bar();
+($var::bar());
+
+$var = $var->bar;
+$var = ($var->bar);
+$var = $var->bar->foo;
+$var = ($var->bar)->foo;
+$var = ($var->bar->foo);
+$var = (($var->bar)->foo);
+
+$var = $var::foo();
+$var = ($var::foo());
+$var = $var::foo()::bar();
+$var = ($var::foo()::bar());
+
+$var = $var->bar();
+$var = ($var->bar());
+$var = $var->bar()->foo();
+$var = ($var->bar())->foo();
+$var = ($var->bar()->foo());
+$var = (($var->bar())->foo());
+
+$var = ((object) ($var->bar())->foo());
+$var = (object) (($var->bar())->foo());
+
+$var = $var[0];
+$var = $var[0][1];
+$var = ($var[0]);
+$var = ($var[0][1]);
+$var = $var[0]->foo;
+$var = ($var[0])->foo;
+$var = ($var[0][1])->foo;
+$var = ($var[0])[1]->foo;
+$var = (($var[0])[1])->foo;
+$var = $var[0]->foo();
+$var = ($var[0])->foo();
+$var = ($var[0][1])->foo();
+$var = ($var[0])[1]->foo();
+$var = (($var[0])[1])->foo();
+
+$var = $var[0]->foo()->baz;
+$var = ((($var[0])->foo())->baz);
+
+$var = (new Foo())->bar;
+$var = (new Foo())->bar();
+$var = (new Foo())[1];
+
+$var = $var->bar()();
+$var = ($var->bar())();
+$var = ($var->bar()());
+$var = (($var->bar())());
+
+$var = $var::bar()();
+$var = ($var::bar())();
+$var = ($var::bar()());
+$var = (($var::bar())());
+
+$var = ($var)->bar;
+$var = (($var)->bar);
+$var = ($var)->bar();
+$var = (($var)->bar());
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+bar;
+$var->bar;
+
+$var->bar();
+$var->bar();
+
+$var::bar();
+$var::bar();
+
+$var = $var->bar;
+$var = $var->bar;
+$var = $var->bar->foo;
+$var = $var->bar->foo;
+$var = $var->bar->foo;
+$var = $var->bar->foo;
+
+$var = $var::foo();
+$var = $var::foo();
+$var = $var::foo()::bar();
+$var = $var::foo()::bar();
+
+$var = $var->bar();
+$var = $var->bar();
+$var = $var->bar()->foo();
+$var = $var->bar()->foo();
+$var = $var->bar()->foo();
+$var = $var->bar()->foo();
+
+$var = (object) $var->bar()->foo();
+$var = (object) $var->bar()->foo();
+
+$var = $var[0];
+$var = $var[0][1];
+$var = $var[0];
+$var = $var[0][1];
+$var = $var[0]->foo;
+$var = $var[0]->foo;
+$var = $var[0][1]->foo;
+$var = $var[0][1]->foo;
+$var = $var[0][1]->foo;
+$var = $var[0]->foo();
+$var = $var[0]->foo();
+$var = $var[0][1]->foo();
+$var = $var[0][1]->foo();
+$var = $var[0][1]->foo();
+
+$var = $var[0]->foo()->baz;
+$var = $var[0]->foo()->baz;
+
+$var = (new Foo())->bar;
+$var = (new Foo())->bar();
+$var = (new Foo())[1];
+
+$var = $var->bar()();
+$var = $var->bar()();
+$var = $var->bar()();
+$var = $var->bar()();
+
+$var = $var::bar()();
+$var = $var::bar()();
+$var = $var::bar()();
+$var = $var::bar()();
+
+$var = $var->bar;
+$var = $var->bar;
+$var = $var->bar();
+$var = $var->bar();
+
+`;
+
exports[`namespace.php 1`] = `
bar()->foo();
$var = (new foo())->bar()->foo();
$var = (new foo())->bar()->foo();
$var = (new foo())->bar()->foo()[0];
-$var = ((new foo())->bar()->foo()[0])[1];
+$var = (new foo())->bar()->foo()[0][1];
$var = (new foo())
->bar()
->foo()
@@ -1380,7 +1523,7 @@ $a = 1 ** +$a;
$var = call(+$a);
$var = call(+$a);
-$var = +($foo->bar);
+$var = +$foo->bar;
$var = +$var || +$var;
$var = +$var || +$var;
diff --git a/tests/parens/lookups.php b/tests/parens/lookups.php
new file mode 100644
index 000000000..493ae7095
--- /dev/null
+++ b/tests/parens/lookups.php
@@ -0,0 +1,69 @@
+bar;
+($var->bar);
+
+$var->bar();
+($var->bar());
+
+$var::bar();
+($var::bar());
+
+$var = $var->bar;
+$var = ($var->bar);
+$var = $var->bar->foo;
+$var = ($var->bar)->foo;
+$var = ($var->bar->foo);
+$var = (($var->bar)->foo);
+
+$var = $var::foo();
+$var = ($var::foo());
+$var = $var::foo()::bar();
+$var = ($var::foo()::bar());
+
+$var = $var->bar();
+$var = ($var->bar());
+$var = $var->bar()->foo();
+$var = ($var->bar())->foo();
+$var = ($var->bar()->foo());
+$var = (($var->bar())->foo());
+
+$var = ((object) ($var->bar())->foo());
+$var = (object) (($var->bar())->foo());
+
+$var = $var[0];
+$var = $var[0][1];
+$var = ($var[0]);
+$var = ($var[0][1]);
+$var = $var[0]->foo;
+$var = ($var[0])->foo;
+$var = ($var[0][1])->foo;
+$var = ($var[0])[1]->foo;
+$var = (($var[0])[1])->foo;
+$var = $var[0]->foo();
+$var = ($var[0])->foo();
+$var = ($var[0][1])->foo();
+$var = ($var[0])[1]->foo();
+$var = (($var[0])[1])->foo();
+
+$var = $var[0]->foo()->baz;
+$var = ((($var[0])->foo())->baz);
+
+$var = (new Foo())->bar;
+$var = (new Foo())->bar();
+$var = (new Foo())[1];
+
+$var = $var->bar()();
+$var = ($var->bar())();
+$var = ($var->bar()());
+$var = (($var->bar())());
+
+$var = $var::bar()();
+$var = ($var::bar())();
+$var = ($var::bar()());
+$var = (($var::bar())());
+
+$var = ($var)->bar;
+$var = (($var)->bar);
+$var = ($var)->bar();
+$var = (($var)->bar());
diff --git a/tests/variable/__snapshots__/jsfmt.spec.js.snap b/tests/variable/__snapshots__/jsfmt.spec.js.snap
index b5e1a6ab5..335b704ce 100644
--- a/tests/variable/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/variable/__snapshots__/jsfmt.spec.js.snap
@@ -91,9 +91,9 @@ $var = $foo->{$bar['baz']}();
// $var = Foo::{$bar['baz']}();
$var = $$foo['bar']['baz'];
-$var = ($foo->$bar)['baz'];
-$var = ($foo->$bar)['baz']();
-$var = (Foo::$bar)['baz']();
+$var = $foo->$bar['baz'];
+$var = $foo->$bar['baz']();
+$var = Foo::$bar['baz']();
$$$$$$$$$$var = "I like playing in the park";
From a97fd454dcab3dccb3007c087dd2d5337ae2a573 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Tue, 11 Sep 2018 18:20:17 +0300
Subject: [PATCH 0002/1031] feat: avoid unnecessary parens around `clone` nodes
(#600)
---
src/needs-parens.js | 1 +
src/util.js | 3 +-
tests/clone/__snapshots__/jsfmt.spec.js.snap | 11 +++
tests/clone/single.php | 3 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 79 +++++++++++++++++++
tests/parens/clone.php | 31 ++++++++
tests/parens/new.php | 5 ++
7 files changed, 132 insertions(+), 1 deletion(-)
create mode 100644 tests/clone/single.php
create mode 100644 tests/parens/clone.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 773af4ef0..b6ea87094 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -67,6 +67,7 @@ function needsParens(path) {
default:
return false;
}
+ case "clone":
case "new": {
if (isLookupNode(parent)) {
return true;
diff --git a/src/util.js b/src/util.js
index 69b1ca907..f1144505b 100644
--- a/src/util.js
+++ b/src/util.js
@@ -303,7 +303,8 @@ function lineShouldEndWithSemicolon(path) {
"nowdoc",
"encapsed",
"variable",
- "cast"
+ "cast",
+ "clone"
];
if (node.kind === "traituse") {
return !node.adaptations;
diff --git a/tests/clone/__snapshots__/jsfmt.spec.js.snap b/tests/clone/__snapshots__/jsfmt.spec.js.snap
index d9312f481..b71c90a8f 100644
--- a/tests/clone/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/clone/__snapshots__/jsfmt.spec.js.snap
@@ -21,3 +21,14 @@ $OneSecAgo = (clone $Now)->veryVeryVeryVeryVeryVeryVeryVeryLongMethod(
);
`;
+
+exports[`single.php 1`] = `
+foo;
+$var = (clone $a->foo);
+$var = (clone $a)->foo();
+
+$var = (clone foo())->bar()->foo();
+$var = ((clone foo())->bar())->foo();
+$var = (((clone foo())->bar())->foo());
+$var = (((clone foo())->bar())->foo())[0];
+$var = ((((clone foo())->bar())->foo())[0])[1];
+$var = (((clone foo())->bar())->foo())->baz();
+$var = (clone $foo())->bar;
+$var = (clone $bar->y)->x;
+$var = (clone $foo)[0];
+$var = (clone $foo)[0]['string'];
+
+$var = clone $a->b;
+$var = clone $a->b();
+$var = (clone $a)->b();
+$var = ((clone $a)->b());
+
+$var = (clone ($var));
+$var = (clone($var));
+$var = (clone($var->foo));
+$var = (clone($var->foo))->foo;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+foo;
+$var = clone $a->foo;
+$var = (clone $a)->foo();
+
+$var = (clone foo())->bar()->foo();
+$var = (clone foo())->bar()->foo();
+$var = (clone foo())->bar()->foo();
+$var = (clone foo())->bar()->foo()[0];
+$var = (clone foo())->bar()->foo()[0][1];
+$var = (clone foo())
+ ->bar()
+ ->foo()
+ ->baz();
+$var = (clone $foo())->bar;
+$var = (clone $bar->y)->x;
+$var = (clone $foo)[0];
+$var = (clone $foo)[0]['string'];
+
+$var = clone $a->b;
+$var = clone $a->b();
+$var = (clone $a)->b();
+$var = (clone $a)->b();
+
+$var = clone $var;
+$var = clone $var;
+$var = clone $var->foo;
+$var = (clone $var->foo)->foo;
+
+`;
+
exports[`continue.php 1`] = `
bar;
$var = (new $bar->y)->x;
$var = (new foo)[0];
$var = (new foo)[0]['string'];
+
+$var = new $a->b;
+$var = new $a->b();
+$var = (new $a)->b();
+$var = ((new $a)->b());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bar;
$var = (new $bar->y())->x;
$var = (new foo())[0];
$var = (new foo())[0]['string'];
+$var = new $a->b();
+$var = new $a->b();
+$var = (new $a())->b();
+$var = (new $a())->b();
`;
diff --git a/tests/parens/clone.php b/tests/parens/clone.php
new file mode 100644
index 000000000..95f66c0b5
--- /dev/null
+++ b/tests/parens/clone.php
@@ -0,0 +1,31 @@
+foo;
+$var = (clone $a->foo);
+$var = (clone $a)->foo();
+
+$var = (clone foo())->bar()->foo();
+$var = ((clone foo())->bar())->foo();
+$var = (((clone foo())->bar())->foo());
+$var = (((clone foo())->bar())->foo())[0];
+$var = ((((clone foo())->bar())->foo())[0])[1];
+$var = (((clone foo())->bar())->foo())->baz();
+$var = (clone $foo())->bar;
+$var = (clone $bar->y)->x;
+$var = (clone $foo)[0];
+$var = (clone $foo)[0]['string'];
+
+$var = clone $a->b;
+$var = clone $a->b();
+$var = (clone $a)->b();
+$var = ((clone $a)->b());
+
+$var = (clone ($var));
+$var = (clone($var));
+$var = (clone($var->foo));
+$var = (clone($var->foo))->foo;
diff --git a/tests/parens/new.php b/tests/parens/new.php
index 8ab38a9c5..edb0c2ced 100644
--- a/tests/parens/new.php
+++ b/tests/parens/new.php
@@ -47,3 +47,8 @@ public function log($msg)
$var = (new $bar->y)->x;
$var = (new foo)[0];
$var = (new foo)[0]['string'];
+
+$var = new $a->b;
+$var = new $a->b();
+$var = (new $a)->b();
+$var = ((new $a)->b());
From 6519e663e3d9cf6dfae9b9edc510f83198e30570 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Tue, 11 Sep 2018 18:38:32 +0300
Subject: [PATCH 0003/1031] feat: avoid unnecessary parens around `isset` and
`empty` nodes (#601)
---
src/needs-parens.js | 2 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 52 +++++++++++++++++++
tests/parens/empty.php | 9 ++++
tests/parens/isset.php | 11 ++++
4 files changed, 74 insertions(+)
create mode 100644 tests/parens/empty.php
create mode 100644 tests/parens/isset.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index b6ea87094..8f250ae1d 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -130,6 +130,8 @@ function needsParens(path) {
case "encapsed":
case "nowdoc":
case "variable":
+ case "isset":
+ case "empty":
return false;
case "string":
case "array":
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index e118e2ca3..2def9b6fe 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -983,6 +983,58 @@ $a->c();
`;
+exports[`empty.php 1`] = `
+
Date: Tue, 11 Sep 2018 19:07:17 +0300
Subject: [PATCH 0004/1031] feat: avoid unnecessary parens around silent node
(#602)
---
src/needs-parens.js | 1 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 19 +++++++++++++++++++
tests/parens/silent.php | 7 +++++++
3 files changed, 27 insertions(+)
create mode 100644 tests/parens/silent.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 8f250ae1d..1a25ef6ba 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -132,6 +132,7 @@ function needsParens(path) {
case "variable":
case "isset":
case "empty":
+ case "silent":
return false;
case "string":
case "array":
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 2def9b6fe..da7dbb768 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1551,6 +1551,25 @@ return 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
`;
+exports[`silent.php 1`] = `
+
Date: Tue, 11 Sep 2018 19:48:34 +0300
Subject: [PATCH 0005/1031] feat: avoid unnecessary parens around `closure`
node (#603)
---
src/needs-parens.js | 6 ++
src/printer.js | 1 +
.../closure/__snapshots__/jsfmt.spec.js.snap | 9 +++
tests/closure/closure.php | 3 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 72 +++++++++++++++++++
tests/parens/closure.php | 20 ++++++
6 files changed, 111 insertions(+)
create mode 100644 tests/parens/closure.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 1a25ef6ba..e0f2603d8 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -124,6 +124,12 @@ function needsParens(path) {
default:
return false;
}
+ case "closure":
+ if (parent.kind === "call" && name === "what" && parent.what === node) {
+ return true;
+ }
+
+ return false;
case "boolean":
case "number":
case "magic":
diff --git a/src/printer.js b/src/printer.js
index dcd9be5b7..9e4850267 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1778,6 +1778,7 @@ function printNode(path, options, print) {
!node.body.comments;
return concat([
+ node.isStatic ? "static " : "",
"function ",
node.byref ? "&" : "",
printArgumentsList(path, options, print),
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index 03642573f..abcac1f0c 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -132,6 +132,9 @@ $var = function &() {
return $collection;
};
+
+$func = static function() {};
+$fn = function &() use (&$value) { return $value; };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo)->foo;
`;
+exports[`closure.php 1`] = `
+
Date: Tue, 11 Sep 2018 20:11:04 +0300
Subject: [PATCH 0006/1031] feat: avoid unnecessary parens for `exit` node
(#604)
---
src/needs-parens.js | 1 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 33 +++++++++++++++++++
tests/parens/exit.php | 12 +++++++
3 files changed, 46 insertions(+)
create mode 100644 tests/parens/exit.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index e0f2603d8..b638c96f1 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -139,6 +139,7 @@ function needsParens(path) {
case "isset":
case "empty":
case "silent":
+ case "exit":
return false;
case "string":
case "array":
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 224bd576d..dc0e3da94 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1079,6 +1079,39 @@ if (empty($var)) {
`;
+exports[`exit.php 1`] = `
+
Date: Wed, 12 Sep 2018 12:25:41 +0300
Subject: [PATCH 0007/1031] test: return parens cases (#612)
---
tests/parens/__snapshots__/jsfmt.spec.js.snap | 5 +++++
tests/parens/return.php | 3 +++
2 files changed, 8 insertions(+)
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index dc0e3da94..e403ea1da 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1637,6 +1637,9 @@ return ('veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' . 'veryVeryVeryVery
return ($var1 + $var2);
return $var ? ($var1 ? 1 : 2) : ($var2 ? 3 : 4);
return ('veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' ? 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' : 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString');
+return static::class . '@' . $method;
+return ($this->customer->paymentService ?? null);
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
customer->paymentService ?? null;
`;
diff --git a/tests/parens/return.php b/tests/parens/return.php
index e5b300d91..309d9764e 100644
--- a/tests/parens/return.php
+++ b/tests/parens/return.php
@@ -8,3 +8,6 @@
return ($var1 + $var2);
return $var ? ($var1 ? 1 : 2) : ($var2 ? 3 : 4);
return ('veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' ? 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' : 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString');
+return static::class . '@' . $method;
+return ($this->customer->paymentService ?? null);
+
From b854e4576b084617666677220030b4dad270a38a Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Wed, 12 Sep 2018 12:25:51 +0300
Subject: [PATCH 0008/1031] feat: avoid parens around eval node (#613)
---
src/needs-parens.js | 1 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 31 +++++++++++++++++++
tests/parens/eval.php | 12 +++++++
3 files changed, 44 insertions(+)
create mode 100644 tests/parens/eval.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index b638c96f1..42787d532 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -140,6 +140,7 @@ function needsParens(path) {
case "empty":
case "silent":
case "exit":
+ case "eval":
return false;
case "string":
case "array":
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index e403ea1da..c64663ae4 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1079,6 +1079,37 @@ if (empty($var)) {
`;
+exports[`eval.php 1`] = `
+
Date: Wed, 12 Sep 2018 13:14:00 +0300
Subject: [PATCH 0009/1031] feat: avoid unnecessary parens for `print` node
(#616)
---
src/needs-parens.js | 6 ++
tests/parens/__snapshots__/jsfmt.spec.js.snap | 92 +++++++++++++++++++
tests/parens/print.php | 44 +++++++++
3 files changed, 142 insertions(+)
create mode 100644 tests/parens/print.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 42787d532..efd3608bc 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -165,6 +165,12 @@ function needsParens(path) {
case "propertylookup":
case "staticlookup":
case "offsetlookup":
+ return false;
+ case "print":
+ if (parent.kind === "bin") {
+ return true;
+ }
+
return false;
case "bin": {
if (["pre", "post"].includes(parent.kind)) {
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index c64663ae4..d2b5a61a4 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1551,6 +1551,98 @@ $var = call(--$var->_uuidCounter);
`;
+exports[`print.php 1`] = `
+" : print "$string_message\\n";
+$var = $var ? (print "$string_message
") : (print "$string_message\\n");
+$var = $var ? (print ("$string_message
")) : (print ("$string_message\\n"));
+
+print 1 . print(2) + 3; // 511
+print 1 . (print(2)) + 3; // 214
+
+print ($var || $var) && $var;
+print (($var || $var) && $var);
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+" : print "$string_message\\n";
+$var = $var ? print "$string_message
" : print "$string_message\\n";
+$var = $var ? print "$string_message
" : print "$string_message\\n";
+
+print 1 . (print 2 + 3); // 511
+print 1 . (print 2) + 3; // 214
+
+print ($var || $var) && $var;
+print ($var || $var) && $var;
+
+`;
+
exports[`program.php 1`] = `
" : print "$string_message\n";
+$var = $var ? (print "$string_message
") : (print "$string_message\n");
+$var = $var ? (print ("$string_message
")) : (print ("$string_message\n"));
+
+print 1 . print(2) + 3; // 511
+print 1 . (print(2)) + 3; // 214
+
+print ($var || $var) && $var;
+print (($var || $var) && $var);
From 8c953521cb96df656310728d8bfc5f356ac4b513 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Wed, 12 Sep 2018 13:43:57 +0300
Subject: [PATCH 0010/1031] test: echo parens (#619)
---
tests/parens/__snapshots__/jsfmt.spec.js.snap | 84 +++++++++++++++++++
tests/parens/echo.php | 38 +++++++++
2 files changed, 122 insertions(+)
create mode 100644 tests/parens/echo.php
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index d2b5a61a4..360c50e3f 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1055,6 +1055,90 @@ $a->c();
`;
+exports[`echo.php 1`] = `
+
Date: Wed, 12 Sep 2018 13:57:46 +0300
Subject: [PATCH 0011/1031] test: throw parens (#620)
---
tests/parens/__snapshots__/jsfmt.spec.js.snap | 39 +++++++++++++++++++
tests/parens/throw.php | 17 ++++++++
2 files changed, 56 insertions(+)
create mode 100644 tests/parens/throw.php
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 360c50e3f..3cd57cf12 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1887,6 +1887,45 @@ $var = @foo();
`;
+exports[`throw.php 1`] = `
+
Date: Wed, 12 Sep 2018 14:07:20 +0300
Subject: [PATCH 0012/1031] test: parens in `retif` node (#615)
---
tests/parens/__snapshots__/jsfmt.spec.js.snap | 2 ++
tests/parens/retif.php | 1 +
2 files changed, 3 insertions(+)
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 3cd57cf12..00c29763a 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1782,6 +1782,7 @@ $var = +($var ? 1 : 2);
$var = +(+$var ? 1 : 2);
$var = +($var++ ? 1 : 2);
$var = ((true ? 'true' : false) ? (true ? 'true' : false) : (true ? 'true' : false));
+$var = $var ? $var1 ? 1 : 2 : $var2 ? 3 : 4;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Wed, 12 Sep 2018 14:14:13 +0300
Subject: [PATCH 0013/1031] refactor: need parens code (#621)
---
src/needs-parens.js | 85 +++++++------------
tests/parens/__snapshots__/jsfmt.spec.js.snap | 76 +++++++++++++++++
tests/parens/include.php | 34 ++++++++
3 files changed, 139 insertions(+), 56 deletions(-)
create mode 100644 tests/parens/include.php
diff --git a/src/needs-parens.js b/src/needs-parens.js
index efd3608bc..7d29a493b 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -39,8 +39,6 @@ function needsParens(path) {
}
switch (node.kind) {
- case "call":
- return false;
case "pre":
case "post":
if (parent.kind === "unary") {
@@ -67,13 +65,34 @@ function needsParens(path) {
default:
return false;
}
- case "clone":
- case "new": {
- if (isLookupNode(parent)) {
+ case "bin": {
+ if (["pre", "post"].includes(parent.kind)) {
return true;
}
- return false;
+ if (["if", "while", "do", "switch", "case"].includes(parent.kind)) {
+ return false;
+ }
+
+ // $var = false or true;
+ // The constant false is assigned to $f before the "or" operation occurs
+ // Acts like: (($var = false) or true)
+ if (
+ node.right.kind === "bin" &&
+ ["and", "xor", "or"].includes(node.right.type)
+ ) {
+ return true;
+ }
+
+ if (parent.kind === "cast") {
+ return true;
+ }
+
+ return node.parenthesizedExpression;
+ }
+ case "clone":
+ case "new": {
+ return isLookupNode(parent);
}
case "yield": {
switch (parent.kind) {
@@ -125,23 +144,7 @@ function needsParens(path) {
return false;
}
case "closure":
- if (parent.kind === "call" && name === "what" && parent.what === node) {
- return true;
- }
-
- return false;
- case "boolean":
- case "number":
- case "magic":
- case "encapsed":
- case "nowdoc":
- case "variable":
- case "isset":
- case "empty":
- case "silent":
- case "exit":
- case "eval":
- return false;
+ return parent.kind === "call" && name === "what" && parent.what === node;
case "string":
case "array":
case "cast":
@@ -161,43 +164,13 @@ function needsParens(path) {
return true;
}
- return false;
- case "propertylookup":
- case "staticlookup":
- case "offsetlookup":
return false;
case "print":
- if (parent.kind === "bin") {
- return true;
- }
-
- return false;
- case "bin": {
- if (["pre", "post"].includes(parent.kind)) {
- return true;
- }
-
- if (["if", "while", "do", "switch", "case"].includes(parent.kind)) {
- return false;
- }
-
- // $var = false or true;
- // The constant false is assigned to $f before the "or" operation occurs
- // Acts like: (($var = false) or true)
- if (
- node.right.kind === "bin" &&
- ["and", "xor", "or"].includes(node.right.type)
- ) {
- return true;
- }
-
- if (parent.kind === "cast") {
- return true;
- }
- }
+ case "include":
+ return parent.kind === "bin";
}
- return node.parenthesizedExpression;
+ return false;
}
module.exports = needsParens;
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 00c29763a..6960f7c95 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1227,6 +1227,82 @@ call(exit(1));
`;
+exports[`include.php 1`] = `
+
Date: Wed, 12 Sep 2018 18:07:16 +0300
Subject: [PATCH 0014/1031] feat: parens around bin node (#622)
---
src/needs-parens.js | 69 +++-
src/util.js | 11 +
tests/bin/__snapshots__/jsfmt.spec.js.snap | 52 +--
.../comments/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/echo/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/if/__snapshots__/jsfmt.spec.js.snap | 10 +-
.../include/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 367 +++++++++++++++++-
tests/parens/bin.php | 166 ++++++++
tests/retif/__snapshots__/jsfmt.spec.js.snap | 8 +-
tests/return/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/silent/__snapshots__/jsfmt.spec.js.snap | 4 +-
12 files changed, 620 insertions(+), 75 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 7d29a493b..44e98e53f 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -1,6 +1,11 @@
"use strict";
-const { isLookupNode } = require("./util");
+const {
+ isLookupNode,
+ getPrecedence,
+ shouldFlatten,
+ isBitwiseOperator
+} = require("./util");
function needsParens(path) {
const parent = path.getParentNode();
@@ -66,29 +71,59 @@ function needsParens(path) {
return false;
}
case "bin": {
- if (["pre", "post"].includes(parent.kind)) {
- return true;
- }
-
- if (["if", "while", "do", "switch", "case"].includes(parent.kind)) {
- return false;
- }
-
// $var = false or true;
// The constant false is assigned to $f before the "or" operation occurs
// Acts like: (($var = false) or true)
- if (
- node.right.kind === "bin" &&
- ["and", "xor", "or"].includes(node.right.type)
- ) {
+ if (["and", "xor", "or"].includes(node.type)) {
return true;
}
- if (parent.kind === "cast") {
- return true;
- }
+ switch (parent.kind) {
+ case "pre":
+ case "post":
+ case "unary":
+ case "cast":
+ case "silent":
+ return true;
+ case "call":
+ case "propertylookup":
+ case "staticlookup":
+ case "offsetlookup":
+ return name === "what" && parent.what === node;
+ case "bin": {
+ const po = parent.type;
+ const pp = getPrecedence(po);
+ const no = node.type;
+ const np = getPrecedence(no);
+
+ if (pp > np) {
+ return true;
+ }
+
+ if (po === "||" && no === "&&") {
+ return true;
+ }
+
+ if (pp === np && !shouldFlatten(po, no)) {
+ return true;
+ }
+
+ if (pp < np && no === "%") {
+ return !shouldFlatten(po, no);
+ }
+
+ // Add parenthesis when working with binary operators
+ // It's not stricly needed but helps with code understanding
+ if (isBitwiseOperator(po)) {
+ return true;
+ }
- return node.parenthesizedExpression;
+ return false;
+ }
+
+ default:
+ return false;
+ }
}
case "clone":
case "new": {
diff --git a/src/util.js b/src/util.js
index f1144505b..bc3ee31d2 100644
--- a/src/util.js
+++ b/src/util.js
@@ -49,6 +49,7 @@ const PRECEDENCE = {};
["+", "-", "."],
["*", "/", "%"],
["!"],
+ ["instanceof"],
["++", "--", "~"],
["**"]
].forEach((tier, i) => {
@@ -66,6 +67,15 @@ const additiveOperators = ["+", "-"];
const multiplicativeOperators = ["*", "/", "%"];
const bitshiftOperators = [">>", "<<"];
+function isBitwiseOperator(operator) {
+ return (
+ !!bitshiftOperators[operator] ||
+ operator === "|" ||
+ operator === "^" ||
+ operator === "&"
+ );
+}
+
function shouldFlatten(parentOp, nodeOp) {
if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) {
// x + y % z --> (x + y) % z
@@ -533,6 +543,7 @@ function hasNewline(text, index, opts) {
module.exports = {
printNumber,
getPrecedence,
+ isBitwiseOperator,
shouldFlatten,
nodeHasStatement,
getNodeListProperty,
diff --git a/tests/bin/__snapshots__/jsfmt.spec.js.snap b/tests/bin/__snapshots__/jsfmt.spec.js.snap
index 0f950882d..e138cef6f 100644
--- a/tests/bin/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/bin/__snapshots__/jsfmt.spec.js.snap
@@ -345,8 +345,8 @@ call(
);
$var =
- $someReallyReallyReallyLongBooleanVariable *
- $someReallyReallyReallyLongBooleanVariable /
+ ($someReallyReallyReallyLongBooleanVariable *
+ $someReallyReallyReallyLongBooleanVariable) /
100;
$var =
($someReallyReallyReallyLongBooleanVariable *
@@ -357,18 +357,18 @@ $var =
($someReallyReallyReallyLongBooleanVariable / 100);
$var =
$someReallyReallyReallyLongBooleanVariable +
- $someReallyReallyReallyLongBooleanVariable % 100;
+ ($someReallyReallyReallyLongBooleanVariable % 100);
$var =
- $someReallyReallyReallyLongBooleanVariable *
- $someReallyReallyReallyLongBooleanVariable %
+ ($someReallyReallyReallyLongBooleanVariable *
+ $someReallyReallyReallyLongBooleanVariable) %
100;
$var =
- $someReallyReallyReallyLongBooleanVariable <<
- $someReallyReallyReallyLongBooleanVariable <<
+ ($someReallyReallyReallyLongBooleanVariable <<
+ $someReallyReallyReallyLongBooleanVariable) <<
100;
$var =
$someReallyReallyReallyLongBooleanVariable **
- $someReallyReallyReallyLongBooleanVariable ** 100;
+ ($someReallyReallyReallyLongBooleanVariable ** 100);
$var =
$someReallyReallyReallyLongBooleanVariable +
$someReallyReallyReallyLongBooleanVariable * 100;
@@ -378,15 +378,15 @@ $var =
100;
$var =
$someReallyReallyReallyLongBooleanVariable +
- $someReallyReallyReallyLongBooleanVariable % 100;
+ ($someReallyReallyReallyLongBooleanVariable % 100);
$var =
- $someReallyReallyReallyLongBooleanVariable %
- $someReallyReallyReallyLongBooleanVariable +
+ ($someReallyReallyReallyLongBooleanVariable %
+ $someReallyReallyReallyLongBooleanVariable) +
100;
if (
- $someReallyReallyReallyLongBooleanVariable *
- $someReallyReallyReallyLongBooleanVariable /
+ ($someReallyReallyReallyLongBooleanVariable *
+ $someReallyReallyReallyLongBooleanVariable) /
100
) {
}
@@ -403,24 +403,24 @@ if (
}
if (
$someReallyReallyReallyLongBooleanVariable +
- $someReallyReallyReallyLongBooleanVariable % 100
+ ($someReallyReallyReallyLongBooleanVariable % 100)
) {
}
if (
- $someReallyReallyReallyLongBooleanVariable *
- $someReallyReallyReallyLongBooleanVariable %
+ ($someReallyReallyReallyLongBooleanVariable *
+ $someReallyReallyReallyLongBooleanVariable) %
100
) {
}
if (
- $someReallyReallyReallyLongBooleanVariable <<
- $someReallyReallyReallyLongBooleanVariable <<
+ ($someReallyReallyReallyLongBooleanVariable <<
+ $someReallyReallyReallyLongBooleanVariable) <<
100
) {
}
if (
$someReallyReallyReallyLongBooleanVariable **
- $someReallyReallyReallyLongBooleanVariable ** 100
+ ($someReallyReallyReallyLongBooleanVariable ** 100)
) {
}
if (
@@ -436,12 +436,12 @@ if (
}
if (
$someReallyReallyReallyLongBooleanVariable +
- $someReallyReallyReallyLongBooleanVariable % 100
+ ($someReallyReallyReallyLongBooleanVariable % 100)
) {
}
if (
- $someReallyReallyReallyLongBooleanVariable %
- $someReallyReallyReallyLongBooleanVariable +
+ ($someReallyReallyReallyLongBooleanVariable %
+ $someReallyReallyReallyLongBooleanVariable) +
100
) {
}
@@ -451,13 +451,13 @@ echo 'This ' .
'string' .
'string' .
'string' .
- (100 + 100) .
+ 100 + 100 .
'string ' .
- (100 - 100) .
+ 100 - 100 .
'was ' .
- (100 * 100) .
+ 100 * 100 .
'made ' .
- (100 / 100) .
+ 100 / 100 .
'with concatenation.';
if ($someReallyReallyReallyLongBooleanVariableWithRddllyReallyLongName || []) {
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index b102d0a26..9545002c9 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -336,7 +336,7 @@ $foo->bar(/* B */);
foo(
/* Comment */ $a /* Comment */,
/* Comment */ [] /* Comment */,
- /* Comment */ ($a + 2) /* Comment */
+ /* Comment */ $a + 2 /* Comment */
);
foo(/* A */);
$foo->bar(/* B */);
diff --git a/tests/echo/__snapshots__/jsfmt.spec.js.snap b/tests/echo/__snapshots__/jsfmt.spec.js.snap
index 0f4669cdc..02095d8cb 100644
--- a/tests/echo/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/echo/__snapshots__/jsfmt.spec.js.snap
@@ -276,7 +276,7 @@ echo 1;
echo 1;
echo 1, 1;
-echo "The sum is " . 1 | 2; // output: "2". Parentheses needed.
+echo ("The sum is " . 1) | 2; // output: "2". Parentheses needed.
echo "The sum is ", 1 | 2; // output: "The sum is 3". Fine.
echo "Foo" . f("bar") . "Foo";
diff --git a/tests/if/__snapshots__/jsfmt.spec.js.snap b/tests/if/__snapshots__/jsfmt.spec.js.snap
index 40a5fefc9..bcdaeb6de 100644
--- a/tests/if/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/if/__snapshots__/jsfmt.spec.js.snap
@@ -155,30 +155,30 @@ if (
if (
$this ||
- $that && $theOtherThing && $someReallyReallyReallyLongBooleanVariable
+ ($that && $theOtherThing && $someReallyReallyReallyLongBooleanVariable)
) {
return true;
}
if (
$this ||
- $that && $theOtherThing < $someReallyReallyReallyLongBooleanVariable
+ ($that && $theOtherThing < $someReallyReallyReallyLongBooleanVariable)
) {
return true;
}
if (
$this ||
- $that &&
+ ($that &&
$theOtherThing < $someReallyReallyReallyLongBooleanVariable ==
- $equalCheck
+ $equalCheck)
) {
return true;
}
if (
$this ||
- $that && $theOtherThing ||
+ ($that && $theOtherThing) ||
$someReallyReallyReallyLongBooleanVariable
) {
return true;
diff --git a/tests/include/__snapshots__/jsfmt.spec.js.snap b/tests/include/__snapshots__/jsfmt.spec.js.snap
index ba7a1a1b2..46152dc55 100644
--- a/tests/include/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/include/__snapshots__/jsfmt.spec.js.snap
@@ -67,7 +67,7 @@ require_once "other/other.php";
include 'http://www.example.com/file.php?foo=1&bar=2';
include $filename;
-(isset($_GET['p']) && $_GET['p'] === 'home') ? include 'pages/home.php' : '';
+isset($_GET['p']) && $_GET['p'] === 'home' ? include 'pages/home.php' : '';
include Kohana::find_file('views', 'admin/system/errors');
include "test.php";
include 'foo.php' . ' MORE OUTPUT';
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 6960f7c95..8e1063beb 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -193,10 +193,10 @@ for ($i = 1; $i <= 10; $i++) {
echo $i;
}
-for ($i = 1; ($i <= 10); $i++) {
+for ($i = 1; $i <= 10; $i++) {
echo $i;
}
-for ($i = 1, $j = 0; ($i <= 10); $j += $i, print $i, $i++);
+for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
if ($a = 1) {
}
@@ -378,6 +378,172 @@ $var = true && false;
$var = (true && false);
$var = true and false;
$var = (true and false);
+
+$var = $var || $var();
+$var = ($var || $var)();
+$var = $var && $var();
+$var = ($var && $var)();
+$var = call($var || $var);
+$var = call(($var || $var));
+$var = call($var && $var);
+$var = call(($var && $var));
+
+$var = +($var || $var);
+$var = -($var || $var);
+$var = ~($var || $var);
+
+$var = ($var || $var)->foo;
+$var = ($var || $var)->foo();
+$var = ($var || $var)[1];
+
+$var = $var || $var && $var;
+$var = ($var || $var) && $var;
+$var = $var || ($var && $var);
+
+$var = $var & ($var || 'test');
+$var = ($var || 'test') & $var;
+$var = $var & ($var . 'test');
+$var = ($var . 'test') & $var;
+$var = ($var & $var) || 'test';
+$var = $var || ('test' & $var);
+
+$var = ($var || $var) % 100;
+$var = ($var + $var) % 100;
+$var = 100 % ($var || $var);
+$var = 100 % ($var + $var);
+$var = $var || ($var % 100);
+$var = $var + ($var % 100);
+$var = (100 % $var) || $var;
+$var = (100 % $var) + $var;
+
+$var = ($var + $var) >> 1;
+$var = (($var - 1) >> $var) & $var;
+$var = $var > $var ? 0 : ($var - $var) >> $var;
+$var = (($var - $var) >> $var) + 1;
+
+if ($var < 1 << ($var + $var)) {}
+
+$var = $var < $var ? 0 : ((($var - 1) >> $var) << $var);
+$var = 1 - (2 * ($var[3] >> 7));
+$var = ((($var[3] << 1) & 0xff) | ($var[2] >> 7)) - 127;
+$var = (($var[2] & 0x7f) << 16) | ($var[1] << 8) | $var[0];
+
+$var = 2 / 3 * 10 / 2 + 2;
+
+$var = (($var / $var) * $var - $var / 2) * call($var);
+$var = (($var / $var) * $var - $var / 2) * call($var);
+
+$var = $var % 10 - 5;
+$var = $var * $var % 10;
+$var = $var % 10 > 5;
+$var = $var % 10 == 0;
+
+$var = $var + $var / $var;
+$var = $var / $var + $var;
+
+$var = $var * $var % $var;
+$var = $var / $var % $var;
+$var = $var % $var * $var;
+$var = $var % $var / $var;
+
+$var = $var % $var % $var;
+
+$var = $var << $var >> $var;
+$var = $var >> $var << $var;
+$var = $var >> $var >> $var;
+$var = $var + $var >> $var;
+$var = ($var + $var) >> $var;
+$var = $var + ($var >> $var);
+
+$var = $var | $var & $var;
+$var = $var & $var | $var;
+$var = $var ^ $var ^ $var;
+$var = $var & $var & $var;
+$var = $var | $var | $var;
+$var = $var & $var >> $var;
+$var = $var << $var | $var;
+
+$var = $var ? 'foo' : 'bar' . 'test';
+$var = ($var ? 'foo' : 'bar') . 'test';
+$var = $var ? 'foo' : ('bar' . 'test');
+
+call(($var + $var));
+
+$var = call(($var + $var));
+
+$var = $var + $var ** 2;
+$var = ($var + $var) ** 2;
+$var = $var + ($var ** 2);
+$var = (+$var) ** 2;
+$var = +$var ** 2;
+
+$var = $foo instanceof Foo;
+$var = $foo instanceof Foo || $foo instanceof Foo;
+$var = ($foo instanceof Foo) || ($foo instanceof Foo);
+$var = (($foo) instanceof Foo);
+
+$var = !$var;
+$var = !($var);
+$var = (!($var));
+$var = !!$var;
+$var = !!($var);
+$var = !(!($var));
+$var = (!(!($var)));
+$var = !!!$var;
+$var = !!!($var);
+$var = !!(!($var));
+$var = !(!(!($var)));
+$var = (!(!(!($var))));
+
+$var = !$var || !$var;
+$var = (!($var) || !($var));
+$var = !(!($var) || !($var));
+
+$var = $var + $var * $var;
+$var = ($var + $var) * $var;
+
+$var = @foo() || @foo();
+$var = @(foo() || foo());
+
+($var += ($var += ($var += $var)));
+($var -= ($var -= ($var -= $var)));
+($var *= ($var *= ($var *= $var)));
+($var **= ($var **= ($var **= $var)));
+($var /= ($var /= ($var /= $var)));
+($var .= ($var .= ($var .= $var)));
+($var %= ($var %= ($var %= $var)));
+($var &= ($var &= ($var &= $var)));
+($var |= ($var |= ($var |= $var)));
+($var ^= ($var ^= ($var ^= $var)));
+($var <<= ($var <<= ($var <<= $var)));
+($var >>= ($var >>= ($var >>= $var)));
+
+$var = $var | $var | $var;
+$var = $var | ($var | $var);
+$var = ($var | $var) | $var;
+$var = $var & $var & $var;
+$var = $var & ($var & $var);
+$var = ($var & $var) & $var;
+
+$var = $var ^ $var | $var;
+$var = ($var ^ $var) | $var;
+$var = $var | $var ^ $var;
+$var = $var | ($var ^ $var);
+$var = ($var | $var) ^ $var;
+
+$var = $var & $var | $var;
+$var = ($var & $var) | $var;
+$var = $var | $var & $var;
+$var = $var | ($var & $var);
+$var = ($var | $var) & $var;
+
+$var = $var == $var || false;
+$var = ($var == $var) || false;
+$var = $var == ($var || false);
+
+$var = false || $var == $var;
+$var = false || ($var == $var);
+$var = ($var || false) == $var;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 20 >> 30;
+$var = (10 >> 20) >> 30;
$var = (10 >> 20) >> 30;
$var = 10 >> (20 >> 30);
@@ -463,14 +629,181 @@ $var = 10 | 20 | 30;
$var = 10 | (20 | 30);
$var = false || true;
-$var = (false || true);
+$var = false || true;
($var = false) or true;
$var = (false or true);
$var = true && false;
-$var = (true && false);
+$var = true && false;
($var = true) and false;
$var = (true and false);
+$var = $var || $var();
+$var = ($var || $var)();
+$var = $var && $var();
+$var = ($var && $var)();
+$var = call($var || $var);
+$var = call($var || $var);
+$var = call($var && $var);
+$var = call($var && $var);
+
+$var = +($var || $var);
+$var = -($var || $var);
+$var = ~($var || $var);
+
+$var = ($var || $var)->foo;
+$var = ($var || $var)->foo();
+$var = ($var || $var)[1];
+
+$var = $var || ($var && $var);
+$var = ($var || $var) && $var;
+$var = $var || ($var && $var);
+
+$var = $var & ($var || 'test');
+$var = ($var || 'test') & $var;
+$var = $var & ($var . 'test');
+$var = ($var . 'test') & $var;
+$var = $var & $var || 'test';
+$var = $var || 'test' & $var;
+
+$var = ($var || $var) % 100;
+$var = ($var + $var) % 100;
+$var = 100 % ($var || $var);
+$var = 100 % ($var + $var);
+$var = $var || $var % 100;
+$var = $var + ($var % 100);
+$var = 100 % $var || $var;
+$var = (100 % $var) + $var;
+
+$var = $var + $var >> 1;
+$var = ($var - 1 >> $var) & $var;
+$var = $var > $var ? 0 : $var - $var >> $var;
+$var = ($var - $var >> $var) + 1;
+
+if ($var < 1 << $var + $var) {
+}
+
+$var = $var < $var ? 0 : ($var - 1 >> $var) << $var;
+$var = 1 - 2 * ($var[3] >> 7);
+$var = ((($var[3] << 1) & 0xff) | ($var[2] >> 7)) - 127;
+$var = (($var[2] & 0x7f) << 16) | ($var[1] << 8) | $var[0];
+
+$var = ((2 / 3) * 10) / 2 + 2;
+
+$var = (($var / $var) * $var - $var / 2) * call($var);
+$var = (($var / $var) * $var - $var / 2) * call($var);
+
+$var = ($var % 10) - 5;
+$var = ($var * $var) % 10;
+$var = $var % 10 > 5;
+$var = $var % 10 == 0;
+
+$var = $var + $var / $var;
+$var = $var / $var + $var;
+
+$var = ($var * $var) % $var;
+$var = ($var / $var) % $var;
+$var = ($var % $var) * $var;
+$var = ($var % $var) / $var;
+
+$var = ($var % $var) % $var;
+
+$var = ($var << $var) >> $var;
+$var = ($var >> $var) << $var;
+$var = ($var >> $var) >> $var;
+$var = $var + $var >> $var;
+$var = $var + $var >> $var;
+$var = $var + ($var >> $var);
+
+$var = $var | ($var & $var);
+$var = ($var & $var) | $var;
+$var = $var ^ $var ^ $var;
+$var = $var & $var & $var;
+$var = $var | $var | $var;
+$var = $var & ($var >> $var);
+$var = ($var << $var) | $var;
+
+$var = $var ? 'foo' : 'bar' . 'test';
+$var = ($var ? 'foo' : 'bar') . 'test';
+$var = $var ? 'foo' : 'bar' . 'test';
+
+call($var + $var);
+
+$var = call($var + $var);
+
+$var = $var + $var ** 2;
+$var = ($var + $var) ** 2;
+$var = $var + $var ** 2;
+$var = (+$var) ** 2;
+$var = (+$var) ** 2;
+
+$var = $foo instanceof Foo;
+$var = $foo instanceof Foo || $foo instanceof Foo;
+$var = $foo instanceof Foo || $foo instanceof Foo;
+$var = $foo instanceof Foo;
+
+$var = !$var;
+$var = !$var;
+$var = !$var;
+$var = !!$var;
+$var = !!$var;
+$var = !!$var;
+$var = !!$var;
+$var = !!!$var;
+$var = !!!$var;
+$var = !!!$var;
+$var = !!!$var;
+$var = !!!$var;
+
+$var = !$var || !$var;
+$var = !$var || !$var;
+$var = !(!$var || !$var);
+
+$var = $var + $var * $var;
+$var = ($var + $var) * $var;
+
+$var = @(foo() || @foo());
+$var = @(foo() || foo());
+
+$var += $var += $var += $var;
+$var -= $var -= $var -= $var;
+$var *= $var *= $var *= $var;
+$var **= $var **= $var **= $var;
+$var /= $var /= $var /= $var;
+$var .= $var .= $var .= $var;
+$var %= $var %= $var %= $var;
+$var &= $var &= $var &= $var;
+$var |= $var |= $var |= $var;
+$var ^= $var ^= $var ^= $var;
+$var <<= $var <<= $var <<= $var;
+$var >>= $var >>= $var >>= $var;
+
+$var = $var | $var | $var;
+$var = $var | ($var | $var);
+$var = $var | $var | $var;
+$var = $var & $var & $var;
+$var = $var & ($var & $var);
+$var = $var & $var & $var;
+
+$var = ($var ^ $var) | $var;
+$var = ($var ^ $var) | $var;
+$var = $var | ($var ^ $var);
+$var = $var | ($var ^ $var);
+$var = ($var | $var) ^ $var;
+
+$var = ($var & $var) | $var;
+$var = ($var & $var) | $var;
+$var = $var | ($var & $var);
+$var = $var | ($var & $var);
+$var = ($var | $var) & $var;
+
+$var = $var == $var || false;
+$var = $var == $var || false;
+$var = $var == ($var || false);
+
+$var = false || $var == $var;
+$var = false || $var == $var;
+$var = ($var || false) == $var;
+
`;
exports[`block.php 1`] = `
@@ -701,7 +1034,7 @@ $var = ((int) $raw['data']) + $value;
$var = (bool) ($var ? 1 : 2);
$var = (bool) ($var + 1 ? 1 : 2);
$var = ((bool) $var) + 1 ? 1 : 2;
-$var = (bool) (($var + 1) ? 1 : 2);
+$var = (bool) ($var + 1 ? 1 : 2);
$var = (bool) $var ? 1 : 2;
$var = (bool) $var ? (bool) 1 : (bool) 2;
$var = (bool) $var ? (bool) 1 : (bool) 2;
@@ -1104,7 +1437,7 @@ echo "Sum: ", 1 + 2;
echo "Hello ", isset($name) ? $name : "John Doe", "!";
echo "Hello ", isset($name) ? $name : "John Doe", "!";
-echo 'Sum: ' . (1 + 2);
+echo 'Sum: ' . 1 + 2;
echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
@@ -1901,7 +2234,7 @@ $var[
]->map();
$var = $var . $var ? "()" : "";
-$var = ($var . $var) ? "()" : "";
+$var = $var . $var ? "()" : "";
$var = $var . ($var ? "()" : "");
$var = +($var ? 1 : 2);
$var = +(+$var ? 1 : 2);
@@ -2111,7 +2444,7 @@ $var = +$foo->bar;
$var = +$var || +$var;
$var = +$var || +$var;
-$var = (+$var || +$var);
+$var = +$var || +$var;
$var = -+$var;
diff --git a/tests/parens/bin.php b/tests/parens/bin.php
index 03b6c9945..d7a82868e 100644
--- a/tests/parens/bin.php
+++ b/tests/parens/bin.php
@@ -89,3 +89,169 @@
$var = (true && false);
$var = true and false;
$var = (true and false);
+
+$var = $var || $var();
+$var = ($var || $var)();
+$var = $var && $var();
+$var = ($var && $var)();
+$var = call($var || $var);
+$var = call(($var || $var));
+$var = call($var && $var);
+$var = call(($var && $var));
+
+$var = +($var || $var);
+$var = -($var || $var);
+$var = ~($var || $var);
+
+$var = ($var || $var)->foo;
+$var = ($var || $var)->foo();
+$var = ($var || $var)[1];
+
+$var = $var || $var && $var;
+$var = ($var || $var) && $var;
+$var = $var || ($var && $var);
+
+$var = $var & ($var || 'test');
+$var = ($var || 'test') & $var;
+$var = $var & ($var . 'test');
+$var = ($var . 'test') & $var;
+$var = ($var & $var) || 'test';
+$var = $var || ('test' & $var);
+
+$var = ($var || $var) % 100;
+$var = ($var + $var) % 100;
+$var = 100 % ($var || $var);
+$var = 100 % ($var + $var);
+$var = $var || ($var % 100);
+$var = $var + ($var % 100);
+$var = (100 % $var) || $var;
+$var = (100 % $var) + $var;
+
+$var = ($var + $var) >> 1;
+$var = (($var - 1) >> $var) & $var;
+$var = $var > $var ? 0 : ($var - $var) >> $var;
+$var = (($var - $var) >> $var) + 1;
+
+if ($var < 1 << ($var + $var)) {}
+
+$var = $var < $var ? 0 : ((($var - 1) >> $var) << $var);
+$var = 1 - (2 * ($var[3] >> 7));
+$var = ((($var[3] << 1) & 0xff) | ($var[2] >> 7)) - 127;
+$var = (($var[2] & 0x7f) << 16) | ($var[1] << 8) | $var[0];
+
+$var = 2 / 3 * 10 / 2 + 2;
+
+$var = (($var / $var) * $var - $var / 2) * call($var);
+$var = (($var / $var) * $var - $var / 2) * call($var);
+
+$var = $var % 10 - 5;
+$var = $var * $var % 10;
+$var = $var % 10 > 5;
+$var = $var % 10 == 0;
+
+$var = $var + $var / $var;
+$var = $var / $var + $var;
+
+$var = $var * $var % $var;
+$var = $var / $var % $var;
+$var = $var % $var * $var;
+$var = $var % $var / $var;
+
+$var = $var % $var % $var;
+
+$var = $var << $var >> $var;
+$var = $var >> $var << $var;
+$var = $var >> $var >> $var;
+$var = $var + $var >> $var;
+$var = ($var + $var) >> $var;
+$var = $var + ($var >> $var);
+
+$var = $var | $var & $var;
+$var = $var & $var | $var;
+$var = $var ^ $var ^ $var;
+$var = $var & $var & $var;
+$var = $var | $var | $var;
+$var = $var & $var >> $var;
+$var = $var << $var | $var;
+
+$var = $var ? 'foo' : 'bar' . 'test';
+$var = ($var ? 'foo' : 'bar') . 'test';
+$var = $var ? 'foo' : ('bar' . 'test');
+
+call(($var + $var));
+
+$var = call(($var + $var));
+
+$var = $var + $var ** 2;
+$var = ($var + $var) ** 2;
+$var = $var + ($var ** 2);
+$var = (+$var) ** 2;
+$var = +$var ** 2;
+
+$var = $foo instanceof Foo;
+$var = $foo instanceof Foo || $foo instanceof Foo;
+$var = ($foo instanceof Foo) || ($foo instanceof Foo);
+$var = (($foo) instanceof Foo);
+
+$var = !$var;
+$var = !($var);
+$var = (!($var));
+$var = !!$var;
+$var = !!($var);
+$var = !(!($var));
+$var = (!(!($var)));
+$var = !!!$var;
+$var = !!!($var);
+$var = !!(!($var));
+$var = !(!(!($var)));
+$var = (!(!(!($var))));
+
+$var = !$var || !$var;
+$var = (!($var) || !($var));
+$var = !(!($var) || !($var));
+
+$var = $var + $var * $var;
+$var = ($var + $var) * $var;
+
+$var = @foo() || @foo();
+$var = @(foo() || foo());
+
+($var += ($var += ($var += $var)));
+($var -= ($var -= ($var -= $var)));
+($var *= ($var *= ($var *= $var)));
+($var **= ($var **= ($var **= $var)));
+($var /= ($var /= ($var /= $var)));
+($var .= ($var .= ($var .= $var)));
+($var %= ($var %= ($var %= $var)));
+($var &= ($var &= ($var &= $var)));
+($var |= ($var |= ($var |= $var)));
+($var ^= ($var ^= ($var ^= $var)));
+($var <<= ($var <<= ($var <<= $var)));
+($var >>= ($var >>= ($var >>= $var)));
+
+$var = $var | $var | $var;
+$var = $var | ($var | $var);
+$var = ($var | $var) | $var;
+$var = $var & $var & $var;
+$var = $var & ($var & $var);
+$var = ($var & $var) & $var;
+
+$var = $var ^ $var | $var;
+$var = ($var ^ $var) | $var;
+$var = $var | $var ^ $var;
+$var = $var | ($var ^ $var);
+$var = ($var | $var) ^ $var;
+
+$var = $var & $var | $var;
+$var = ($var & $var) | $var;
+$var = $var | $var & $var;
+$var = $var | ($var & $var);
+$var = ($var | $var) & $var;
+
+$var = $var == $var || false;
+$var = ($var == $var) || false;
+$var = $var == ($var || false);
+
+$var = false || $var == $var;
+$var = false || ($var == $var);
+$var = ($var || false) == $var;
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index 5efeb9a51..6e93a9de2 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -72,12 +72,12 @@ $arr = [
= 1)
+return $testReallyReallyLong >= 1
? $someOtherReallyReallyLongVariable
: $thisOtherVariable;
return true ?: 2;
-return ($testReallyReallyLong === $someOtherReallyReallyLongVariable &&
- $otherTest != $someOtherTest)
+return $testReallyReallyLong === $someOtherReallyReallyLongVariable &&
+ $otherTest != $someOtherTest
? 1
: 0;
@@ -117,7 +117,7 @@ $test = $foo
$someOtherReallyReallyLongVariable
);
$test =
- ($testReallyReallyReallyReallyReallyReallyLong >= 1)
+ $testReallyReallyReallyReallyReallyReallyLong >= 1
?: $someOtherReallyReallyReallyReallyReallyReallyLongVariable;
$test = $foo ?: bar(['foo' => 'bar', 'bar' => 'foo']);
$test = $foo
diff --git a/tests/return/__snapshots__/jsfmt.spec.js.snap b/tests/return/__snapshots__/jsfmt.spec.js.snap
index b6c5bddac..bd59f0cf6 100644
--- a/tests/return/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/return/__snapshots__/jsfmt.spec.js.snap
@@ -83,6 +83,6 @@ return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable
? true
: false;
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable->veryVeryVeryVeryVeryVeryLongProperty;
-return $true ? ('foo' . ('foo' . 'bar')) : "foobarfoo";
+return $true ? 'foo' . 'foo' . 'bar' : "foobarfoo";
`;
diff --git a/tests/silent/__snapshots__/jsfmt.spec.js.snap b/tests/silent/__snapshots__/jsfmt.spec.js.snap
index 0a9695b28..5c62c45ff 100644
--- a/tests/silent/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/silent/__snapshots__/jsfmt.spec.js.snap
@@ -18,8 +18,8 @@ $value = @ $cache[$key];
Date: Wed, 12 Sep 2018 22:33:35 +0300
Subject: [PATCH 0015/1031] fix: bin string concatenation (#625)
---
src/needs-parens.js | 10 ++++-
tests/bin/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 40 ++++++++++++++++++-
tests/parens/bin.php | 19 +++++++++
4 files changed, 68 insertions(+), 5 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 44e98e53f..4e5aa1cc3 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -104,8 +104,14 @@ function needsParens(path) {
return true;
}
- if (pp === np && !shouldFlatten(po, no)) {
- return true;
+ if (pp === np) {
+ if (po === "." && no !== ".") {
+ return true;
+ }
+
+ if (!shouldFlatten(po, no)) {
+ return true;
+ }
}
if (pp < np && no === "%") {
diff --git a/tests/bin/__snapshots__/jsfmt.spec.js.snap b/tests/bin/__snapshots__/jsfmt.spec.js.snap
index e138cef6f..58ed762f6 100644
--- a/tests/bin/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/bin/__snapshots__/jsfmt.spec.js.snap
@@ -451,9 +451,9 @@ echo 'This ' .
'string' .
'string' .
'string' .
- 100 + 100 .
+ (100 + 100) .
'string ' .
- 100 - 100 .
+ (100 - 100) .
'was ' .
100 * 100 .
'made ' .
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 8e1063beb..ac89c4ce0 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -544,6 +544,25 @@ $var = $var == ($var || false);
$var = false || $var == $var;
$var = false || ($var == $var);
$var = ($var || false) == $var;
+
+$var = 'string' . true ? '1' : '2';
+$var = 'string' . (true ? '1' : '2');
+
+$var = 'string' . (100 + 100);
+$var = (100 + 100) . 'string';
+$var = 'string' . ($var || 100);
+$var = ($var || 100) . 'string';
+$var = 'string' . ($var * 100);
+$var = ($var * 100) . 'string';
+$var = 'string' . !$var;
+$var = !$var . 'string';
+$var = 'string' . ($var | 100);
+$var = ($var | 100) . 'string';
+
+$var = $var . $var % $var;
+$var = ($var . $var) % $var;
+$var = $var % $var . $var;
+$var = $var % ($var . $var);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Wed, 12 Sep 2018 23:04:10 +0300
Subject: [PATCH 0016/1031] fix: parens for equal bin operator (#626)
---
src/needs-parens.js | 16 +++++++++-------
tests/parens/__snapshots__/jsfmt.spec.js.snap | 12 ++++++++++--
tests/parens/bin.php | 4 ++++
tests/return/__snapshots__/jsfmt.spec.js.snap | 2 +-
4 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 4e5aa1cc3..c324730b8 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -1,5 +1,7 @@
"use strict";
+const assert = require("assert");
+
const {
isLookupNode,
getPrecedence,
@@ -104,14 +106,14 @@ function needsParens(path) {
return true;
}
- if (pp === np) {
- if (po === "." && no !== ".") {
- return true;
- }
+ if (pp === np && name === "right") {
+ assert.strictEqual(parent.right, node);
- if (!shouldFlatten(po, no)) {
- return true;
- }
+ return true;
+ }
+
+ if (pp === np && !shouldFlatten(po, no)) {
+ return true;
}
if (pp < np && no === "%") {
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index ac89c4ce0..59e5bf334 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -563,6 +563,10 @@ $var = $var . $var % $var;
$var = ($var . $var) % $var;
$var = $var % $var . $var;
$var = $var % ($var . $var);
+
+$var = '100' - '100' - '100';
+$var = ('100' - '100') - '100';
+$var = '100' - ('100' - '100');
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
veryVeryVeryVeryVeryVeryLongProperty;
-return $true ? 'foo' . 'foo' . 'bar' : "foobarfoo";
+return $true ? 'foo' . ('foo' . 'bar') : "foobarfoo";
`;
From 670be2ee5a01c0c454ad0ee8b1560bca1ccb0e54 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Wed, 12 Sep 2018 23:48:52 +0300
Subject: [PATCH 0017/1031] fix: broken `retif` without parens (#627)
---
src/needs-parens.js | 2 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 14 +++++--
tests/retif/__snapshots__/jsfmt.spec.js.snap | 38 +++++++++----------
3 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index c324730b8..7ba666872 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -176,7 +176,7 @@ function needsParens(path) {
case "cast":
return true;
case "retif":
- return name === "test" && parent.test === node;
+ return true;
case "propertylookup":
case "staticlookup":
case "offsetlookup":
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 59e5bf334..eb6cb1d26 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -2285,8 +2285,16 @@ $var = $var . ($var ? "()" : "");
$var = +($var ? 1 : 2);
$var = +(+$var ? 1 : 2);
$var = +($var++ ? 1 : 2);
-$var = (true ? 'true' : false) ? true ? 'true' : false : true ? 'true' : false;
-$var = ($var ? $var1 ? 1 : 2 : $var2) ? 3 : 4;
+$var = (true
+ ? 'true'
+ : false)
+ ? (true
+ ? 'true'
+ : false)
+ : (true
+ ? 'true'
+ : false);
+$var = ($var ? ($var1 ? 1 : 2) : $var2) ? 3 : 4;
`;
@@ -2316,7 +2324,7 @@ return (
'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
);
return $var1 + $var2;
-return $var ? $var1 ? 1 : 2 : $var2 ? 3 : 4;
+return $var ? ($var1 ? 1 : 2) : ($var2 ? 3 : 4);
return 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
? 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
: 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index 6e93a9de2..25d37ec1d 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -90,26 +90,26 @@ $category_color = get_field(Category_Meta::COLOR, 'category_' . $term_id)
?: 'gold';
$var = ($someOtherReallyReallyLongVariable
- ? $someOtherReallyReallyLongVariable
+ ? ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable
+ : $someOtherReallyReallyLongVariable)
: $someOtherReallyReallyLongVariable)
? $someOtherReallyReallyLongVariable
: $someOtherReallyReallyLongVariable;
$var = ($someOtherReallyReallyLongVariable
- ? $someOtherReallyReallyLongVariable
+ ? ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable
+ : $someOtherReallyReallyLongVariable)
: $someOtherReallyReallyLongVariable)
? $someOtherReallyReallyLongVariable
: $someOtherReallyReallyLongVariable;
$var = $someOtherReallyReallyLongVariable
- ? $someOtherReallyReallyLongVariable
+ ? ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable
+ : $someOtherReallyReallyLongVariable)
+ : ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable;
+ : $someOtherReallyReallyLongVariable);
$test = $foo
?: bar(
$someOtherReallyReallyLongVariable,
@@ -171,28 +171,28 @@ $var = 1
? $someOtherReallyReallyLongVariable
: $someOtherReallyReallyLongVariable
)->call();
-$var = $test ?: true ? 1 : 2;
+$var = $test ?: (true ? 1 : 2);
$var = $test
- ?: $someOtherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariable
+ ?: ($someOtherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariable
? 1
- : 2;
+ : 2);
$var = $test
- ?: true
+ ?: (true
? $someOtherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariable
- : 2;
+ : 2);
$var = $test
- ?: true
+ ?: (true
? 1
- : $someOtherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariable;
+ : $someOtherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariable);
$var = $test
? 'string'
- : $someOtherReallyReallyLongVariable
+ : ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable;
+ : $someOtherReallyReallyLongVariable);
$var = $test
- ? $someOtherReallyReallyLongVariable
+ ? ($someOtherReallyReallyLongVariable
? $someOtherReallyReallyLongVariable
- : $someOtherReallyReallyLongVariable
+ : $someOtherReallyReallyLongVariable)
: 'string';
$arr = [
From 34f89d9bfaaae314d8c00585b3da4fe58d256e9f Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 02:32:52 +0300
Subject: [PATCH 0018/1031] fix: `nowdoc/heredoc` newline (#624)
---
src/printer.js | 9 +++++
src/util.js | 31 +++++++++++++++-
tests/echo/__snapshots__/jsfmt.spec.js.snap | 32 ++++++++++++++++
tests/echo/echo.php | 16 ++++++++
.../encapsed/__snapshots__/jsfmt.spec.js.snap | 35 ++++++++++++++++++
tests/encapsed/encapsed.php | 17 +++++++++
tests/nowdoc/__snapshots__/jsfmt.spec.js.snap | 37 +++++++++++++++++++
tests/nowdoc/nowdoc.php | 18 +++++++++
8 files changed, 194 insertions(+), 1 deletion(-)
diff --git a/src/printer.js b/src/printer.js
index 9e4850267..2dbc42d01 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2041,8 +2041,17 @@ function printNode(path, options, print) {
);
case "echo": {
const printedArguments = path.map((argumentPath, index) => {
+ const argumentNode = argumentPath.getValue();
let printed = print(argumentPath);
+ if (
+ (argumentNode.kind === "encapsed" &&
+ argumentNode.type === "heredoc") ||
+ argumentNode.kind === "nowdoc"
+ ) {
+ return printed;
+ }
+
if (index === 0) {
printed = dedent(printed);
}
diff --git a/src/util.js b/src/util.js
index bc3ee31d2..b7fae92cf 100644
--- a/src/util.js
+++ b/src/util.js
@@ -241,7 +241,36 @@ function isFirstChildrenInlineNode(path) {
* appear as function arguments or array elements
*/
function docShouldHaveTrailingNewline(path) {
- return ["array", "parameter"].includes(path.getParentNode().kind);
+ const node = path.getValue();
+ const parent = path.getParentNode();
+
+ if (!parent) {
+ return false;
+ }
+
+ if (parent.kind === "echo") {
+ const lastIndex = parent.arguments.length - 1;
+ const index = parent.arguments.indexOf(node);
+
+ return index !== lastIndex;
+ }
+
+ if (parent.kind === "array") {
+ const lastIndex = parent.items.length - 1;
+ const index = parent.items.indexOf(node);
+
+ return index !== lastIndex;
+ }
+
+ if (parent.kind === "parameter") {
+ const parentParent = path.getParentNode(1);
+ const lastIndex = parentParent.arguments.length - 1;
+ const index = parentParent.arguments.indexOf(parent);
+
+ return index !== lastIndex;
+ }
+
+ return false;
}
function lineShouldEndWithSemicolon(path) {
diff --git a/tests/echo/__snapshots__/jsfmt.spec.js.snap b/tests/echo/__snapshots__/jsfmt.spec.js.snap
index 02095d8cb..1cf4008c5 100644
--- a/tests/echo/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/echo/__snapshots__/jsfmt.spec.js.snap
@@ -168,6 +168,22 @@ Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
+
+echo <<
Date: Thu, 13 Sep 2018 12:16:37 +0300
Subject: [PATCH 0019/1031] feat: better print empty closure (#623)
---
src/printer.js | 2 +-
.../closure/__snapshots__/jsfmt.spec.js.snap | 10 ++--
tests/closure/closure.php | 2 +
tests/parens/__snapshots__/jsfmt.spec.js.snap | 50 +++++--------------
4 files changed, 21 insertions(+), 43 deletions(-)
diff --git a/src/printer.js b/src/printer.js
index 2dbc42d01..0b278913f 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1811,7 +1811,7 @@ function printNode(path, options, print) {
indent(
concat([hasEmptyBody ? "" : hardline, path.call(print, "body")])
),
- concat([hardline, "}"])
+ concat([hasEmptyBody ? "" : hardline, "}"])
]);
}
case "retif": {
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index abcac1f0c..c6b3ae7cf 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -135,6 +135,8 @@ $var = function &() {
$func = static function() {};
$fn = function &() use (&$value) { return $value; };
+
+$var = function () {};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bar(
$longLongLongLongLongLongLongLongLongLongArg3
);
-$emptyFunc = function () {
-};
+$emptyFunc = function () {};
$emptyFuncWithComment = function () {
/* Comment */
@@ -267,10 +268,11 @@ $var = function &() {
return $collection;
};
-$func = static function () {
-};
+$func = static function () {};
$fn = function &() use (&$value) {
return $value;
};
+$var = function () {};
+
`;
diff --git a/tests/closure/closure.php b/tests/closure/closure.php
index 7373482bc..28e02fefc 100644
--- a/tests/closure/closure.php
+++ b/tests/closure/closure.php
@@ -132,3 +132,5 @@ function ($arg2) use ($var1) {
$func = static function() {};
$fn = function &() use (&$value) { return $value; };
+
+$var = function () {};
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index eb6cb1d26..08feb794f 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1203,51 +1203,25 @@ var_dump(...(function() use ($type) {})());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Thu, 13 Sep 2018 12:58:39 +0300
Subject: [PATCH 0020/1031] docs: issue template (#617)
---
.github/ISSUE_TEMPLATE/formatting.md | 44 ++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
create mode 100644 .github/ISSUE_TEMPLATE/formatting.md
diff --git a/.github/ISSUE_TEMPLATE/formatting.md b/.github/ISSUE_TEMPLATE/formatting.md
new file mode 100644
index 000000000..77870dd6a
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/formatting.md
@@ -0,0 +1,44 @@
+---
+name: ✨ Formatting
+about: Issues for ugly or incorrect code
+---
+
+
+
+**Prettier 1.14.2**
+
+**PHP Plugin 0.6.0**
+
+```sh
+# Options (if any):
+--single-quote
+```
+
+**Input:**
+
+```php
+// code snippet
+```
+
+**Output:**
+
+```php
+// code snippet
+```
+
+**Expected behavior:**
From 9da65abfc2a071d7b4c5f6cac524bcf518f944de Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 12:59:38 +0300
Subject: [PATCH 0021/1031] fix: broken cast in `retif` and `silence` node
(#628)
---
src/needs-parens.js | 39 ++++++----
tests/parens/__snapshots__/jsfmt.spec.js.snap | 76 +++++++++++++++++--
tests/parens/array.php | 9 +++
tests/parens/cast.php | 22 ++++++
4 files changed, 125 insertions(+), 21 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 7ba666872..b627914bb 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -188,26 +188,37 @@ function needsParens(path) {
}
case "closure":
return parent.kind === "call" && name === "what" && parent.what === node;
- case "string":
- case "array":
case "cast":
- if (
- name === "what" &&
- parent.what === node &&
- (isLookupNode(parent) || parent.kind === "call")
+ if (parent.kind === "bin") {
+ return true;
+ } else if (
+ parent.kind === "retif" &&
+ name === "test" &&
+ parent.test === node
) {
- if (node.kind === "string" && parent.kind === "offsetlookup") {
- return false;
- }
-
return true;
- }
-
- if (node.kind === "cast" && parent.kind === "bin") {
+ } else if (parent.kind === "silent") {
return true;
}
+ // else fallthrough
+ case "string":
+ case "array":
+ switch (parent.kind) {
+ case "propertylookup":
+ case "staticlookup":
+ case "offsetlookup":
+ case "call":
+ if (
+ ["string", "array"].includes(node.kind) &&
+ parent.kind === "offsetlookup"
+ ) {
+ return false;
+ }
- return false;
+ return name === "what" && parent.what === node;
+ default:
+ return false;
+ }
case "print":
case "include":
return parent.kind === "bin";
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 08feb794f..5dd3aabd5 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -38,6 +38,15 @@ $arr = ([('key') => ($var), ('other-key') => ($other_var)]);
[$var, $other_var] = $arr;
[($var), ($other_var)] = $arr;
[('key') => ($var), ('other-key') => ($other_var)] = $arr;
+
+$var = array(1, 2, 3)[1];
+$var = (array(1, 2, 3))[1];
+$var = [1, 2, 3][1];
+$var = ([1, 2, 3])[1];
+$var = array(new stdClass())[0];
+$var = (array((new stdClass())))[0];
+$var = [new stdClass()][0];
+$var = ([(new stdClass())])[0];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$var, 'other-key' => $other_var];
[$var, $other_var] = $arr;
['key' => $var, 'other-key' => $other_var] = $arr;
+$var = array(1, 2, 3)[1];
+$var = array(1, 2, 3)[1];
+$var = [1, 2, 3][1];
+$var = [1, 2, 3][1];
+$var = array(new stdClass())[0];
+$var = array(new stdClass())[0];
+$var = [new stdClass()][0];
+$var = [new stdClass()][0];
+
`;
exports[`assign.php 1`] = `
@@ -1031,8 +1049,30 @@ $var = ((bool) $var) ? ((bool) 1) : ((bool) 2);
$var = ((bool) $var) ? (bool) 1 + 2 : (bool) 2 + 3;
$var = ((bool) $var) ? ((bool) 1 + 2) : ((bool) 2 + 3);
$var = ((bool) $var) ? (bool) $var ? 1 : 2 : (bool) $var ? 1 : 2;
+$var = ((bool) $var) ? ((bool) $var) ? 1 : 2 : ((bool) $var) ? 1 : 2;
$var = ((bool) $var) ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
$var = (bool) ($var ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2));
+$var = (bool) $var->foo;
+$var = (bool) ($var->foo);
+$var = ((bool) $var->foo);
+$var = ((object) $var)->foo;
+$var = ((object) $var)[0];
+$var = (int) 'test' + (int) 'test';
+$var = ((int) 'test') + ((int) 'test');
+$var = ((int) 'test') * ((int) 'test');
+$var = ((int) 'test') | ((int) 'test');
+$var = ((int) 'test') % ((int) 'test');
+$var = @((int) 'test');
+$var = @((int) call());
+$var = (int) (int) 1;
+$var = (int) ((int) 1);
+$var = call((int) $var, (int) call(), (int) $minutes * 60);
+$var = call(((int) $var), ((int) call()), (int) ($minutes * 60));
+$var = $var + (int) $minutes * 60;
+$var = $var + (int) ($minutes * 60);
+
+return (int) $var;
+return ((int) $var);
if ((int) 1 === 1) {}
if (1 === (int) 1) {}
@@ -1081,14 +1121,36 @@ $var = (bool) ($var ? 1 : 2);
$var = (bool) ($var + 1 ? 1 : 2);
$var = ((bool) $var) + 1 ? 1 : 2;
$var = (bool) ($var + 1 ? 1 : 2);
-$var = (bool) $var ? 1 : 2;
-$var = (bool) $var ? (bool) 1 : (bool) 2;
-$var = (bool) $var ? (bool) 1 : (bool) 2;
-$var = (bool) $var ? (bool) (1 + 2) : (bool) (2 + 3);
-$var = (bool) $var ? (bool) (1 + 2) : (bool) (2 + 3);
-$var = (bool) $var ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
-$var = (bool) $var ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
+$var = ((bool) $var) ? 1 : 2;
+$var = ((bool) $var) ? (bool) 1 : (bool) 2;
+$var = ((bool) $var) ? (bool) 1 : (bool) 2;
+$var = ((bool) $var) ? (bool) (1 + 2) : (bool) (2 + 3);
+$var = ((bool) $var) ? (bool) (1 + 2) : (bool) (2 + 3);
+$var = ((bool) $var) ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
+$var = (((bool) $var) ? (((bool) $var) ? 1 : 2) : (bool) $var) ? 1 : 2;
+$var = ((bool) $var) ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
$var = (bool) ($var ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2));
+$var = (bool) $var->foo;
+$var = (bool) $var->foo;
+$var = (bool) $var->foo;
+$var = ((object) $var)->foo;
+$var = ((object) $var)[0];
+$var = (int) ('test' + ((int) 'test'));
+$var = ((int) 'test') + ((int) 'test');
+$var = ((int) 'test') * ((int) 'test');
+$var = ((int) 'test') | ((int) 'test');
+$var = ((int) 'test') % ((int) 'test');
+$var = @((int) 'test');
+$var = @((int) call());
+$var = (int) (int) 1;
+$var = (int) (int) 1;
+$var = call((int) $var, (int) call(), (int) ($minutes * 60));
+$var = call((int) $var, (int) call(), (int) ($minutes * 60));
+$var = $var + ((int) ($minutes * 60));
+$var = $var + ((int) ($minutes * 60));
+
+return (int) $var;
+return (int) $var;
if ((int) (1 === 1)) {
}
diff --git a/tests/parens/array.php b/tests/parens/array.php
index 2513d29f3..32f549314 100644
--- a/tests/parens/array.php
+++ b/tests/parens/array.php
@@ -35,3 +35,12 @@
[$var, $other_var] = $arr;
[($var), ($other_var)] = $arr;
[('key') => ($var), ('other-key') => ($other_var)] = $arr;
+
+$var = array(1, 2, 3)[1];
+$var = (array(1, 2, 3))[1];
+$var = [1, 2, 3][1];
+$var = ([1, 2, 3])[1];
+$var = array(new stdClass())[0];
+$var = (array((new stdClass())))[0];
+$var = [new stdClass()][0];
+$var = ([(new stdClass())])[0];
diff --git a/tests/parens/cast.php b/tests/parens/cast.php
index 2dd8ffc56..51f5bdddf 100644
--- a/tests/parens/cast.php
+++ b/tests/parens/cast.php
@@ -42,8 +42,30 @@
$var = ((bool) $var) ? (bool) 1 + 2 : (bool) 2 + 3;
$var = ((bool) $var) ? ((bool) 1 + 2) : ((bool) 2 + 3);
$var = ((bool) $var) ? (bool) $var ? 1 : 2 : (bool) $var ? 1 : 2;
+$var = ((bool) $var) ? ((bool) $var) ? 1 : 2 : ((bool) $var) ? 1 : 2;
$var = ((bool) $var) ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2);
$var = (bool) ($var ? (bool) ($var ? 1 : 2) : (bool) ($var ? 1 : 2));
+$var = (bool) $var->foo;
+$var = (bool) ($var->foo);
+$var = ((bool) $var->foo);
+$var = ((object) $var)->foo;
+$var = ((object) $var)[0];
+$var = (int) 'test' + (int) 'test';
+$var = ((int) 'test') + ((int) 'test');
+$var = ((int) 'test') * ((int) 'test');
+$var = ((int) 'test') | ((int) 'test');
+$var = ((int) 'test') % ((int) 'test');
+$var = @((int) 'test');
+$var = @((int) call());
+$var = (int) (int) 1;
+$var = (int) ((int) 1);
+$var = call((int) $var, (int) call(), (int) $minutes * 60);
+$var = call(((int) $var), ((int) call()), (int) ($minutes * 60));
+$var = $var + (int) $minutes * 60;
+$var = $var + (int) ($minutes * 60);
+
+return (int) $var;
+return ((int) $var);
if ((int) 1 === 1) {}
if (1 === (int) 1) {}
From 51897cce3f72e2779301715faf4eb6589ecfcad1 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 17:07:12 +0300
Subject: [PATCH 0022/1031] fix: ast compare (#550)
---
src/clean.js | 74 +++++++++---------
src/comments.js | 17 ++++-
src/printer.js | 13 ++--
src/util.js | 4 +-
.../comments/__snapshots__/jsfmt.spec.js.snap | 3 +-
tests/halt/__snapshots__/jsfmt.spec.js.snap | 38 ++++++++++
tests/halt/other-single.php | 12 +++
tests/halt/single.php | 2 +
tests/string/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/string/string.php | 3 +-
.../variable/__snapshots__/jsfmt.spec.js.snap | 14 +++-
tests/variable/variable.php | 7 +-
tests_config/run_spec.js | 75 ++++++-------------
13 files changed, 156 insertions(+), 112 deletions(-)
create mode 100644 tests/halt/other-single.php
create mode 100644 tests/halt/single.php
diff --git a/src/clean.js b/src/clean.js
index 3f6f2563c..cc47848a2 100644
--- a/src/clean.js
+++ b/src/clean.js
@@ -9,22 +9,41 @@ const util = require("./util");
* changed by the printer, etc.)
*/
function clean(node, newObj) {
- // Ignore `parenthesis` inside `parenthesis`
- if (node.kind === "parenthesis" && node.inner.kind === "parenthesis") {
- while (newObj.inner.kind === "parenthesis") {
- newObj.inner = newObj.inner.inner;
- }
- }
+ [
+ "loc",
+ "range",
+ "raw",
+ "comments",
+ "leadingComments",
+ "trailingComments",
+ "parenthesizedExpression",
+ "parent",
+ "prev",
+ "start",
+ "end",
+ "tokens",
+ "errors",
+ "extra"
+ ].forEach(name => {
+ delete newObj[name];
+ });
// continue ((2)); -> continue 2;
// continue 1; -> continue;
if ((node.kind === "continue" || node.kind === "break") && node.level) {
- let { level } = newObj;
- if (level.kind === "parenthesis") {
- level = level.inner;
- }
+ const { level } = newObj;
+
if (level.kind === "number") {
- newObj.level = level.value == 1 ? {} : level;
+ newObj.level = level.value == 1 ? null : level;
+ }
+ }
+
+ // if () {{ }} -> if () {}
+ if (node.kind === "block") {
+ if (node.children.length === 1 && node.children[0].kind === "block") {
+ while (newObj.children[0].kind === "block") {
+ newObj.children = newObj.children[0].children;
+ }
}
}
@@ -99,31 +118,6 @@ function clean(node, newObj) {
}
}
- // Ignore `parenthesis` for `return`
- if (node.kind === "return" && node.expr && node.expr.kind === "parenthesis") {
- newObj.expr = newObj.expr.inner;
- }
-
- // Ignore `parenthesis` for `print`
- if (node.kind === "print" && node.arguments.kind === "parenthesis") {
- newObj.arguments = newObj.arguments.inner;
- }
-
- // Ignore `parenthesis` for `echo`
- if (node.kind === "echo" && node.arguments.length > 0) {
- node.arguments.forEach((argument, index) => {
- newObj.arguments[index] =
- node.arguments[index].kind === "parenthesis"
- ? newObj.arguments[index].inner
- : newObj.arguments[index];
- });
- }
-
- // Ignore `parenthesis` for `include`
- if (node.kind === "include" && node.target.kind === "parenthesis") {
- newObj.target = newObj.target.inner;
- }
-
if (node.kind === "usegroup" && typeof node.name === "string") {
newObj.name = newObj.name.replace(/^\\/, "");
}
@@ -131,6 +125,14 @@ function clean(node, newObj) {
if (node.kind === "useitem") {
newObj.name = newObj.name.replace(/^\\/, "");
}
+
+ // TODO: remove after resolve https://github.com/glayzzle/php-parser/issues/181
+ if (node.kind === "unary" && ["+", "-"].includes(node.type)) {
+ return {
+ kind: "number",
+ value: `${newObj.type}${newObj.what.value}`
+ };
+ }
}
module.exports = clean;
diff --git a/src/comments.js b/src/comments.js
index d3d49e518..f2001772f 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -37,7 +37,8 @@ const handleOwnLineComment = (comment, text, options, ast, isLastComment) => {
handleTryCatch(comment) ||
handleAlternate(comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
- handleInlineComments(comment)
+ handleInlineComments(comment) ||
+ handleHalt(comment)
);
};
@@ -48,7 +49,8 @@ const handleEndOfLineComment = (comment, text, options, ast, isLastComment) => {
handleFunctionParameter(comment, text, options) ||
handleFunction(comment, text, options) ||
handleTryCatch(comment) ||
- handleOnlyComments(enclosingNode, ast, comment, isLastComment)
+ handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
+ handleHalt(comment)
);
};
@@ -241,11 +243,18 @@ const handleGoto = comment => {
};
const handleHalt = comment => {
- const { enclosingNode } = comment;
+ const { precedingNode, enclosingNode } = comment;
+
if (enclosingNode && enclosingNode.kind === "halt") {
- addTrailingComment(enclosingNode, comment);
+ addLeadingComment(enclosingNode, comment);
return true;
}
+
+ if (precedingNode && precedingNode.kind === "halt") {
+ addLeadingComment(precedingNode, comment);
+ return true;
+ }
+
return false;
};
diff --git a/src/printer.js b/src/printer.js
index 0b278913f..00c195f57 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -937,11 +937,12 @@ function printLines(path, options, print, childrenAttribute = "children") {
? ""
: hardline
: "";
- const after = shouldBreak
- ? isBlockNestedNode && isLastNode
- ? ""
- : hardline
- : "";
+ const after =
+ shouldBreak && childNode.kind !== "halt"
+ ? isBlockNestedNode && isLastNode
+ ? ""
+ : hardline
+ : "";
if (shouldBreak) {
beforeCloseTagInlineNode = "";
@@ -2029,7 +2030,7 @@ function printNode(path, options, print) {
case "silent":
return concat(["@", path.call(print, "expr")]);
case "halt":
- return concat(["__halt_compiler();", node.after.replace(/\n$/, "")]);
+ return concat(["__halt_compiler();", node.after]);
case "eval":
return group(
concat([
diff --git a/src/util.js b/src/util.js
index b7fae92cf..4cce82b8a 100644
--- a/src/util.js
+++ b/src/util.js
@@ -369,7 +369,7 @@ function fileShouldEndWithHardline(path) {
return false;
}
- if (lastNode && lastNode.kind === "inline") {
+ if (lastNode && ["halt", "inline"].includes(lastNode.kind)) {
return false;
}
@@ -380,7 +380,7 @@ function fileShouldEndWithHardline(path) {
const lastNestedNode =
lastNode.children.length > 0 && getLast(lastNode.children);
- if (lastNestedNode && lastNestedNode.kind === "inline") {
+ if (lastNestedNode && ["halt", "inline"].includes(lastNestedNode.kind)) {
return false;
}
}
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 9545002c9..8e99009e0 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1226,8 +1226,7 @@ exports[`halt.php 1`] = `
/*Comment*/__halt_compiler/*Comment*/(/*Comment*/)/*Comment*/;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Foo
+
+Foo
+
+Foo
+call()}.";
$string = "combined \${$encapsed}.";
-$string = "He drank some $juices[koolaid1] juice.";
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $string = "He drank some $juices[koolaid1] juice.";
$string = "$people->john drank some $juices[0] juice.";
$string = "This is { $great}";
$string = "This is {$great}";
@@ -204,7 +205,8 @@ $string = "encapsed variable {$value}.";
$string = "encapsed offset {$value[0]}.";
$string = "encapsed {$method->call()}.";
$string = "combined \${$encapsed}.";
-$string = "He drank some {$juices[koolaid1]} juice.";
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $string = "He drank some $juices[koolaid1] juice.";
$string = "{$people->john} drank some {$juices[0]} juice.";
$string = "This is { $great}";
$string = "This is {$great}";
diff --git a/tests/string/string.php b/tests/string/string.php
index 16c3966b8..4a5b17d04 100644
--- a/tests/string/string.php
+++ b/tests/string/string.php
@@ -43,7 +43,8 @@
$string = "encapsed offset {$value[0]}.";
$string = "encapsed {$method->call()}.";
$string = "combined ${$encapsed}.";
-$string = "He drank some $juices[koolaid1] juice.";
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $string = "He drank some $juices[koolaid1] juice.";
$string = "$people->john drank some $juices[0] juice.";
$string = "This is { $great}";
$string = "This is {$great}";
diff --git a/tests/variable/__snapshots__/jsfmt.spec.js.snap b/tests/variable/__snapshots__/jsfmt.spec.js.snap
index 335b704ce..38bd250f7 100644
--- a/tests/variable/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/variable/__snapshots__/jsfmt.spec.js.snap
@@ -27,7 +27,8 @@ This should print a capital 'A': \\x41
EOT;
$var = "He drank some $juice juice.".PHP_EOL;
$var = "He drank some $juices[0] juice.".PHP_EOL;
-$var = "He drank some $juices[koolaid1] juice.".PHP_EOL;
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $var = "He drank some $juices[koolaid1] juice.".PHP_EOL;
$var = "$people->john then said hello to $people->jane.".PHP_EOL;
$var = "{$foo->{$baz[1]}}\\n";
$var = $var->{$var->property};
@@ -44,12 +45,14 @@ $var = Foo::$bar['baz']();
$var = \${$foo['bar']['baz']};
$var = $foo->{$bar['baz']};
$var = $foo->{$bar['baz']}();
+// Todo https://github.com/glayzzle/php-parser/issues/167
// $var = Foo::{$bar['baz']}();
$var = ($$foo)['bar']['baz'];
$var = ($foo->$bar)['baz'];
$var = ($foo->$bar)['baz']();
-$var = (Foo::$bar)['baz']();
+// Todo https://github.com/glayzzle/php-parser/issues/182
+// $var = (Foo::$bar)['baz']();
$$$$$$$$$$var = "I like playing in the park";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -71,7 +74,8 @@ This should print a capital 'A': \\x41
EOT;
$var = "He drank some $juice juice." . PHP_EOL;
$var = "He drank some {$juices[0]} juice." . PHP_EOL;
-$var = "He drank some {$juices[koolaid1]} juice." . PHP_EOL;
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $var = "He drank some $juices[koolaid1] juice.".PHP_EOL;
$var = "{$people->john} then said hello to {$people->jane}." . PHP_EOL;
$var = "{$foo->{$baz[1]}}\\n";
$var = $var->{$var->property};
@@ -88,12 +92,14 @@ $var = Foo::$bar['baz']();
$var = \${$foo['bar']['baz']};
$var = $foo->{$bar['baz']};
$var = $foo->{$bar['baz']}();
+// Todo https://github.com/glayzzle/php-parser/issues/167
// $var = Foo::{$bar['baz']}();
$var = $$foo['bar']['baz'];
$var = $foo->$bar['baz'];
$var = $foo->$bar['baz']();
-$var = Foo::$bar['baz']();
+// Todo https://github.com/glayzzle/php-parser/issues/182
+// $var = (Foo::$bar)['baz']();
$$$$$$$$$$var = "I like playing in the park";
diff --git a/tests/variable/variable.php b/tests/variable/variable.php
index f68bb7757..04cffdfa1 100644
--- a/tests/variable/variable.php
+++ b/tests/variable/variable.php
@@ -15,7 +15,8 @@
EOT;
$var = "He drank some $juice juice.".PHP_EOL;
$var = "He drank some $juices[0] juice.".PHP_EOL;
-$var = "He drank some $juices[koolaid1] juice.".PHP_EOL;
+// Todo https://github.com/glayzzle/php-parser/issues/175
+// $var = "He drank some $juices[koolaid1] juice.".PHP_EOL;
$var = "$people->john then said hello to $people->jane.".PHP_EOL;
$var = "{$foo->{$baz[1]}}\n";
$var = $var->{$var->property};
@@ -32,11 +33,13 @@
$var = ${$foo['bar']['baz']};
$var = $foo->{$bar['baz']};
$var = $foo->{$bar['baz']}();
+// Todo https://github.com/glayzzle/php-parser/issues/167
// $var = Foo::{$bar['baz']}();
$var = ($$foo)['bar']['baz'];
$var = ($foo->$bar)['baz'];
$var = ($foo->$bar)['baz']();
-$var = (Foo::$bar)['baz']();
+// Todo https://github.com/glayzzle/php-parser/issues/182
+// $var = (Foo::$bar)['baz']();
$$$$$$$$$$var = "I like playing in the park";
diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js
index 86f51f7da..55fa0c9b0 100644
--- a/tests_config/run_spec.js
+++ b/tests_config/run_spec.js
@@ -3,9 +3,6 @@
const fs = require("fs");
const { extname } = require("path");
const prettier = require("prettier");
-const plugin = require("../src");
-const massageAST = require("prettier/src/main/massage-ast");
-const { normalize } = require("prettier/src/main/options");
const { AST_COMPARE } = process.env;
@@ -37,42 +34,37 @@ function run_spec(dirname, parsers, options) {
});
const output = prettyprint(source, path, mergedOptions);
test(`${filename} - ${mergedOptions.parser}-verify`, () => {
- expect(raw(`${source + "~".repeat(80)}\n${output}`)).toMatchSnapshot(
- filename
- );
+ expect(
+ raw(`${source + "~".repeat(mergedOptions.printWidth)}\n${output}`)
+ ).toMatchSnapshot(filename);
});
- parsers.slice(1).forEach(parserName => {
- test(`${filename} - ${parserName}-verify`, () => {
- const verifyOptions = Object.assign(mergedOptions, {
- parser: parserName
- });
+ parsers.slice(1).forEach(parser => {
+ const verifyOptions = Object.assign(mergedOptions, { parser });
+ test(`${filename} - ${parser}-verify`, () => {
const verifyOutput = prettyprint(source, path, verifyOptions);
expect(output).toEqual(verifyOutput);
});
});
// this will only work for php tests (since we're in the php repo)
- if (AST_COMPARE && parsers.slice(1) === "php") {
- const normalizedOptions = normalize(mergedOptions);
- const ast = parse(source, mergedOptions);
- const astMassaged = massageAST(ast, normalizedOptions);
- let ppastMassaged;
- let pperr = null;
- try {
- const ppast = parse(
- prettyprint(source, path, mergedOptions),
- mergedOptions
+ if (AST_COMPARE && parsers[0] === "php") {
+ const compareOptions = Object.assign({}, mergedOptions);
+
+ const astMassaged = parse(source, compareOptions);
+
+ let ppastMassaged = undefined;
+
+ expect(() => {
+ ppastMassaged = parse(
+ prettyprint(source, path, compareOptions),
+ compareOptions
);
- ppastMassaged = massageAST(ppast, normalizedOptions);
- } catch (e) {
- pperr = e.stack;
- }
+ }).not.toThrow();
+ expect(ppastMassaged).toBeDefined();
test(`${path} parse`, () => {
- expect(pperr).toBe(null);
- expect(ppastMassaged).toBeDefined();
- if (!ast.errors || ast.errors.length === 0) {
+ if (!astMassaged.errors || astMassaged.errors.length === 0) {
expect(astMassaged).toEqual(ppastMassaged);
}
});
@@ -80,34 +72,11 @@ function run_spec(dirname, parsers, options) {
}
});
}
-global.run_spec = run_spec;
-function stripLocation(ast) {
- if (Array.isArray(ast)) {
- return ast.map(e => stripLocation(e));
- }
- if (typeof ast === "object") {
- const newObj = {};
- for (const key in ast) {
- if (
- key === "loc" ||
- key === "range" ||
- key === "raw" ||
- key === "comments" ||
- key === "parent" ||
- key === "prev"
- ) {
- continue;
- }
- newObj[key] = stripLocation(ast[key]);
- }
- return newObj;
- }
- return ast;
-}
+global.run_spec = run_spec;
function parse(string, opts) {
- return stripLocation(plugin.parsers.php.parse(string, {}, opts));
+ return prettier.__debug.parse(string, opts, /* massage */ true).ast;
}
function prettyprint(src, filename, options) {
From 32e1fb0f363a63c215a5dff939bd3df90920d91e Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 17:34:37 +0300
Subject: [PATCH 0023/1031] refactor: remove unused stuff (#630)
---
src/index.js | 4 ----
src/util.js | 5 +----
2 files changed, 1 insertion(+), 8 deletions(-)
diff --git a/src/index.js b/src/index.js
index d3ac6818c..9b64c25c6 100644
--- a/src/index.js
+++ b/src/index.js
@@ -196,10 +196,6 @@ const printers = {
kinds: ["echo"],
children: { listNodes: ["arguments"], nodes: [] }
},
- {
- kinds: ["parenthesis"],
- children: { listNodes: [], nodes: ["inner"] }
- },
{
kinds: ["include"],
children: { listNodes: [], nodes: ["target"] }
diff --git a/src/util.js b/src/util.js
index 4cce82b8a..305885d69 100644
--- a/src/util.js
+++ b/src/util.js
@@ -274,10 +274,7 @@ function docShouldHaveTrailingNewline(path) {
}
function lineShouldEndWithSemicolon(path) {
- let node = path.getValue();
- while (node.kind === "parenthesis") {
- node = node.inner;
- }
+ const node = path.getValue();
const parentNode = path.getParentNode();
if (!parentNode) {
return false;
From a3c0ac25c9bb28e721ff0cd1cee88ff530739ad7 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 17:41:04 +0300
Subject: [PATCH 0024/1031] refactor: stuff (#631)
---
src/parser.js | 3 +-
src/printer.js | 735 ++++++++++++++++++++++++-------------------------
2 files changed, 364 insertions(+), 374 deletions(-)
diff --git a/src/parser.js b/src/parser.js
index 22feb4a73..fb4d7f8df 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -14,8 +14,7 @@ function parse(text, parsers, opts) {
// initialize a new parser instance
const parser = new engine({
parser: {
- extractDoc: true,
- extractTokens: true
+ extractDoc: true
},
ast: {
withPositions: true,
diff --git a/src/printer.js b/src/printer.js
index 00c195f57..f1b125b74 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1230,11 +1230,6 @@ function printNode(path, options, print) {
const node = path.getValue();
switch (node.kind) {
- case "block":
- return concat([
- printLines(path, options, print),
- comments.printDanglingComments(path, options, true)
- ]);
case "program": {
return group(
concat([
@@ -1248,6 +1243,43 @@ function printNode(path, options, print) {
])
);
}
+ case "block":
+ return concat([
+ printLines(path, options, print),
+ comments.printDanglingComments(path, options, true)
+ ]);
+ case "declare": {
+ const printDeclareArguments = path => {
+ const [directive] = Object.keys(path.getValue().what);
+ return concat([directive, "=", path.call(print, "what", directive)]);
+ };
+
+ if (node.mode === "short") {
+ return concat([
+ "declare(",
+ printDeclareArguments(path),
+ "):",
+ node.children.length > 0
+ ? concat([hardline, concat(path.map(print, "children"))])
+ : "",
+ hardline,
+ "enddeclare;"
+ ]);
+ } else if (node.mode === "block") {
+ return concat([
+ "declare(",
+ printDeclareArguments(path),
+ ") {",
+ node.children.length > 0
+ ? indent(concat([hardline, concat(path.map(print, "children"))]))
+ : "",
+ hardline,
+ "}"
+ ]);
+ }
+
+ return concat(["declare(", printDeclareArguments(path), ");"]);
+ }
case "namespace": {
const printed = printLines(path, options, print);
const hasName = node.name && typeof node.name === "string";
@@ -1270,6 +1302,44 @@ function printNode(path, options, print) {
node.withBrackets ? concat([hardline, "}"]) : ""
]);
}
+ case "usegroup":
+ return group(
+ concat([
+ "use ",
+ node.type ? concat([node.type, " "]) : "",
+ indent(
+ concat([
+ node.name
+ ? concat([
+ maybeStripLeadingSlashFromUse(node.name),
+ "\\{",
+ softline
+ ])
+ : "",
+ join(
+ concat([",", line]),
+ path.map(item => concat([print(item)]), "items")
+ )
+ ])
+ ),
+ node.name
+ ? concat([
+ ifBreak(shouldPrintComma(options) ? "," : ""),
+ softline,
+ "}"
+ ])
+ : ""
+ ])
+ );
+ case "useitem":
+ return concat([
+ node.type ? concat([node.type, " "]) : "",
+ maybeStripLeadingSlashFromUse(node.name),
+ hasDanglingComments(node)
+ ? concat([" ", comments.printDanglingComments(path, options, true)])
+ : "",
+ node.alias ? concat([" as ", node.alias]) : ""
+ ]);
case "class": {
const classPrefixes = [
...(node.isFinal ? ["final"] : []),
@@ -1364,6 +1434,66 @@ function printNode(path, options, print) {
options
});
}
+ case "interface":
+ return printDeclarationBlock({
+ declaration: concat([
+ concat(["interface ", node.name]),
+ node.extends
+ ? indent(
+ concat([
+ line,
+ "extends ",
+ join(", ", path.map(print, "extends"))
+ ])
+ )
+ : ""
+ ]),
+ bodyContents: printLines(path, options, print, "body"),
+ node,
+ path,
+ options
+ });
+ case "trait":
+ return printDeclarationBlock({
+ declaration: concat([
+ concat(["trait ", node.name]),
+ node.extends
+ ? indent(concat([line, "extends ", path.call(print, "extends")]))
+ : "",
+ node.implements
+ ? indent(
+ concat([
+ line,
+ "implements ",
+ join(", ", path.map(print, "implements"))
+ ])
+ )
+ : ""
+ ]),
+ bodyContents: printLines(path, options, print, "body"),
+ node,
+ path,
+ options
+ });
+ case "traitprecedence":
+ return concat([
+ path.call(print, "trait"),
+ "::",
+ node.method,
+ " insteadof ",
+ join(", ", path.map(print, "instead"))
+ ]);
+ case "traitalias":
+ return concat([
+ path.call(print, "trait"),
+ node.trait ? "::" : "",
+ node.method || "",
+ " as ",
+ join(" ", [
+ ...(node.visibility ? [node.visibility] : []),
+ ...(node.as ? [node.as] : [])
+ ])
+ ]);
case "function":
return printDeclarationBlock({
declaration: concat(["function ", node.byref ? "&" : "", node.name]),
@@ -1389,6 +1519,50 @@ function printNode(path, options, print) {
path,
options
});
+ case "closure": {
+ const hasEmptyBody =
+ node.body &&
+ node.body.children &&
+ node.body.children.length === 0 &&
+ !node.body.comments;
+
+ return concat([
+ node.isStatic ? "static " : "",
+ "function ",
+ node.byref ? "&" : "",
+ printArgumentsList(path, options, print),
+ node.uses && node.uses.length > 0
+ ? group(
+ concat([
+ " use ",
+ printArgumentsList(path, options, print, "uses")
+ ])
+ )
+ : "",
+ node.type
+ ? concat([
+ ": ",
+ hasDanglingComments(node.type)
+ ? concat([
+ path.call(
+ typePath =>
+ comments.printDanglingComments(typePath, options, true),
+ "type"
+ ),
+ " "
+ ])
+ : "",
+ node.nullable ? "?" : "",
+ path.call(print, "type")
+ ])
+ : "",
+ " {",
+ indent(
+ concat([hasEmptyBody ? "" : hardline, path.call(print, "body")])
+ ),
+ concat([hasEmptyBody ? "" : hardline, "}"])
+ ]);
+ }
case "method": {
const methodPrefixes = [
...(node.isFinal ? ["final"] : []),
@@ -1462,6 +1636,8 @@ function printNode(path, options, print) {
return name;
}
+ case "variadic":
+ return concat(["...", path.call(print, "what")]);
case "property":
return group(
concat([
@@ -1482,47 +1658,6 @@ function printNode(path, options, print) {
: ""
])
);
- case "interface":
- return printDeclarationBlock({
- declaration: concat([
- concat(["interface ", node.name]),
- node.extends
- ? indent(
- concat([
- line,
- "extends ",
- join(", ", path.map(print, "extends"))
- ])
- )
- : ""
- ]),
- bodyContents: printLines(path, options, print, "body"),
- node,
- path,
- options
- });
- case "trait":
- return printDeclarationBlock({
- declaration: concat([
- concat(["trait ", node.name]),
- node.extends
- ? indent(concat([line, "extends ", path.call(print, "extends")]))
- : "",
- node.implements
- ? indent(
- concat([
- line,
- "implements ",
- join(", ", path.map(print, "implements"))
- ])
- )
- : ""
- ]),
- bodyContents: printLines(path, options, print, "body"),
- node,
- path,
- options
- });
case "constant":
case "classconstant":
return concat([
@@ -1532,16 +1667,6 @@ function printNode(path, options, print) {
" = ",
path.call(print, "value")
]);
- case "assign": {
- return printAssignment(
- node.left,
- path.call(print, "left"),
- concat([" ", node.operator]),
- node.right,
- path.call(print, "right"),
- options
- );
- }
case "if": {
const handleIfAlternate = alternate => {
if (!alternate) {
@@ -1722,99 +1847,103 @@ function printNode(path, options, print) {
),
printBodyControlStructure(path, print, "body", options)
]);
- case "call": {
- // chain: Call (*LookupNode (Call (*LookupNode (...))))
- if (isLookupNode(node.what)) {
- return printMemberChain(path, options, print);
- }
-
+ case "case":
return concat([
- path.call(print, "what"),
- printArgumentsList(path, options, print)
- ]);
- }
- case "usegroup":
- return group(
- concat([
- "use ",
- node.type ? concat([node.type, " "]) : "",
- indent(
- concat([
- node.name
- ? concat([
- maybeStripLeadingSlashFromUse(node.name),
- "\\{",
- softline
- ])
- : "",
- join(
- concat([",", line]),
- path.map(item => concat([print(item)]), "items")
+ node.test
+ ? concat(["case ", path.call(print, "test"), ":"])
+ : "default:",
+ node.body
+ ? node.body.children && node.body.children.length
+ ? indent(
+ concat([
+ isFirstChildrenInlineNode(path) ? "" : hardline,
+ path.call(print, "body")
+ ])
)
- ])
- ),
- node.name
- ? concat([
- ifBreak(shouldPrintComma(options) ? "," : ""),
- softline,
- "}"
- ])
- : ""
- ])
- );
- case "useitem":
+ : ";"
+ : ""
+ ]);
+ case "break":
+ case "continue":
+ if (node.level) {
+ if (node.level.kind == "number" && node.level.value != 1) {
+ return concat([`${node.kind} `, path.call(print, "level")]);
+ }
+
+ return node.kind;
+ }
+
+ return node.kind;
+ case "call": {
+ // chain: Call (*LookupNode (Call (*LookupNode (...))))
+ if (isLookupNode(node.what)) {
+ return printMemberChain(path, options, print);
+ }
+
return concat([
- node.type ? concat([node.type, " "]) : "",
- maybeStripLeadingSlashFromUse(node.name),
- hasDanglingComments(node)
- ? concat([" ", comments.printDanglingComments(path, options, true)])
- : "",
- node.alias ? concat([" as ", node.alias]) : ""
+ path.call(print, "what"),
+ printArgumentsList(path, options, print)
]);
- case "closure": {
- const hasEmptyBody =
- node.body &&
- node.body.children &&
- node.body.children.length === 0 &&
- !node.body.comments;
+ }
+ case "new": {
+ // if the child node is an anonymous class, we need to store the arguments
+ // so the child class node can access them later
+ const isAnonymousClassNode =
+ node.what && node.what.kind === "class" && node.what.isAnonymous;
+
+ if (isAnonymousClassNode && node.arguments.length > 0) {
+ anonymousClassesNewArguments = printArgumentsList(path, options, print);
+ }
+
+ return group(
+ concat([
+ "new ",
+ path.call(print, "what"),
+ isAnonymousClassNode ? "" : printArgumentsList(path, options, print)
+ ])
+ );
+ }
+ case "clone":
+ return concat(["clone ", path.call(print, "what")]);
+ case "propertylookup":
+ case "staticlookup":
+ case "offsetlookup": {
+ const parent = path.getParentNode();
+
+ let firstNonMemberParent;
+ let i = 0;
+
+ do {
+ firstNonMemberParent = path.getParentNode(i);
+ i++;
+ } while (firstNonMemberParent && isLookupNode(firstNonMemberParent));
+
+ const shouldInline =
+ (firstNonMemberParent &&
+ (firstNonMemberParent.kind === "new" ||
+ (firstNonMemberParent.kind === "assign" &&
+ firstNonMemberParent.left.kind !== "variable"))) ||
+ node.kind === "offsetlookup" ||
+ (node.what.kind === "variable" &&
+ (node.offset.kind === "constref" ||
+ node.offset.kind === "variable") &&
+ (parent && !isLookupNode(parent)));
return concat([
- node.isStatic ? "static " : "",
- "function ",
- node.byref ? "&" : "",
- printArgumentsList(path, options, print),
- node.uses && node.uses.length > 0
- ? group(
- concat([
- " use ",
- printArgumentsList(path, options, print, "uses")
- ])
+ path.call(print, "what"),
+ shouldInline
+ ? printLookupNodes(path, options, print)
+ : group(
+ indent(concat([softline, printLookupNodes(path, options, print)]))
)
- : "",
- node.type
- ? concat([
- ": ",
- hasDanglingComments(node.type)
- ? concat([
- path.call(
- typePath =>
- comments.printDanglingComments(typePath, options, true),
- "type"
- ),
- " "
- ])
- : "",
- node.nullable ? "?" : "",
- path.call(print, "type")
- ])
- : "",
- " {",
- indent(
- concat([hasEmptyBody ? "" : hardline, path.call(print, "body")])
- ),
- concat([hasEmptyBody ? "" : hardline, "}"])
]);
}
+ case "constref":
+ if (typeof node.name === "object") {
+ return path.call(print, "name");
+ }
+
+ return node.name;
case "retif": {
const parts = [];
const parent = path.getParentNode();
@@ -1887,40 +2016,6 @@ function printNode(path, options, print) {
")"
])
);
- case "clone":
- return concat(["clone ", path.call(print, "what")]);
- case "declare": {
- const printDeclareArguments = path => {
- const [directive] = Object.keys(path.getValue().what);
- return concat([directive, "=", path.call(print, "what", directive)]);
- };
-
- if (node.mode === "short") {
- return concat([
- "declare(",
- printDeclareArguments(path),
- "):",
- node.children.length > 0
- ? concat([hardline, concat(path.map(print, "children"))])
- : "",
- hardline,
- "enddeclare;"
- ]);
- } else if (node.mode === "block") {
- return concat([
- "declare(",
- printDeclareArguments(path),
- ") {",
- node.children.length > 0
- ? indent(concat([hardline, concat(path.map(print, "children"))]))
- : "",
- hardline,
- "}"
- ]);
- }
-
- return concat(["declare(", printDeclareArguments(path), ");"]);
- }
case "global":
return group(
concat([
@@ -1928,31 +2023,6 @@ function printNode(path, options, print) {
indent(concat([join(concat([",", line]), path.map(print, "items"))]))
])
);
- case "static":
- return group(
- concat([
- "static ",
- indent(
- concat([
- join(
- concat([",", line]),
- path.map(item => {
- // @TODO: hacking this for now. assignments nested inside a static
- // declaration doesn't have the operator set, so printing manually
- if (item.getValue().kind === "assign") {
- return concat([
- item.call(print, "left"),
- " = ",
- item.call(print, "right")
- ]);
- }
- return item.call(print);
- }, "items")
- )
- ])
- )
- ])
- );
case "include":
return concat([
node.require ? "require" : "include",
@@ -1960,26 +2030,10 @@ function printNode(path, options, print) {
" ",
path.call(print, "target")
]);
+ case "label":
+ return concat([node.name, ":"]);
case "goto":
return concat(["goto ", node.label]);
- case "new": {
- // if the child node is an anonymous class, we need to store the arguments
- // so the child class node can access them later
- const isAnonymousClassNode =
- node.what && node.what.kind === "class" && node.what.isAnonymous;
-
- if (isAnonymousClassNode && node.arguments.length > 0) {
- anonymousClassesNewArguments = printArgumentsList(path, options, print);
- }
-
- return group(
- concat([
- "new ",
- path.call(print, "what"),
- isAnonymousClassNode ? "" : printArgumentsList(path, options, print)
- ])
- );
- }
case "try":
return concat([
"try {",
@@ -2072,6 +2126,48 @@ function printNode(path, options, print) {
case "print": {
return concat(["print ", path.call(print, "arguments")]);
}
+ case "return": {
+ const parts = [];
+
+ parts.push("return");
+
+ if (node.expr) {
+ const printedExpr = path.call(print, "expr");
+
+ if (comments.returnArgumentHasLeadingComment(options, node.expr)) {
+ parts.push(
+ concat([
+ " (",
+ indent(concat([hardline, printedExpr])),
+ hardline,
+ ")"
+ ])
+ );
+ } else if (node.expr.kind === "bin") {
+ parts.push(
+ group(
+ concat([
+ ifBreak(" (", " "),
+ indent(concat([softline, printedExpr])),
+ softline,
+ ifBreak(")")
+ ])
+ )
+ );
+ } else {
+ parts.push(" ", printedExpr);
+ }
+ }
+
+ if (hasDanglingComments(node)) {
+ parts.push(
+ " ",
+ comments.printDanglingComments(path, options, /* sameIndent */ true)
+ );
+ }
+
+ return concat(parts);
+ }
case "isset":
case "unset":
case "empty":
@@ -2086,12 +2182,31 @@ function printNode(path, options, print) {
path.call(print, "name"),
node.curly ? "}" : ""
]);
- case "constref":
- if (typeof node.name === "object") {
- return path.call(print, "name");
- }
-
- return node.name;
+ case "static":
+ return group(
+ concat([
+ "static ",
+ indent(
+ concat([
+ join(
+ concat([",", line]),
+ path.map(item => {
+ // @TODO: hacking this for now. assignments nested inside a static
+ // declaration doesn't have the operator set, so printing manually
+ if (item.getValue().kind === "assign") {
+ return concat([
+ item.call(print, "left"),
+ " = ",
+ item.call(print, "right")
+ ]);
+ }
+ return item.call(print);
+ }, "items")
+ )
+ ])
+ )
+ ])
+ );
case "list":
case "array": {
const open = node.shortForm ? "[" : concat([node.kind, "("]);
@@ -2152,6 +2267,15 @@ function printNode(path, options, print) {
{ shouldBreak }
);
}
+ case "entry":
+ return printAssignment(
+ node.key,
+ path.call(print, "key"),
+ " =>",
+ node.value,
+ path.call(print, "value"),
+ options
+ );
case "yield":
return concat([
"yield",
@@ -2161,45 +2285,24 @@ function printNode(path, options, print) {
]);
case "yieldfrom":
return concat(["yield from ", path.call(print, "value")]);
- case "variadic":
- return concat(["...", path.call(print, "what")]);
- case "propertylookup":
- case "staticlookup":
- case "offsetlookup": {
- const parent = path.getParentNode();
-
- let firstNonMemberParent;
- let i = 0;
-
- do {
- firstNonMemberParent = path.getParentNode(i);
- i++;
- } while (firstNonMemberParent && isLookupNode(firstNonMemberParent));
-
- const shouldInline =
- (firstNonMemberParent &&
- (firstNonMemberParent.kind === "new" ||
- (firstNonMemberParent.kind === "assign" &&
- firstNonMemberParent.left.kind !== "variable"))) ||
- node.kind === "offsetlookup" ||
- (node.what.kind === "variable" &&
- (node.offset.kind === "constref" ||
- node.offset.kind === "variable") &&
- (parent && !isLookupNode(parent)));
-
- return concat([
- path.call(print, "what"),
- shouldInline
- ? printLookupNodes(path, options, print)
- : group(
- indent(concat([softline, printLookupNodes(path, options, print)]))
- )
- ]);
- }
+ case "unary":
+ return concat([node.type, path.call(print, "what")]);
case "pre":
return concat([node.type + node.type, path.call(print, "what")]);
case "post":
return concat([path.call(print, "what"), node.type + node.type]);
+ case "cast":
+ return concat(["(", node.type, ") ", path.call(print, "what")]);
+ case "assign": {
+ return printAssignment(
+ node.left,
+ path.call(print, "left"),
+ concat([" ", node.operator]),
+ node.right,
+ path.call(print, "right"),
+ options
+ );
+ }
case "bin": {
const parent = path.getParentNode();
const parentParent = path.getParentNode(1);
@@ -2281,12 +2384,10 @@ function printNode(path, options, print) {
])
);
}
- case "unary":
- return concat([node.type, path.call(print, "what")]);
- case "cast":
- return concat(["(", node.type, ") ", path.call(print, "what")]);
case "boolean":
return node.value ? "true" : "false";
+ case "number":
+ return printNumber(node.value);
case "string": {
// @TODO: need resolve https://github.com/glayzzle/php-parser/issues/101
// @TODO: for now just reusing double/single quote preference from doc. could eventually
@@ -2316,8 +2417,6 @@ function printNode(path, options, print) {
quote
]);
}
- case "number":
- return printNumber(node.value);
case "encapsed":
if (node.type === "offset") {
return group(concat(path.map(print, "value")));
@@ -2406,93 +2505,6 @@ function printNode(path, options, print) {
return isLowerCase ? lowerCasedName : node.name;
}
- case "case":
- return concat([
- node.test
- ? concat(["case ", path.call(print, "test"), ":"])
- : "default:",
- node.body
- ? node.body.children && node.body.children.length
- ? indent(
- concat([
- isFirstChildrenInlineNode(path) ? "" : hardline,
- path.call(print, "body")
- ])
- )
- : ";"
- : ""
- ]);
- case "break":
- if (node.level) {
- if (node.level.kind == "number" && node.level.value != 1) {
- return concat(["break ", path.call(print, "level")]);
- }
-
- return "break";
- }
-
- return "break";
- case "continue":
- if (node.level) {
- if (node.level.kind == "number" && node.level.value != 1) {
- return concat(["continue ", path.call(print, "level")]);
- }
-
- return "continue";
- }
-
- return "continue";
- case "return": {
- const parts = [];
-
- parts.push("return");
-
- if (node.expr) {
- const printedExpr = path.call(print, "expr");
-
- if (comments.returnArgumentHasLeadingComment(options, node.expr)) {
- parts.push(
- concat([
- " (",
- indent(concat([hardline, printedExpr])),
- hardline,
- ")"
- ])
- );
- } else if (node.expr.kind === "bin") {
- parts.push(
- group(
- concat([
- ifBreak(" (", " "),
- indent(concat([softline, printedExpr])),
- softline,
- ifBreak(")")
- ])
- )
- );
- } else {
- parts.push(" ", printedExpr);
- }
- }
-
- if (hasDanglingComments(node)) {
- parts.push(
- " ",
- comments.printDanglingComments(path, options, /* sameIndent */ true)
- );
- }
-
- return concat(parts);
- }
- case "entry":
- return printAssignment(
- node.key,
- path.call(print, "key"),
- " =>",
- node.value,
- path.call(print, "value"),
- options
- );
case "traituse":
return group(
concat([
@@ -2513,27 +2525,6 @@ function printNode(path, options, print) {
: ""
])
);
- case "traitprecedence":
- return concat([
- path.call(print, "trait"),
- "::",
- node.method,
- " insteadof ",
- join(", ", path.map(print, "instead"))
- ]);
- case "traitalias":
- return concat([
- path.call(print, "trait"),
- node.trait ? "::" : "",
- node.method || "",
- " as ",
- join(" ", [
- ...(node.visibility ? [node.visibility] : []),
- ...(node.as ? [node.as] : [])
- ])
- ]);
- case "label":
- return concat([node.name, ":"]);
case "error":
default:
return `Have not implemented kind ${node.kind} yet.`;
From b10d79382909863a8352c1a70c7eb33b4e045aae Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Thu, 13 Sep 2018 19:03:20 +0300
Subject: [PATCH 0025/1031] fix: identifier printing (#632)
---
src/clean.js | 3 +-
src/printer.js | 9 +-
.../__snapshots__/jsfmt.spec.js.snap | 170 ++++++++++++++++++
tests/identifier/identifier.php | 51 ++++++
tests/identifier/jsfmt.spec.js | 1 +
5 files changed, 232 insertions(+), 2 deletions(-)
create mode 100644 tests/identifier/__snapshots__/jsfmt.spec.js.snap
create mode 100644 tests/identifier/identifier.php
create mode 100644 tests/identifier/jsfmt.spec.js
diff --git a/src/clean.js b/src/clean.js
index cc47848a2..7b907a1a9 100644
--- a/src/clean.js
+++ b/src/clean.js
@@ -70,7 +70,8 @@ function clean(node, newObj) {
"void",
"iterable",
"object",
- "self"
+ "self",
+ "parent"
].indexOf(lowerCasedName) !== -1;
newObj.name = isLowerCase ? lowerCasedName : node.name;
diff --git a/src/printer.js b/src/printer.js
index f1b125b74..cbbf0b500 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2481,6 +2481,12 @@ function printNode(path, options, print) {
docShouldHaveTrailingNewline(path) ? hardline : ""
]);
case "identifier": {
+ const parent = path.getParentNode();
+
+ if (parent.kind === "constref" && node.name.toLowerCase() !== "null") {
+ return node.name;
+ }
+
// this is a hack until https://github.com/glayzzle/php-parser/issues/113 is resolved
// for reserved words we prefer lowercase case
if (node.name === "\\array") {
@@ -2500,7 +2506,8 @@ function printNode(path, options, print) {
"void",
"iterable",
"object",
- "self"
+ "self",
+ "parent"
].indexOf(lowerCasedName) !== -1;
return isLowerCase ? lowerCasedName : node.name;
diff --git a/tests/identifier/__snapshots__/jsfmt.spec.js.snap b/tests/identifier/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000..0f752bc73
--- /dev/null
+++ b/tests/identifier/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,170 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`identifier.php 1`] = `
+{int};
+$var = $array->{iNt};
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+{int};
+$var = $array->{iNt};
+
+`;
diff --git a/tests/identifier/identifier.php b/tests/identifier/identifier.php
new file mode 100644
index 000000000..8d45e7da9
--- /dev/null
+++ b/tests/identifier/identifier.php
@@ -0,0 +1,51 @@
+{int};
+$var = $array->{iNt};
diff --git a/tests/identifier/jsfmt.spec.js b/tests/identifier/jsfmt.spec.js
new file mode 100644
index 000000000..e395434ee
--- /dev/null
+++ b/tests/identifier/jsfmt.spec.js
@@ -0,0 +1 @@
+run_spec(__dirname, ["php"]);
From 2f8df75a89b985af26fe3f727bf81d379e08c4ca Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Fri, 14 Sep 2018 12:46:14 +0300
Subject: [PATCH 0026/1031] refactor: stuff (#634)
---
src/printer.js | 44 ++++++++++++++++++++------------------------
1 file changed, 20 insertions(+), 24 deletions(-)
diff --git a/src/printer.js b/src/printer.js
index cbbf0b500..784b5c957 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1494,6 +1494,26 @@ function printNode(path, options, print) {
...(node.as ? [node.as] : [])
])
]);
+ case "traituse":
+ return group(
+ concat([
+ "use ",
+ indent(group(join(concat([",", line]), path.map(print, "traits")))),
+ node.adaptations
+ ? concat([
+ " {",
+ indent(
+ concat([
+ hardline,
+ printLines(path, options, print, "adaptations")
+ ])
+ ),
+ hardline,
+ "}"
+ ])
+ : ""
+ ])
+ );
case "function":
return printDeclarationBlock({
declaration: concat(["function ", node.byref ? "&" : "", node.name]),
@@ -2389,10 +2409,6 @@ function printNode(path, options, print) {
case "number":
return printNumber(node.value);
case "string": {
- // @TODO: need resolve https://github.com/glayzzle/php-parser/issues/101
- // @TODO: for now just reusing double/single quote preference from doc. could eventually
- // use setting from options. need to figure out how this works w/ complex strings and interpolation
- // also need to figure out splitting long strings
const quote = node.isDoubleQuote ? '"' : "'";
let stringValue = node.raw;
@@ -2512,26 +2528,6 @@ function printNode(path, options, print) {
return isLowerCase ? lowerCasedName : node.name;
}
- case "traituse":
- return group(
- concat([
- "use ",
- indent(group(join(concat([",", line]), path.map(print, "traits")))),
- node.adaptations
- ? concat([
- " {",
- indent(
- concat([
- hardline,
- printLines(path, options, print, "adaptations")
- ])
- ),
- hardline,
- "}"
- ])
- : ""
- ])
- );
case "error":
default:
return `Have not implemented kind ${node.kind} yet.`;
From c1e52f1bd67dfb91dd80a025af94cdcba9f6d173 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Fri, 14 Sep 2018 14:04:59 +0300
Subject: [PATCH 0027/1031] feat: avoid unnecessary parens around pre and post
nodes (#635)
---
src/needs-parens.js | 5 ++--
tests/parens/__snapshots__/jsfmt.spec.js.snap | 26 +++++++++++++++++--
tests/parens/pre-post.php | 8 ++++++
tests/parens/unary.php | 3 +++
4 files changed, 38 insertions(+), 4 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index b627914bb..97416b882 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -50,8 +50,9 @@ function needsParens(path) {
case "post":
if (parent.kind === "unary") {
return (
- (node.type === "+" && parent.type === "+") ||
- (node.type === "-" && parent.type === "-")
+ node.kind === "pre" &&
+ ((node.type === "+" && parent.type === "+") ||
+ (node.type === "-" && parent.type === "-"))
);
}
// else fallthrough
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 5dd3aabd5..1e6cd15c2 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -2043,10 +2043,18 @@ $var = +(++$var);
$var = -(++$var);
$var = ~(++$var);
+$var = +(--$var);
+$var = -(--$var);
+$var = ~(--$var);
+
$var = +($var++);
$var = -($var++);
$var = ~($var++);
+$var = +($var--);
+$var = -($var--);
+$var = ~($var--);
+
$var = ++$var ** 2;
$var = (++$var) ** 2;
@@ -2094,10 +2102,18 @@ $var = +(++$var);
$var = -++$var;
$var = ~++$var;
-$var = +($var++);
+$var = +--$var;
+$var = -(--$var);
+$var = ~--$var;
+
+$var = +$var++;
$var = -$var++;
$var = ~$var++;
+$var = +$var--;
+$var = -$var--;
+$var = ~$var--;
+
$var = (++$var) ** 2;
$var = (++$var) ** 2;
@@ -2105,7 +2121,7 @@ $var = ($var++) ** 2;
$var = ($var++) ** 2;
$var = +(+(++$var));
-$var = +(+($var++));
+$var = +(+$var++);
$var = ~-++$var;
$var = ~-$var++;
@@ -2457,6 +2473,9 @@ $var = +(+$var);
$var = (-$var);
$var = -(-$var);
+$var = -(+$var);
+$var = +(-$var);
+
$var = (~$var);
$var = ~(~$var);
@@ -2517,6 +2536,9 @@ $var = +(+$var);
$var = -$var;
$var = -(-$var);
+$var = -+$var;
+$var = +-$var;
+
$var = ~$var;
$var = ~~$var;
diff --git a/tests/parens/pre-post.php b/tests/parens/pre-post.php
index 645681ae8..840e50c34 100644
--- a/tests/parens/pre-post.php
+++ b/tests/parens/pre-post.php
@@ -19,10 +19,18 @@
$var = -(++$var);
$var = ~(++$var);
+$var = +(--$var);
+$var = -(--$var);
+$var = ~(--$var);
+
$var = +($var++);
$var = -($var++);
$var = ~($var++);
+$var = +($var--);
+$var = -($var--);
+$var = ~($var--);
+
$var = ++$var ** 2;
$var = (++$var) ** 2;
diff --git a/tests/parens/unary.php b/tests/parens/unary.php
index 0ef766701..8a1c44978 100644
--- a/tests/parens/unary.php
+++ b/tests/parens/unary.php
@@ -27,6 +27,9 @@
$var = (-$var);
$var = -(-$var);
+$var = -(+$var);
+$var = +(-$var);
+
$var = (~$var);
$var = ~(~$var);
From 012ca50e8f78970e083cb6e40c20ee01bdf82854 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 13:17:29 +0300
Subject: [PATCH 0028/1031] feat: better print static (#637)
---
src/needs-parens.js | 2 +
src/printer.js | 46 +++++-----
tests/static/__snapshots__/jsfmt.spec.js.snap | 86 +++++++++++++++++++
tests/static/static.php | 25 ++++++
4 files changed, 139 insertions(+), 20 deletions(-)
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 97416b882..868a84123 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -166,6 +166,8 @@ function needsParens(path) {
return false;
} else if (parent.kind === "assign") {
return false;
+ } else if (parent.kind === "static") {
+ return false;
}
return true;
diff --git a/src/printer.js b/src/printer.js
index 784b5c957..5f85ed078 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2202,31 +2202,36 @@ function printNode(path, options, print) {
path.call(print, "name"),
node.curly ? "}" : ""
]);
- case "static":
+ case "static": {
+ const printed = path.map(childPath => {
+ return print(childPath);
+ }, "items");
+
+ const hasValue = node.items.some(item => item.kind === "assign");
+
+ let firstVariable;
+
+ if (printed.length === 1) {
+ [firstVariable] = printed;
+ } else if (printed.length > 1) {
+ // Indent first item
+ firstVariable = indent(printed[0]);
+ }
+
return group(
concat([
- "static ",
+ "static",
+ firstVariable ? concat([" ", firstVariable]) : "",
indent(
- concat([
- join(
- concat([",", line]),
- path.map(item => {
- // @TODO: hacking this for now. assignments nested inside a static
- // declaration doesn't have the operator set, so printing manually
- if (item.getValue().kind === "assign") {
- return concat([
- item.call(print, "left"),
- " = ",
- item.call(print, "right")
- ]);
- }
- return item.call(print);
- }, "items")
- )
- ])
+ concat(
+ printed
+ .slice(1)
+ .map(p => concat([",", hasValue ? hardline : line, p]))
+ )
)
])
);
+ }
case "list":
case "array": {
const open = node.shortForm ? "[" : concat([node.kind, "("]);
@@ -2317,7 +2322,8 @@ function printNode(path, options, print) {
return printAssignment(
node.left,
path.call(print, "left"),
- concat([" ", node.operator]),
+ // Assignments nested inside a static declaration doesn't have the operator set, so printing manually
+ concat([" ", node.operator ? node.operator : "="]),
node.right,
path.call(print, "right"),
options
diff --git a/tests/static/__snapshots__/jsfmt.spec.js.snap b/tests/static/__snapshots__/jsfmt.spec.js.snap
index 539d486c5..183cee7f5 100644
--- a/tests/static/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/static/__snapshots__/jsfmt.spec.js.snap
@@ -3,14 +3,100 @@
exports[`static.php 1`] = `
'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'], $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ],
+ $b = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ];
+static $a = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
+static $a, $b = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'],
+ $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ],
+ $b = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ];
+static $a =
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string';
+static $a,
+ $b =
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string';
`;
diff --git a/tests/static/static.php b/tests/static/static.php
index 103bb15eb..833446aa4 100644
--- a/tests/static/static.php
+++ b/tests/static/static.php
@@ -1,4 +1,29 @@
'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'], $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ],
+ $b = [
+ 'key' => 'value',
+ 'other-key' => 'other-value'
+ ];
+static $a = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
+static $a, $b = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
From 10581b982e6cd458849556904b204ef82923bf4d Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 14:40:59 +0300
Subject: [PATCH 0029/1031] refactor: comments (#638)
---
src/comments.js | 121 ++++++++++++++++++++++++------------------------
1 file changed, 60 insertions(+), 61 deletions(-)
diff --git a/src/comments.js b/src/comments.js
index f2001772f..ce23bed46 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -27,54 +27,59 @@ args:
isLastComment
*/
-const handleOwnLineComment = (comment, text, options, ast, isLastComment) => {
+function handleOwnLineComment(comment, text, options, ast, isLastComment) {
const { precedingNode, enclosingNode, followingNode } = comment;
return (
- handleClass(comment) ||
- handleFunctionParameter(comment, text, options) ||
- handleFunction(comment, text, options) ||
+ handleClassComments(enclosingNode, followingNode, comment) ||
+ handleFunctionParameter(text, enclosingNode, comment, options) ||
+ handleFunction(text, enclosingNode, followingNode, comment, options) ||
handleForComments(enclosingNode, precedingNode, followingNode, comment) ||
- handleTryCatch(comment) ||
- handleAlternate(comment) ||
+ handleTryCatch(enclosingNode, comment) ||
+ handleAlternate(enclosingNode, followingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
- handleInlineComments(comment) ||
- handleHalt(comment)
+ handleInlineComments(
+ enclosingNode,
+ precedingNode,
+ followingNode,
+ comment
+ ) ||
+ handleHalt(enclosingNode, precedingNode, comment)
);
-};
+}
-const handleEndOfLineComment = (comment, text, options, ast, isLastComment) => {
- const { enclosingNode } = comment;
+function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
+ const { precedingNode, enclosingNode, followingNode } = comment;
return (
- handleClass(comment) ||
- handleFunctionParameter(comment, text, options) ||
- handleFunction(comment, text, options) ||
- handleTryCatch(comment) ||
+ handleClassComments(enclosingNode, followingNode, comment) ||
+ handleFunctionParameter(text, enclosingNode, comment, options) ||
+ handleFunction(text, enclosingNode, followingNode, comment, options) ||
+ handleTryCatch(enclosingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
- handleHalt(comment)
+ handleHalt(enclosingNode, precedingNode, comment)
);
-};
+}
-const handleRemainingComment = (comment, text, options, ast, isLastComment) => {
- const { enclosingNode } = comment;
+function handleRemainingComment(comment, text, options, ast, isLastComment) {
+ const { precedingNode, enclosingNode, followingNode } = comment;
return (
- handleClass(comment) ||
- handleFunctionParameter(comment, text, options) ||
- handleFunction(comment, text, options) ||
- handleTryCatch(comment) ||
- handleBreakAndContinueStatementComments(comment) ||
- handleGoto(comment) ||
- handleHalt(comment) ||
- handleCall(comment) ||
+ handleClassComments(enclosingNode, followingNode, comment) ||
+ handleFunctionParameter(text, enclosingNode, comment, options) ||
+ handleFunction(text, enclosingNode, followingNode, comment, options) ||
+ handleTryCatch(enclosingNode, comment) ||
+ handleBreakAndContinueStatementComments(enclosingNode, comment) ||
+ handleGoto(enclosingNode, comment) ||
+ handleHalt(enclosingNode, precedingNode, comment) ||
+ handleCall(enclosingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
);
-};
+}
-const handleForComments = (
+function handleForComments(
enclosingNode,
precedingNode,
followingNode,
comment
-) => {
+) {
if (
enclosingNode &&
(enclosingNode.kind === "for" || enclosingNode.kind === "foreach")
@@ -90,10 +95,9 @@ const handleForComments = (
}
return false;
-};
+}
-const handleClass = comment => {
- const { enclosingNode, followingNode } = comment;
+function handleClassComments(enclosingNode, followingNode, comment) {
if (enclosingNode && enclosingNode.kind === "class") {
// for extends nodes that have leading comments, we can store them as
// dangling comments so we can handle them in the printer
@@ -123,10 +127,9 @@ const handleClass = comment => {
}
}
return false;
-};
+}
-const handleFunction = (comment, text, options) => {
- const { enclosingNode, followingNode } = comment;
+function handleFunction(text, enclosingNode, followingNode, comment, options) {
if (
enclosingNode &&
(enclosingNode.kind === "function" || enclosingNode.kind === "method")
@@ -178,10 +181,9 @@ const handleFunction = (comment, text, options) => {
}
}
return false;
-};
+}
-const handleFunctionParameter = (comment, text, options) => {
- const { enclosingNode } = comment;
+function handleFunctionParameter(text, enclosingNode, comment, options) {
if (
!enclosingNode ||
!["function", "method", "parameter"].includes(enclosingNode.kind)
@@ -202,10 +204,9 @@ const handleFunctionParameter = (comment, text, options) => {
return true;
}
return false;
-};
+}
-const handleTryCatch = comment => {
- const { enclosingNode } = comment;
+function handleTryCatch(enclosingNode, comment) {
if (
enclosingNode &&
(enclosingNode.kind === "try" || enclosingNode.kind === "catch")
@@ -218,10 +219,9 @@ const handleTryCatch = comment => {
}
}
return false;
-};
+}
-function handleBreakAndContinueStatementComments(comment) {
- const { enclosingNode } = comment;
+function handleBreakAndContinueStatementComments(enclosingNode, comment) {
if (
enclosingNode &&
(enclosingNode.kind === "continue" || enclosingNode.kind === "break") &&
@@ -233,18 +233,15 @@ function handleBreakAndContinueStatementComments(comment) {
return false;
}
-const handleGoto = comment => {
- const { enclosingNode } = comment;
+function handleGoto(enclosingNode, comment) {
if (enclosingNode && enclosingNode.kind === "goto") {
addTrailingComment(enclosingNode, comment);
return true;
}
return false;
-};
-
-const handleHalt = comment => {
- const { precedingNode, enclosingNode } = comment;
+}
+function handleHalt(enclosingNode, precedingNode, comment) {
if (enclosingNode && enclosingNode.kind === "halt") {
addLeadingComment(enclosingNode, comment);
return true;
@@ -256,10 +253,9 @@ const handleHalt = comment => {
}
return false;
-};
+}
-const handleCall = comment => {
- const { enclosingNode } = comment;
+function handleCall(enclosingNode, comment) {
if (
enclosingNode &&
enclosingNode.kind === "call" &&
@@ -269,10 +265,9 @@ const handleCall = comment => {
return true;
}
return false;
-};
+}
-const handleAlternate = comment => {
- const { enclosingNode, followingNode } = comment;
+function handleAlternate(enclosingNode, followingNode, comment) {
if (
enclosingNode &&
enclosingNode.kind === "if" &&
@@ -283,10 +278,14 @@ const handleAlternate = comment => {
return true;
}
return false;
-};
+}
-const handleInlineComments = comment => {
- const { precedingNode, enclosingNode, followingNode } = comment;
+function handleInlineComments(
+ enclosingNode,
+ precedingNode,
+ followingNode,
+ comment
+) {
if (!enclosingNode && followingNode && followingNode.kind === "inline") {
return true;
} else if (
@@ -299,7 +298,7 @@ const handleInlineComments = comment => {
return true;
}
return false;
-};
+}
function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
if (
From 7f0c94d018b625a0d96430ebabffb39588c56736 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 15:08:49 +0300
Subject: [PATCH 0030/1031] feat: better print variable comments (#639)
---
src/comments.js | 18 +-
.../comments/__snapshots__/jsfmt.spec.js.snap | 184 ++++++++++++++++++
tests/comments/variable.php | 84 ++++++++
3 files changed, 285 insertions(+), 1 deletion(-)
create mode 100644 tests/comments/variable.php
diff --git a/src/comments.js b/src/comments.js
index ce23bed46..a50ad2635 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -55,7 +55,8 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
handleFunction(text, enclosingNode, followingNode, comment, options) ||
handleTryCatch(enclosingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
- handleHalt(enclosingNode, precedingNode, comment)
+ handleHalt(enclosingNode, precedingNode, comment) ||
+ handleVariableComments(enclosingNode, followingNode, comment)
);
}
@@ -316,6 +317,21 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
return false;
}
+function handleVariableComments(enclosingNode, followingNode, comment) {
+ if (
+ enclosingNode &&
+ enclosingNode.kind === "assign" &&
+ followingNode &&
+ (followingNode.kind === "array" ||
+ followingNode.kind === "string" ||
+ followingNode.kind === "encapsed")
+ ) {
+ addLeadingComment(followingNode, comment);
+ return true;
+ }
+ return false;
+}
+
// https://github.com/prettier/prettier/blob/master/src/main/comments.js#L335
function printComment(commentPath, options) {
const comment = commentPath.getValue();
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 8e99009e0..6ec9c8404 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1898,6 +1898,190 @@ use some\\namespaces\\{
`;
+exports[`variable.php 1`] = `
+ 'val'
+];
+
+$obj // Comment
+= [
+'key' => 'val'
+];
+
+$obj = [ // Comment
+'key' => 'val'
+];
+
+$obj = [
+// Comment
+'key' => 'val'
+];
+
+$obj = // Comment
+[
+'val'
+];
+
+$obj // Comment
+= [
+'val'
+];
+
+$obj = [ // Comment
+'val'
+];
+
+$obj = [
+// Comment
+'val'
+];
+
+$obj = // Comment
+'val';
+
+$obj = // Comment
+'
+val
+val
+';
+
+$obj = // Comment
+ '
+ val
+ val
+ ';
+
+$obj = // Comment
+ "val";
+
+$obj = // Comment
+"
+val
+val
+";
+
+$obj = // Comment
+ "
+ val
+ val
+ ";
+
+$obj = // Comment
+ "val \${$var}";
+
+$obj = // Comment
+"
+val
+\${$var}
+";
+
+$obj = // Comment
+ "
+ val
+ \${$var}
+ ";
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ 'val'
+ ];
+
+$obj =
+ // Comment
+ [
+ 'key' => 'val'
+ ];
+
+$obj = [
+ // Comment
+ 'key' => 'val'
+];
+
+$obj = [
+ // Comment
+ 'key' => 'val'
+];
+
+$obj =
+ // Comment
+ ['val'];
+
+$obj =
+ // Comment
+ ['val'];
+
+$obj = [
+ // Comment
+ 'val'
+];
+
+$obj = [
+ // Comment
+ 'val'
+];
+
+$obj =
+ // Comment
+ 'val';
+
+$obj =
+ // Comment
+ '
+val
+val
+';
+
+$obj =
+ // Comment
+ '
+ val
+ val
+ ';
+
+$obj =
+ // Comment
+ "val";
+
+$obj =
+ // Comment
+ "
+val
+val
+";
+
+$obj =
+ // Comment
+ "
+ val
+ val
+ ";
+
+$obj =
+ // Comment
+ "val \${$var}";
+
+$obj =
+ // Comment
+ "
+val
+\${$var}
+";
+
+$obj =
+ // Comment
+ "
+ val
+ \${$var}
+ ";
+
+`;
+
exports[`variadic.php 1`] = `
'val'
+];
+
+$obj // Comment
+= [
+'key' => 'val'
+];
+
+$obj = [ // Comment
+'key' => 'val'
+];
+
+$obj = [
+// Comment
+'key' => 'val'
+];
+
+$obj = // Comment
+[
+'val'
+];
+
+$obj // Comment
+= [
+'val'
+];
+
+$obj = [ // Comment
+'val'
+];
+
+$obj = [
+// Comment
+'val'
+];
+
+$obj = // Comment
+'val';
+
+$obj = // Comment
+'
+val
+val
+';
+
+$obj = // Comment
+ '
+ val
+ val
+ ';
+
+$obj = // Comment
+ "val";
+
+$obj = // Comment
+"
+val
+val
+";
+
+$obj = // Comment
+ "
+ val
+ val
+ ";
+
+$obj = // Comment
+ "val ${$var}";
+
+$obj = // Comment
+"
+val
+${$var}
+";
+
+$obj = // Comment
+ "
+ val
+ ${$var}
+ ";
From 3692572ef0cd05659a38d696252fa75251dbc2fd Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 15:49:12 +0300
Subject: [PATCH 0031/1031] feat: better handle entry comments (#640)
---
src/comments.js | 9 +++++
.../comments/__snapshots__/jsfmt.spec.js.snap | 37 +++++++++++++++++++
tests/comments/array.php | 18 +++++++++
3 files changed, 64 insertions(+)
diff --git a/src/comments.js b/src/comments.js
index a50ad2635..e7e12d3a4 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -54,6 +54,7 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
handleFunctionParameter(text, enclosingNode, comment, options) ||
handleFunction(text, enclosingNode, followingNode, comment, options) ||
handleTryCatch(enclosingNode, comment) ||
+ handleEntryComments(enclosingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
handleHalt(enclosingNode, precedingNode, comment) ||
handleVariableComments(enclosingNode, followingNode, comment)
@@ -301,6 +302,14 @@ function handleInlineComments(
return false;
}
+function handleEntryComments(enclosingNode, comment) {
+ if (enclosingNode && enclosingNode.kind === "entry") {
+ addLeadingComment(enclosingNode, comment);
+ return true;
+ }
+ return false;
+}
+
function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
if (
enclosingNode &&
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 6ec9c8404..dedda2419 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -56,6 +56,24 @@ $array = []; // Comment
/* Comment */
$array /* Comment */ = /* Comment */ []; /* Comment */
/* Comment */
+
+$array = [
+ 'key' =>
+ // Comment A
+ // Comment B
+ 'string'
+];
+
+$array = [
+ 'key' => // Comment A
+ // Comment B
+ 'string'
+];
+
+$array = [
+ 'key' => // Comment A
+ 'string'
+];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ // Comment A
+ // Comment B
+ 'string'
+];
+
+$array = [
+ // Comment A
+ 'key' =>
+ // Comment B
+ 'string'
+];
+
+$array = [
+ // Comment A
+ 'key' => 'string'
+];
+
`;
exports[`assign.php 1`] = `
diff --git a/tests/comments/array.php b/tests/comments/array.php
index 8ce550046..9963d314e 100644
--- a/tests/comments/array.php
+++ b/tests/comments/array.php
@@ -53,3 +53,21 @@
/* Comment */
$array /* Comment */ = /* Comment */ []; /* Comment */
/* Comment */
+
+$array = [
+ 'key' =>
+ // Comment A
+ // Comment B
+ 'string'
+];
+
+$array = [
+ 'key' => // Comment A
+ // Comment B
+ 'string'
+];
+
+$array = [
+ 'key' => // Comment A
+ 'string'
+];
From 87ed276d8f578607da46c5833fbeed7172f145cd Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 18:06:52 +0300
Subject: [PATCH 0032/1031] feat: better print parens in empty parens (#641)
---
src/comments.js | 14 +++++++----
.../comments/__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++
tests/comments/closure.php | 4 ++++
tests/comments/new.php | 3 +++
4 files changed, 40 insertions(+), 5 deletions(-)
create mode 100644 tests/comments/closure.php
create mode 100644 tests/comments/new.php
diff --git a/src/comments.js b/src/comments.js
index e7e12d3a4..6b6632615 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -64,15 +64,15 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
function handleRemainingComment(comment, text, options, ast, isLastComment) {
const { precedingNode, enclosingNode, followingNode } = comment;
return (
+ handleCommentInEmptyParens(text, enclosingNode, comment, options) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(text, enclosingNode, comment, options) ||
handleFunction(text, enclosingNode, followingNode, comment, options) ||
handleTryCatch(enclosingNode, comment) ||
- handleBreakAndContinueStatementComments(enclosingNode, comment) ||
handleGoto(enclosingNode, comment) ||
handleHalt(enclosingNode, precedingNode, comment) ||
- handleCall(enclosingNode, comment) ||
- handleOnlyComments(enclosingNode, ast, comment, isLastComment)
+ handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
+ handleBreakAndContinueStatementComments(enclosingNode, comment)
);
}
@@ -257,10 +257,14 @@ function handleHalt(enclosingNode, precedingNode, comment) {
return false;
}
-function handleCall(enclosingNode, comment) {
+function handleCommentInEmptyParens(text, enclosingNode, comment) {
+ // Only add dangling comments to fix the case when no arguments are present,
+ // i.e. a function without any argument.
if (
enclosingNode &&
- enclosingNode.kind === "call" &&
+ (enclosingNode.kind === "closure" ||
+ enclosingNode.kind === "call" ||
+ enclosingNode.kind === "new") &&
enclosingNode.arguments.length === 0
) {
addDanglingComment(enclosingNode, comment);
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index dedda2419..a3b19c551 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -566,6 +566,19 @@ $obj2 = clone /* Comments */ $obj /* Comments */;
`;
+exports[`closure.php 1`] = `
+
Date: Mon, 17 Sep 2018 20:04:03 +0300
Subject: [PATCH 0033/1031] feat: better print comments in `retif` node (#642)
---
src/comments.js | 38 ++++++++++-
src/util.js | 13 +++-
.../comments/__snapshots__/jsfmt.spec.js.snap | 64 +++++++++++++++++++
tests/comments/retif.php | 29 +++++++++
4 files changed, 142 insertions(+), 2 deletions(-)
create mode 100644 tests/comments/retif.php
diff --git a/src/comments.js b/src/comments.js
index 6b6632615..6c205000d 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -9,7 +9,7 @@ const {
} = require("prettier").util;
const { concat, join, indent, hardline } = require("prettier").doc.builders;
// TODO: remove after resolve https://github.com/prettier/prettier/pull/5049
-const { hasNewline } = require("./util");
+const { hasNewline, hasNewlineInRange } = require("./util");
/*
Comment functions are meant to inspect various edge cases using given comment nodes,
with information about where those comment nodes exist in the tree (ie enclosingNode,
@@ -50,6 +50,14 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
const { precedingNode, enclosingNode, followingNode } = comment;
return (
+ handleRetifComments(
+ enclosingNode,
+ precedingNode,
+ followingNode,
+ comment,
+ text,
+ options
+ ) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(text, enclosingNode, comment, options) ||
handleFunction(text, enclosingNode, followingNode, comment, options) ||
@@ -76,6 +84,34 @@ function handleRemainingComment(comment, text, options, ast, isLastComment) {
);
}
+function handleRetifComments(
+ enclosingNode,
+ precedingNode,
+ followingNode,
+ comment,
+ text,
+ options
+) {
+ const isSameLineAsPrecedingNode =
+ precedingNode &&
+ !hasNewlineInRange(
+ text,
+ options.locEnd(precedingNode),
+ options.locStart(comment)
+ );
+
+ if (
+ (!precedingNode || !isSameLineAsPrecedingNode) &&
+ enclosingNode &&
+ enclosingNode.kind === "retif" &&
+ followingNode
+ ) {
+ addLeadingComment(followingNode, comment);
+ return true;
+ }
+ return false;
+}
+
function handleForComments(
enclosingNode,
precedingNode,
diff --git a/src/util.js b/src/util.js
index 305885d69..d88cc4720 100644
--- a/src/util.js
+++ b/src/util.js
@@ -566,6 +566,16 @@ function hasNewline(text, index, opts) {
return idx !== idx2;
}
+// TODO: remove after resolve https://github.com/prettier/prettier/pull/5049
+function hasNewlineInRange(text, start, end) {
+ for (let i = start; i < end; ++i) {
+ if (text.charAt(i) === "\n") {
+ return true;
+ }
+ }
+ return false;
+}
+
module.exports = {
printNumber,
getPrecedence,
@@ -593,5 +603,6 @@ module.exports = {
getLastNestedChildNode,
isProgramLikeNode,
getNodeKindIncludingLogical,
- hasNewline
+ hasNewline,
+ hasNewlineInRange
};
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index a3b19c551..ed2e70312 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1761,6 +1761,70 @@ echo /*Comment*/ /*Comment*/ $a->bar();
`;
+exports[`retif.php 1`] = `
+inspect($v, 0, 0, $colors);
+ }
+ : // node > 0.8.x
+ function($v, $colors) {
+ return $util->inspect($v, [ 'colors' => colors ]);
+ };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ // Making sure that the publicPath goes back to to build folder.
+ ? [ 'publicPath' => (new Foo($cssFilename->split('/')->length))->join('../') ] :
+ [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ ? // Making sure that the publicPath goes back to to build folder.
+ [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+ : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+ ? [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+ : [];
+
+$var = $process->env->NODE_ENV === "production"
+ ? call("./configureProdStore") // a
+ : call("./configureDevStore"); // b
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+inspect($v, 0, 0, $colors);
+ }
+ : // node > 0.8.x
+ function ($v, $colors) {
+ return $util->inspect($v, ['colors' => colors]);
+ };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ ? // Making sure that the publicPath goes back to to build folder.
+ ['publicPath' => (new Foo($cssFilename->split('/')->length))->join('../')]
+ : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ ? // Making sure that the publicPath goes back to to build folder.
+ ['publicPath' => (new Foo($cssFilename->split("/")->length))->join("../")]
+ : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+ ? ['publicPath' => (new Foo($cssFilename->split("/")->length))->join("../")]
+ : [];
+
+$var =
+ $process->env->NODE_ENV === "production"
+ ? call("./configureProdStore") // a
+ : call("./configureDevStore"); // b
+
+`;
+
exports[`return.php 1`] = `
inspect($v, 0, 0, $colors);
+ }
+ : // node > 0.8.x
+ function($v, $colors) {
+ return $util->inspect($v, [ 'colors' => colors ]);
+ };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ // Making sure that the publicPath goes back to to build folder.
+ ? [ 'publicPath' => (new Foo($cssFilename->split('/')->length))->join('../') ] :
+ [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+ ? // Making sure that the publicPath goes back to to build folder.
+ [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+ : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+ ? [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+ : [];
+
+$var = $process->env->NODE_ENV === "production"
+ ? call("./configureProdStore") // a
+ : call("./configureDevStore"); // b
From 857f4373f24ef59d37136f16d0328397506bb78c Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Mon, 17 Sep 2018 22:40:15 +0300
Subject: [PATCH 0034/1031] refactor: if node (#645)
* refactor: `if` node
* refactor: `if` node
---
src/comments.js | 5 +++
src/index.js | 4 +--
src/printer.js | 90 +++++++++++++++++++++++++++++++------------------
3 files changed, 64 insertions(+), 35 deletions(-)
diff --git a/src/comments.js b/src/comments.js
index 6c205000d..fe40d6e5c 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -463,10 +463,15 @@ function returnArgumentHasLeadingComment(options, argument) {
return false;
}
+function isBlockComment(comment) {
+ return comment.kind === "commentblock";
+}
+
module.exports = {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
+ isBlockComment,
printDanglingComments,
hasLeadingComment,
hasTrailingComment,
diff --git a/src/index.js b/src/index.js
index 9b64c25c6..b060d646b 100644
--- a/src/index.js
+++ b/src/index.js
@@ -267,9 +267,7 @@ const printers = {
node.kind && node.kind !== "commentblock" && node.kind !== "commentline"
);
},
- isBlockComment(comment) {
- return comment.kind === "commentblock";
- },
+ isBlockComment: comments.isBlockComment,
handleComments: {
ownLine: comments.handleOwnLineComment,
endOfLine: comments.handleEndOfLineComment,
diff --git a/src/printer.js b/src/printer.js
index 5f85ed078..3f776f837 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1226,6 +1226,18 @@ function printAssignmentRight(leftNode, rightNode, printedRight, options) {
return concat([" ", printedRight]);
}
+function needsHardlineAfterDanglingComment(node) {
+ if (!node.comments) {
+ return false;
+ }
+
+ const lastDanglingComment = getLast(
+ node.comments.filter(comment => !comment.leading && !comment.trailing)
+ );
+
+ return lastDanglingComment && !comments.isBlockComment(lastDanglingComment);
+}
+
function printNode(path, options, print) {
const node = path.getValue();
@@ -1688,43 +1700,57 @@ function printNode(path, options, print) {
path.call(print, "value")
]);
case "if": {
- const handleIfAlternate = alternate => {
- if (!alternate) {
- return node.body ? (node.shortForm ? "endif;" : "}") : "";
- }
+ const parts = [];
+ const body = printBodyControlStructure(path, print, "body", options);
+ const opening = group(
+ concat([
+ "if (",
+ group(
+ concat([
+ indent(concat([softline, path.call(print, "test")])),
+ softline
+ ])
+ ),
+ ")",
+ body
+ ])
+ );
+
+ parts.push(
+ opening,
+ isFirstChildrenInlineNode(path) || !node.body ? "" : hardline
+ );
- if (alternate.kind === "if") {
- return concat([
- node.shortForm ? "" : "} ",
- "else",
- path.call(print, "alternate")
- ]);
+ if (node.alternate) {
+ parts.push(node.shortForm ? "" : "} ");
+
+ const commentOnOwnLine =
+ (hasTrailingComment(node.body) &&
+ node.body.comments.some(
+ comment => comment.trailing && !comments.isBlockComment(comment)
+ )) ||
+ needsHardlineAfterDanglingComment(node);
+
+ if (hasDanglingComments(node)) {
+ parts.push(
+ comments.printDanglingComments(path, options, true),
+ commentOnOwnLine ? hardline : " "
+ );
}
- return concat([
- node.shortForm ? "" : "} ",
+ parts.push(
"else",
- printBodyControlStructure(path, print, "alternate", options)
- ]);
- };
+ group(
+ node.alternate.kind === "if"
+ ? path.call(print, "alternate")
+ : printBodyControlStructure(path, print, "alternate", options)
+ )
+ );
+ } else {
+ parts.push(node.body ? (node.shortForm ? "endif;" : "}") : "");
+ }
- return concat([
- group(
- concat([
- "if (",
- group(
- concat([
- indent(concat([softline, path.call(print, "test")])),
- softline
- ])
- ),
- ")",
- printBodyControlStructure(path, print, "body", options)
- ])
- ),
- isFirstChildrenInlineNode(path) || !node.body ? "" : hardline,
- handleIfAlternate(node.alternate)
- ]);
+ return concat(parts);
}
case "do":
return concat([
From 9972d829c8696135457956feef0b12a45f46c95a Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Tue, 18 Sep 2018 12:37:24 +0300
Subject: [PATCH 0035/1031] feat: better print comments in `if` node (#646)
---
src/comments.js | 74 ++++-
src/index.js | 2 +-
.../comments/__snapshots__/jsfmt.spec.js.snap | 296 ++++++++++++++++++
tests/comments/if.php | 132 ++++++++
4 files changed, 489 insertions(+), 15 deletions(-)
diff --git a/src/comments.js b/src/comments.js
index fe40d6e5c..f5f47b5ec 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -30,12 +30,19 @@ args:
function handleOwnLineComment(comment, text, options, ast, isLastComment) {
const { precedingNode, enclosingNode, followingNode } = comment;
return (
+ handleIfStatementComments(
+ text,
+ precedingNode,
+ enclosingNode,
+ followingNode,
+ comment,
+ options
+ ) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(text, enclosingNode, comment, options) ||
handleFunction(text, enclosingNode, followingNode, comment, options) ||
handleForComments(enclosingNode, precedingNode, followingNode, comment) ||
handleTryCatch(enclosingNode, comment) ||
- handleAlternate(enclosingNode, followingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
handleInlineComments(
enclosingNode,
@@ -58,6 +65,14 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
text,
options
) ||
+ handleIfStatementComments(
+ text,
+ precedingNode,
+ enclosingNode,
+ followingNode,
+ comment,
+ options
+ ) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(text, enclosingNode, comment, options) ||
handleFunction(text, enclosingNode, followingNode, comment, options) ||
@@ -72,6 +87,14 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
function handleRemainingComment(comment, text, options, ast, isLastComment) {
const { precedingNode, enclosingNode, followingNode } = comment;
return (
+ handleIfStatementComments(
+ text,
+ precedingNode,
+ enclosingNode,
+ followingNode,
+ comment,
+ options
+ ) ||
handleCommentInEmptyParens(text, enclosingNode, comment, options) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(text, enclosingNode, comment, options) ||
@@ -84,6 +107,42 @@ function handleRemainingComment(comment, text, options, ast, isLastComment) {
);
}
+function addBlockStatementFirstComment(node, comment) {
+ const { children } = node;
+ if (children.length === 0) {
+ addDanglingComment(node, comment);
+ } else {
+ addLeadingComment(children[0], comment);
+ }
+}
+
+function addBlockOrNotComment(node, comment) {
+ if (node.kind === "block") {
+ addBlockStatementFirstComment(node, comment);
+ } else {
+ addLeadingComment(node, comment);
+ }
+}
+
+function handleIfStatementComments(
+ text,
+ precedingNode,
+ enclosingNode,
+ followingNode,
+ comment
+) {
+ if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
+ return false;
+ }
+
+ if (followingNode.kind === "if") {
+ addBlockOrNotComment(followingNode.body, comment);
+ return true;
+ }
+
+ return false;
+}
+
function handleRetifComments(
enclosingNode,
precedingNode,
@@ -309,19 +368,6 @@ function handleCommentInEmptyParens(text, enclosingNode, comment) {
return false;
}
-function handleAlternate(enclosingNode, followingNode, comment) {
- if (
- enclosingNode &&
- enclosingNode.kind === "if" &&
- followingNode &&
- followingNode.kind == "if"
- ) {
- addLeadingComment(followingNode.body, comment);
- return true;
- }
- return false;
-}
-
function handleInlineComments(
enclosingNode,
precedingNode,
diff --git a/src/index.js b/src/index.js
index b060d646b..e47b33b85 100644
--- a/src/index.js
+++ b/src/index.js
@@ -111,7 +111,7 @@ const printers = {
},
{
kinds: ["if"],
- children: { listNodes: [], nodes: ["body", "alternate", "test"] }
+ children: { listNodes: [], nodes: ["test", "body", "alternate"] }
},
{
kinds: ["block", "program", "namespace", "declare"],
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index ed2e70312..6943b317a 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1339,6 +1339,138 @@ if (false)
// and this is a comment on an elseif
elseif (false)
also_do_nothing();
+
+if (1)
+// comment
+{
+ false;
+}
+// comment
+else if (2)
+ true;
+// multi
+// ple
+// lines
+else if (3)
+ // existing comment
+ true;
+// okay?
+else if (4) {
+ // empty with existing comment
+}
+// comment
+else {
+}
+
+if (5) // comment
+true;
+
+if (6) // comment
+{true;}
+else if (7) // comment
+true;
+else // comment
+{true;}
+
+if (8) // comment
+// comment
+{true;}
+else if (9) // comment
+// comment
+true;
+else // comment
+// comment
+{true;}
+
+if (10) /* comment */ // comment
+{true;}
+else if (11) /* comment */
+true;
+else if (12) // comment /* comment */ // comment
+true;
+else if (13) /* comment */ /* comment */ // comment
+true;
+else /* comment */
+{true;}
+
+if (14) // comment
+/* comment */
+// comment
+{true;}
+else if (15) // comment
+/* comment */
+/* comment */ // comment
+true;
+
+if ($cond) {
+ stuff();
+} /* comment */ else if ($cond) {
+ stuff();
+}
+// comment
+else {
+ stuff();
+}
+
+if ($cond) stuff();
+// comment
+else stuff();
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */ {
+ return null;
+ } else if ($untrackedChoice === 1) /* Add */ {
+ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } else if ($untrackedChoice === 2) /* Allow Untracked */ {
+ $allowUntracked = true;
+ }
+}
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */
+ null;
+ else if ($untrackedChoice === 1) /* Add */
+ $shouldAmend = true;
+ else if ($untrackedChoice === 2) /* Allow Untracked */
+ $allowUntracked = true;
+}
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */ // Cancel
+ null;
+ else if ($untrackedChoice === 1) /* Add */ // Add
+ $shouldAmend = true;
+ else if ($untrackedChoice === 2) /* Allow Untracked */ // Allow Untracked
+ $allowUntracked = true;
+}
+
+function f() {
+ if ($untrackedChoice === 0)
+ /* Cancel */ {
+ return null;
+ }
+ else if ($untrackedChoice === 1)
+ /* Add */ {
+ yield $repository->addAll($args);
+ $shouldAmend = true;
+ }
+ else if ($untrackedChoice === 2)
+ /* Allow Untracked */ {
+ $allowUntracked = true;
+ }
+}
+
+function f() {
+ if ($untrackedChoice === 0) {
+ /* Cancel */ return null;
+ } else if ($untrackedChoice === 1) {
+ /* Add */ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } else if ($untrackedChoice === 2) {
+ /* Allow Untracked */ $allowUntracked = true;
+ }
+}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
addAll($args);
+ $shouldAmend = true;
+ } elseif ($untrackedChoice === 2) {
+ /* Allow Untracked */ $allowUntracked = true;
+ }
+}
+
+function f()
+{
+ if ($untrackedChoice === 0 /* Cancel */) {
+ null;
+ } elseif ($untrackedChoice === 1 /* Add */) {
+ $shouldAmend = true;
+ } elseif ($untrackedChoice === 2 /* Allow Untracked */) {
+ $allowUntracked = true;
+ }
+}
+
+function f()
+{
+ if (
+ $untrackedChoice === 0 /* Cancel */ // Cancel
+ ) {
+ null;
+ } elseif (
+ $untrackedChoice === 1 /* Add */ // Add
+ ) {
+ $shouldAmend = true;
+ } elseif (
+ $untrackedChoice === 2 /* Allow Untracked */ // Allow Untracked
+ ) {
+ $allowUntracked = true;
+ }
+}
+
+function f()
+{
+ if ($untrackedChoice === 0) {
+ /* Cancel */ return null;
+ } elseif ($untrackedChoice === 1) {
+ /* Add */ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } elseif ($untrackedChoice === 2) {
+ /* Allow Untracked */ $allowUntracked = true;
+ }
+}
+
+function f()
+{
+ if ($untrackedChoice === 0) {
+ /* Cancel */ return null;
+ } elseif ($untrackedChoice === 1) {
+ /* Add */ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } elseif ($untrackedChoice === 2) {
+ /* Allow Untracked */ $allowUntracked = true;
+ }
+}
+
`;
exports[`include.php 1`] = `
diff --git a/tests/comments/if.php b/tests/comments/if.php
index 91dcca27b..2e8460ebf 100644
--- a/tests/comments/if.php
+++ b/tests/comments/if.php
@@ -34,3 +34,135 @@
// and this is a comment on an elseif
elseif (false)
also_do_nothing();
+
+if (1)
+// comment
+{
+ false;
+}
+// comment
+else if (2)
+ true;
+// multi
+// ple
+// lines
+else if (3)
+ // existing comment
+ true;
+// okay?
+else if (4) {
+ // empty with existing comment
+}
+// comment
+else {
+}
+
+if (5) // comment
+true;
+
+if (6) // comment
+{true;}
+else if (7) // comment
+true;
+else // comment
+{true;}
+
+if (8) // comment
+// comment
+{true;}
+else if (9) // comment
+// comment
+true;
+else // comment
+// comment
+{true;}
+
+if (10) /* comment */ // comment
+{true;}
+else if (11) /* comment */
+true;
+else if (12) // comment /* comment */ // comment
+true;
+else if (13) /* comment */ /* comment */ // comment
+true;
+else /* comment */
+{true;}
+
+if (14) // comment
+/* comment */
+// comment
+{true;}
+else if (15) // comment
+/* comment */
+/* comment */ // comment
+true;
+
+if ($cond) {
+ stuff();
+} /* comment */ else if ($cond) {
+ stuff();
+}
+// comment
+else {
+ stuff();
+}
+
+if ($cond) stuff();
+// comment
+else stuff();
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */ {
+ return null;
+ } else if ($untrackedChoice === 1) /* Add */ {
+ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } else if ($untrackedChoice === 2) /* Allow Untracked */ {
+ $allowUntracked = true;
+ }
+}
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */
+ null;
+ else if ($untrackedChoice === 1) /* Add */
+ $shouldAmend = true;
+ else if ($untrackedChoice === 2) /* Allow Untracked */
+ $allowUntracked = true;
+}
+
+function f() {
+ if ($untrackedChoice === 0) /* Cancel */ // Cancel
+ null;
+ else if ($untrackedChoice === 1) /* Add */ // Add
+ $shouldAmend = true;
+ else if ($untrackedChoice === 2) /* Allow Untracked */ // Allow Untracked
+ $allowUntracked = true;
+}
+
+function f() {
+ if ($untrackedChoice === 0)
+ /* Cancel */ {
+ return null;
+ }
+ else if ($untrackedChoice === 1)
+ /* Add */ {
+ yield $repository->addAll($args);
+ $shouldAmend = true;
+ }
+ else if ($untrackedChoice === 2)
+ /* Allow Untracked */ {
+ $allowUntracked = true;
+ }
+}
+
+function f() {
+ if ($untrackedChoice === 0) {
+ /* Cancel */ return null;
+ } else if ($untrackedChoice === 1) {
+ /* Add */ yield $repository->addAll($args);
+ $shouldAmend = true;
+ } else if ($untrackedChoice === 2) {
+ /* Allow Untracked */ $allowUntracked = true;
+ }
+}
From ac3fdfce1cb270ae03adadb00bd51f0a6812ef72 Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Tue, 18 Sep 2018 12:38:03 +0300
Subject: [PATCH 0036/1031] feat: avoid parens in `return` node (#644)
---
src/printer.js | 20 +--
.../comments/__snapshots__/jsfmt.spec.js.snap | 5 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/retif/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/return/__snapshots__/jsfmt.spec.js.snap | 145 ++++++++++++++++--
tests/return/return.php | 45 ++++++
6 files changed, 182 insertions(+), 45 deletions(-)
diff --git a/src/printer.js b/src/printer.js
index 3f776f837..99d639505 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2181,25 +2181,9 @@ function printNode(path, options, print) {
const printedExpr = path.call(print, "expr");
if (comments.returnArgumentHasLeadingComment(options, node.expr)) {
- parts.push(
- concat([
- " (",
- indent(concat([hardline, printedExpr])),
- hardline,
- ")"
- ])
- );
+ parts.push(indent(concat([hardline, printedExpr])));
} else if (node.expr.kind === "bin") {
- parts.push(
- group(
- concat([
- ifBreak(" (", " "),
- indent(concat([softline, printedExpr])),
- softline,
- ifBreak(")")
- ])
- )
- );
+ parts.push(group(indent(concat([" ", printedExpr]))));
} else {
parts.push(" ", printedExpr);
}
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 6943b317a..57f3d4a04 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -841,11 +841,10 @@ $constraint = new UniqueEntity(array(
// no "em" option set
));
-return (
+return
// parens test
// some comment
- $test
-);
+ $test;
/***
* Configures the tasks tabs (sub_menu)
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 1e6cd15c2..fa6386153 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -2371,10 +2371,8 @@ return;
return 1;
return 1;
return 1 + 2;
-return (
- 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
- 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
-);
+return 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
+ 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
return $var1 + $var2;
return $var ? ($var1 ? 1 : 2) : ($var2 ? 3 : 4);
return 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index 25d37ec1d..d276f002f 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -134,11 +134,9 @@ $var =
? $this->basePath . DIRECTORY_SEPARATOR . 'other'
: $this->basePath . DIRECTORY_SEPARATOR . 'database') .
($path ? DIRECTORY_SEPARATOR . $path : $path);
-return (
- ($this->databasePath
+return ($this->databasePath
?: $this->basePath . DIRECTORY_SEPARATOR . 'database') .
- ($path ? DIRECTORY_SEPARATOR . $path : $path)
-);
+ ($path ? DIRECTORY_SEPARATOR . $path : $path);
$test = $foo
? foo([
diff --git a/tests/return/__snapshots__/jsfmt.spec.js.snap b/tests/return/__snapshots__/jsfmt.spec.js.snap
index 7df1e071b..7aa74f7fc 100644
--- a/tests/return/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/return/__snapshots__/jsfmt.spec.js.snap
@@ -9,6 +9,12 @@ function sum($a, $b) {
return;
return $name;
+return
+
+$name;
+return
+
+$VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName;
return [1, 2, 3];
return [$veryVeryVeryVeryVeryVeryLongKey => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongString', $veryVeryVeryVeryVeryVeryLongKey => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongString', 3];
return ($a + $b);
@@ -22,6 +28,45 @@ return (1000000000 + 1000000000 + 1000000000 + 1000000000 + 1000000000 + 1000000
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable ? true : false;
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable->veryVeryVeryVeryVeryVeryLongProperty;
return ((($true ? ('foo' . ('foo'. 'bar')) : "foobarfoo")));
+return 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
+return ('string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string');
+
+return function () {};
+return (function () {})();
+
+return call();
+return $a->b->c->d->e->f;
+return $a->b()->c()->d()->e()->f();
+return call()->b()->c()->d()->e()->f();
+return $a::b()::c()::d()::e()::f();
+return Foo::a()::b()::c()::d()::e()::f();
+
+return "
+string
+string
+string
+";
+
+return $str = <<
@@ -44,18 +91,12 @@ return $a + $b;
return $a / $b;
return $a && $b;
return new $VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName();
-return (
- "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString" .
- "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString"
-);
-return (
+return "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString" .
+ "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
+return "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString" .
"VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString" .
- "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString" .
- "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString"
-);
-return (
- 1000000000 +
- 1000000000 +
+ "VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
+return 1000000000 +
1000000000 +
1000000000 +
1000000000 +
@@ -64,10 +105,9 @@ return (
1000000000 +
1000000000 +
1000000000 +
- 1000000000
-);
-return (
1000000000 +
+ 1000000000;
+return 1000000000 +
1000000000 +
1000000000 +
1000000000 +
@@ -77,12 +117,85 @@ return (
1000000000 +
1000000000 +
1000000000 +
- 1000000000
-);
+ 1000000000;
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable
? true
: false;
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable->veryVeryVeryVeryVeryVeryLongProperty;
return $true ? 'foo' . ('foo' . 'bar') : "foobarfoo";
+return 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string';
+return 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string' .
+ 'string';
+
+return function () {};
+return (function () {})();
+
+return call();
+return $a->b->c->d->e->f;
+return $a
+ ->b()
+ ->c()
+ ->d()
+ ->e()
+ ->f();
+return call()
+ ->b()
+ ->c()
+ ->d()
+ ->e()
+ ->f();
+return $a
+ ::b()
+ ::c()
+ ::d()
+ ::e()
+ ::f();
+return Foo::a()
+ ::b()
+ ::c()
+ ::d()
+ ::e()
+ ::f();
+
+return "
+string
+string
+string
+";
+
+return $str = << 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongString', $veryVeryVeryVeryVeryVeryLongKey => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongString', 3];
return ($a + $b);
@@ -19,3 +25,42 @@ function sum($a, $b) {
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable ? true : false;
return $veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable->veryVeryVeryVeryVeryVeryLongProperty;
return ((($true ? ('foo' . ('foo'. 'bar')) : "foobarfoo")));
+return 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
+return ('string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string');
+
+return function () {};
+return (function () {})();
+
+return call();
+return $a->b->c->d->e->f;
+return $a->b()->c()->d()->e()->f();
+return call()->b()->c()->d()->e()->f();
+return $a::b()::c()::d()::e()::f();
+return Foo::a()::b()::c()::d()::e()::f();
+
+return "
+string
+string
+string
+";
+
+return $str = <<
Date: Wed, 19 Sep 2018 17:41:09 +0300
Subject: [PATCH 0037/1031] fix: parens before staticlookup node (#647)
---
package.json | 2 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 32 +++++++++++++++++++
tests/parens/lookups.php | 16 ++++++++++
.../__snapshots__/jsfmt.spec.js.snap | 4 +++
tests/staticlookup/staticlookup.php | 2 ++
.../variable/__snapshots__/jsfmt.spec.js.snap | 20 +++++++++---
tests/variable/variable.php | 10 ++++--
yarn.lock | 4 +--
8 files changed, 81 insertions(+), 9 deletions(-)
diff --git a/package.json b/package.json
index be573ccde..4c7456cd3 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
],
"dependencies": {
"mem": "^3.0.1",
- "php-parser": "3.0.0-prerelease.4"
+ "php-parser": "glayzzle/php-parser#85eb5e622fcd4ef1f70bf3b2529afe5e005bdd52"
},
"devDependencies": {
"eslint": "4.1.1",
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index fa6386153..4be3976e6 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1768,7 +1768,9 @@ $var = (($var->bar)->foo);
$var = $var::foo();
$var = ($var::foo());
$var = $var::foo()::bar();
+$var = ($var::foo())::bar();
$var = ($var::foo()::bar());
+$var = (($var::foo())::bar());
$var = $var->bar();
$var = ($var->bar());
@@ -1789,17 +1791,31 @@ $var = ($var[0])->foo;
$var = ($var[0][1])->foo;
$var = ($var[0])[1]->foo;
$var = (($var[0])[1])->foo;
+$var = $var[0]::foo;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = ($var[0])::foo;
+// $var = ($var[0][1])::foo;
+// $var = ($var[0])[1]::foo;
+// $var = (($var[0])[1])::foo;
$var = $var[0]->foo();
$var = ($var[0])->foo();
$var = ($var[0][1])->foo();
$var = ($var[0])[1]->foo();
$var = (($var[0])[1])->foo();
+$var = $var[0]::foo();
+$var = ($var[0])::foo();
+$var = ($var[0][1])::foo();
+$var = ($var[0])[1]::foo();
+$var = (($var[0])[1])::foo();
$var = $var[0]->foo()->baz;
$var = ((($var[0])->foo())->baz);
$var = (new Foo())->bar;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = (new Foo())::bar;
$var = (new Foo())->bar();
+$var = (new Foo())::bar();
$var = (new Foo())[1];
$var = $var->bar()();
@@ -1839,6 +1855,8 @@ $var = $var::foo();
$var = $var::foo();
$var = $var::foo()::bar();
$var = $var::foo()::bar();
+$var = $var::foo()::bar();
+$var = $var::foo()::bar();
$var = $var->bar();
$var = $var->bar();
@@ -1859,17 +1877,31 @@ $var = $var[0]->foo;
$var = $var[0][1]->foo;
$var = $var[0][1]->foo;
$var = $var[0][1]->foo;
+$var = $var[0]::foo;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = ($var[0])::foo;
+// $var = ($var[0][1])::foo;
+// $var = ($var[0])[1]::foo;
+// $var = (($var[0])[1])::foo;
$var = $var[0]->foo();
$var = $var[0]->foo();
$var = $var[0][1]->foo();
$var = $var[0][1]->foo();
$var = $var[0][1]->foo();
+$var = $var[0]::foo();
+$var = $var[0]::foo();
+$var = $var[0][1]::foo();
+$var = $var[0][1]::foo();
+$var = $var[0][1]::foo();
$var = $var[0]->foo()->baz;
$var = $var[0]->foo()->baz;
$var = (new Foo())->bar;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = (new Foo())::bar;
$var = (new Foo())->bar();
+$var = (new Foo())::bar();
$var = (new Foo())[1];
$var = $var->bar()();
diff --git a/tests/parens/lookups.php b/tests/parens/lookups.php
index 493ae7095..d01c0f299 100644
--- a/tests/parens/lookups.php
+++ b/tests/parens/lookups.php
@@ -19,7 +19,9 @@
$var = $var::foo();
$var = ($var::foo());
$var = $var::foo()::bar();
+$var = ($var::foo())::bar();
$var = ($var::foo()::bar());
+$var = (($var::foo())::bar());
$var = $var->bar();
$var = ($var->bar());
@@ -40,17 +42,31 @@
$var = ($var[0][1])->foo;
$var = ($var[0])[1]->foo;
$var = (($var[0])[1])->foo;
+$var = $var[0]::foo;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = ($var[0])::foo;
+// $var = ($var[0][1])::foo;
+// $var = ($var[0])[1]::foo;
+// $var = (($var[0])[1])::foo;
$var = $var[0]->foo();
$var = ($var[0])->foo();
$var = ($var[0][1])->foo();
$var = ($var[0])[1]->foo();
$var = (($var[0])[1])->foo();
+$var = $var[0]::foo();
+$var = ($var[0])::foo();
+$var = ($var[0][1])::foo();
+$var = ($var[0])[1]::foo();
+$var = (($var[0])[1])::foo();
$var = $var[0]->foo()->baz;
$var = ((($var[0])->foo())->baz);
$var = (new Foo())->bar;
+// TODO: uncomment after resolve https://github.com/glayzzle/php-parser/issues/185
+// $var = (new Foo())::bar;
$var = (new Foo())->bar();
+$var = (new Foo())::bar();
$var = (new Foo())[1];
$var = $var->bar()();
diff --git a/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap b/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
index 960ad72be..d5b596022 100644
--- a/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
@@ -98,6 +98,8 @@ $obj::veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVe
$obj::$veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty;
$obj::veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty();
$obj::$veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty();
+
+$var = ($this->modelClass)::where('name', 'like', strtoupper("%\${key}%"))->get();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
modelClass::where('name', 'like', strtoupper("%\${key}%"))->get();
+
`;
diff --git a/tests/staticlookup/staticlookup.php b/tests/staticlookup/staticlookup.php
index 33a1474a3..806575e0b 100644
--- a/tests/staticlookup/staticlookup.php
+++ b/tests/staticlookup/staticlookup.php
@@ -77,3 +77,5 @@ final public static function getInstance(){
$obj::$veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty;
$obj::veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty();
$obj::$veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongProperty();
+
+$var = ($this->modelClass)::where('name', 'like', strtoupper("%${key}%"))->get();
diff --git a/tests/variable/__snapshots__/jsfmt.spec.js.snap b/tests/variable/__snapshots__/jsfmt.spec.js.snap
index 38bd250f7..8a5c7a2b2 100644
--- a/tests/variable/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/variable/__snapshots__/jsfmt.spec.js.snap
@@ -51,8 +51,14 @@ $var = $foo->{$bar['baz']}();
$var = ($$foo)['bar']['baz'];
$var = ($foo->$bar)['baz'];
$var = ($foo->$bar)['baz']();
-// Todo https://github.com/glayzzle/php-parser/issues/182
-// $var = (Foo::$bar)['baz']();
+$var = Foo::$bar()['baz']();
+$var = (Foo::$bar())['baz']();
+$var = Foo::bar()['baz']();
+$var = (Foo::bar())['baz']();
+
+// Todo https://github.com/glayzzle/php-parser/issues/186
+// $var = Foo::$bar['baz'];
+// $var = (Foo::$bar)['baz'];
$$$$$$$$$$var = "I like playing in the park";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -98,8 +104,14 @@ $var = $foo->{$bar['baz']}();
$var = $$foo['bar']['baz'];
$var = $foo->$bar['baz'];
$var = $foo->$bar['baz']();
-// Todo https://github.com/glayzzle/php-parser/issues/182
-// $var = (Foo::$bar)['baz']();
+$var = Foo::$bar()['baz']();
+$var = Foo::$bar()['baz']();
+$var = Foo::bar()['baz']();
+$var = Foo::bar()['baz']();
+
+// Todo https://github.com/glayzzle/php-parser/issues/186
+// $var = Foo::$bar['baz'];
+// $var = (Foo::$bar)['baz'];
$$$$$$$$$$var = "I like playing in the park";
diff --git a/tests/variable/variable.php b/tests/variable/variable.php
index 04cffdfa1..5d4d30bb1 100644
--- a/tests/variable/variable.php
+++ b/tests/variable/variable.php
@@ -39,7 +39,13 @@
$var = ($$foo)['bar']['baz'];
$var = ($foo->$bar)['baz'];
$var = ($foo->$bar)['baz']();
-// Todo https://github.com/glayzzle/php-parser/issues/182
-// $var = (Foo::$bar)['baz']();
+$var = Foo::$bar()['baz']();
+$var = (Foo::$bar())['baz']();
+$var = Foo::bar()['baz']();
+$var = (Foo::bar())['baz']();
+
+// Todo https://github.com/glayzzle/php-parser/issues/186
+// $var = Foo::$bar['baz'];
+// $var = (Foo::$bar)['baz'];
$$$$$$$$$$var = "I like playing in the park";
diff --git a/yarn.lock b/yarn.lock
index ff0392afe..9df8f8d72 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2864,9 +2864,9 @@ performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
-php-parser@3.0.0-prerelease.4:
+php-parser@glayzzle/php-parser#85eb5e622fcd4ef1f70bf3b2529afe5e005bdd52:
version "3.0.0-prerelease.4"
- resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.0.0-prerelease.4.tgz#f2af522540244994633df1f646345c7481477870"
+ resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/85eb5e622fcd4ef1f70bf3b2529afe5e005bdd52"
pify@3.0.0, pify@^3.0.0:
version "3.0.0"
From 1cbb65e26cf956a786fd87a665b13ef68824ea3d Mon Sep 17 00:00:00 2001
From: Evilebot Tnawi
Date: Wed, 19 Sep 2018 17:48:18 +0300
Subject: [PATCH 0038/1031] test: heredoc and nowdoc in `new` node (#648)
---
tests/new/__snapshots__/jsfmt.spec.js.snap | 120 +++++++++++++++++++++
tests/new/new.php | 60 +++++++++++
2 files changed, 180 insertions(+)
diff --git a/tests/new/__snapshots__/jsfmt.spec.js.snap b/tests/new/__snapshots__/jsfmt.spec.js.snap
index 95e2a7b9e..9711e050d 100644
--- a/tests/new/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/new/__snapshots__/jsfmt.spec.js.snap
@@ -33,6 +33,66 @@ $result = (new Pipeline(new \\Illuminate\\Container\\Container))
->then(function ($piped) {
return $piped;
});
+
+$var = new Foo(
+<<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+ ,
+ $arg
+);
+
+$var = new Foo(
+ $arg,
+ <<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+);
+
+$var = new Foo(
+ $arg,
+ <<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+ ,
+ $arg
+);
+
+$var = new Foo(
+ <<then(function ($piped) {
return $piped;
});
+
+$var = new Foo(
+<<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+ ,
+ $arg
+);
+
+$var = new Foo(
+ $arg,
+ <<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+);
+
+$var = new Foo(
+ $arg,
+ <<<'EOD'
+Example of string
+spanning multiple lines
+using nowdoc syntax.
+EOD
+ ,
+ $arg
+);
+
+$var = new Foo(
+ <<
Date: Wed, 19 Sep 2018 17:53:56 +0300
Subject: [PATCH 0039/1031] chore(deps): update (#649)
---
package.json | 14 +-
tests/array/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/assign/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/bin/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/block/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../boolean/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/break/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/call/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/case/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/cast/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/class/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/clone/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../closure/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../comments/__snapshots__/jsfmt.spec.js.snap | 124 +-
.../constant/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../continue/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../declare/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/do/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/echo/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../encapsed/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/errors/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/eval/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/exit/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/for/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../foreach/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/global/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/goto/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/halt/__snapshots__/jsfmt.spec.js.snap | 6 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/if/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/ignore/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../include/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/inline/__snapshots__/jsfmt.spec.js.snap | 82 +-
.../__snapshots__/jsfmt.spec.js.snap | 12 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/list/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/magic/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../markdown/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 18 +-
tests/method/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 8 +-
tests/new/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../newline/__snapshots__/jsfmt.spec.js.snap | 8 +-
tests/nowdoc/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/number/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../__snapshots__/jsfmt.spec.js.snap | 4 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/parens/__snapshots__/jsfmt.spec.js.snap | 64 +-
tests/print/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../program/__snapshots__/jsfmt.spec.js.snap | 16 +-
.../property/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 4 +-
.../__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/retif/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/return/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../shebang/__snapshots__/jsfmt.spec.js.snap | 4 +-
tests/silent/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/static/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/string/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/switch/__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/sys/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../__snapshots__/jsfmt.spec.js.snap | 12 +-
.../__snapshots__/jsfmt.spec.js.snap | 6 +-
tests/traits/__snapshots__/jsfmt.spec.js.snap | 2 +-
.../traituse/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/use/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../variable/__snapshots__/jsfmt.spec.js.snap | 4 +-
.../variadic/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/while/__snapshots__/jsfmt.spec.js.snap | 2 +-
tests/yield/__snapshots__/jsfmt.spec.js.snap | 2 +-
yarn.lock | 1256 +++++++++++------
77 files changed, 1085 insertions(+), 721 deletions(-)
diff --git a/package.json b/package.json
index 4c7456cd3..9e3994ab4 100644
--- a/package.json
+++ b/package.json
@@ -14,13 +14,13 @@
"php-parser": "glayzzle/php-parser#85eb5e622fcd4ef1f70bf3b2529afe5e005bdd52"
},
"devDependencies": {
- "eslint": "4.1.1",
- "eslint-config-prettier": "2.9.0",
- "eslint-plugin-import": "2.6.1",
- "eslint-plugin-jest": "21.5.0",
- "eslint-plugin-prettier": "2.4.0",
- "jest": "21.1.0",
- "jest-runner-eslint": "0.3.0",
+ "eslint": "5.6.0",
+ "eslint-config-prettier": "3.0.1",
+ "eslint-plugin-import": "2.14.0",
+ "eslint-plugin-jest": "21.22.0",
+ "eslint-plugin-prettier": "2.6.2",
+ "jest": "23.6.0",
+ "jest-runner-eslint": "0.6.0",
"prettier": "prettier/prettier"
},
"peerDependencies": {
diff --git a/tests/array/__snapshots__/jsfmt.spec.js.snap b/tests/array/__snapshots__/jsfmt.spec.js.snap
index 9e7d72de5..8b97367d5 100644
--- a/tests/array/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/array/__snapshots__/jsfmt.spec.js.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`arrays.php 1`] = `
+exports[`arrays.php - php-verify: arrays.php 1`] = `
veryVeryVeryVeryVeryVeryVeryVeryLongMethod(
`;
-exports[`single.php 1`] = `
+exports[`single.php - php-verify: single.php 1`] = `
modelData) ||
@@ -311,7 +311,7 @@ $bool = /*Comment */ true /*Comment */ || /*Comment */ false /*Comment */;
`;
-exports[`blank_lines.php 1`] = `
+exports[`blank_lines.php - php-verify: blank_lines.php 1`] = `
bar(/* B */);
`;
-exports[`cast.php 1`] = `
+exports[`cast.php - php-verify: cast.php 1`] = `
@@ -1735,7 +1735,7 @@ exports[`inline2.php 1`] = `
`;
-exports[`inline3.php 1`] = `
+exports[`inline3.php - php-verify: inline3.php 1`] = `
Test
`;
-exports[`inline4.php 1`] = `
+exports[`inline4.php - php-verify: inline4.php 1`] = `
Test
Test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;
-exports[`isset.php 1`] = `
+exports[`isset.php - php-verify: isset.php 1`] = `
/*Comment*/bar();
@@ -2056,7 +2056,7 @@ echo /*Comment*/ /*Comment*/ $a->bar();
`;
-exports[`retif.php 1`] = `
+exports[`retif.php - php-verify: retif.php 1`] = `
@@ -19,7 +19,7 @@ exports[`declare.php 1`] = `
`;
-exports[`inline.php 1`] = `
+exports[`inline.php - php-verify: inline.php 1`] = `
Test.
Test.
@@ -549,7 +549,7 @@ echo 'bar';
`;
-exports[`mixed-1.php 1`] = `
+exports[`mixed-1.php - php-verify: mixed-1.php 1`] = `
@@ -561,7 +561,7 @@ $a = 1; ?>
`;
-exports[`mixed-2.php 1`] = `
+exports[`mixed-2.php - php-verify: mixed-2.php 1`] = `
Foo
Test.
@@ -590,7 +590,7 @@ exports[`mixed-3.php 1`] = `
`;
-exports[`mixed-4.php 1`] = `
+exports[`mixed-4.php - php-verify: mixed-4.php 1`] = `
Hello world
Test.
@@ -607,7 +607,7 @@ exports[`mixed-4.php 1`] = `
`;
-exports[`mixed-5.php 1`] = `
+exports[`mixed-5.php - php-verify: mixed-5.php 1`] = `
text
">