@zackad/prettier-plugin-twig 0.17.0 → 0.18.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/package.json +1 -1
- package/src/melody/melody-extension-core/operators.js +3 -2
- 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 +15 -1
- package/src/melody/melody-parser/GenericTagParser.js +1 -1
- package/src/melody/melody-parser/Lexer.js +25 -6
- 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/BinaryExpression.js +23 -2
- package/src/print/ConditionalExpression.js +8 -1
- 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/StringLiteral.js +53 -18
- package/src/print/TwigComment.js +6 -10
- package/src/print/UnarySubclass.js +11 -1
- package/src/printer.js +14 -3
- package/src/util/printFunctions.js +74 -10
- package/src/util/publicFunctions.js +38 -0
- package/src/util/publicSymbols.js +7 -0
|
@@ -16,6 +16,8 @@ const argumentNeedsParentheses = node => isMultipartExpression(node);
|
|
|
16
16
|
|
|
17
17
|
const isLogicalOperator = operator => operator === "not";
|
|
18
18
|
|
|
19
|
+
const isSignOperator = operator => operator === "-" || operator === "+";
|
|
20
|
+
|
|
19
21
|
const printLogicalExpression = (node, path, print) => {
|
|
20
22
|
const foundRootAbove = firstValueInAncestorChain(
|
|
21
23
|
path,
|
|
@@ -65,7 +67,15 @@ const printUnarySubclass = (node, path, print) => {
|
|
|
65
67
|
} else if (!hasTestExpressionArgument) {
|
|
66
68
|
parts.push(node.operator, " ");
|
|
67
69
|
}
|
|
68
|
-
|
|
70
|
+
if (
|
|
71
|
+
isSignOperator(node.operator) &&
|
|
72
|
+
!hasTestExpressionArgument &&
|
|
73
|
+
argumentNeedsParentheses(node.argument)
|
|
74
|
+
) {
|
|
75
|
+
parts.push("(", path.call(print, "argument"), ")");
|
|
76
|
+
} else {
|
|
77
|
+
parts.push(path.call(print, "argument"));
|
|
78
|
+
}
|
|
69
79
|
wrapExpressionIfNeeded(path, parts, node);
|
|
70
80
|
|
|
71
81
|
return parts;
|
package/src/printer.js
CHANGED
|
@@ -5,6 +5,7 @@ import { printElement } from "./print/Element.js";
|
|
|
5
5
|
import { printAttribute } from "./print/Attribute.js";
|
|
6
6
|
import { printIdentifier } from "./print/Identifier.js";
|
|
7
7
|
import { printExpressionStatement } from "./print/ExpressionStatement.js";
|
|
8
|
+
import { printErroredStatement } from "./print/ErroredStatement.js";
|
|
8
9
|
import { printMemberExpression } from "./print/MemberExpression.js";
|
|
9
10
|
import { printFilterExpression } from "./print/FilterExpression.js";
|
|
10
11
|
import { printArrowFunction } from "./print/ArrowFunction.js";
|
|
@@ -49,11 +50,14 @@ import { printSwitchTag } from "./print/SwitchTag.js";
|
|
|
49
50
|
import { printTypesDeclarationStatement } from "./print/TypesDeclarationStatement.js";
|
|
50
51
|
import { printTypesVariableDeclaration } from "./print/TypesVariableDeclaration.js";
|
|
51
52
|
import {
|
|
53
|
+
printComments,
|
|
52
54
|
isWhitespaceNode,
|
|
53
55
|
isHtmlCommentEqualTo,
|
|
54
56
|
isTwigCommentEqualTo
|
|
55
57
|
} from "./util/index.js";
|
|
58
|
+
import { PRETTIER_IGNORED_CODE } from "./util/publicSymbols.js";
|
|
56
59
|
import { ORIGINAL_SOURCE } from "./parser.js";
|
|
60
|
+
import { INLINE_COMMENT_ERROR } from "./melody/melody-parser/index.js";
|
|
57
61
|
|
|
58
62
|
const printFunctions = {};
|
|
59
63
|
|
|
@@ -102,15 +106,21 @@ const print = (path, options, print) => {
|
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
checkForIgnoreEnd(node);
|
|
105
|
-
|
|
109
|
+
node[PRETTIER_IGNORED_CODE] =
|
|
106
110
|
(shouldApplyIgnoreNext(node) && ignoreNext) || ignoreRegion;
|
|
107
111
|
const hasPrintFunction = printFunctions[nodeType];
|
|
108
112
|
|
|
109
113
|
// Happy path: We have a formatting function, and the user wants the
|
|
110
114
|
// node formatted
|
|
111
|
-
if (!
|
|
115
|
+
if (!node[PRETTIER_IGNORED_CODE] && hasPrintFunction) {
|
|
116
|
+
if (node[INLINE_COMMENT_ERROR]) {
|
|
117
|
+
throw node[INLINE_COMMENT_ERROR];
|
|
118
|
+
}
|
|
112
119
|
checkForIgnoreStart(node);
|
|
113
|
-
return
|
|
120
|
+
return printComments(
|
|
121
|
+
node,
|
|
122
|
+
printFunctions[nodeType](node, path, print, options)
|
|
123
|
+
);
|
|
114
124
|
} else if (!hasPrintFunction) {
|
|
115
125
|
console.warn(`No print function available for node type "${nodeType}"`);
|
|
116
126
|
}
|
|
@@ -193,6 +203,7 @@ printFunctions["Element"] = printElement;
|
|
|
193
203
|
printFunctions["Attribute"] = printAttribute;
|
|
194
204
|
printFunctions["PrintTextStatement"] = printTextStatement;
|
|
195
205
|
printFunctions["PrintExpressionStatement"] = printExpressionStatement;
|
|
206
|
+
printFunctions["PrintErroredStatement"] = printErroredStatement;
|
|
196
207
|
printFunctions["MemberExpression"] = printMemberExpression;
|
|
197
208
|
printFunctions["FilterExpression"] = printFilterExpression;
|
|
198
209
|
printFunctions["ArrowFunction"] = printArrowFunction;
|
|
@@ -1,28 +1,54 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
2
|
import { Node } from "../melody/melody-types/index.js";
|
|
3
3
|
|
|
4
|
-
const { line, indent, group } =
|
|
4
|
+
const { line, indent, group, hardline, join, lineSuffix, breakParent } =
|
|
5
|
+
doc.builders;
|
|
5
6
|
|
|
6
7
|
const noSpaceBeforeToken = {
|
|
7
8
|
",": true
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
const noSpaceAfterToken = {};
|
|
12
|
+
|
|
10
13
|
const printSingleTwigTag = (node, path, print) => {
|
|
11
14
|
const opener = node.trimLeft ? "{%-" : "{%";
|
|
12
15
|
const parts = [opener, " ", node.tagName];
|
|
13
16
|
const printedParts = path.map(print, "parts");
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
|
|
18
|
+
const groupedNodes = [];
|
|
19
|
+
const groupedDocs = [];
|
|
20
|
+
for (let i = 0; i < node.parts.length; i++) {
|
|
21
|
+
const equalsToken = node.parts[i + 1];
|
|
22
|
+
const isAssignment =
|
|
23
|
+
Node.isGenericToken(equalsToken) &&
|
|
24
|
+
equalsToken.tokenText === "=" &&
|
|
25
|
+
node.parts[i + 2] !== undefined;
|
|
26
|
+
if (isAssignment) {
|
|
27
|
+
groupedNodes.push(node.parts[i]);
|
|
28
|
+
groupedDocs.push([printedParts[i], " = ", printedParts[i + 2]]);
|
|
29
|
+
i += 2;
|
|
30
|
+
} else {
|
|
31
|
+
groupedNodes.push(node.parts[i]);
|
|
32
|
+
groupedDocs.push(printedParts[i]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (groupedDocs.length > 0) {
|
|
37
|
+
parts.push(" ", groupedDocs[0]);
|
|
16
38
|
}
|
|
17
39
|
const indentedParts = [];
|
|
18
|
-
for (let i = 1; i <
|
|
19
|
-
const part =
|
|
40
|
+
for (let i = 1; i < groupedDocs.length; i++) {
|
|
41
|
+
const part = groupedNodes[i];
|
|
42
|
+
const prevPart = groupedNodes[i - 1];
|
|
20
43
|
const isToken = Node.isGenericToken(part);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
const isPrevToken = Node.isGenericToken(prevPart);
|
|
45
|
+
const noSpaceBefore = isToken && noSpaceBeforeToken[part.tokenText];
|
|
46
|
+
const noSpaceAfter =
|
|
47
|
+
isPrevToken && noSpaceAfterToken[prevPart.tokenText];
|
|
48
|
+
const separator = noSpaceBefore || noSpaceAfter ? "" : line;
|
|
49
|
+
indentedParts.push(separator, groupedDocs[i]);
|
|
24
50
|
}
|
|
25
|
-
if (
|
|
51
|
+
if (groupedDocs.length > 1) {
|
|
26
52
|
parts.push(indent(indentedParts));
|
|
27
53
|
}
|
|
28
54
|
const closing = node.trimRight ? "-%}" : "%}";
|
|
@@ -30,4 +56,42 @@ const printSingleTwigTag = (node, path, print) => {
|
|
|
30
56
|
return group(parts);
|
|
31
57
|
};
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
const hasLeadingComments = node =>
|
|
60
|
+
!!node &&
|
|
61
|
+
Array.isArray(node.leadingComments) &&
|
|
62
|
+
node.leadingComments.length > 0;
|
|
63
|
+
|
|
64
|
+
const hasTrailingComments = node =>
|
|
65
|
+
!!node &&
|
|
66
|
+
Array.isArray(node.trailingComments) &&
|
|
67
|
+
node.trailingComments.length > 0;
|
|
68
|
+
|
|
69
|
+
const hasComments = node =>
|
|
70
|
+
hasLeadingComments(node) || hasTrailingComments(node);
|
|
71
|
+
|
|
72
|
+
// Own group, so the line breaks forced by the comments do not break the node itself
|
|
73
|
+
const printComments = (node, printed) => {
|
|
74
|
+
if (!hasComments(node)) {
|
|
75
|
+
return printed;
|
|
76
|
+
}
|
|
77
|
+
const parts = [];
|
|
78
|
+
if (hasLeadingComments(node)) {
|
|
79
|
+
parts.push(join(hardline, node.leadingComments), hardline);
|
|
80
|
+
}
|
|
81
|
+
parts.push(group(printed));
|
|
82
|
+
if (hasTrailingComments(node)) {
|
|
83
|
+
parts.push(
|
|
84
|
+
lineSuffix([" ", join(" ", node.trailingComments)]),
|
|
85
|
+
breakParent
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return parts;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
printSingleTwigTag,
|
|
93
|
+
printComments,
|
|
94
|
+
hasLeadingComments,
|
|
95
|
+
hasTrailingComments,
|
|
96
|
+
hasComments
|
|
97
|
+
};
|
|
@@ -444,6 +444,42 @@ const stripTwigCommentChars = s => {
|
|
|
444
444
|
return result;
|
|
445
445
|
};
|
|
446
446
|
|
|
447
|
+
/**
|
|
448
|
+
* Splits a Twig comment into its text and the delimiters around it.
|
|
449
|
+
* Documentation comments (Twig 3.29) open with "{##" and may close
|
|
450
|
+
* symmetrically with "##}".
|
|
451
|
+
*
|
|
452
|
+
* @param {string} s
|
|
453
|
+
*/
|
|
454
|
+
const parseTwigComment = s => {
|
|
455
|
+
let text = s.startsWith("{#") ? s.slice(2) : s;
|
|
456
|
+
// "{##}" is an empty regular comment, not a documentation comment
|
|
457
|
+
const isDocumentation = text.startsWith("#") && text !== "#}";
|
|
458
|
+
if (isDocumentation) {
|
|
459
|
+
text = text.slice(1);
|
|
460
|
+
}
|
|
461
|
+
const trimLeft = text.startsWith("-");
|
|
462
|
+
if (trimLeft) {
|
|
463
|
+
text = text.slice(1);
|
|
464
|
+
}
|
|
465
|
+
if (text.endsWith("#}")) {
|
|
466
|
+
text = text.slice(0, -2);
|
|
467
|
+
}
|
|
468
|
+
const hasSymmetricClose = isDocumentation && text.endsWith("#");
|
|
469
|
+
if (hasSymmetricClose) {
|
|
470
|
+
text = text.slice(0, -1);
|
|
471
|
+
}
|
|
472
|
+
const trimRight = text.endsWith("-");
|
|
473
|
+
if (trimRight) {
|
|
474
|
+
text = text.slice(0, -1);
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
text,
|
|
478
|
+
open: (isDocumentation ? "{##" : "{#") + (trimLeft ? "-" : ""),
|
|
479
|
+
close: (trimRight ? "-" : "") + (hasSymmetricClose ? "##}" : "#}")
|
|
480
|
+
};
|
|
481
|
+
};
|
|
482
|
+
|
|
447
483
|
const normalizeHtmlComment = s => {
|
|
448
484
|
const commentText = stripHtmlCommentChars(s);
|
|
449
485
|
return "<!-- " + unifyWhitespace(commentText) + " -->";
|
|
@@ -599,6 +635,8 @@ export {
|
|
|
599
635
|
hasAtLeastTwoNewlines,
|
|
600
636
|
stripHtmlCommentChars,
|
|
601
637
|
stripTwigCommentChars,
|
|
638
|
+
parseTwigComment,
|
|
639
|
+
unifyWhitespace,
|
|
602
640
|
normalizeHtmlComment,
|
|
603
641
|
normalizeTwigComment,
|
|
604
642
|
isHtmlCommentEqualTo,
|
|
@@ -55,6 +55,12 @@ const PRESERVE_LEADING_WHITESPACE = Symbol("PRESERVE_LEADING_WHITESPACE");
|
|
|
55
55
|
*/
|
|
56
56
|
const PRESERVE_TRAILING_WHITESPACE = Symbol("PRESERVE_TRAILING_WHITESPACE");
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Signals to statements that they should be ignored
|
|
60
|
+
* and printed as is.
|
|
61
|
+
*/
|
|
62
|
+
const PRETTIER_IGNORED_CODE = Symbol("PRETTIER_IGNORED_CODE");
|
|
63
|
+
|
|
58
64
|
/**
|
|
59
65
|
* Signals to text statements that only newlines should be
|
|
60
66
|
* preserved when hitting a whitespace-only node
|
|
@@ -77,6 +83,7 @@ const IS_ROOT_LOGICAL_EXPRESSION = Symbol("IS_ROOT_LOGICAL_EXPRESSION");
|
|
|
77
83
|
export {
|
|
78
84
|
STRING_NEEDS_QUOTES,
|
|
79
85
|
OVERRIDE_QUOTE_CHAR,
|
|
86
|
+
PRETTIER_IGNORED_CODE,
|
|
80
87
|
INSIDE_OF_STRING,
|
|
81
88
|
EXPRESSION_NEEDED,
|
|
82
89
|
FILTER_BLOCK,
|