@zackad/prettier-plugin-twig 0.16.3 → 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/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
|
*
|
|
@@ -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"] =
|