@zackad/prettier-plugin-twig 0.16.2 → 0.17.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/types.js +96 -0
- package/src/melody/melody-extension-core/types.js +44 -0
- package/src/melody/melody-parser/Lexer.js +11 -4
- package/src/melody/melody-parser/Parser.js +1 -1
- package/src/melody/melody-types/index.js +3 -1
- package/src/print/Attribute.js +3 -2
- package/src/print/TypesDeclarationStatement.js +43 -0
- package/src/print/TypesVariableDeclaration.js +27 -0
- package/src/printer.js +4 -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,
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TypesDeclarationStatement,
|
|
3
|
+
TypesVariableDeclaration
|
|
4
|
+
} from "../types.js";
|
|
5
|
+
import {
|
|
6
|
+
createNode,
|
|
7
|
+
setEndFromToken,
|
|
8
|
+
setStartFromToken,
|
|
9
|
+
Types
|
|
10
|
+
} from "../../melody-parser/index.js";
|
|
11
|
+
import { Identifier, StringLiteral } from "../../melody-types/index.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {import('../../melody-parser/Parser.js').Parser} Parser
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {import('../../melody-parser/TokenStream.js').TokenStream} TokenStream
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {import('../../melody-types/index.js').Node} Node
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The `{% types %}` tag node parser.
|
|
27
|
+
*
|
|
28
|
+
* Introduced in Twig version 3.13.
|
|
29
|
+
*/
|
|
30
|
+
export const TypesParser = {
|
|
31
|
+
name: "types",
|
|
32
|
+
/**
|
|
33
|
+
* @param {Parser} parser
|
|
34
|
+
* @param {Node} token
|
|
35
|
+
* @return {TypesDeclarationStatement}
|
|
36
|
+
*/
|
|
37
|
+
parse(parser, token) {
|
|
38
|
+
const tokens = parser.tokens;
|
|
39
|
+
|
|
40
|
+
const typesStatement = new TypesDeclarationStatement();
|
|
41
|
+
typesStatement.enclosed = !!tokens.nextIf(Types.LBRACKET);
|
|
42
|
+
|
|
43
|
+
while (
|
|
44
|
+
!tokens.test(Types.RBRACKET) &&
|
|
45
|
+
!tokens.test(Types.TAG_END) &&
|
|
46
|
+
!tokens.test(Types.EOF)
|
|
47
|
+
) {
|
|
48
|
+
const nameToken = tokens.expect(Types.SYMBOL);
|
|
49
|
+
const nameNode = createNode(Identifier, nameToken, nameToken.text);
|
|
50
|
+
|
|
51
|
+
const isVariableOptional = !!tokens.nextIf(Types.QUESTION_MARK);
|
|
52
|
+
|
|
53
|
+
tokens.expect(Types.COLON);
|
|
54
|
+
const valueNode = matchLiteralStringExpression(tokens);
|
|
55
|
+
if (valueNode === false) {
|
|
56
|
+
parser.error({
|
|
57
|
+
title: "types declaration must be a string",
|
|
58
|
+
pos: tokens.la(0).pos,
|
|
59
|
+
advice: `The variable declaration for types must be a string.`
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const varType = new TypesVariableDeclaration(nameNode, valueNode);
|
|
64
|
+
varType.optional = isVariableOptional;
|
|
65
|
+
typesStatement.declarations.push(varType);
|
|
66
|
+
|
|
67
|
+
tokens.nextIf(Types.COMMA);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (typesStatement.enclosed) {
|
|
71
|
+
tokens.expect(Types.RBRACKET);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setStartFromToken(typesStatement, token);
|
|
75
|
+
setEndFromToken(typesStatement, tokens.expect(Types.TAG_END));
|
|
76
|
+
return typesStatement;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {TokenStream} tokens
|
|
82
|
+
* @return {StringLiteral|false}
|
|
83
|
+
*/
|
|
84
|
+
function matchLiteralStringExpression(tokens) {
|
|
85
|
+
const stringStartToken = tokens.nextIf(Types.STRING_START);
|
|
86
|
+
if (!stringStartToken) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const valueToken = tokens.expect(Types.STRING);
|
|
91
|
+
if (!tokens.nextIf(Types.STRING_END)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return createNode(StringLiteral, valueToken, valueToken.text);
|
|
96
|
+
}
|
|
@@ -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
|
*
|
|
@@ -31,6 +31,7 @@ const State = {
|
|
|
31
31
|
const STATE = Symbol("STATE");
|
|
32
32
|
const OPERATORS = Symbol("OPERATORS");
|
|
33
33
|
const STRING_START = Symbol("STRING_START");
|
|
34
|
+
const ATTR_QUOTE = Symbol("ATTR_QUOTE");
|
|
34
35
|
|
|
35
36
|
const CHAR_TO_TOKEN = {
|
|
36
37
|
"[": TokenTypes.LBRACE,
|
|
@@ -57,6 +58,7 @@ export default class Lexer {
|
|
|
57
58
|
this[STATE] = [State.TEXT];
|
|
58
59
|
this[OPERATORS] = [];
|
|
59
60
|
this[STRING_START] = null;
|
|
61
|
+
this[ATTR_QUOTE] = null;
|
|
60
62
|
this.options = {
|
|
61
63
|
preserveSourceLiterally: preserveSourceLiterally === true
|
|
62
64
|
};
|
|
@@ -74,6 +76,7 @@ export default class Lexer {
|
|
|
74
76
|
reset() {
|
|
75
77
|
this.input.reset();
|
|
76
78
|
this[STATE] = [State.TEXT];
|
|
79
|
+
this[ATTR_QUOTE] = null;
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
get source() {
|
|
@@ -265,6 +268,8 @@ export default class Lexer {
|
|
|
265
268
|
this.popState();
|
|
266
269
|
return this.createToken(TokenTypes.ELEMENT_END, pos);
|
|
267
270
|
case '"':
|
|
271
|
+
case "'":
|
|
272
|
+
this[ATTR_QUOTE] = c;
|
|
268
273
|
input.next();
|
|
269
274
|
this.pushState(State.ATTRIBUTE_VALUE);
|
|
270
275
|
return this.createToken(TokenTypes.STRING_START, pos);
|
|
@@ -275,12 +280,14 @@ export default class Lexer {
|
|
|
275
280
|
return this.matchSymbol(pos);
|
|
276
281
|
}
|
|
277
282
|
} else if (this.state === State.ATTRIBUTE_VALUE) {
|
|
278
|
-
|
|
283
|
+
const quoteChar = this[ATTR_QUOTE];
|
|
284
|
+
if (c === quoteChar) {
|
|
279
285
|
input.next();
|
|
280
286
|
this.popState();
|
|
287
|
+
this[ATTR_QUOTE] = null;
|
|
281
288
|
return this.createToken(TokenTypes.STRING_END, pos);
|
|
282
289
|
}
|
|
283
|
-
return this.matchAttributeValue(pos);
|
|
290
|
+
return this.matchAttributeValue(pos, quoteChar);
|
|
284
291
|
} else if (this.state === State.DECLARATION) {
|
|
285
292
|
switch (c) {
|
|
286
293
|
case ">":
|
|
@@ -569,9 +576,9 @@ export default class Lexer {
|
|
|
569
576
|
return result;
|
|
570
577
|
}
|
|
571
578
|
|
|
572
|
-
matchAttributeValue(pos) {
|
|
579
|
+
matchAttributeValue(pos, quoteChar = '"') {
|
|
573
580
|
const input = this.input;
|
|
574
|
-
const start =
|
|
581
|
+
const start = quoteChar;
|
|
575
582
|
let c = input.la(0);
|
|
576
583
|
const c2 = input.la(1);
|
|
577
584
|
if (c === "{" && (c2 === "{" || c2 === "#" || c2 === "%")) {
|
|
@@ -432,7 +432,7 @@ export default class Parser {
|
|
|
432
432
|
if (nodes.length > 1) {
|
|
433
433
|
expr.wasImplicitConcatenation = true;
|
|
434
434
|
}
|
|
435
|
-
const attr = new n.Attribute(keyNode, expr);
|
|
435
|
+
const attr = new n.Attribute(keyNode, expr, start.text);
|
|
436
436
|
copyStart(attr, keyNode);
|
|
437
437
|
copyEnd(attr, expr);
|
|
438
438
|
element.attributes.push(attr);
|
|
@@ -448,11 +448,13 @@ export class Attribute extends Node {
|
|
|
448
448
|
/**
|
|
449
449
|
* @param {Node} name
|
|
450
450
|
* @param {Node} value
|
|
451
|
+
* @param {string} quoteChar
|
|
451
452
|
*/
|
|
452
|
-
constructor(name, value = null) {
|
|
453
|
+
constructor(name, value = null, quoteChar = '"') {
|
|
453
454
|
super();
|
|
454
455
|
this.name = name;
|
|
455
456
|
this.value = value;
|
|
457
|
+
this.quoteChar = quoteChar;
|
|
456
458
|
}
|
|
457
459
|
|
|
458
460
|
isImmutable() {
|
package/src/print/Attribute.js
CHANGED
|
@@ -25,7 +25,8 @@ const printAttribute = (node, path, print = print) => {
|
|
|
25
25
|
node[EXPRESSION_NEEDED] = true;
|
|
26
26
|
node[STRING_NEEDS_QUOTES] = false;
|
|
27
27
|
if (node.value) {
|
|
28
|
-
|
|
28
|
+
const quote = node.quoteChar || '"';
|
|
29
|
+
docs.push("=", quote);
|
|
29
30
|
if (
|
|
30
31
|
Node.isBinaryConcatExpression(node.value) &&
|
|
31
32
|
node.value.wasImplicitConcatenation
|
|
@@ -41,7 +42,7 @@ const printAttribute = (node, path, print = print) => {
|
|
|
41
42
|
}
|
|
42
43
|
docs.push(path.call(print, "value"));
|
|
43
44
|
}
|
|
44
|
-
docs.push(
|
|
45
|
+
docs.push(quote);
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
return docs;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
|
|
3
|
+
const { group, join, line, indentIfBreak, hardline, softline } = doc.builders;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('../melody/melody-extension-core/types.js').TypesDeclarationStatement} TypesDeclarationStatement
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `{% types %}` tag node printer.
|
|
11
|
+
*
|
|
12
|
+
* Introduced in Twig version 3.13.
|
|
13
|
+
*
|
|
14
|
+
* @param {TypesDeclarationStatement} node
|
|
15
|
+
*/
|
|
16
|
+
const printTypesDeclarationStatement = (node, path, print) => {
|
|
17
|
+
const typesGroupId = Symbol("types-declarations");
|
|
18
|
+
|
|
19
|
+
const mappedTypes = path.map(print, "declarations");
|
|
20
|
+
const joinedTypes = join([",", line], mappedTypes);
|
|
21
|
+
const hasManyTypes = node.declarations.length > 1 ? hardline : softline;
|
|
22
|
+
|
|
23
|
+
const parts = [node.trimLeft ? "{%-" : "{%", " types "];
|
|
24
|
+
|
|
25
|
+
if (node.enclosed) {
|
|
26
|
+
parts.push("{");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
parts.push(
|
|
30
|
+
indentIfBreak([hasManyTypes, joinedTypes], { groupId: typesGroupId }),
|
|
31
|
+
line
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (node.enclosed) {
|
|
35
|
+
parts.push("}", " ");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
parts.push(node.trimRight ? "-%}" : "%}");
|
|
39
|
+
|
|
40
|
+
return group(parts, { id: typesGroupId });
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export { printTypesDeclarationStatement };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group } = doc.builders;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('../melody/melody-extension-core/types.js').TypesVariableDeclaration} TypesVariableDeclaration
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The `{% types foo?: 'string' %}` variable type node printer.
|
|
12
|
+
*
|
|
13
|
+
* Introduced in Twig version 3.13.
|
|
14
|
+
*
|
|
15
|
+
* @param {TypesVariableDeclaration} node
|
|
16
|
+
*/
|
|
17
|
+
const printTypesVariableDeclaration = (node, path, print) => {
|
|
18
|
+
const printedName = path.call(print, "name");
|
|
19
|
+
const printedAssignment = node.optional ? "?: " : ": ";
|
|
20
|
+
|
|
21
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
22
|
+
const printedValue = path.call(print, "value");
|
|
23
|
+
|
|
24
|
+
return group([printedName, [printedAssignment, printedValue]]);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { printTypesVariableDeclaration };
|
package/src/printer.js
CHANGED
|
@@ -46,6 +46,8 @@ import { printFilterBlockStatement } from "./print/FilterBlockStatement.js";
|
|
|
46
46
|
import { printVariableDeclarationStatement } from "./print/VariableDeclarationStatement.js";
|
|
47
47
|
import { printNamedArgumentExpression } from "./print/NamedArgumentExpression.js";
|
|
48
48
|
import { printSwitchTag } from "./print/SwitchTag.js";
|
|
49
|
+
import { printTypesDeclarationStatement } from "./print/TypesDeclarationStatement.js";
|
|
50
|
+
import { printTypesVariableDeclaration } from "./print/TypesVariableDeclaration.js";
|
|
49
51
|
import {
|
|
50
52
|
isWhitespaceNode,
|
|
51
53
|
isHtmlCommentEqualTo,
|
|
@@ -223,6 +225,8 @@ printFunctions["MountStatement"] = printMountStatement;
|
|
|
223
225
|
printFunctions["ForStatement"] = printForStatement;
|
|
224
226
|
printFunctions["BinaryConcatExpression"] = printBinaryExpression;
|
|
225
227
|
printFunctions["SetStatement"] = printSetStatement;
|
|
228
|
+
printFunctions["TypesDeclarationStatement"] = printTypesDeclarationStatement;
|
|
229
|
+
printFunctions["TypesVariableDeclaration"] = printTypesVariableDeclaration;
|
|
226
230
|
printFunctions["PropsStatement"] = printPropsStatement;
|
|
227
231
|
printFunctions["PropItem"] = printPropItem;
|
|
228
232
|
printFunctions["VariableDeclarationStatement"] =
|