@prettier/plugin-php 0.20.1 → 0.22.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 +11 -1
- package/package.json +41 -27
- package/src/{clean.js → clean.mjs} +1 -1
- package/src/{comments.js → comments.mjs} +45 -47
- package/src/{index.js → index.mjs} +10 -13
- package/src/linguist-languages.cjs +4 -4
- package/src/loc.mjs +4 -0
- package/src/{needs-parens.js → needs-parens.mjs} +1 -4
- package/src/{pragma.js → pragma.mjs} +1 -1
- package/src/{printer.js → printer.mjs} +67 -71
- package/src/{util.js → util.mjs} +49 -125
- package/standalone.js +1 -3
- /package/src/{options.js → options.mjs} +0 -0
- /package/src/{parser.js → parser.mjs} +0 -0
package/README.md
CHANGED
|
@@ -97,6 +97,16 @@ npm install --save-dev prettier @prettier/plugin-php
|
|
|
97
97
|
npm install --global prettier @prettier/plugin-php
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
### Activate the plugin
|
|
101
|
+
|
|
102
|
+
Create or modify your [prettier configuration file](https://prettier.io/docs/en/configuration) to activate the plugin:
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"plugins": ["@prettier/plugin-php"]
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
100
110
|
## Use
|
|
101
111
|
|
|
102
112
|
### With Node.js
|
|
@@ -234,7 +244,7 @@ You may also need to declare php as the parser in your prettier config file:
|
|
|
234
244
|
```json
|
|
235
245
|
{
|
|
236
246
|
"parser": "php",
|
|
237
|
-
"plugins": ["@prettier/plugin-php"]
|
|
247
|
+
"plugins": ["@prettier/plugin-php"]
|
|
238
248
|
}
|
|
239
249
|
```
|
|
240
250
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prettier/plugin-php",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Prettier PHP Plugin",
|
|
5
5
|
"repository": "prettier/prettier-php",
|
|
6
6
|
"author": "Lucas Azzola <@azz>",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"type": "module",
|
|
9
8
|
"unpkg": "./standalone.js",
|
|
10
9
|
"browser": "./standalone.js",
|
|
11
10
|
"exports": {
|
|
@@ -21,47 +20,62 @@
|
|
|
21
20
|
"standalone.js"
|
|
22
21
|
],
|
|
23
22
|
"dependencies": {
|
|
24
|
-
"linguist-languages": "^7.
|
|
23
|
+
"linguist-languages": "^7.27.0",
|
|
25
24
|
"mem": "^9.0.2",
|
|
26
25
|
"php-parser": "^3.1.5"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
|
-
"@babel/preset-env": "^7.
|
|
30
|
-
"@rollup/plugin-
|
|
31
|
-
"@rollup/plugin-
|
|
32
|
-
"
|
|
28
|
+
"@babel/preset-env": "^7.23.6",
|
|
29
|
+
"@rollup/plugin-alias": "^5.1.0",
|
|
30
|
+
"@rollup/plugin-babel": "^6.0.3",
|
|
31
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
32
|
+
"@rollup/plugin-inject": "^5.0.5",
|
|
33
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
34
|
+
"@rollup/plugin-node-resolve": "^15.2.1",
|
|
35
|
+
"@rollup/plugin-replace": "^5.0.5",
|
|
36
|
+
"@rollup/plugin-terser": "^0.4.3",
|
|
37
|
+
"c8": "^8.0.1",
|
|
33
38
|
"cross-env": "^7.0.2",
|
|
34
|
-
"eslint": "8.
|
|
35
|
-
"eslint-config-prettier": "9.
|
|
36
|
-
"eslint-plugin-import": "2.
|
|
37
|
-
"eslint-plugin-jest": "27.
|
|
38
|
-
"eslint-plugin-prettier": "5.0.
|
|
39
|
+
"eslint": "8.56.0",
|
|
40
|
+
"eslint-config-prettier": "9.1.0",
|
|
41
|
+
"eslint-plugin-import": "2.29.1",
|
|
42
|
+
"eslint-plugin-jest": "27.6.0",
|
|
43
|
+
"eslint-plugin-prettier": "5.0.1",
|
|
39
44
|
"eslint-plugin-prettier-doc": "^1.1.0",
|
|
40
|
-
"jest": "29.
|
|
41
|
-
"jest-environment-jsdom": "29.
|
|
42
|
-
"jest-light-runner": "^0.
|
|
43
|
-
"jest-
|
|
44
|
-
"jest-snapshot-serializer-raw": "^1.1.0",
|
|
45
|
+
"jest": "29.7.0",
|
|
46
|
+
"jest-environment-jsdom": "29.7.0",
|
|
47
|
+
"jest-light-runner": "^0.6.0",
|
|
48
|
+
"jest-snapshot-serializer-raw": "^2.0.0",
|
|
45
49
|
"prettier": "^3.0.1",
|
|
46
|
-
"rollup": "^
|
|
47
|
-
"rollup-plugin-alias": "^2.0.0",
|
|
48
|
-
"rollup-plugin-babel": "^4.3.2",
|
|
49
|
-
"rollup-plugin-inject": "^3.0.1",
|
|
50
|
-
"rollup-plugin-json": "^4.0.0",
|
|
51
|
-
"rollup-plugin-replace": "^2.1.0",
|
|
52
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
50
|
+
"rollup": "^3.28.1",
|
|
53
51
|
"strip-ansi": "^7.1.0"
|
|
54
52
|
},
|
|
55
53
|
"peerDependencies": {
|
|
56
54
|
"prettier": "^3.0.0"
|
|
57
55
|
},
|
|
58
56
|
"scripts": {
|
|
57
|
+
"lint": "yarn lint:eslint && yarn lint:prettier",
|
|
58
|
+
"lint:eslint": "eslint .",
|
|
59
|
+
"lint:prettier": "prettier . --check",
|
|
60
|
+
"fix": "yarn fix:eslint && yarn fix:prettier",
|
|
61
|
+
"fix:eslint": "eslint . --fix",
|
|
62
|
+
"fix:prettier": "prettier . --write",
|
|
59
63
|
"test": "yarn test:node && yarn test:standalone",
|
|
60
64
|
"test:node": "jest",
|
|
61
|
-
"test:standalone": "yarn run build
|
|
65
|
+
"test:standalone": "yarn run build && cross-env RUN_STANDALONE_TESTS=true yarn jest",
|
|
62
66
|
"prepublishOnly": "yarn test",
|
|
63
|
-
"prettier": "prettier --plugin=src/index.
|
|
64
|
-
"build
|
|
67
|
+
"prettier": "prettier --plugin=src/index.mjs --parser=php",
|
|
68
|
+
"build": "rollup --config build/rollup.config.mjs",
|
|
65
69
|
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
|
|
70
|
+
},
|
|
71
|
+
"c8": {
|
|
72
|
+
"reporter": [
|
|
73
|
+
"lcov",
|
|
74
|
+
"text"
|
|
75
|
+
],
|
|
76
|
+
"all": true,
|
|
77
|
+
"include": [
|
|
78
|
+
"src/**"
|
|
79
|
+
]
|
|
66
80
|
}
|
|
67
81
|
}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { util as prettierUtil, doc } from "prettier";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
isNextLineEmpty,
|
|
5
|
-
isPreviousLineEmpty,
|
|
6
|
-
isLookupNode,
|
|
7
|
-
} from "./util.js";
|
|
2
|
+
import { isLookupNode } from "./util.mjs";
|
|
3
|
+
import { locStart, locEnd } from "./loc.mjs";
|
|
8
4
|
|
|
9
5
|
const {
|
|
10
6
|
addLeadingComment,
|
|
@@ -13,6 +9,9 @@ const {
|
|
|
13
9
|
skipNewline,
|
|
14
10
|
hasNewline,
|
|
15
11
|
hasNewlineInRange,
|
|
12
|
+
getNextNonSpaceNonCommentCharacterIndex,
|
|
13
|
+
isNextLineEmpty,
|
|
14
|
+
isPreviousLineEmpty,
|
|
16
15
|
} = prettierUtil;
|
|
17
16
|
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
|
|
18
17
|
doc.builders;
|
|
@@ -274,13 +273,12 @@ function handleLastFunctionArgComments(
|
|
|
274
273
|
precedingNode,
|
|
275
274
|
enclosingNode,
|
|
276
275
|
followingNode,
|
|
277
|
-
comment
|
|
278
|
-
options
|
|
276
|
+
comment
|
|
277
|
+
/* options */
|
|
279
278
|
) {
|
|
280
279
|
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
|
|
281
280
|
text,
|
|
282
|
-
comment
|
|
283
|
-
options
|
|
281
|
+
locEnd(comment)
|
|
284
282
|
);
|
|
285
283
|
const nextCharacter = text.charAt(nextCharIndex);
|
|
286
284
|
|
|
@@ -329,8 +327,8 @@ function handleIfStatementComments(
|
|
|
329
327
|
precedingNode,
|
|
330
328
|
enclosingNode,
|
|
331
329
|
followingNode,
|
|
332
|
-
comment
|
|
333
|
-
options
|
|
330
|
+
comment
|
|
331
|
+
/* options */
|
|
334
332
|
) {
|
|
335
333
|
if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
|
|
336
334
|
return false;
|
|
@@ -338,8 +336,7 @@ function handleIfStatementComments(
|
|
|
338
336
|
|
|
339
337
|
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
|
|
340
338
|
text,
|
|
341
|
-
comment
|
|
342
|
-
options
|
|
339
|
+
locEnd(comment)
|
|
343
340
|
);
|
|
344
341
|
const nextCharacter = text.charAt(nextCharIndex);
|
|
345
342
|
|
|
@@ -380,16 +377,12 @@ function handleRetifComments(
|
|
|
380
377
|
precedingNode,
|
|
381
378
|
followingNode,
|
|
382
379
|
comment,
|
|
383
|
-
text
|
|
384
|
-
options
|
|
380
|
+
text
|
|
381
|
+
/* options */
|
|
385
382
|
) {
|
|
386
383
|
const isSameLineAsPrecedingNode =
|
|
387
384
|
precedingNode &&
|
|
388
|
-
!hasNewlineInRange(
|
|
389
|
-
text,
|
|
390
|
-
options.locEnd(precedingNode),
|
|
391
|
-
options.locStart(comment)
|
|
392
|
-
);
|
|
385
|
+
!hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
|
|
393
386
|
|
|
394
387
|
if (
|
|
395
388
|
(!precedingNode || !isSameLineAsPrecedingNode) &&
|
|
@@ -507,7 +500,13 @@ function handleClassComments(enclosingNode, followingNode, comment) {
|
|
|
507
500
|
return false;
|
|
508
501
|
}
|
|
509
502
|
|
|
510
|
-
function handleFunction(
|
|
503
|
+
function handleFunction(
|
|
504
|
+
text,
|
|
505
|
+
enclosingNode,
|
|
506
|
+
followingNode,
|
|
507
|
+
comment
|
|
508
|
+
/* options */
|
|
509
|
+
) {
|
|
511
510
|
if (
|
|
512
511
|
enclosingNode &&
|
|
513
512
|
(enclosingNode.kind === "function" || enclosingNode.kind === "method")
|
|
@@ -518,18 +517,17 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
|
|
|
518
517
|
let argumentsLocEnd = 0;
|
|
519
518
|
for (let i = 0; i < enclosingNode.arguments.length; i++) {
|
|
520
519
|
argumentsLocEnd =
|
|
521
|
-
|
|
522
|
-
?
|
|
520
|
+
locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
|
|
521
|
+
? locEnd(enclosingNode.arguments[i])
|
|
523
522
|
: argumentsLocEnd;
|
|
524
523
|
}
|
|
525
524
|
const commentIsBetweenArgumentsAndBody =
|
|
526
525
|
enclosingNode.body &&
|
|
527
|
-
|
|
528
|
-
|
|
526
|
+
locStart(comment) > argumentsLocEnd &&
|
|
527
|
+
locEnd(comment) < locStart(enclosingNode.body);
|
|
529
528
|
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
|
|
530
529
|
text,
|
|
531
|
-
comment
|
|
532
|
-
options
|
|
530
|
+
locEnd(comment)
|
|
533
531
|
);
|
|
534
532
|
// we additionally need to check if this isn't a trailing argument comment,
|
|
535
533
|
// by checking the next character isn't ")"
|
|
@@ -538,7 +536,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
|
|
|
538
536
|
commentIsBetweenArgumentsAndBody &&
|
|
539
537
|
text.charAt(nextCharIndex) !== ")"
|
|
540
538
|
) {
|
|
541
|
-
if (
|
|
539
|
+
if (locEnd(comment) < locStart(enclosingNode.type)) {
|
|
542
540
|
// we need to store this as a dangling comment in case the type is nullable
|
|
543
541
|
// ie function(): ?string {} - the "nullable" attribute is part of the
|
|
544
542
|
// function node, not the type.
|
|
@@ -609,11 +607,15 @@ function handleHalt(precedingNode, enclosingNode, followingNode, comment) {
|
|
|
609
607
|
return false;
|
|
610
608
|
}
|
|
611
609
|
|
|
612
|
-
function handleCommentInEmptyParens(
|
|
610
|
+
function handleCommentInEmptyParens(
|
|
611
|
+
text,
|
|
612
|
+
enclosingNode,
|
|
613
|
+
comment
|
|
614
|
+
/* options */
|
|
615
|
+
) {
|
|
613
616
|
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
|
|
614
617
|
text,
|
|
615
|
-
comment
|
|
616
|
-
options
|
|
618
|
+
locEnd(comment)
|
|
617
619
|
);
|
|
618
620
|
|
|
619
621
|
if (text.charAt(nextCharIndex) !== ")") {
|
|
@@ -790,8 +792,8 @@ function handleWhileComments(
|
|
|
790
792
|
precedingNode,
|
|
791
793
|
enclosingNode,
|
|
792
794
|
followingNode,
|
|
793
|
-
comment
|
|
794
|
-
options
|
|
795
|
+
comment
|
|
796
|
+
/* options */
|
|
795
797
|
) {
|
|
796
798
|
if (!enclosingNode || enclosingNode.kind !== "while" || !followingNode) {
|
|
797
799
|
return false;
|
|
@@ -804,8 +806,7 @@ function handleWhileComments(
|
|
|
804
806
|
|
|
805
807
|
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
|
|
806
808
|
text,
|
|
807
|
-
comment
|
|
808
|
-
options
|
|
809
|
+
locEnd(comment)
|
|
809
810
|
);
|
|
810
811
|
const nextCharacter = text.charAt(nextCharIndex);
|
|
811
812
|
|
|
@@ -829,7 +830,7 @@ function printComment(path, options) {
|
|
|
829
830
|
return options.printer.printComment(path, options);
|
|
830
831
|
}
|
|
831
832
|
|
|
832
|
-
// https://github.com/prettier/prettier/blob/
|
|
833
|
+
// https://github.com/prettier/prettier/blob/6bff776efd0951b7812818d02d1cae3fda184737/src/main/comments/print.js#L119
|
|
833
834
|
function printDanglingComments(path, options, sameIndent, filter) {
|
|
834
835
|
const parts = [];
|
|
835
836
|
const node = path.getValue();
|
|
@@ -868,11 +869,11 @@ function hasTrailingComment(node) {
|
|
|
868
869
|
return node.comments && node.comments.some((comment) => comment.trailing);
|
|
869
870
|
}
|
|
870
871
|
|
|
871
|
-
function hasLeadingOwnLineComment(text, node
|
|
872
|
+
function hasLeadingOwnLineComment(text, node) {
|
|
872
873
|
return (
|
|
873
874
|
node.comments &&
|
|
874
875
|
node.comments.some(
|
|
875
|
-
(comment) => comment.leading && hasNewline(text,
|
|
876
|
+
(comment) => comment.leading && hasNewline(text, locEnd(comment))
|
|
876
877
|
)
|
|
877
878
|
);
|
|
878
879
|
}
|
|
@@ -887,7 +888,7 @@ function printComments(comments, options) {
|
|
|
887
888
|
parts.push(hardline);
|
|
888
889
|
}
|
|
889
890
|
if (
|
|
890
|
-
isNextLineEmpty(options.originalText, comment
|
|
891
|
+
isNextLineEmpty(options.originalText, locEnd(comment)) &&
|
|
891
892
|
!isLastComment
|
|
892
893
|
) {
|
|
893
894
|
parts.push(hardline);
|
|
@@ -941,9 +942,7 @@ function printLeadingComment(path, print, options) {
|
|
|
941
942
|
if (isBlock) {
|
|
942
943
|
return [
|
|
943
944
|
contents,
|
|
944
|
-
hasNewline(options.originalText,
|
|
945
|
-
? hardline
|
|
946
|
-
: " ",
|
|
945
|
+
hasNewline(options.originalText, locEnd(comment)) ? hardline : " ",
|
|
947
946
|
];
|
|
948
947
|
}
|
|
949
948
|
|
|
@@ -961,7 +960,7 @@ function printTrailingComment(path, print, options) {
|
|
|
961
960
|
options.printer.isBlockComment && options.printer.isBlockComment(comment);
|
|
962
961
|
|
|
963
962
|
if (
|
|
964
|
-
hasNewline(options.originalText,
|
|
963
|
+
hasNewline(options.originalText, locStart(comment), {
|
|
965
964
|
backwards: true,
|
|
966
965
|
})
|
|
967
966
|
) {
|
|
@@ -979,8 +978,7 @@ function printTrailingComment(path, print, options) {
|
|
|
979
978
|
|
|
980
979
|
const isLineBeforeEmpty = isPreviousLineEmpty(
|
|
981
980
|
options.originalText,
|
|
982
|
-
comment
|
|
983
|
-
options
|
|
981
|
+
locStart(comment)
|
|
984
982
|
);
|
|
985
983
|
|
|
986
984
|
return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);
|
|
@@ -1015,7 +1013,7 @@ function printAllComments(path, print, options, needsSemi) {
|
|
|
1015
1013
|
leadingParts.push(contents);
|
|
1016
1014
|
|
|
1017
1015
|
const text = options.originalText;
|
|
1018
|
-
if (hasNewline(text, skipNewline(text,
|
|
1016
|
+
if (hasNewline(text, skipNewline(text, locEnd(comment)))) {
|
|
1019
1017
|
leadingParts.push(hardline);
|
|
1020
1018
|
}
|
|
1021
1019
|
} else if (trailing) {
|
|
@@ -3,10 +3,10 @@ import {
|
|
|
3
3
|
LINGUIST_LANGUAGES_PHP,
|
|
4
4
|
LINGUIST_LANGUAGES_HTML_PHP,
|
|
5
5
|
} from "./linguist-languages.cjs";
|
|
6
|
-
import parse from "./parser.
|
|
7
|
-
import print from "./printer.
|
|
8
|
-
import clean from "./clean.
|
|
9
|
-
import options from "./options.
|
|
6
|
+
import parse from "./parser.mjs";
|
|
7
|
+
import print from "./printer.mjs";
|
|
8
|
+
import clean from "./clean.mjs";
|
|
9
|
+
import options from "./options.mjs";
|
|
10
10
|
import {
|
|
11
11
|
handleOwnLineComment,
|
|
12
12
|
handleEndOfLineComment,
|
|
@@ -14,8 +14,9 @@ import {
|
|
|
14
14
|
getCommentChildNodes,
|
|
15
15
|
canAttachComment,
|
|
16
16
|
isBlockComment,
|
|
17
|
-
} from "./comments.
|
|
18
|
-
import { hasPragma, insertPragma } from "./pragma.
|
|
17
|
+
} from "./comments.mjs";
|
|
18
|
+
import { hasPragma, insertPragma } from "./pragma.mjs";
|
|
19
|
+
import { locStart, locEnd } from "./loc.mjs";
|
|
19
20
|
|
|
20
21
|
const { join, hardline } = doc.builders;
|
|
21
22
|
|
|
@@ -55,16 +56,12 @@ const languages = [
|
|
|
55
56
|
}),
|
|
56
57
|
];
|
|
57
58
|
|
|
58
|
-
const loc = (prop) => (node) => {
|
|
59
|
-
return node.loc && node.loc[prop] && node.loc[prop].offset;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
59
|
const parsers = {
|
|
63
60
|
php: {
|
|
64
61
|
parse,
|
|
65
62
|
astFormat: "php",
|
|
66
|
-
locStart
|
|
67
|
-
locEnd
|
|
63
|
+
locStart,
|
|
64
|
+
locEnd,
|
|
68
65
|
hasPragma,
|
|
69
66
|
},
|
|
70
67
|
};
|
|
@@ -138,7 +135,7 @@ const printers = {
|
|
|
138
135
|
case "commentline": {
|
|
139
136
|
return comment.value.trimRight();
|
|
140
137
|
}
|
|
141
|
-
/*
|
|
138
|
+
/* c8 ignore next 2 */
|
|
142
139
|
default:
|
|
143
140
|
throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
|
|
144
141
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
module.exports.LINGUIST_LANGUAGES_PHP = require("linguist-languages/data/PHP");
|
|
4
|
-
module.exports.LINGUIST_LANGUAGES_HTML_PHP = require("linguist-languages/data/HTML+PHP");
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports.LINGUIST_LANGUAGES_PHP = require("linguist-languages/data/PHP");
|
|
4
|
+
module.exports.LINGUIST_LANGUAGES_HTML_PHP = require("linguist-languages/data/HTML+PHP");
|
package/src/loc.mjs
ADDED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
|
|
1
|
+
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";
|
|
3
2
|
|
|
4
3
|
function needsParens(path) {
|
|
5
4
|
const { parent } = path;
|
|
@@ -92,8 +91,6 @@ function needsParens(path) {
|
|
|
92
91
|
}
|
|
93
92
|
|
|
94
93
|
if (pp === np && key === "right") {
|
|
95
|
-
assert.strictEqual(parent.right, node);
|
|
96
|
-
|
|
97
94
|
return true;
|
|
98
95
|
}
|
|
99
96
|
|