Repository: prettier/plugin-php
Pull request #914 (2019-01-23 16:14:51)

refactor: code
Includes:
- More test
- Remove unused code
- Fix member chain for `encapsed` and `literal`

czosel: @evilebottnawi Nice! 

The only thing missing for `0.10.0` might be `brace-style`, right?
evilebottnawi: @czosel yep

Patch:
diff --git a/src/printer.js b/src/printer.js
index 342efb4..e28ada0 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -126,8 +126,7 @@ function printPropertyLookup(path, options, print) {
 
   if (
     node.offset.kind === "variable" ||
-    (node.offset.kind === "identifier" &&
-      typeof node.offset.name === "string") ||
+    node.offset.kind === "identifier" ||
     (node.offset.kind === "encapsed" && node.offset.type === "offset")
   ) {
     needCurly = false;
@@ -435,8 +434,7 @@ function printMemberChain(path, options, print) {
     return (
       isLookupNode(lastNode) &&
       (lastNode.offset.kind === "identifier" ||
-        lastNode.offset.kind === "variable" ||
-        lastNode.offset.kind === "encapsed") &&
+        lastNode.offset.kind === "variable") &&
       hasComputed
     );
   }
@@ -2053,10 +2051,9 @@ function printNode(path, options, print) {
               firstNonMemberParent.left.kind !== "variable"))) ||
         node.kind === "offsetlookup" ||
         ((isReferenceLikeNode(node.what) || node.what.kind === "variable") &&
-          (node.offset.kind === "identifier" ||
-            node.offset.kind === "variable" ||
-            node.offset.kind === "encapsed" ||
-            node.offset.kind === "literal") &&
+          ["identifier", "variable", "literal", "encapsed"].includes(
+            node.offset.kind
+          ) &&
           (parent && !isLookupNode(parent)));
 
       return concat([


Test patch:
diff --git a/tests/member_chain/__snapshots__/jsfmt.spec.js.snap b/tests/member_chain/__snapshots__/jsfmt.spec.js.snap
index 3c6acfe..d68d2b4 100644
--- a/tests/member_chain/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/member_chain/__snapshots__/jsfmt.spec.js.snap
@@ -706,6 +706,24 @@ $window->Data['key']['foo']("foo")
     ->then(function () { return $a; })
     ->catch(function () { return $b; });
 
+$window->foo_{'bar' . 'baz'}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
+
+$window->{call()}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
+
 =====================================output=====================================
 <?php
 
@@ -765,6 +783,24 @@ $window->Data['key']
         return $b;
     });
 
+$window->foo_{'bar' . 'baz'}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
+
+$window->{call()}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
+
 ================================================================================
 `;
 
diff --git a/tests/member_chain/offsetlookup.php b/tests/member_chain/offsetlookup.php
index b7c8c74..a619ac7 100644
--- a/tests/member_chain/offsetlookup.php
+++ b/tests/member_chain/offsetlookup.php
@@ -35,3 +35,21 @@ $window->Data['key']("foo")
 $window->Data['key']['foo']("foo")
     ->then(function () { return $a; })
     ->catch(function () { return $b; });
+
+$window->foo_{'bar' . 'baz'}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
+
+$window->{call()}
+    ['key']("foo")
+    ->then(function () {
+        return $a;
+    })
+    ->catch(function () {
+        return $b;
+    });
diff --git a/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap b/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
index db0486a..d7938e5 100644
--- a/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,55 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`offsets.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+// identifier
+$obj->var;
+// variable
+$obj->$var;
+// variable variable
+$obj->$$var;
+// literal with identifier
+$obj->{foo};
+// literal with variable
+$obj->{$var};
+// literal with call
+$obj->{call()};
+// encapsed (offset type)
+$obj->foo_{'test' . 'bar'};
+// variable with literal with call
+// TODO https://github.com/glayzzle/php-parser/issues/254
+// $obj->\${call()};
+
+=====================================output=====================================
+<?php
+
+// identifier
+$obj->var;
+// variable
+$obj->$var;
+// variable variable
+$obj->$$var;
+// literal with identifier
+$obj->{foo};
+// literal with variable
+$obj->{$var};
+// literal with call
+$obj->{call()};
+// encapsed (offset type)
+$obj->foo_{'test' . 'bar'};
+// variable with literal with call
+// TODO https://github.com/glayzzle/php-parser/issues/254
+// $obj->\${call()};
+
+================================================================================
+`;
+
 exports[`propertylookup.php 1`] = `
 ====================================options=====================================
 parsers: ["php"]
diff --git a/tests/propertylookup/offsets.php b/tests/propertylookup/offsets.php
new file mode 100644
index 0000000..a4e61e9
--- /dev/null
+++ b/tests/propertylookup/offsets.php
@@ -0,0 +1,19 @@
+<?php
+
+// identifier
+$obj->var;
+// variable
+$obj->$var;
+// variable variable
+$obj->$$var;
+// literal with identifier
+$obj->{foo};
+// literal with variable
+$obj->{$var};
+// literal with call
+$obj->{call()};
+// encapsed (offset type)
+$obj->foo_{'test' . 'bar'};
+// variable with literal with call
+// TODO https://github.com/glayzzle/php-parser/issues/254
+// $obj->${call()};