@prettier/plugin-php 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/printer.js CHANGED
@@ -1,23 +1,4 @@
1
1
  import { util as prettierUtil, doc } from "prettier";
2
- const {
3
- breakParent,
4
- join,
5
- line,
6
- lineSuffix,
7
- group,
8
- conditionalGroup,
9
- indent,
10
- dedent,
11
- ifBreak,
12
- hardline,
13
- softline,
14
- literalline,
15
- align,
16
- dedentToRoot,
17
- } = doc.builders;
18
- const { willBreak } = doc.utils;
19
- const { isNextLineEmptyAfterIndex, hasNewline, hasNewlineInRange } =
20
- prettierUtil;
21
2
  import {
22
3
  printAllComments,
23
4
  hasTrailingComment,
@@ -28,11 +9,11 @@ import {
28
9
  hasLeadingOwnLineComment,
29
10
  } from "./comments.js";
30
11
  import pathNeedsParens from "./needs-parens.js";
12
+ import { locStart, locEnd } from "./loc.js";
31
13
 
32
14
  import {
33
15
  getLast,
34
16
  getPenultimate,
35
- isLastStatement,
36
17
  lineShouldEndWithSemicolon,
37
18
  printNumber,
38
19
  shouldFlatten,
@@ -54,12 +35,34 @@ import {
54
35
  isDocNode,
55
36
  getAncestorNode,
56
37
  isReferenceLikeNode,
57
- getNextNode,
58
38
  normalizeMagicMethodName,
39
+ } from "./util.js";
40
+
41
+ const {
42
+ breakParent,
43
+ join,
44
+ line,
45
+ lineSuffix,
46
+ group,
47
+ conditionalGroup,
48
+ indent,
49
+ dedent,
50
+ ifBreak,
51
+ hardline,
52
+ softline,
53
+ literalline,
54
+ align,
55
+ dedentToRoot,
56
+ } = doc.builders;
57
+ const { willBreak } = doc.utils;
58
+ const {
59
+ isNextLineEmptyAfterIndex,
60
+ hasNewline,
61
+ hasNewlineInRange,
59
62
  getNextNonSpaceNonCommentCharacterIndex,
60
63
  isNextLineEmpty,
61
64
  isPreviousLineEmpty,
62
- } from "./util.js";
65
+ } = prettierUtil;
63
66
 
64
67
  function isMinVersion(actualVersion, requiredVersion) {
65
68
  return parseFloat(actualVersion) >= parseFloat(requiredVersion);
@@ -196,8 +199,7 @@ function printMemberChain(path, options, print) {
196
199
  const { originalText } = options;
197
200
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
198
201
  originalText,
199
- node,
200
- options
202
+ locEnd(node)
201
203
  );
202
204
  const nextChar = originalText.charAt(nextCharIndex);
203
205
 
@@ -211,7 +213,7 @@ function printMemberChain(path, options, print) {
211
213
  );
212
214
  }
213
215
 
214
- return isNextLineEmpty(originalText, node, options);
216
+ return isNextLineEmpty(originalText, locEnd(node));
215
217
  }
216
218
 
217
219
  function traverse(path) {
@@ -591,7 +593,7 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
591
593
 
592
594
  if (isLast) {
593
595
  // do nothing
594
- } else if (isNextLineEmpty(options.originalText, arg, options)) {
596
+ } else if (isNextLineEmpty(options.originalText, locEnd(arg))) {
595
597
  if (isFirst) {
596
598
  hasEmptyLineFollowingFirstArg = true;
597
599
  }
@@ -824,9 +826,9 @@ function printLookupNodes(path, options, print) {
824
826
  return printStaticLookup(path, options, print);
825
827
  case "offsetlookup":
826
828
  return printOffsetLookup(path, options, print);
827
- /* istanbul ignore next */
829
+ /* c8 ignore next 2 */
828
830
  default:
829
- return `Have not implemented lookup kind ${node.kind} yet.`;
831
+ throw new Error(`Have not implemented lookup kind ${node.kind} yet.`);
830
832
  }
831
833
  }
832
834
 
@@ -844,8 +846,8 @@ function getEncapsedQuotes(node, { opening = true } = {}) {
844
846
  return quotes[node.type];
845
847
  }
846
848
 
847
- /* istanbul ignore next */
848
- return `Unimplemented encapsed type ${node.type}`;
849
+ /* c8 ignore next */
850
+ throw new Error(`Unimplemented encapsed type ${node.type}`);
849
851
  }
850
852
 
851
853
  function printArrayItems(path, options, print) {
@@ -858,7 +860,7 @@ function printArrayItems(path, options, print) {
858
860
 
859
861
  separatorParts = [",", line];
860
862
 
861
- if (node && isNextLineEmpty(options.originalText, node, options)) {
863
+ if (node && isNextLineEmpty(options.originalText, locEnd(node))) {
862
864
  separatorParts.push(softline);
863
865
  }
864
866
  }, "items");
@@ -930,7 +932,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
930
932
  const isInlineNode = childNode.kind === "inline";
931
933
  const printedPath = print();
932
934
  const canPrintBlankLine =
933
- !isLastStatement(path) &&
935
+ !isLastNode &&
934
936
  !isInlineNode &&
935
937
  (nextNode && nextNode.kind === "case"
936
938
  ? !isFirstChildrenInlineNode(path)
@@ -940,7 +942,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
940
942
  printedPath,
941
943
  canPrintBlankLine ? hardline : "",
942
944
  canPrintBlankLine &&
943
- isNextLineEmpty(options.originalText, childNode, options)
945
+ isNextLineEmpty(options.originalText, locEnd(childNode))
944
946
  ? hardline
945
947
  : "",
946
948
  ];
@@ -1069,8 +1071,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
1069
1071
  ? [
1070
1072
  hasNewlineInRange(
1071
1073
  options.originalText.trimEnd(),
1072
- options.locEnd(lastNode),
1073
- options.locEnd(node)
1074
+ locEnd(lastNode),
1075
+ locEnd(node)
1074
1076
  )
1075
1077
  ? !(
1076
1078
  lastNode.kind === "inline" &&
@@ -1080,7 +1082,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
1080
1082
  ? hardline
1081
1083
  : ""
1082
1084
  : " ",
1083
- isNextLineEmpty(options.originalText, lastNode, options)
1085
+ isNextLineEmpty(options.originalText, locEnd(lastNode))
1084
1086
  ? hardline
1085
1087
  : "",
1086
1088
  ]
@@ -1098,15 +1100,15 @@ function printLines(path, options, print, childrenAttribute = "children") {
1098
1100
  }
1099
1101
 
1100
1102
  function printStatements(path, options, print, childrenAttribute) {
1101
- return path.map(() => {
1103
+ return path.map(({ node, isLast }) => {
1102
1104
  const parts = [];
1103
1105
 
1104
1106
  parts.push(print());
1105
1107
 
1106
- if (!isLastStatement(path)) {
1108
+ if (!isLast) {
1107
1109
  parts.push(hardline);
1108
1110
 
1109
- if (isNextLineEmpty(options.originalText, path.node, options)) {
1111
+ if (isNextLineEmpty(options.originalText, locEnd(node))) {
1110
1112
  parts.push(hardline);
1111
1113
  }
1112
1114
  }
@@ -1502,7 +1504,7 @@ function printAssignmentRight(
1502
1504
  ) {
1503
1505
  const ref = hasRef ? "&" : "";
1504
1506
 
1505
- if (hasLeadingOwnLineComment(options.originalText, rightNode, options)) {
1507
+ if (hasLeadingOwnLineComment(options.originalText, rightNode)) {
1506
1508
  return indent([hardline, ref, printedRight]);
1507
1509
  }
1508
1510
 
@@ -1545,13 +1547,13 @@ function stringHasNewLines(node) {
1545
1547
  return node.raw.includes("\n");
1546
1548
  }
1547
1549
 
1548
- function isStringOnItsOwnLine(node, text, options) {
1550
+ function isStringOnItsOwnLine(node, text) {
1549
1551
  return (
1550
1552
  (node.kind === "string" ||
1551
1553
  (node.kind === "encapsed" &&
1552
1554
  (node.type === "string" || node.type === "shell"))) &&
1553
1555
  stringHasNewLines(node) &&
1554
- !hasNewline(text, options.locStart(node), { backwards: true })
1556
+ !hasNewline(text, locStart(node), { backwards: true })
1555
1557
  );
1556
1558
  }
1557
1559
 
@@ -1603,13 +1605,11 @@ function printNode(path, options, print) {
1603
1605
  ];
1604
1606
  }
1605
1607
 
1606
- const nextNode = getNextNode(path, node);
1607
-
1608
1608
  return [
1609
1609
  "declare(",
1610
1610
  printDeclareArguments(path),
1611
1611
  ")",
1612
- nextNode && nextNode.kind === "inline" ? "" : ";",
1612
+ path.next?.kind === "inline" ? "" : ";",
1613
1613
  ];
1614
1614
  }
1615
1615
  case "declaredirective":
@@ -1632,11 +1632,7 @@ function printNode(path, options, print) {
1632
1632
  ? ""
1633
1633
  : [
1634
1634
  hardline,
1635
- isNextLineEmptyAfterNamespace(
1636
- options.originalText,
1637
- node,
1638
- options.locStart
1639
- )
1635
+ isNextLineEmptyAfterNamespace(options.originalText, node)
1640
1636
  ? hardline
1641
1637
  : "",
1642
1638
  ],
@@ -1857,7 +1853,7 @@ function printNode(path, options, print) {
1857
1853
 
1858
1854
  if (hasDanglingComments(node)) {
1859
1855
  parts.push(
1860
- isNextLineEmpty(options.originalText, node.body, options)
1856
+ isNextLineEmpty(options.originalText, locEnd(node.body))
1861
1857
  ? hardline
1862
1858
  : "",
1863
1859
  printDanglingComments(path, options, true),
@@ -2039,7 +2035,7 @@ function printNode(path, options, print) {
2039
2035
  // Multiline strings as single arguments
2040
2036
  if (
2041
2037
  node.arguments.length === 1 &&
2042
- isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
2038
+ isStringOnItsOwnLine(node.arguments[0], options.originalText)
2043
2039
  ) {
2044
2040
  return [
2045
2041
  print("what"),
@@ -2064,7 +2060,7 @@ function printNode(path, options, print) {
2064
2060
  if (
2065
2061
  !isAnonymousClassNode &&
2066
2062
  node.arguments.length === 1 &&
2067
- isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
2063
+ isStringOnItsOwnLine(node.arguments[0], options.originalText)
2068
2064
  ) {
2069
2065
  return [
2070
2066
  "new ",
@@ -2156,7 +2152,7 @@ function printNode(path, options, print) {
2156
2152
  node.useDie ? "die" : "exit",
2157
2153
  "(",
2158
2154
  node.expression
2159
- ? isStringOnItsOwnLine(node.expression, options.originalText, options)
2155
+ ? isStringOnItsOwnLine(node.expression, options.originalText)
2160
2156
  ? print("expression")
2161
2157
  : [indent([softline, print("expression")]), softline]
2162
2158
  : printDanglingComments(path, options),
@@ -2199,7 +2195,7 @@ function printNode(path, options, print) {
2199
2195
  case "eval":
2200
2196
  return group([
2201
2197
  "eval(",
2202
- isStringOnItsOwnLine(node.source, options.originalText, options)
2198
+ isStringOnItsOwnLine(node.source, options.originalText)
2203
2199
  ? print("source")
2204
2200
  : [indent([softline, print("source")]), softline],
2205
2201
  ")",
@@ -2388,15 +2384,15 @@ function printNode(path, options, print) {
2388
2384
 
2389
2385
  const [firstProperty] = node.items
2390
2386
  .filter((node) => node.kind !== "noop")
2391
- .sort((a, b) => options.locStart(a) - options.locStart(b));
2387
+ .sort((a, b) => locStart(a) - locStart(b));
2392
2388
  const isAssociative = !!(firstProperty && firstProperty.key);
2393
2389
  const shouldBreak =
2394
2390
  isAssociative &&
2395
2391
  firstProperty &&
2396
2392
  hasNewlineInRange(
2397
2393
  options.originalText,
2398
- options.locStart(node),
2399
- options.locStart(firstProperty)
2394
+ locStart(node),
2395
+ locStart(firstProperty)
2400
2396
  );
2401
2397
 
2402
2398
  return group(
@@ -2754,9 +2750,9 @@ function printNode(path, options, print) {
2754
2750
  : "",
2755
2751
  ];
2756
2752
  }
2757
- // istanbul ignore next
2753
+ /* c8 ignore next 2 */
2758
2754
  default:
2759
- return `Have not implemented kind ${node.type} yet.`;
2755
+ throw new Error(`Have not implemented kind ${node.type} yet.`);
2760
2756
  }
2761
2757
  case "inline":
2762
2758
  return join(
@@ -2831,7 +2827,7 @@ function printNode(path, options, print) {
2831
2827
  const body = print("body");
2832
2828
  const maybeEmptyLineBetweenArms =
2833
2829
  !path.isFirst &&
2834
- isPreviousLineEmpty(options.originalText, armNode, options)
2830
+ isPreviousLineEmpty(options.originalText, locStart(armNode))
2835
2831
  ? hardline
2836
2832
  : "";
2837
2833
 
@@ -2851,7 +2847,7 @@ function printNode(path, options, print) {
2851
2847
  }, "arms");
2852
2848
  return group([
2853
2849
  "match (",
2854
- group([softline, indent(print("cond")), softline]),
2850
+ group([indent([softline, print("cond")]), softline]),
2855
2851
  ") {",
2856
2852
  group(indent([...arms])),
2857
2853
  " ",
@@ -2885,10 +2881,10 @@ function printNode(path, options, print) {
2885
2881
  case "variadicplaceholder":
2886
2882
  return "...";
2887
2883
 
2884
+ /* c8 ignore next 3 */
2888
2885
  case "error":
2889
2886
  default:
2890
- // istanbul ignore next
2891
- return `Have not implemented kind ${node.kind} yet.`;
2887
+ throw new Error(`Have not implemented kind '${node.kind}' yet.`);
2892
2888
  }
2893
2889
  }
2894
2890
 
package/src/util.js CHANGED
@@ -1,38 +1,7 @@
1
- import { util as prettierUtil, version as prettierVersion } from "prettier";
2
-
3
- const {
4
- hasNewline,
5
- skipEverythingButNewLine,
6
- skipNewline,
7
- isNextLineEmpty: _isNextLineEmpty,
8
- isPreviousLineEmpty: _isPreviousLineEmpty,
9
- getNextNonSpaceNonCommentCharacterIndex:
10
- _getNextNonSpaceNonCommentCharacterIndex,
11
- } = prettierUtil;
12
-
13
- function lookupIfPrettier2(options, prop) {
14
- return parseInt(prettierVersion[0]) > 1 ? options[prop] : options;
15
- }
16
-
17
- function isPreviousLineEmpty(text, node, options) {
18
- return _isPreviousLineEmpty(
19
- text,
20
- node,
21
- lookupIfPrettier2(options, "locStart")
22
- );
23
- }
1
+ import { util as prettierUtil } from "prettier";
2
+ import { locStart } from "./loc.js";
24
3
 
25
- function isNextLineEmpty(text, node, options) {
26
- return _isNextLineEmpty(text, node, lookupIfPrettier2(options, "locEnd"));
27
- }
28
-
29
- function getNextNonSpaceNonCommentCharacterIndex(text, node, options) {
30
- return _getNextNonSpaceNonCommentCharacterIndex(
31
- text,
32
- node,
33
- lookupIfPrettier2(options, "locEnd")
34
- );
35
- }
4
+ const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil;
36
5
 
37
6
  function printNumber(rawNumber) {
38
7
  return (
@@ -52,49 +21,47 @@ function printNumber(rawNumber) {
52
21
  }
53
22
 
54
23
  // http://php.net/manual/en/language.operators.precedence.php
55
- const PRECEDENCE = {};
56
- [
57
- ["or"],
58
- ["xor"],
59
- ["and"],
24
+ const PRECEDENCE = new Map(
60
25
  [
61
- "=",
62
- "+=",
63
- "-=",
64
- "*=",
65
- "**=",
66
- "/=",
67
- ".=",
68
- "%=",
69
- "&=",
70
- "|=",
71
- "^=",
72
- "<<=",
73
- ">>=",
74
- ],
75
- ["??"],
76
- ["||"],
77
- ["&&"],
78
- ["|"],
79
- ["^"],
80
- ["&"],
81
- ["==", "===", "!=", "!==", "<>", "<=>"],
82
- ["<", ">", "<=", ">="],
83
- [">>", "<<"],
84
- ["+", "-", "."],
85
- ["*", "/", "%"],
86
- ["!"],
87
- ["instanceof"],
88
- ["++", "--", "~"],
89
- ["**"],
90
- ].forEach((tier, i) => {
91
- tier.forEach((op) => {
92
- PRECEDENCE[op] = i;
93
- });
94
- });
95
-
96
- function getPrecedence(op) {
97
- return PRECEDENCE[op];
26
+ ["or"],
27
+ ["xor"],
28
+ ["and"],
29
+ [
30
+ "=",
31
+ "+=",
32
+ "-=",
33
+ "*=",
34
+ "**=",
35
+ "/=",
36
+ ".=",
37
+ "%=",
38
+ "&=",
39
+ "|=",
40
+ "^=",
41
+ "<<=",
42
+ ">>=",
43
+ ],
44
+ ["??"],
45
+ ["||"],
46
+ ["&&"],
47
+ ["|"],
48
+ ["^"],
49
+ ["&"],
50
+ ["==", "===", "!=", "!==", "<>", "<=>"],
51
+ ["<", ">", "<=", ">="],
52
+ [">>", "<<"],
53
+ ["+", "-", "."],
54
+ ["*", "/", "%"],
55
+ ["!"],
56
+ ["instanceof"],
57
+ ["++", "--", "~"],
58
+ ["**"],
59
+ ].flatMap((operators, index) =>
60
+ operators.map((operator) => [operator, index])
61
+ )
62
+ );
63
+ function getPrecedence(operator) {
64
+ return PRECEDENCE.get(operator);
98
65
  }
99
66
 
100
67
  const equalityOperators = ["==", "!=", "===", "!==", "<>", "<=>"];
@@ -187,14 +154,6 @@ function getNodeListProperty(node) {
187
154
  return Array.isArray(body) ? body : null;
188
155
  }
189
156
 
190
- function getParentNodeListProperty(path) {
191
- const { parent } = path;
192
- if (!parent) {
193
- return null;
194
- }
195
- return getNodeListProperty(parent);
196
- }
197
-
198
157
  function getLast(arr) {
199
158
  if (arr.length > 0) {
200
159
  return arr[arr.length - 1];
@@ -209,15 +168,6 @@ function getPenultimate(arr) {
209
168
  return null;
210
169
  }
211
170
 
212
- function isLastStatement(path) {
213
- const body = getParentNodeListProperty(path);
214
- if (!body) {
215
- return true;
216
- }
217
- const { node } = path;
218
- return body[body.length - 1] === node;
219
- }
220
-
221
171
  function isFirstChildrenInlineNode(path) {
222
172
  const { node } = path;
223
173
 
@@ -532,23 +482,6 @@ function getAlignment(text) {
532
482
  return lastLine.length - lastLine.trimLeft().length + 1;
533
483
  }
534
484
 
535
- function getNextNode(path, node) {
536
- const { parent } = path;
537
- const children = getNodeListProperty(parent);
538
-
539
- if (!children) {
540
- return null;
541
- }
542
-
543
- const index = children.indexOf(node);
544
-
545
- if (index === -1) {
546
- return null;
547
- }
548
-
549
- return parent.children[index + 1];
550
- }
551
-
552
485
  function isProgramLikeNode(node) {
553
486
  return ["program", "declare", "namespace"].includes(node.kind);
554
487
  }
@@ -605,7 +538,7 @@ function hasEmptyBody(path, name = "body") {
605
538
  );
606
539
  }
607
540
 
608
- function isNextLineEmptyAfterNamespace(text, node, locStart) {
541
+ function isNextLineEmptyAfterNamespace(text, node) {
609
542
  let idx = locStart(node);
610
543
  idx = skipEverythingButNewLine(text, idx);
611
544
  idx = skipNewline(text, idx);
@@ -665,17 +598,15 @@ const magicMethods = [
665
598
  "__clone",
666
599
  "__debugInfo",
667
600
  ];
668
- const MagicMethodsMap = magicMethods.reduce((map, obj) => {
669
- map[obj.toLowerCase()] = obj;
670
-
671
- return map;
672
- }, {});
601
+ const magicMethodsMap = new Map(
602
+ magicMethods.map((name) => [name.toLowerCase(), name])
603
+ );
673
604
 
674
605
  function normalizeMagicMethodName(name) {
675
606
  const loweredName = name.toLowerCase();
676
607
 
677
- if (MagicMethodsMap[loweredName]) {
678
- return MagicMethodsMap[loweredName];
608
+ if (magicMethodsMap.has(loweredName)) {
609
+ return magicMethodsMap.get(loweredName);
679
610
  }
680
611
 
681
612
  return name;
@@ -687,11 +618,8 @@ export {
687
618
  isBitwiseOperator,
688
619
  shouldFlatten,
689
620
  nodeHasStatement,
690
- getNodeListProperty,
691
- getParentNodeListProperty,
692
621
  getLast,
693
622
  getPenultimate,
694
- isLastStatement,
695
623
  getBodyFirstChild,
696
624
  lineShouldEndWithSemicolon,
697
625
  fileShouldEndWithHardline,
@@ -712,9 +640,5 @@ export {
712
640
  shouldPrintHardlineBeforeTrailingComma,
713
641
  isDocNode,
714
642
  getAncestorNode,
715
- getNextNode,
716
643
  normalizeMagicMethodName,
717
- isPreviousLineEmpty,
718
- isNextLineEmpty,
719
- getNextNonSpaceNonCommentCharacterIndex,
720
644
  };