@prettier/plugin-php 0.20.0 → 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/README.md +35 -6
- package/package.json +51 -26
- package/src/clean.js +5 -7
- package/src/comments.js +67 -68
- package/src/index.js +40 -37
- package/src/linguist-languages.cjs +4 -0
- package/src/loc.js +4 -0
- package/src/needs-parens.js +16 -27
- package/src/options.js +1 -3
- package/src/parser.js +2 -3
- package/src/pragma.js +3 -8
- package/src/printer.js +190 -243
- package/src/util.js +57 -146
- package/standalone.js +1 -3
- package/src/module.js +0 -2
package/src/util.js
CHANGED
|
@@ -1,40 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
hasNewline,
|
|
5
|
-
skipEverythingButNewLine,
|
|
6
|
-
skipNewline,
|
|
7
|
-
isNextLineEmpty: _isNextLineEmpty,
|
|
8
|
-
isPreviousLineEmpty: _isPreviousLineEmpty,
|
|
9
|
-
getNextNonSpaceNonCommentCharacterIndex:
|
|
10
|
-
_getNextNonSpaceNonCommentCharacterIndex,
|
|
11
|
-
} = require("prettier").util;
|
|
12
|
-
|
|
13
|
-
const prettierVersion = require("prettier").version;
|
|
14
|
-
|
|
15
|
-
function lookupIfPrettier2(options, prop) {
|
|
16
|
-
return parseInt(prettierVersion[0]) > 1 ? options[prop] : options;
|
|
17
|
-
}
|
|
1
|
+
import { util as prettierUtil } from "prettier";
|
|
2
|
+
import { locStart } from "./loc.js";
|
|
18
3
|
|
|
19
|
-
|
|
20
|
-
return _isPreviousLineEmpty(
|
|
21
|
-
text,
|
|
22
|
-
node,
|
|
23
|
-
lookupIfPrettier2(options, "locStart")
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function isNextLineEmpty(text, node, options) {
|
|
28
|
-
return _isNextLineEmpty(text, node, lookupIfPrettier2(options, "locEnd"));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getNextNonSpaceNonCommentCharacterIndex(text, node, options) {
|
|
32
|
-
return _getNextNonSpaceNonCommentCharacterIndex(
|
|
33
|
-
text,
|
|
34
|
-
node,
|
|
35
|
-
lookupIfPrettier2(options, "locEnd")
|
|
36
|
-
);
|
|
37
|
-
}
|
|
4
|
+
const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil;
|
|
38
5
|
|
|
39
6
|
function printNumber(rawNumber) {
|
|
40
7
|
return (
|
|
@@ -54,49 +21,47 @@ function printNumber(rawNumber) {
|
|
|
54
21
|
}
|
|
55
22
|
|
|
56
23
|
// http://php.net/manual/en/language.operators.precedence.php
|
|
57
|
-
const PRECEDENCE =
|
|
58
|
-
[
|
|
59
|
-
["or"],
|
|
60
|
-
["xor"],
|
|
61
|
-
["and"],
|
|
24
|
+
const PRECEDENCE = new Map(
|
|
62
25
|
[
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
]
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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);
|
|
100
65
|
}
|
|
101
66
|
|
|
102
67
|
const equalityOperators = ["==", "!=", "===", "!==", "<>", "<=>"];
|
|
@@ -189,14 +154,6 @@ function getNodeListProperty(node) {
|
|
|
189
154
|
return Array.isArray(body) ? body : null;
|
|
190
155
|
}
|
|
191
156
|
|
|
192
|
-
function getParentNodeListProperty(path) {
|
|
193
|
-
const parent = path.getParentNode();
|
|
194
|
-
if (!parent) {
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
return getNodeListProperty(parent);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
157
|
function getLast(arr) {
|
|
201
158
|
if (arr.length > 0) {
|
|
202
159
|
return arr[arr.length - 1];
|
|
@@ -211,17 +168,8 @@ function getPenultimate(arr) {
|
|
|
211
168
|
return null;
|
|
212
169
|
}
|
|
213
170
|
|
|
214
|
-
function isLastStatement(path) {
|
|
215
|
-
const body = getParentNodeListProperty(path);
|
|
216
|
-
if (!body) {
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
const node = path.getValue();
|
|
220
|
-
return body[body.length - 1] === node;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
171
|
function isFirstChildrenInlineNode(path) {
|
|
224
|
-
const node = path
|
|
172
|
+
const { node } = path;
|
|
225
173
|
|
|
226
174
|
if (node.kind === "program") {
|
|
227
175
|
const children = getNodeListProperty(node);
|
|
@@ -377,8 +325,7 @@ function docShouldHaveTrailingNewline(path, recurse = 0) {
|
|
|
377
325
|
}
|
|
378
326
|
|
|
379
327
|
function lineShouldEndWithSemicolon(path) {
|
|
380
|
-
const node = path
|
|
381
|
-
const parentNode = path.getParentNode();
|
|
328
|
+
const { node, parent: parentNode } = path;
|
|
382
329
|
if (!parentNode) {
|
|
383
330
|
return false;
|
|
384
331
|
}
|
|
@@ -407,7 +354,7 @@ function lineShouldEndWithSemicolon(path) {
|
|
|
407
354
|
return true;
|
|
408
355
|
}
|
|
409
356
|
if (node.kind === "method") {
|
|
410
|
-
const parent = path
|
|
357
|
+
const { parent } = path;
|
|
411
358
|
if (parent && parent.kind === "interface") {
|
|
412
359
|
return true;
|
|
413
360
|
}
|
|
@@ -435,7 +382,7 @@ function lineShouldEndWithSemicolon(path) {
|
|
|
435
382
|
}
|
|
436
383
|
|
|
437
384
|
function fileShouldEndWithHardline(path) {
|
|
438
|
-
const node = path
|
|
385
|
+
const { node } = path;
|
|
439
386
|
const isProgramNode = node.kind === "program";
|
|
440
387
|
const lastNode = node.children && getLast(node.children);
|
|
441
388
|
|
|
@@ -477,14 +424,6 @@ function hasDanglingComments(node) {
|
|
|
477
424
|
);
|
|
478
425
|
}
|
|
479
426
|
|
|
480
|
-
function hasLeadingComment(node) {
|
|
481
|
-
return node.comments && node.comments.some((comment) => comment.leading);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function hasTrailingComment(node) {
|
|
485
|
-
return node.comments && node.comments.some((comment) => comment.trailing);
|
|
486
|
-
}
|
|
487
|
-
|
|
488
427
|
function isLookupNode(node) {
|
|
489
428
|
return (
|
|
490
429
|
node.kind === "propertylookup" ||
|
|
@@ -495,7 +434,7 @@ function isLookupNode(node) {
|
|
|
495
434
|
}
|
|
496
435
|
|
|
497
436
|
function shouldPrintHardLineAfterStartInControlStructure(path) {
|
|
498
|
-
const node = path
|
|
437
|
+
const { node } = path;
|
|
499
438
|
|
|
500
439
|
if (["try", "catch"].includes(node.kind)) {
|
|
501
440
|
return false;
|
|
@@ -505,7 +444,7 @@ function shouldPrintHardLineAfterStartInControlStructure(path) {
|
|
|
505
444
|
}
|
|
506
445
|
|
|
507
446
|
function shouldPrintHardLineBeforeEndInControlStructure(path) {
|
|
508
|
-
const node = path
|
|
447
|
+
const { node } = path;
|
|
509
448
|
|
|
510
449
|
if (["try", "catch"].includes(node.kind)) {
|
|
511
450
|
return true;
|
|
@@ -543,23 +482,6 @@ function getAlignment(text) {
|
|
|
543
482
|
return lastLine.length - lastLine.trimLeft().length + 1;
|
|
544
483
|
}
|
|
545
484
|
|
|
546
|
-
function getNextNode(path, node) {
|
|
547
|
-
const parent = path.getParentNode();
|
|
548
|
-
const children = getNodeListProperty(parent);
|
|
549
|
-
|
|
550
|
-
if (!children) {
|
|
551
|
-
return null;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
const index = children.indexOf(node);
|
|
555
|
-
|
|
556
|
-
if (index === -1) {
|
|
557
|
-
return null;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
return parent.children[index + 1];
|
|
561
|
-
}
|
|
562
|
-
|
|
563
485
|
function isProgramLikeNode(node) {
|
|
564
486
|
return ["program", "declare", "namespace"].includes(node.kind);
|
|
565
487
|
}
|
|
@@ -606,7 +528,7 @@ function useDoubleQuote(node, options) {
|
|
|
606
528
|
}
|
|
607
529
|
|
|
608
530
|
function hasEmptyBody(path, name = "body") {
|
|
609
|
-
const node = path
|
|
531
|
+
const { node } = path;
|
|
610
532
|
|
|
611
533
|
return (
|
|
612
534
|
node[name] &&
|
|
@@ -616,7 +538,7 @@ function hasEmptyBody(path, name = "body") {
|
|
|
616
538
|
);
|
|
617
539
|
}
|
|
618
540
|
|
|
619
|
-
function isNextLineEmptyAfterNamespace(text, node
|
|
541
|
+
function isNextLineEmptyAfterNamespace(text, node) {
|
|
620
542
|
let idx = locStart(node);
|
|
621
543
|
idx = skipEverythingButNewLine(text, idx);
|
|
622
544
|
idx = skipNewline(text, idx);
|
|
@@ -676,40 +598,33 @@ const magicMethods = [
|
|
|
676
598
|
"__clone",
|
|
677
599
|
"__debugInfo",
|
|
678
600
|
];
|
|
679
|
-
const
|
|
680
|
-
map[
|
|
681
|
-
|
|
682
|
-
return map;
|
|
683
|
-
}, {});
|
|
601
|
+
const magicMethodsMap = new Map(
|
|
602
|
+
magicMethods.map((name) => [name.toLowerCase(), name])
|
|
603
|
+
);
|
|
684
604
|
|
|
685
605
|
function normalizeMagicMethodName(name) {
|
|
686
606
|
const loweredName = name.toLowerCase();
|
|
687
607
|
|
|
688
|
-
if (
|
|
689
|
-
return
|
|
608
|
+
if (magicMethodsMap.has(loweredName)) {
|
|
609
|
+
return magicMethodsMap.get(loweredName);
|
|
690
610
|
}
|
|
691
611
|
|
|
692
612
|
return name;
|
|
693
613
|
}
|
|
694
614
|
|
|
695
|
-
|
|
615
|
+
export {
|
|
696
616
|
printNumber,
|
|
697
617
|
getPrecedence,
|
|
698
618
|
isBitwiseOperator,
|
|
699
619
|
shouldFlatten,
|
|
700
620
|
nodeHasStatement,
|
|
701
|
-
getNodeListProperty,
|
|
702
|
-
getParentNodeListProperty,
|
|
703
621
|
getLast,
|
|
704
622
|
getPenultimate,
|
|
705
|
-
isLastStatement,
|
|
706
623
|
getBodyFirstChild,
|
|
707
624
|
lineShouldEndWithSemicolon,
|
|
708
625
|
fileShouldEndWithHardline,
|
|
709
626
|
maybeStripLeadingSlashFromUse,
|
|
710
627
|
hasDanglingComments,
|
|
711
|
-
hasLeadingComment,
|
|
712
|
-
hasTrailingComment,
|
|
713
628
|
docShouldHaveTrailingNewline,
|
|
714
629
|
isLookupNode,
|
|
715
630
|
isFirstChildrenInlineNode,
|
|
@@ -725,9 +640,5 @@ module.exports = {
|
|
|
725
640
|
shouldPrintHardlineBeforeTrailingComma,
|
|
726
641
|
isDocNode,
|
|
727
642
|
getAncestorNode,
|
|
728
|
-
getNextNode,
|
|
729
643
|
normalizeMagicMethodName,
|
|
730
|
-
isPreviousLineEmpty,
|
|
731
|
-
isNextLineEmpty,
|
|
732
|
-
getNextNonSpaceNonCommentCharacterIndex,
|
|
733
644
|
};
|