@prettier/plugin-php 0.19.7 → 0.20.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 +13 -13
- package/package.json +18 -16
- package/src/comments.js +8 -20
- package/src/index.js +18 -0
- package/src/parser.js +1 -1
- package/src/printer.js +101 -140
- package/standalone.js +3 -1
package/README.md
CHANGED
|
@@ -141,7 +141,7 @@ Then use Prettier with PHP, just like this:
|
|
|
141
141
|
```js
|
|
142
142
|
prettier.format(YOUR_CODE, {
|
|
143
143
|
plugins: prettierPlugins,
|
|
144
|
-
parser: "php"
|
|
144
|
+
parser: "php",
|
|
145
145
|
});
|
|
146
146
|
```
|
|
147
147
|
|
|
@@ -157,7 +157,7 @@ import phpPlugin from "@prettier/plugin-php/standalone";
|
|
|
157
157
|
|
|
158
158
|
prettier.format(YOUR_CODE, {
|
|
159
159
|
plugins: [phpPlugin],
|
|
160
|
-
parser: "php"
|
|
160
|
+
parser: "php",
|
|
161
161
|
});
|
|
162
162
|
```
|
|
163
163
|
|
|
@@ -165,17 +165,17 @@ prettier.format(YOUR_CODE, {
|
|
|
165
165
|
|
|
166
166
|
Prettier for PHP supports the following options. We recommend that all users set the `phpVersion` option.
|
|
167
167
|
|
|
168
|
-
| Name | Default | Description
|
|
169
|
-
| ------------------
|
|
168
|
+
| Name | Default | Description |
|
|
169
|
+
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
170
170
|
| `phpVersion` | `"7.0"` | Allows specifying the PHP version you're using. If you're using PHP 7.1 or later, setting this option will make use of modern language features in the printed output. If you're using PHP lower than 7.0, you'll have to set this option or Prettier will generate incompatible code. |
|
|
171
|
-
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width))
|
|
172
|
-
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard.
|
|
173
|
-
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs))
|
|
174
|
-
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`.
|
|
175
|
-
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed.
|
|
176
|
-
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line.
|
|
177
|
-
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma))
|
|
178
|
-
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma))
|
|
171
|
+
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
|
|
172
|
+
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard. |
|
|
173
|
+
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
|
|
174
|
+
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`. |
|
|
175
|
+
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
|
|
176
|
+
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
|
|
177
|
+
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
|
|
178
|
+
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
|
|
179
179
|
|
|
180
180
|
## Ignoring code
|
|
181
181
|
|
|
@@ -232,7 +232,7 @@ The official [prettier plugin for vscode](https://github.com/prettier/prettier-v
|
|
|
232
232
|
- Tick the On Save button, if you want your files formatting updated on file save
|
|
233
233
|
- Clock OK to save settings
|
|
234
234
|
|
|
235
|
-
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
|
|
235
|
+
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
|
|
236
236
|
alternatively you can use the Prettier shortcut Ctrl+Alt+Shift+P_
|
|
237
237
|
|
|
238
238
|
### Sublime Text
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prettier/plugin-php",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Prettier PHP Plugin",
|
|
5
5
|
"repository": "prettier/prettier-php",
|
|
6
6
|
"author": "Lucas Azzola <@azz>",
|
|
@@ -16,20 +16,21 @@
|
|
|
16
16
|
"php-parser": "^3.1.5"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@babel/preset-env": "^7.
|
|
19
|
+
"@babel/preset-env": "^7.22.10",
|
|
20
20
|
"codecov": "3.8.3",
|
|
21
21
|
"cross-env": "^7.0.2",
|
|
22
|
-
"eslint": "8.
|
|
23
|
-
"eslint-config-prettier": "
|
|
24
|
-
"eslint-plugin-import": "2.
|
|
25
|
-
"eslint-plugin-jest": "27.2.
|
|
26
|
-
"eslint-plugin-prettier": "
|
|
22
|
+
"eslint": "8.47.0",
|
|
23
|
+
"eslint-config-prettier": "9.0.0",
|
|
24
|
+
"eslint-plugin-import": "2.28.0",
|
|
25
|
+
"eslint-plugin-jest": "27.2.3",
|
|
26
|
+
"eslint-plugin-prettier": "5.0.0",
|
|
27
27
|
"eslint-plugin-prettier-doc": "^1.1.0",
|
|
28
|
-
"jest": "29.
|
|
29
|
-
"jest-environment-jsdom": "29.
|
|
28
|
+
"jest": "29.6.2",
|
|
29
|
+
"jest-environment-jsdom": "29.6.2",
|
|
30
|
+
"jest-light-runner": "^0.5.0",
|
|
30
31
|
"jest-runner-eslint": "2.1.0",
|
|
31
32
|
"jest-snapshot-serializer-raw": "^1.1.0",
|
|
32
|
-
"prettier": "^
|
|
33
|
+
"prettier": "^3.0.1",
|
|
33
34
|
"rollup": "^2.75.7",
|
|
34
35
|
"rollup-plugin-alias": "^2.0.0",
|
|
35
36
|
"rollup-plugin-babel": "^4.3.2",
|
|
@@ -39,16 +40,17 @@
|
|
|
39
40
|
"rollup-plugin-node-resolve": "^5.2.0",
|
|
40
41
|
"rollup-plugin-replace": "^2.1.0",
|
|
41
42
|
"rollup-plugin-terser": "^7.0.2",
|
|
42
|
-
"strip-ansi": "^6.0.0"
|
|
43
|
-
"yarpm": "^1.1.1"
|
|
43
|
+
"strip-ansi": "^6.0.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"prettier": "^
|
|
46
|
+
"prettier": "^3.0.0"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
|
-
"test": "
|
|
50
|
-
"
|
|
51
|
-
"
|
|
49
|
+
"test": "yarn test:node && yarn test:standalone",
|
|
50
|
+
"test:node": "jest",
|
|
51
|
+
"test:standalone": "yarn run build-standalone && cross-env RUN_STANDALONE_TESTS=true yarn jest",
|
|
52
|
+
"prepublishOnly": "yarn test",
|
|
53
|
+
"prettier": "prettier --plugin=src/index.js --parser=php",
|
|
52
54
|
"build-standalone": "rollup -c build/rollup.config.js",
|
|
53
55
|
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
|
|
54
56
|
}
|
package/src/comments.js
CHANGED
|
@@ -445,6 +445,10 @@ function handleClassComments(enclosingNode, followingNode, comment) {
|
|
|
445
445
|
enclosingNode &&
|
|
446
446
|
["class", "interface", "trait"].includes(enclosingNode.kind)
|
|
447
447
|
) {
|
|
448
|
+
if (enclosingNode.__parent_new_arguments?.includes(followingNode)) {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
448
452
|
// for extends nodes that have leading comments, we can store them as
|
|
449
453
|
// dangling comments so we can handle them in the printer
|
|
450
454
|
|
|
@@ -897,27 +901,11 @@ function isBlockComment(comment) {
|
|
|
897
901
|
}
|
|
898
902
|
|
|
899
903
|
function getCommentChildNodes(node) {
|
|
900
|
-
if (
|
|
901
|
-
|
|
904
|
+
if (node.kind === "new" && node.what.kind === "class") {
|
|
905
|
+
// Pretend to be child of `class`
|
|
906
|
+
node.what.__parent_new_arguments = [...node.arguments];
|
|
907
|
+
return [node.what];
|
|
902
908
|
}
|
|
903
|
-
|
|
904
|
-
const getChildNodes = (node) =>
|
|
905
|
-
Object.keys(node)
|
|
906
|
-
.filter(
|
|
907
|
-
(n) =>
|
|
908
|
-
n !== "kind" &&
|
|
909
|
-
n !== "loc" &&
|
|
910
|
-
n !== "errors" &&
|
|
911
|
-
n !== "extra" &&
|
|
912
|
-
n !== "comments" &&
|
|
913
|
-
n !== "leadingComments" &&
|
|
914
|
-
n !== "enclosingNode" &&
|
|
915
|
-
n !== "precedingNode" &&
|
|
916
|
-
n !== "followingNode"
|
|
917
|
-
)
|
|
918
|
-
.map((n) => node[n]);
|
|
919
|
-
|
|
920
|
-
return getChildNodes(node);
|
|
921
909
|
}
|
|
922
910
|
|
|
923
911
|
function canAttachComment(node) {
|
package/src/index.js
CHANGED
|
@@ -58,9 +58,27 @@ const parsers = {
|
|
|
58
58
|
},
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
+
const ignoredKeys = new Set([
|
|
62
|
+
"kind",
|
|
63
|
+
"loc",
|
|
64
|
+
"errors",
|
|
65
|
+
"extra",
|
|
66
|
+
"comments",
|
|
67
|
+
"leadingComments",
|
|
68
|
+
"enclosingNode",
|
|
69
|
+
"precedingNode",
|
|
70
|
+
"followingNode",
|
|
71
|
+
]);
|
|
72
|
+
function getVisitorKeys(node, nonTraversableKeys) {
|
|
73
|
+
return Object.keys(node).filter(
|
|
74
|
+
(key) => !nonTraversableKeys.has(key) && !ignoredKeys.has(key)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
61
78
|
const printers = {
|
|
62
79
|
php: {
|
|
63
80
|
print,
|
|
81
|
+
getVisitorKeys,
|
|
64
82
|
insertPragma,
|
|
65
83
|
massageAstNode: clean,
|
|
66
84
|
getCommentChildNodes: comments.getCommentChildNodes,
|