@prettier/plugin-php 0.19.7 → 0.20.1
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 +38 -19
- package/package.json +32 -20
- package/src/clean.js +5 -7
- package/src/comments.js +39 -50
- package/src/index.js +54 -30
- package/src/linguist-languages.cjs +4 -0
- package/src/needs-parens.js +17 -25
- package/src/options.js +1 -3
- package/src/parser.js +3 -4
- package/src/pragma.js +3 -8
- package/src/printer.js +236 -324
- package/src/util.js +13 -26
- package/standalone.js +3 -1
- package/src/module.js +0 -2
package/README.md
CHANGED
|
@@ -139,9 +139,9 @@ First, grab both standalone scripts from an npm CDN like [unpkg](https://unpkg.c
|
|
|
139
139
|
Then use Prettier with PHP, just like this:
|
|
140
140
|
|
|
141
141
|
```js
|
|
142
|
-
prettier.format(YOUR_CODE, {
|
|
142
|
+
await prettier.format(YOUR_CODE, {
|
|
143
143
|
plugins: prettierPlugins,
|
|
144
|
-
parser: "php"
|
|
144
|
+
parser: "php",
|
|
145
145
|
});
|
|
146
146
|
```
|
|
147
147
|
|
|
@@ -153,11 +153,11 @@ Bundlers like webpack, Rollup or browserify automatically recognize how to handl
|
|
|
153
153
|
|
|
154
154
|
```js
|
|
155
155
|
import prettier from "prettier/standalone";
|
|
156
|
-
import
|
|
156
|
+
import * as prettierPluginPhp from "@prettier/plugin-php/standalone";
|
|
157
157
|
|
|
158
|
-
prettier.format(YOUR_CODE, {
|
|
159
|
-
plugins: [
|
|
160
|
-
parser: "php"
|
|
158
|
+
await prettier.format(YOUR_CODE, {
|
|
159
|
+
plugins: [prettierPluginPhp],
|
|
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
|
|
|
@@ -217,9 +217,28 @@ matrix(
|
|
|
217
217
|
|
|
218
218
|
The official [prettier plugin for atom](https://github.com/prettier/prettier-atom) supports plugins.
|
|
219
219
|
|
|
220
|
-
###
|
|
220
|
+
### Visual Studio Code
|
|
221
221
|
|
|
222
|
-
The official [prettier plugin for vscode](https://github.com/prettier/prettier-vscode) supports plugins since Version 1.10.0. To enable it, install the extension and make sure the plugin is installed _locally_ (in your project folder).
|
|
222
|
+
The official [prettier plugin for vscode](https://github.com/prettier/prettier-vscode) supports plugins since Version 1.10.0. To enable it, install the extension and make sure the plugin is installed _locally_ (in your project folder).
|
|
223
|
+
|
|
224
|
+
Visual Studio Code may not recognise the document selectors provided by this plugin, and so you can add php to your document selectors by adding the following line to `.vscode/settings.json`:
|
|
225
|
+
|
|
226
|
+
```json
|
|
227
|
+
"prettier.documentSelectors": [
|
|
228
|
+
"**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
|
|
229
|
+
]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
You may also need to declare php as the parser in your prettier config file:
|
|
233
|
+
|
|
234
|
+
```json
|
|
235
|
+
{
|
|
236
|
+
"parser": "php",
|
|
237
|
+
"plugins": ["@prettier/plugin-php"],
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
See (this issue)[https://github.com/prettier/plugin-php/issues/1730] for more discussion around potential VS Code solutions.
|
|
223
242
|
|
|
224
243
|
### PhpStorm / IntelliJ / Jetbrains IDE
|
|
225
244
|
|
|
@@ -232,7 +251,7 @@ The official [prettier plugin for vscode](https://github.com/prettier/prettier-v
|
|
|
232
251
|
- Tick the On Save button, if you want your files formatting updated on file save
|
|
233
252
|
- Clock OK to save settings
|
|
234
253
|
|
|
235
|
-
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
|
|
254
|
+
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
|
|
236
255
|
alternatively you can use the Prettier shortcut Ctrl+Alt+Shift+P_
|
|
237
256
|
|
|
238
257
|
### Sublime Text
|
package/package.json
CHANGED
|
@@ -1,54 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prettier/plugin-php",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.1",
|
|
4
4
|
"description": "Prettier PHP Plugin",
|
|
5
5
|
"repository": "prettier/prettier-php",
|
|
6
6
|
"author": "Lucas Azzola <@azz>",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"
|
|
8
|
+
"type": "module",
|
|
9
|
+
"unpkg": "./standalone.js",
|
|
10
|
+
"browser": "./standalone.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"browser": "./standalone.js",
|
|
14
|
+
"default": "./src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./standalone": "./standalone.js",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
9
19
|
"files": [
|
|
10
20
|
"src",
|
|
11
21
|
"standalone.js"
|
|
12
22
|
],
|
|
13
23
|
"dependencies": {
|
|
14
24
|
"linguist-languages": "^7.21.0",
|
|
15
|
-
"mem": "^
|
|
25
|
+
"mem": "^9.0.2",
|
|
16
26
|
"php-parser": "^3.1.5"
|
|
17
27
|
},
|
|
18
28
|
"devDependencies": {
|
|
19
|
-
"@babel/preset-env": "^7.
|
|
29
|
+
"@babel/preset-env": "^7.22.10",
|
|
30
|
+
"@rollup/plugin-commonjs": "^25.0.4",
|
|
31
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
|
20
32
|
"codecov": "3.8.3",
|
|
21
33
|
"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": "
|
|
34
|
+
"eslint": "8.47.0",
|
|
35
|
+
"eslint-config-prettier": "9.0.0",
|
|
36
|
+
"eslint-plugin-import": "2.28.0",
|
|
37
|
+
"eslint-plugin-jest": "27.2.3",
|
|
38
|
+
"eslint-plugin-prettier": "5.0.0",
|
|
27
39
|
"eslint-plugin-prettier-doc": "^1.1.0",
|
|
28
|
-
"jest": "29.
|
|
29
|
-
"jest-environment-jsdom": "29.
|
|
40
|
+
"jest": "29.6.2",
|
|
41
|
+
"jest-environment-jsdom": "29.6.2",
|
|
42
|
+
"jest-light-runner": "^0.5.0",
|
|
30
43
|
"jest-runner-eslint": "2.1.0",
|
|
31
44
|
"jest-snapshot-serializer-raw": "^1.1.0",
|
|
32
|
-
"prettier": "^
|
|
45
|
+
"prettier": "^3.0.1",
|
|
33
46
|
"rollup": "^2.75.7",
|
|
34
47
|
"rollup-plugin-alias": "^2.0.0",
|
|
35
48
|
"rollup-plugin-babel": "^4.3.2",
|
|
36
|
-
"rollup-plugin-commonjs": "^10.0.2",
|
|
37
49
|
"rollup-plugin-inject": "^3.0.1",
|
|
38
50
|
"rollup-plugin-json": "^4.0.0",
|
|
39
|
-
"rollup-plugin-node-resolve": "^5.2.0",
|
|
40
51
|
"rollup-plugin-replace": "^2.1.0",
|
|
41
52
|
"rollup-plugin-terser": "^7.0.2",
|
|
42
|
-
"strip-ansi": "^
|
|
43
|
-
"yarpm": "^1.1.1"
|
|
53
|
+
"strip-ansi": "^7.1.0"
|
|
44
54
|
},
|
|
45
55
|
"peerDependencies": {
|
|
46
|
-
"prettier": "^
|
|
56
|
+
"prettier": "^3.0.0"
|
|
47
57
|
},
|
|
48
58
|
"scripts": {
|
|
49
|
-
"test": "
|
|
50
|
-
"
|
|
51
|
-
"
|
|
59
|
+
"test": "yarn test:node && yarn test:standalone",
|
|
60
|
+
"test:node": "jest",
|
|
61
|
+
"test:standalone": "yarn run build-standalone && cross-env RUN_STANDALONE_TESTS=true yarn jest",
|
|
62
|
+
"prepublishOnly": "yarn test",
|
|
63
|
+
"prettier": "prettier --plugin=src/index.js --parser=php",
|
|
52
64
|
"build-standalone": "rollup -c build/rollup.config.js",
|
|
53
65
|
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
|
|
54
66
|
}
|
package/src/clean.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const util = require("./util");
|
|
1
|
+
import { printNumber, normalizeMagicMethodName } from "./util.js";
|
|
4
2
|
|
|
5
3
|
const ignoredProperties = new Set([
|
|
6
4
|
"loc",
|
|
@@ -28,7 +26,7 @@ const ignoredProperties = new Set([
|
|
|
28
26
|
function clean(node, newObj) {
|
|
29
27
|
if (node.kind === "string") {
|
|
30
28
|
// TODO if options are available in this method, replace with
|
|
31
|
-
// newObj.isDoubleQuote = !
|
|
29
|
+
// newObj.isDoubleQuote = !useSingleQuote(node, options);
|
|
32
30
|
delete newObj.isDoubleQuote;
|
|
33
31
|
}
|
|
34
32
|
|
|
@@ -66,7 +64,7 @@ function clean(node, newObj) {
|
|
|
66
64
|
|
|
67
65
|
// Normalize numbers
|
|
68
66
|
if (node.kind === "number") {
|
|
69
|
-
newObj.value =
|
|
67
|
+
newObj.value = printNumber(node.value);
|
|
70
68
|
}
|
|
71
69
|
|
|
72
70
|
const statements = ["foreach", "for", "if", "while", "do"];
|
|
@@ -100,7 +98,7 @@ function clean(node, newObj) {
|
|
|
100
98
|
}
|
|
101
99
|
|
|
102
100
|
if (node.kind === "method" && node.name.kind === "identifier") {
|
|
103
|
-
newObj.name.name =
|
|
101
|
+
newObj.name.name = normalizeMagicMethodName(newObj.name.name);
|
|
104
102
|
}
|
|
105
103
|
|
|
106
104
|
if (node.kind === "noop") {
|
|
@@ -110,4 +108,4 @@ function clean(node, newObj) {
|
|
|
110
108
|
|
|
111
109
|
clean.ignoredProperties = ignoredProperties;
|
|
112
110
|
|
|
113
|
-
|
|
111
|
+
export default clean;
|
package/src/comments.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { util as prettierUtil, doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
getNextNonSpaceNonCommentCharacterIndex,
|
|
4
|
+
isNextLineEmpty,
|
|
5
|
+
isPreviousLineEmpty,
|
|
6
|
+
isLookupNode,
|
|
7
|
+
} from "./util.js";
|
|
2
8
|
|
|
3
9
|
const {
|
|
4
10
|
addLeadingComment,
|
|
@@ -7,15 +13,9 @@ const {
|
|
|
7
13
|
skipNewline,
|
|
8
14
|
hasNewline,
|
|
9
15
|
hasNewlineInRange,
|
|
10
|
-
} =
|
|
16
|
+
} = prettierUtil;
|
|
11
17
|
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
getNextNonSpaceNonCommentCharacterIndex,
|
|
15
|
-
isNextLineEmpty,
|
|
16
|
-
isPreviousLineEmpty,
|
|
17
|
-
isLookupNode,
|
|
18
|
-
} = require("./util");
|
|
18
|
+
doc.builders;
|
|
19
19
|
|
|
20
20
|
/*
|
|
21
21
|
Comment functions are meant to inspect various edge cases using given comment nodes,
|
|
@@ -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
|
|
|
@@ -818,11 +822,11 @@ function handleWhileComments(
|
|
|
818
822
|
return false;
|
|
819
823
|
}
|
|
820
824
|
|
|
821
|
-
// https://github.com/prettier/prettier/blob/
|
|
822
|
-
function printComment(
|
|
823
|
-
const comment =
|
|
825
|
+
// https://github.com/prettier/prettier/blob/c01661f311a2e1e033f1f9cb127882cc13e293bd/src/main/comments/print.js#L23
|
|
826
|
+
function printComment(path, options) {
|
|
827
|
+
const comment = path.node;
|
|
824
828
|
comment.printed = true;
|
|
825
|
-
return options.printer.printComment(
|
|
829
|
+
return options.printer.printComment(path, options);
|
|
826
830
|
}
|
|
827
831
|
|
|
828
832
|
// https://github.com/prettier/prettier/blob/master/src/main/comments.js#L440
|
|
@@ -834,15 +838,15 @@ function printDanglingComments(path, options, sameIndent, filter) {
|
|
|
834
838
|
return "";
|
|
835
839
|
}
|
|
836
840
|
|
|
837
|
-
path.each((
|
|
838
|
-
const comment =
|
|
841
|
+
path.each(() => {
|
|
842
|
+
const comment = path.node;
|
|
839
843
|
if (
|
|
840
844
|
comment &&
|
|
841
845
|
!comment.leading &&
|
|
842
846
|
!comment.trailing &&
|
|
843
847
|
(!filter || filter(comment))
|
|
844
848
|
) {
|
|
845
|
-
parts.push(printComment(
|
|
849
|
+
parts.push(printComment(path, options));
|
|
846
850
|
}
|
|
847
851
|
}, "comments");
|
|
848
852
|
|
|
@@ -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) {
|
|
@@ -929,21 +917,22 @@ function canAttachComment(node) {
|
|
|
929
917
|
// Based on https://github.com/prettier/prettier/blob/master/src/main/comments.js
|
|
930
918
|
// TODO remove after https://github.com/prettier/prettier/issues/5087
|
|
931
919
|
function prependCursorPlaceholder(path, options, printed) {
|
|
932
|
-
|
|
920
|
+
const { node } = path;
|
|
921
|
+
if (node && node === options.cursorNode) {
|
|
933
922
|
return [cursor, printed, cursor];
|
|
934
923
|
}
|
|
935
924
|
|
|
936
925
|
return printed;
|
|
937
926
|
}
|
|
938
927
|
|
|
939
|
-
function printLeadingComment(
|
|
940
|
-
const
|
|
941
|
-
const contents = printComment(commentPath, options);
|
|
928
|
+
function printLeadingComment(path, print, options) {
|
|
929
|
+
const contents = printComment(path, options);
|
|
942
930
|
|
|
943
931
|
if (!contents) {
|
|
944
932
|
return "";
|
|
945
933
|
}
|
|
946
934
|
|
|
935
|
+
const comment = path.node;
|
|
947
936
|
const isBlock =
|
|
948
937
|
options.printer.isBlockComment && options.printer.isBlockComment(comment);
|
|
949
938
|
|
|
@@ -961,12 +950,13 @@ function printLeadingComment(commentPath, print, options) {
|
|
|
961
950
|
return [contents, hardline];
|
|
962
951
|
}
|
|
963
952
|
|
|
964
|
-
function printTrailingComment(
|
|
965
|
-
const
|
|
966
|
-
const contents = printComment(commentPath, options);
|
|
953
|
+
function printTrailingComment(path, print, options) {
|
|
954
|
+
const contents = printComment(path, options);
|
|
967
955
|
if (!contents) {
|
|
968
956
|
return "";
|
|
969
957
|
}
|
|
958
|
+
|
|
959
|
+
const comment = path.node;
|
|
970
960
|
const isBlock =
|
|
971
961
|
options.printer.isBlockComment && options.printer.isBlockComment(comment);
|
|
972
962
|
|
|
@@ -1003,9 +993,9 @@ function printTrailingComment(commentPath, print, options) {
|
|
|
1003
993
|
}
|
|
1004
994
|
|
|
1005
995
|
function printAllComments(path, print, options, needsSemi) {
|
|
1006
|
-
const
|
|
996
|
+
const { node } = path;
|
|
1007
997
|
const printed = print(path);
|
|
1008
|
-
const comments =
|
|
998
|
+
const comments = node && node.comments;
|
|
1009
999
|
|
|
1010
1000
|
if (!comments || comments.length === 0) {
|
|
1011
1001
|
return prependCursorPlaceholder(path, options, printed);
|
|
@@ -1014,12 +1004,11 @@ function printAllComments(path, print, options, needsSemi) {
|
|
|
1014
1004
|
const leadingParts = [];
|
|
1015
1005
|
const trailingParts = [needsSemi ? ";" : "", printed];
|
|
1016
1006
|
|
|
1017
|
-
path.each((
|
|
1018
|
-
const comment = commentPath.getValue();
|
|
1007
|
+
path.each(({ node: comment }) => {
|
|
1019
1008
|
const { leading, trailing } = comment;
|
|
1020
1009
|
|
|
1021
1010
|
if (leading) {
|
|
1022
|
-
const contents = printLeadingComment(
|
|
1011
|
+
const contents = printLeadingComment(path, print, options);
|
|
1023
1012
|
if (!contents) {
|
|
1024
1013
|
return;
|
|
1025
1014
|
}
|
|
@@ -1030,7 +1019,7 @@ function printAllComments(path, print, options, needsSemi) {
|
|
|
1030
1019
|
leadingParts.push(hardline);
|
|
1031
1020
|
}
|
|
1032
1021
|
} else if (trailing) {
|
|
1033
|
-
trailingParts.push(printTrailingComment(
|
|
1022
|
+
trailingParts.push(printTrailingComment(path, print, options));
|
|
1034
1023
|
}
|
|
1035
1024
|
}, "comments");
|
|
1036
1025
|
|
|
@@ -1041,7 +1030,7 @@ function printAllComments(path, print, options, needsSemi) {
|
|
|
1041
1030
|
);
|
|
1042
1031
|
}
|
|
1043
1032
|
|
|
1044
|
-
|
|
1033
|
+
export {
|
|
1045
1034
|
handleOwnLineComment,
|
|
1046
1035
|
handleEndOfLineComment,
|
|
1047
1036
|
handleRemainingComment,
|
package/src/index.js
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
LINGUIST_LANGUAGES_PHP,
|
|
4
|
+
LINGUIST_LANGUAGES_HTML_PHP,
|
|
5
|
+
} from "./linguist-languages.cjs";
|
|
6
|
+
import parse from "./parser.js";
|
|
7
|
+
import print from "./printer.js";
|
|
8
|
+
import clean from "./clean.js";
|
|
9
|
+
import options from "./options.js";
|
|
10
|
+
import {
|
|
11
|
+
handleOwnLineComment,
|
|
12
|
+
handleEndOfLineComment,
|
|
13
|
+
handleRemainingComment,
|
|
14
|
+
getCommentChildNodes,
|
|
15
|
+
canAttachComment,
|
|
16
|
+
isBlockComment,
|
|
17
|
+
} from "./comments.js";
|
|
18
|
+
import { hasPragma, insertPragma } from "./pragma.js";
|
|
19
|
+
|
|
20
|
+
const { join, hardline } = doc.builders;
|
|
10
21
|
|
|
11
22
|
function createLanguage(linguistData, { extend, override }) {
|
|
12
23
|
const language = {};
|
|
@@ -30,13 +41,13 @@ function createLanguage(linguistData, { extend, override }) {
|
|
|
30
41
|
}
|
|
31
42
|
|
|
32
43
|
const languages = [
|
|
33
|
-
createLanguage(
|
|
44
|
+
createLanguage(LINGUIST_LANGUAGES_PHP, {
|
|
34
45
|
override: {
|
|
35
46
|
parsers: ["php"],
|
|
36
47
|
vscodeLanguageIds: ["php"],
|
|
37
48
|
},
|
|
38
49
|
}),
|
|
39
|
-
createLanguage(
|
|
50
|
+
createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
|
|
40
51
|
override: {
|
|
41
52
|
parsers: ["php"],
|
|
42
53
|
vscodeLanguageIds: ["php"],
|
|
@@ -58,26 +69,44 @@ const parsers = {
|
|
|
58
69
|
},
|
|
59
70
|
};
|
|
60
71
|
|
|
72
|
+
const ignoredKeys = new Set([
|
|
73
|
+
"kind",
|
|
74
|
+
"loc",
|
|
75
|
+
"errors",
|
|
76
|
+
"extra",
|
|
77
|
+
"comments",
|
|
78
|
+
"leadingComments",
|
|
79
|
+
"enclosingNode",
|
|
80
|
+
"precedingNode",
|
|
81
|
+
"followingNode",
|
|
82
|
+
]);
|
|
83
|
+
function getVisitorKeys(node, nonTraversableKeys) {
|
|
84
|
+
return Object.keys(node).filter(
|
|
85
|
+
(key) => !nonTraversableKeys.has(key) && !ignoredKeys.has(key)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
61
89
|
const printers = {
|
|
62
90
|
php: {
|
|
63
91
|
print,
|
|
92
|
+
getVisitorKeys,
|
|
64
93
|
insertPragma,
|
|
65
94
|
massageAstNode: clean,
|
|
66
|
-
getCommentChildNodes
|
|
67
|
-
canAttachComment
|
|
68
|
-
isBlockComment
|
|
95
|
+
getCommentChildNodes,
|
|
96
|
+
canAttachComment,
|
|
97
|
+
isBlockComment,
|
|
69
98
|
handleComments: {
|
|
70
|
-
ownLine:
|
|
71
|
-
endOfLine:
|
|
72
|
-
remaining:
|
|
99
|
+
ownLine: handleOwnLineComment,
|
|
100
|
+
endOfLine: handleEndOfLineComment,
|
|
101
|
+
remaining: handleRemainingComment,
|
|
73
102
|
},
|
|
74
103
|
willPrintOwnComments(path) {
|
|
75
|
-
const node = path
|
|
104
|
+
const { node } = path;
|
|
76
105
|
|
|
77
106
|
return node && node.kind === "noop";
|
|
78
107
|
},
|
|
79
|
-
printComment(
|
|
80
|
-
const comment =
|
|
108
|
+
printComment(path) {
|
|
109
|
+
const comment = path.node;
|
|
81
110
|
|
|
82
111
|
switch (comment.kind) {
|
|
83
112
|
case "commentblock": {
|
|
@@ -120,8 +149,7 @@ const printers = {
|
|
|
120
149
|
!comment.value.includes("prettier-ignore-start") &&
|
|
121
150
|
!comment.value.includes("prettier-ignore-end");
|
|
122
151
|
|
|
123
|
-
const parentNode = path
|
|
124
|
-
const node = path.getNode();
|
|
152
|
+
const { node, parent: parentNode } = path;
|
|
125
153
|
|
|
126
154
|
return (
|
|
127
155
|
(node &&
|
|
@@ -143,12 +171,8 @@ const printers = {
|
|
|
143
171
|
},
|
|
144
172
|
};
|
|
145
173
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
printers,
|
|
149
|
-
parsers,
|
|
150
|
-
options,
|
|
151
|
-
defaultOptions: {
|
|
152
|
-
tabWidth: 4,
|
|
153
|
-
},
|
|
174
|
+
const defaultOptions = {
|
|
175
|
+
tabWidth: 4,
|
|
154
176
|
};
|
|
177
|
+
|
|
178
|
+
export { languages, printers, parsers, options, defaultOptions };
|