@zackad/prettier-plugin-twig 0.16.3 → 0.18.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/package.json +1 -1
- package/src/melody/melody-extension-core/index.js +4 -0
- package/src/melody/melody-extension-core/parser/for.js +13 -12
- package/src/melody/melody-extension-core/parser/macro.js +19 -2
- package/src/melody/melody-extension-core/parser/props.js +13 -1
- package/src/melody/melody-extension-core/parser/set.js +7 -3
- package/src/melody/melody-extension-core/parser/types.js +110 -0
- package/src/melody/melody-extension-core/types.js +44 -0
- package/src/melody/melody-parser/GenericTagParser.js +1 -1
- package/src/melody/melody-parser/Lexer.js +18 -0
- package/src/melody/melody-parser/Parser.js +180 -77
- package/src/melody/melody-parser/TokenStream.js +89 -18
- package/src/melody/melody-parser/TokenTypes.js +10 -0
- package/src/melody/melody-parser/index.js +8 -0
- package/src/melody/melody-parser/util.js +35 -0
- package/src/melody/melody-types/index.js +18 -1
- package/src/parser.js +1 -0
- package/src/print/ErroredStatement.js +15 -0
- package/src/print/ExpressionStatement.js +4 -2
- package/src/print/ForStatement.js +29 -0
- package/src/print/MacroDeclarationStatement.js +5 -0
- package/src/print/SetStatement.js +32 -7
- package/src/print/TwigComment.js +6 -10
- package/src/print/TypesDeclarationStatement.js +43 -0
- package/src/print/TypesVariableDeclaration.js +27 -0
- package/src/printer.js +18 -3
- package/src/util/printFunctions.js +74 -10
- package/src/util/publicFunctions.js +38 -0
- package/src/util/publicSymbols.js +7 -0
package/package.json
CHANGED
|
@@ -29,6 +29,7 @@ import { IncludeParser } from "./parser/include.js";
|
|
|
29
29
|
import { MacroParser } from "./parser/macro.js";
|
|
30
30
|
import { SetParser } from "./parser/set.js";
|
|
31
31
|
import { SpacelessParser } from "./parser/spaceless.js";
|
|
32
|
+
import { TypesParser } from "./parser/types.js";
|
|
32
33
|
import { UseParser } from "./parser/use.js";
|
|
33
34
|
import { MountParser } from "./parser/mount.js";
|
|
34
35
|
import { PropsParser } from "./parser/props.js";
|
|
@@ -97,6 +98,7 @@ export const extension = {
|
|
|
97
98
|
MacroParser,
|
|
98
99
|
SetParser,
|
|
99
100
|
SpacelessParser,
|
|
101
|
+
TypesParser,
|
|
100
102
|
UseParser,
|
|
101
103
|
MountParser,
|
|
102
104
|
PropsParser
|
|
@@ -128,6 +130,8 @@ export {
|
|
|
128
130
|
VariableDeclarationStatement,
|
|
129
131
|
SetStatement,
|
|
130
132
|
SpacelessBlock,
|
|
133
|
+
TypesDeclarationStatement,
|
|
134
|
+
TypesVariableDeclaration,
|
|
131
135
|
AliasExpression,
|
|
132
136
|
UseStatement,
|
|
133
137
|
UnaryNotExpression,
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
setStartFromToken,
|
|
20
20
|
setEndFromToken,
|
|
21
21
|
createNode,
|
|
22
|
+
attachLeadingComments,
|
|
22
23
|
hasTagStartTokenTrimLeft,
|
|
23
24
|
hasTagEndTokenTrimRight
|
|
24
25
|
} from "../../melody-parser/index.js";
|
|
@@ -32,23 +33,23 @@ export const ForParser = {
|
|
|
32
33
|
|
|
33
34
|
const keyTarget = tokens.expect(Types.SYMBOL);
|
|
34
35
|
if (tokens.nextIf(Types.COMMA)) {
|
|
35
|
-
forStatement.keyTarget =
|
|
36
|
-
Identifier,
|
|
37
|
-
|
|
38
|
-
keyTarget
|
|
36
|
+
forStatement.keyTarget = attachLeadingComments(
|
|
37
|
+
createNode(Identifier, keyTarget, keyTarget.text),
|
|
38
|
+
tokens,
|
|
39
|
+
keyTarget
|
|
39
40
|
);
|
|
40
41
|
const valueTarget = tokens.expect(Types.SYMBOL);
|
|
41
|
-
forStatement.valueTarget =
|
|
42
|
-
Identifier,
|
|
43
|
-
|
|
44
|
-
valueTarget
|
|
42
|
+
forStatement.valueTarget = attachLeadingComments(
|
|
43
|
+
createNode(Identifier, valueTarget, valueTarget.text),
|
|
44
|
+
tokens,
|
|
45
|
+
valueTarget
|
|
45
46
|
);
|
|
46
47
|
} else {
|
|
47
48
|
forStatement.keyTarget = null;
|
|
48
|
-
forStatement.valueTarget =
|
|
49
|
-
Identifier,
|
|
50
|
-
|
|
51
|
-
keyTarget
|
|
49
|
+
forStatement.valueTarget = attachLeadingComments(
|
|
50
|
+
createNode(Identifier, keyTarget, keyTarget.text),
|
|
51
|
+
tokens,
|
|
52
|
+
keyTarget
|
|
52
53
|
);
|
|
53
54
|
}
|
|
54
55
|
|
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
setStartFromToken,
|
|
26
26
|
setEndFromToken,
|
|
27
27
|
createNode,
|
|
28
|
+
attachListItemComments,
|
|
29
|
+
attachTrailingComments,
|
|
28
30
|
hasTagStartTokenTrimLeft,
|
|
29
31
|
hasTagEndTokenTrimRight
|
|
30
32
|
} from "../../melody-parser/index.js";
|
|
@@ -43,7 +45,14 @@ export const MacroParser = {
|
|
|
43
45
|
const arg = tokens.expect(Types.SYMBOL);
|
|
44
46
|
// if default value is defined
|
|
45
47
|
if (!tokens.test(Types.ASSIGNMENT)) {
|
|
46
|
-
args.push(
|
|
48
|
+
args.push(
|
|
49
|
+
attachListItemComments(
|
|
50
|
+
createNode(Identifier, arg, arg.text),
|
|
51
|
+
args[args.length - 1],
|
|
52
|
+
tokens,
|
|
53
|
+
arg
|
|
54
|
+
)
|
|
55
|
+
);
|
|
47
56
|
} else {
|
|
48
57
|
tokens.expect(Types.ASSIGNMENT);
|
|
49
58
|
let value;
|
|
@@ -76,7 +85,14 @@ export const MacroParser = {
|
|
|
76
85
|
value = tokens.expect(Types.FALSE);
|
|
77
86
|
node = createNode(BooleanLiteral, value, value.text);
|
|
78
87
|
}
|
|
79
|
-
args.push(
|
|
88
|
+
args.push(
|
|
89
|
+
attachListItemComments(
|
|
90
|
+
createNode(Identifier, arg, arg.text, node),
|
|
91
|
+
args[args.length - 1],
|
|
92
|
+
tokens,
|
|
93
|
+
arg
|
|
94
|
+
)
|
|
95
|
+
);
|
|
80
96
|
}
|
|
81
97
|
|
|
82
98
|
if (!tokens.nextIf(Types.COMMA) && !tokens.test(Types.RPAREN)) {
|
|
@@ -88,6 +104,7 @@ export const MacroParser = {
|
|
|
88
104
|
});
|
|
89
105
|
}
|
|
90
106
|
}
|
|
107
|
+
attachTrailingComments(args[args.length - 1], tokens, tokens.la(0));
|
|
91
108
|
tokens.expect(Types.RPAREN);
|
|
92
109
|
|
|
93
110
|
const openingTagEndToken = tokens.la(0);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { DoStatement, PropItem, PropsStatement } from "../types.js";
|
|
2
2
|
import {
|
|
3
|
+
attachListItemComments,
|
|
4
|
+
attachTrailingComments,
|
|
3
5
|
createNode,
|
|
4
6
|
setEndFromToken,
|
|
5
7
|
setStartFromToken,
|
|
@@ -25,9 +27,19 @@ export const PropsParser = {
|
|
|
25
27
|
} else {
|
|
26
28
|
valueNode = undefined;
|
|
27
29
|
}
|
|
28
|
-
const item =
|
|
30
|
+
const item = attachListItemComments(
|
|
31
|
+
new PropItem(nameNode, valueNode),
|
|
32
|
+
items[items.length - 1],
|
|
33
|
+
tokenStream,
|
|
34
|
+
name
|
|
35
|
+
);
|
|
29
36
|
items.push(item);
|
|
30
37
|
} while (tokenStream.nextIf(Types.COMMA));
|
|
38
|
+
attachTrailingComments(
|
|
39
|
+
items[items.length - 1],
|
|
40
|
+
tokenStream,
|
|
41
|
+
tokenStream.la(0)
|
|
42
|
+
);
|
|
31
43
|
|
|
32
44
|
const propsStatement = new PropsStatement(items);
|
|
33
45
|
setStartFromToken(propsStatement, token);
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
setStartFromToken,
|
|
20
20
|
setEndFromToken,
|
|
21
21
|
createNode,
|
|
22
|
+
attachLeadingComments,
|
|
22
23
|
hasTagStartTokenTrimLeft,
|
|
23
24
|
hasTagEndTokenTrimRight
|
|
24
25
|
} from "../../melody-parser/index.js";
|
|
@@ -29,6 +30,7 @@ export const SetParser = {
|
|
|
29
30
|
parse(parser, token) {
|
|
30
31
|
const tokens = parser.tokens;
|
|
31
32
|
const names = [];
|
|
33
|
+
const nameTokens = [];
|
|
32
34
|
const values = [];
|
|
33
35
|
|
|
34
36
|
let openingTagEndToken;
|
|
@@ -37,6 +39,7 @@ export const SetParser = {
|
|
|
37
39
|
do {
|
|
38
40
|
const name = tokens.expect(Types.SYMBOL);
|
|
39
41
|
names.push(createNode(Identifier, name, name.text));
|
|
42
|
+
nameTokens.push(name);
|
|
40
43
|
} while (tokens.nextIf(Types.COMMA));
|
|
41
44
|
|
|
42
45
|
if (tokens.nextIf(Types.ASSIGNMENT)) {
|
|
@@ -79,9 +82,10 @@ ${names.length} variable names and ${values.length} values.`
|
|
|
79
82
|
// now join names and values
|
|
80
83
|
const assignments = [];
|
|
81
84
|
for (let i = 0, len = names.length; i < len; i++) {
|
|
82
|
-
assignments[i] =
|
|
83
|
-
names[i],
|
|
84
|
-
|
|
85
|
+
assignments[i] = attachLeadingComments(
|
|
86
|
+
new VariableDeclarationStatement(names[i], values[i]),
|
|
87
|
+
tokens,
|
|
88
|
+
nameTokens[i]
|
|
85
89
|
);
|
|
86
90
|
}
|
|
87
91
|
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TypesDeclarationStatement,
|
|
3
|
+
TypesVariableDeclaration
|
|
4
|
+
} from "../types.js";
|
|
5
|
+
import {
|
|
6
|
+
attachListItemComments,
|
|
7
|
+
attachTrailingComments,
|
|
8
|
+
createNode,
|
|
9
|
+
setEndFromToken,
|
|
10
|
+
setStartFromToken,
|
|
11
|
+
Types
|
|
12
|
+
} from "../../melody-parser/index.js";
|
|
13
|
+
import { Identifier, StringLiteral } from "../../melody-types/index.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('../../melody-parser/Parser.js').Parser} Parser
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {import('../../melody-parser/TokenStream.js').TokenStream} TokenStream
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {import('../../melody-types/index.js').Node} Node
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The `{% types %}` tag node parser.
|
|
29
|
+
*
|
|
30
|
+
* Introduced in Twig version 3.13.
|
|
31
|
+
*/
|
|
32
|
+
export const TypesParser = {
|
|
33
|
+
name: "types",
|
|
34
|
+
/**
|
|
35
|
+
* @param {Parser} parser
|
|
36
|
+
* @param {Node} token
|
|
37
|
+
* @return {TypesDeclarationStatement}
|
|
38
|
+
*/
|
|
39
|
+
parse(parser, token) {
|
|
40
|
+
const tokens = parser.tokens;
|
|
41
|
+
|
|
42
|
+
const typesStatement = new TypesDeclarationStatement();
|
|
43
|
+
typesStatement.enclosed = !!tokens.nextIf(Types.LBRACKET);
|
|
44
|
+
|
|
45
|
+
while (
|
|
46
|
+
!tokens.test(Types.RBRACKET) &&
|
|
47
|
+
!tokens.test(Types.TAG_END) &&
|
|
48
|
+
!tokens.test(Types.EOF)
|
|
49
|
+
) {
|
|
50
|
+
const nameToken = tokens.expect(Types.SYMBOL);
|
|
51
|
+
const nameNode = createNode(Identifier, nameToken, nameToken.text);
|
|
52
|
+
|
|
53
|
+
const isVariableOptional = !!tokens.nextIf(Types.QUESTION_MARK);
|
|
54
|
+
|
|
55
|
+
tokens.expect(Types.COLON);
|
|
56
|
+
const valueNode = matchLiteralStringExpression(tokens);
|
|
57
|
+
if (valueNode === false) {
|
|
58
|
+
parser.error({
|
|
59
|
+
title: "types declaration must be a string",
|
|
60
|
+
pos: tokens.la(0).pos,
|
|
61
|
+
advice: `The variable declaration for types must be a string.`
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const varType = attachListItemComments(
|
|
66
|
+
new TypesVariableDeclaration(nameNode, valueNode),
|
|
67
|
+
typesStatement.declarations[
|
|
68
|
+
typesStatement.declarations.length - 1
|
|
69
|
+
],
|
|
70
|
+
tokens,
|
|
71
|
+
nameToken
|
|
72
|
+
);
|
|
73
|
+
varType.optional = isVariableOptional;
|
|
74
|
+
typesStatement.declarations.push(varType);
|
|
75
|
+
|
|
76
|
+
tokens.nextIf(Types.COMMA);
|
|
77
|
+
}
|
|
78
|
+
attachTrailingComments(
|
|
79
|
+
typesStatement.declarations[typesStatement.declarations.length - 1],
|
|
80
|
+
tokens,
|
|
81
|
+
tokens.la(0)
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (typesStatement.enclosed) {
|
|
85
|
+
tokens.expect(Types.RBRACKET);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
setStartFromToken(typesStatement, token);
|
|
89
|
+
setEndFromToken(typesStatement, tokens.expect(Types.TAG_END));
|
|
90
|
+
return typesStatement;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {TokenStream} tokens
|
|
96
|
+
* @return {StringLiteral|false}
|
|
97
|
+
*/
|
|
98
|
+
function matchLiteralStringExpression(tokens) {
|
|
99
|
+
const stringStartToken = tokens.nextIf(Types.STRING_START);
|
|
100
|
+
if (!stringStartToken) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const valueToken = tokens.expect(Types.STRING);
|
|
105
|
+
if (!tokens.nextIf(Types.STRING_END)) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return createNode(StringLiteral, valueToken, valueToken.text);
|
|
110
|
+
}
|
|
@@ -319,6 +319,50 @@ type(SetStatement, "SetStatement");
|
|
|
319
319
|
alias(SetStatement, "Statement", "ContextMutation");
|
|
320
320
|
visitor(SetStatement, "assignments");
|
|
321
321
|
|
|
322
|
+
/**
|
|
323
|
+
* The `{% types foo?: 'string' %}` variable type node.
|
|
324
|
+
*
|
|
325
|
+
* Introduced in Twig version 3.13.
|
|
326
|
+
*
|
|
327
|
+
* @property {boolean} optional
|
|
328
|
+
*/
|
|
329
|
+
export class TypesVariableDeclaration extends Node {
|
|
330
|
+
/**
|
|
331
|
+
* @param {Identifier} name
|
|
332
|
+
* @param {string} value
|
|
333
|
+
*/
|
|
334
|
+
constructor(name, value) {
|
|
335
|
+
super();
|
|
336
|
+
this.name = name;
|
|
337
|
+
this.value = value;
|
|
338
|
+
this.optional = false;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
type(TypesVariableDeclaration, "TypesVariableDeclaration");
|
|
342
|
+
alias(TypesVariableDeclaration, "Statement");
|
|
343
|
+
visitor(TypesVariableDeclaration, "name", "value");
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The `{% types %}` tag node.
|
|
347
|
+
*
|
|
348
|
+
* Introduced in Twig version 3.13.
|
|
349
|
+
*
|
|
350
|
+
* @property {boolean} enclosed
|
|
351
|
+
*/
|
|
352
|
+
export class TypesDeclarationStatement extends Node {
|
|
353
|
+
/**
|
|
354
|
+
* @param {Array<TypesVariableDeclaration>} declarations
|
|
355
|
+
*/
|
|
356
|
+
constructor(declarations = []) {
|
|
357
|
+
super();
|
|
358
|
+
this.declarations = declarations;
|
|
359
|
+
this.enclosed = false;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
type(TypesDeclarationStatement, "TypesDeclarationStatement");
|
|
363
|
+
alias(TypesDeclarationStatement, "Statement");
|
|
364
|
+
visitor(TypesDeclarationStatement, "declarations");
|
|
365
|
+
|
|
322
366
|
export class PropsStatement extends Node {
|
|
323
367
|
/**
|
|
324
368
|
*
|
|
@@ -186,6 +186,8 @@ export default class Lexer {
|
|
|
186
186
|
input.next();
|
|
187
187
|
input.next();
|
|
188
188
|
if (!(c = input.next()) === ">") {
|
|
189
|
+
// Unused error; Prettier appears to
|
|
190
|
+
// fix malformed HTML comments.
|
|
189
191
|
this.error(
|
|
190
192
|
"Unexpected end for HTML comment",
|
|
191
193
|
input.mark(),
|
|
@@ -304,6 +306,7 @@ export default class Lexer {
|
|
|
304
306
|
return this.matchSymbol(pos);
|
|
305
307
|
}
|
|
306
308
|
} else {
|
|
309
|
+
// Unreachable error?
|
|
307
310
|
return this.error(`Invalid state ${this.state}`, pos);
|
|
308
311
|
}
|
|
309
312
|
}
|
|
@@ -353,6 +356,8 @@ export default class Lexer {
|
|
|
353
356
|
this.pushState(State.STRING_DOUBLE);
|
|
354
357
|
input.next();
|
|
355
358
|
return this.createToken(TokenTypes.STRING_START, pos);
|
|
359
|
+
case "#":
|
|
360
|
+
return this.matchInlineComment(pos);
|
|
356
361
|
case "=":
|
|
357
362
|
// Lookahead for '>'
|
|
358
363
|
if (input.la(1) === ">") {
|
|
@@ -410,11 +415,13 @@ export default class Lexer {
|
|
|
410
415
|
input.next();
|
|
411
416
|
return this.createToken(CHAR_TO_TOKEN[c], pos);
|
|
412
417
|
} else if (c === "\xa0") {
|
|
418
|
+
input.next();
|
|
413
419
|
return this.error(
|
|
414
420
|
"Unsupported token: Non-breaking space",
|
|
415
421
|
pos
|
|
416
422
|
);
|
|
417
423
|
}
|
|
424
|
+
input.next();
|
|
418
425
|
return this.error(`Unknown token ${c}`, pos);
|
|
419
426
|
}
|
|
420
427
|
}
|
|
@@ -521,6 +528,7 @@ export default class Lexer {
|
|
|
521
528
|
}
|
|
522
529
|
const end = input.mark();
|
|
523
530
|
if (pos.index === end.index) {
|
|
531
|
+
// Unreachable error?
|
|
524
532
|
return this.error(
|
|
525
533
|
"Expected an Identifier",
|
|
526
534
|
pos,
|
|
@@ -663,6 +671,16 @@ export default class Lexer {
|
|
|
663
671
|
return this.createToken(TokenTypes.TEXT, pos);
|
|
664
672
|
}
|
|
665
673
|
|
|
674
|
+
// Inline comment (Twig 3.15), runs to the end of the line
|
|
675
|
+
matchInlineComment(pos) {
|
|
676
|
+
const input = this.input;
|
|
677
|
+
let c;
|
|
678
|
+
while ((c = input.la(0)) !== EOF && c !== "\n") {
|
|
679
|
+
input.next();
|
|
680
|
+
}
|
|
681
|
+
return this.createToken(TokenTypes.INLINE_COMMENT, pos);
|
|
682
|
+
}
|
|
683
|
+
|
|
666
684
|
matchComment(pos) {
|
|
667
685
|
const input = this.input;
|
|
668
686
|
let c;
|