@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/index.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
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
|
+
import { locStart, locEnd } from "./loc.js";
|
|
20
|
+
|
|
21
|
+
const { join, hardline } = doc.builders;
|
|
10
22
|
|
|
11
23
|
function createLanguage(linguistData, { extend, override }) {
|
|
12
24
|
const language = {};
|
|
@@ -30,13 +42,13 @@ function createLanguage(linguistData, { extend, override }) {
|
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
const languages = [
|
|
33
|
-
createLanguage(
|
|
45
|
+
createLanguage(LINGUIST_LANGUAGES_PHP, {
|
|
34
46
|
override: {
|
|
35
47
|
parsers: ["php"],
|
|
36
48
|
vscodeLanguageIds: ["php"],
|
|
37
49
|
},
|
|
38
50
|
}),
|
|
39
|
-
createLanguage(
|
|
51
|
+
createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
|
|
40
52
|
override: {
|
|
41
53
|
parsers: ["php"],
|
|
42
54
|
vscodeLanguageIds: ["php"],
|
|
@@ -44,16 +56,12 @@ const languages = [
|
|
|
44
56
|
}),
|
|
45
57
|
];
|
|
46
58
|
|
|
47
|
-
const loc = (prop) => (node) => {
|
|
48
|
-
return node.loc && node.loc[prop] && node.loc[prop].offset;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
59
|
const parsers = {
|
|
52
60
|
php: {
|
|
53
61
|
parse,
|
|
54
62
|
astFormat: "php",
|
|
55
|
-
locStart
|
|
56
|
-
locEnd
|
|
63
|
+
locStart,
|
|
64
|
+
locEnd,
|
|
57
65
|
hasPragma,
|
|
58
66
|
},
|
|
59
67
|
};
|
|
@@ -81,21 +89,21 @@ const printers = {
|
|
|
81
89
|
getVisitorKeys,
|
|
82
90
|
insertPragma,
|
|
83
91
|
massageAstNode: clean,
|
|
84
|
-
getCommentChildNodes
|
|
85
|
-
canAttachComment
|
|
86
|
-
isBlockComment
|
|
92
|
+
getCommentChildNodes,
|
|
93
|
+
canAttachComment,
|
|
94
|
+
isBlockComment,
|
|
87
95
|
handleComments: {
|
|
88
|
-
ownLine:
|
|
89
|
-
endOfLine:
|
|
90
|
-
remaining:
|
|
96
|
+
ownLine: handleOwnLineComment,
|
|
97
|
+
endOfLine: handleEndOfLineComment,
|
|
98
|
+
remaining: handleRemainingComment,
|
|
91
99
|
},
|
|
92
100
|
willPrintOwnComments(path) {
|
|
93
|
-
const node = path
|
|
101
|
+
const { node } = path;
|
|
94
102
|
|
|
95
103
|
return node && node.kind === "noop";
|
|
96
104
|
},
|
|
97
|
-
printComment(
|
|
98
|
-
const comment =
|
|
105
|
+
printComment(path) {
|
|
106
|
+
const comment = path.node;
|
|
99
107
|
|
|
100
108
|
switch (comment.kind) {
|
|
101
109
|
case "commentblock": {
|
|
@@ -127,7 +135,7 @@ const printers = {
|
|
|
127
135
|
case "commentline": {
|
|
128
136
|
return comment.value.trimRight();
|
|
129
137
|
}
|
|
130
|
-
/*
|
|
138
|
+
/* c8 ignore next 2 */
|
|
131
139
|
default:
|
|
132
140
|
throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
|
|
133
141
|
}
|
|
@@ -138,8 +146,7 @@ const printers = {
|
|
|
138
146
|
!comment.value.includes("prettier-ignore-start") &&
|
|
139
147
|
!comment.value.includes("prettier-ignore-end");
|
|
140
148
|
|
|
141
|
-
const parentNode = path
|
|
142
|
-
const node = path.getNode();
|
|
149
|
+
const { node, parent: parentNode } = path;
|
|
143
150
|
|
|
144
151
|
return (
|
|
145
152
|
(node &&
|
|
@@ -161,12 +168,8 @@ const printers = {
|
|
|
161
168
|
},
|
|
162
169
|
};
|
|
163
170
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
printers,
|
|
167
|
-
parsers,
|
|
168
|
-
options,
|
|
169
|
-
defaultOptions: {
|
|
170
|
-
tabWidth: 4,
|
|
171
|
-
},
|
|
171
|
+
const defaultOptions = {
|
|
172
|
+
tabWidth: 4,
|
|
172
173
|
};
|
|
174
|
+
|
|
175
|
+
export { languages, printers, parsers, options, defaultOptions };
|
package/src/loc.js
ADDED
package/src/needs-parens.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const assert = require("assert");
|
|
4
|
-
|
|
5
|
-
const { getPrecedence, shouldFlatten, isBitwiseOperator } = require("./util");
|
|
1
|
+
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
|
|
6
2
|
|
|
7
3
|
function needsParens(path) {
|
|
8
|
-
const parent = path
|
|
4
|
+
const { parent } = path;
|
|
9
5
|
|
|
10
6
|
if (!parent) {
|
|
11
7
|
return false;
|
|
12
8
|
}
|
|
13
9
|
|
|
14
|
-
const
|
|
15
|
-
const node = path.getNode();
|
|
10
|
+
const { key, node } = path;
|
|
16
11
|
|
|
17
12
|
if (
|
|
18
13
|
[
|
|
@@ -56,9 +51,9 @@ function needsParens(path) {
|
|
|
56
51
|
case "staticlookup":
|
|
57
52
|
case "offsetlookup":
|
|
58
53
|
case "call":
|
|
59
|
-
return
|
|
54
|
+
return key === "what";
|
|
60
55
|
case "bin":
|
|
61
|
-
return parent.type === "**" &&
|
|
56
|
+
return parent.type === "**" && key === "left";
|
|
62
57
|
default:
|
|
63
58
|
return false;
|
|
64
59
|
}
|
|
@@ -80,7 +75,7 @@ function needsParens(path) {
|
|
|
80
75
|
case "nullsafepropertylookup":
|
|
81
76
|
case "staticlookup":
|
|
82
77
|
case "offsetlookup":
|
|
83
|
-
return
|
|
78
|
+
return key === "what";
|
|
84
79
|
case "bin": {
|
|
85
80
|
const po = parent.type;
|
|
86
81
|
const pp = getPrecedence(po);
|
|
@@ -95,9 +90,7 @@ function needsParens(path) {
|
|
|
95
90
|
return true;
|
|
96
91
|
}
|
|
97
92
|
|
|
98
|
-
if (pp === np &&
|
|
99
|
-
assert.strictEqual(parent.right, node);
|
|
100
|
-
|
|
93
|
+
if (pp === np && key === "right") {
|
|
101
94
|
return true;
|
|
102
95
|
}
|
|
103
96
|
|
|
@@ -127,11 +120,7 @@ function needsParens(path) {
|
|
|
127
120
|
case "staticlookup": {
|
|
128
121
|
switch (parent.kind) {
|
|
129
122
|
case "call":
|
|
130
|
-
return
|
|
131
|
-
name === "what" &&
|
|
132
|
-
parent.what === node &&
|
|
133
|
-
node.parenthesizedExpression
|
|
134
|
-
);
|
|
123
|
+
return key === "what" && node.parenthesizedExpression;
|
|
135
124
|
|
|
136
125
|
default:
|
|
137
126
|
return false;
|
|
@@ -145,7 +134,7 @@ function needsParens(path) {
|
|
|
145
134
|
case "staticlookup":
|
|
146
135
|
case "offsetlookup":
|
|
147
136
|
case "call":
|
|
148
|
-
return
|
|
137
|
+
return key === "what";
|
|
149
138
|
default:
|
|
150
139
|
return false;
|
|
151
140
|
}
|
|
@@ -157,10 +146,10 @@ function needsParens(path) {
|
|
|
157
146
|
case "staticlookup":
|
|
158
147
|
case "offsetlookup":
|
|
159
148
|
case "call":
|
|
160
|
-
return
|
|
149
|
+
return key === "what";
|
|
161
150
|
|
|
162
151
|
case "retif":
|
|
163
|
-
return
|
|
152
|
+
return key === "test";
|
|
164
153
|
|
|
165
154
|
default:
|
|
166
155
|
return !!(node.key || node.value);
|
|
@@ -195,7 +184,7 @@ function needsParens(path) {
|
|
|
195
184
|
case "unary":
|
|
196
185
|
case "bin":
|
|
197
186
|
case "retif":
|
|
198
|
-
if (
|
|
187
|
+
if (key === "test" && !parent.trueExpr) {
|
|
199
188
|
return false;
|
|
200
189
|
}
|
|
201
190
|
|
|
@@ -205,7 +194,7 @@ function needsParens(path) {
|
|
|
205
194
|
case "staticlookup":
|
|
206
195
|
case "offsetlookup":
|
|
207
196
|
case "call":
|
|
208
|
-
return
|
|
197
|
+
return key === "what";
|
|
209
198
|
|
|
210
199
|
default:
|
|
211
200
|
return false;
|
|
@@ -213,7 +202,7 @@ function needsParens(path) {
|
|
|
213
202
|
case "closure":
|
|
214
203
|
switch (parent.kind) {
|
|
215
204
|
case "call":
|
|
216
|
-
return
|
|
205
|
+
return key === "what";
|
|
217
206
|
|
|
218
207
|
// https://github.com/prettier/plugin-php/issues/1675
|
|
219
208
|
case "propertylookup":
|
|
@@ -243,7 +232,7 @@ function needsParens(path) {
|
|
|
243
232
|
return false;
|
|
244
233
|
}
|
|
245
234
|
|
|
246
|
-
return
|
|
235
|
+
return key === "what";
|
|
247
236
|
default:
|
|
248
237
|
return false;
|
|
249
238
|
}
|
|
@@ -255,4 +244,4 @@ function needsParens(path) {
|
|
|
255
244
|
return false;
|
|
256
245
|
}
|
|
257
246
|
|
|
258
|
-
|
|
247
|
+
export default needsParens;
|
package/src/options.js
CHANGED
package/src/parser.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const engine = require("php-parser");
|
|
1
|
+
import engine from "php-parser";
|
|
3
2
|
|
|
4
3
|
function parse(text, opts) {
|
|
5
4
|
const inMarkdown = opts && opts.parentParser === "markdown";
|
|
@@ -61,4 +60,4 @@ function parse(text, opts) {
|
|
|
61
60
|
return ast;
|
|
62
61
|
}
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
export default parse;
|
package/src/pragma.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const parse = require("./parser");
|
|
4
|
-
const memoize = require("mem");
|
|
1
|
+
import memoize from "mem";
|
|
2
|
+
import parse from "./parser.js";
|
|
5
3
|
|
|
6
4
|
const reHasPragma = /@prettier|@format/;
|
|
7
5
|
|
|
@@ -91,7 +89,4 @@ function insertPragma(text) {
|
|
|
91
89
|
${after}`;
|
|
92
90
|
}
|
|
93
91
|
|
|
94
|
-
|
|
95
|
-
hasPragma,
|
|
96
|
-
insertPragma,
|
|
97
|
-
};
|
|
92
|
+
export { hasPragma, insertPragma };
|