@zackad/prettier-plugin-twig 0.7.0-alpha.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/LICENSE +201 -0
- package/README.md +141 -0
- package/package.json +39 -0
- package/src/index.js +130 -0
- package/src/parser.js +82 -0
- package/src/print/AliasExpression.js +5 -0
- package/src/print/ArrayExpression.js +14 -0
- package/src/print/Attribute.js +50 -0
- package/src/print/AutoescapeBlock.js +31 -0
- package/src/print/BinaryExpression.js +155 -0
- package/src/print/BlockStatement.js +48 -0
- package/src/print/CallExpression.js +38 -0
- package/src/print/ConditionalExpression.js +27 -0
- package/src/print/Declaration.js +15 -0
- package/src/print/DoStatement.js +10 -0
- package/src/print/Element.js +63 -0
- package/src/print/EmbedStatement.js +38 -0
- package/src/print/ExpressionStatement.js +25 -0
- package/src/print/ExtendsStatement.js +13 -0
- package/src/print/FilterBlockStatement.js +27 -0
- package/src/print/FilterExpression.js +131 -0
- package/src/print/FlushStatement.js +7 -0
- package/src/print/ForStatement.js +58 -0
- package/src/print/FromStatement.js +32 -0
- package/src/print/GenericToken.js +5 -0
- package/src/print/GenericTwigTag.js +30 -0
- package/src/print/HtmlComment.js +22 -0
- package/src/print/Identifier.js +15 -0
- package/src/print/IfStatement.js +81 -0
- package/src/print/ImportDeclaration.js +18 -0
- package/src/print/IncludeStatement.js +26 -0
- package/src/print/MacroDeclarationStatement.js +31 -0
- package/src/print/MemberExpression.js +23 -0
- package/src/print/MountStatement.js +84 -0
- package/src/print/NamedArgumentExpression.js +10 -0
- package/src/print/ObjectExpression.js +21 -0
- package/src/print/ObjectProperty.js +25 -0
- package/src/print/SequenceExpression.js +21 -0
- package/src/print/SetStatement.js +91 -0
- package/src/print/SliceExpression.js +8 -0
- package/src/print/SpacelessBlock.js +23 -0
- package/src/print/StringLiteral.js +63 -0
- package/src/print/SwitchTag.js +35 -0
- package/src/print/TestExpression.js +47 -0
- package/src/print/TextStatement.js +45 -0
- package/src/print/TwigComment.js +23 -0
- package/src/print/UnaryExpression.js +18 -0
- package/src/print/UnarySubclass.js +58 -0
- package/src/print/UseStatement.js +25 -0
- package/src/print/VariableDeclarationStatement.js +21 -0
- package/src/printer.js +252 -0
- package/src/util/index.js +3 -0
- package/src/util/printFunctions.js +33 -0
- package/src/util/publicFunctions.js +593 -0
- package/src/util/publicSymbols.js +88 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { printChildBlock, quoteChar } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { hardline } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const createOpener = (node, options) => {
|
|
7
|
+
return [
|
|
8
|
+
node.trimLeft ? "{%-" : "{%",
|
|
9
|
+
" autoescape ",
|
|
10
|
+
quoteChar(options),
|
|
11
|
+
node.escapeType || "html",
|
|
12
|
+
quoteChar(options),
|
|
13
|
+
" ",
|
|
14
|
+
node.trimRightAutoescape ? "-%}" : "%}"
|
|
15
|
+
];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const p = (node, path, print, options) => {
|
|
19
|
+
const parts = [createOpener(node, options)];
|
|
20
|
+
parts.push(printChildBlock(node, path, print, "expressions"));
|
|
21
|
+
parts.push(
|
|
22
|
+
hardline,
|
|
23
|
+
node.trimLeftEndautoescape ? "{%-" : "{%",
|
|
24
|
+
" endautoescape ",
|
|
25
|
+
node.trimRight ? "-%}" : "%}"
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return parts;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { p as printAutoescapeBlock };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import { extension as coreExtension } from "melody-extension-core";
|
|
4
|
+
import {
|
|
5
|
+
EXPRESSION_NEEDED,
|
|
6
|
+
STRING_NEEDS_QUOTES,
|
|
7
|
+
INSIDE_OF_STRING,
|
|
8
|
+
GROUP_TOP_LEVEL_LOGICAL,
|
|
9
|
+
IS_ROOT_LOGICAL_EXPRESSION,
|
|
10
|
+
firstValueInAncestorChain,
|
|
11
|
+
findParentNode,
|
|
12
|
+
wrapExpressionIfNeeded
|
|
13
|
+
} from "../util/index.js";
|
|
14
|
+
|
|
15
|
+
const { group, line, softline, indent } = doc.builders;
|
|
16
|
+
const ALREADY_INDENTED = Symbol("ALREADY_INDENTED");
|
|
17
|
+
const OPERATOR_PRECEDENCE = Symbol("OPERATOR_PRECEDENCE");
|
|
18
|
+
const NO_WHITESPACE_AROUND = [".."];
|
|
19
|
+
|
|
20
|
+
const operatorPrecedence = coreExtension.binaryOperators.reduce((acc, curr) => {
|
|
21
|
+
acc[curr.text] = curr.precedence;
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
24
|
+
|
|
25
|
+
const printInterpolatedString = (node, path, print, options) => {
|
|
26
|
+
node[STRING_NEEDS_QUOTES] = false;
|
|
27
|
+
node[INSIDE_OF_STRING] = true;
|
|
28
|
+
|
|
29
|
+
const printedFragments = ['"']; // For interpolated strings, we HAVE to use double quotes
|
|
30
|
+
let currentNode = node;
|
|
31
|
+
const currentPath = [];
|
|
32
|
+
while (Node.isBinaryConcatExpression(currentNode)) {
|
|
33
|
+
printedFragments.unshift(path.call(print, ...currentPath, "right"));
|
|
34
|
+
currentPath.push("left");
|
|
35
|
+
currentNode = currentNode.left;
|
|
36
|
+
}
|
|
37
|
+
printedFragments.unshift(path.call(print, ...currentPath));
|
|
38
|
+
printedFragments.unshift('"');
|
|
39
|
+
return printedFragments;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const operatorNeedsSpaces = operator => {
|
|
43
|
+
return NO_WHITESPACE_AROUND.indexOf(operator) < 0;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const hasLogicalOperator = node => {
|
|
47
|
+
return node.operator === "or" || node.operator === "and";
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const otherNeedsParentheses = (node, otherProp) => {
|
|
51
|
+
const other = node[otherProp];
|
|
52
|
+
const isBinaryOther = Node.isBinaryExpression(other);
|
|
53
|
+
const ownPrecedence = operatorPrecedence[node.operator];
|
|
54
|
+
const otherPrecedence = isBinaryOther
|
|
55
|
+
? operatorPrecedence[node[otherProp].operator]
|
|
56
|
+
: Number.MAX_SAFE_INTEGER;
|
|
57
|
+
return (
|
|
58
|
+
otherPrecedence < ownPrecedence ||
|
|
59
|
+
(otherPrecedence > ownPrecedence &&
|
|
60
|
+
isBinaryOther &&
|
|
61
|
+
hasLogicalOperator(other)) ||
|
|
62
|
+
Node.isFilterExpression(other) ||
|
|
63
|
+
(Node.isBinaryConcatExpression(node) &&
|
|
64
|
+
Node.isConditionalExpression(other))
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const printBinaryExpression = (node, path, print) => {
|
|
69
|
+
node[EXPRESSION_NEEDED] = false;
|
|
70
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
71
|
+
|
|
72
|
+
const isBinaryRight = Node.isBinaryExpression(node.right);
|
|
73
|
+
const isLogicalOperator = ["and", "or"].indexOf(node.operator) > -1;
|
|
74
|
+
const whitespaceAroundOperator = operatorNeedsSpaces(node.operator);
|
|
75
|
+
|
|
76
|
+
const alreadyIndented = firstValueInAncestorChain(
|
|
77
|
+
path,
|
|
78
|
+
ALREADY_INDENTED,
|
|
79
|
+
false
|
|
80
|
+
);
|
|
81
|
+
if (!alreadyIndented && isBinaryRight) {
|
|
82
|
+
node.right[ALREADY_INDENTED] = true;
|
|
83
|
+
}
|
|
84
|
+
const foundRootAbove = firstValueInAncestorChain(
|
|
85
|
+
path,
|
|
86
|
+
IS_ROOT_LOGICAL_EXPRESSION,
|
|
87
|
+
false
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const parentNode = findParentNode(path);
|
|
91
|
+
const shouldGroupOnTopLevel = parentNode[GROUP_TOP_LEVEL_LOGICAL] !== false;
|
|
92
|
+
|
|
93
|
+
if (!foundRootAbove) {
|
|
94
|
+
node[IS_ROOT_LOGICAL_EXPRESSION] = true;
|
|
95
|
+
}
|
|
96
|
+
const parentOperator = foundRootAbove
|
|
97
|
+
? firstValueInAncestorChain(path, "operator")
|
|
98
|
+
: "";
|
|
99
|
+
|
|
100
|
+
node[OPERATOR_PRECEDENCE] = operatorPrecedence[node.operator];
|
|
101
|
+
|
|
102
|
+
const printedLeft = path.call(print, "left");
|
|
103
|
+
const printedRight = path.call(print, "right");
|
|
104
|
+
|
|
105
|
+
const parts = [];
|
|
106
|
+
const leftNeedsParens = otherNeedsParentheses(node, "left");
|
|
107
|
+
const rightNeedsParens = otherNeedsParentheses(node, "right");
|
|
108
|
+
|
|
109
|
+
if (leftNeedsParens) {
|
|
110
|
+
parts.push("(");
|
|
111
|
+
}
|
|
112
|
+
parts.push(printedLeft);
|
|
113
|
+
if (leftNeedsParens) {
|
|
114
|
+
parts.push(")");
|
|
115
|
+
}
|
|
116
|
+
const potentiallyIndented = [
|
|
117
|
+
whitespaceAroundOperator ? line : softline,
|
|
118
|
+
node.operator,
|
|
119
|
+
whitespaceAroundOperator ? " " : ""
|
|
120
|
+
];
|
|
121
|
+
if (rightNeedsParens) {
|
|
122
|
+
potentiallyIndented.push("(");
|
|
123
|
+
}
|
|
124
|
+
potentiallyIndented.push(printedRight);
|
|
125
|
+
if (rightNeedsParens) {
|
|
126
|
+
potentiallyIndented.push(")");
|
|
127
|
+
}
|
|
128
|
+
const rightHandSide = alreadyIndented
|
|
129
|
+
? potentiallyIndented
|
|
130
|
+
: indent(potentiallyIndented);
|
|
131
|
+
const result = wrapExpressionIfNeeded(
|
|
132
|
+
path,
|
|
133
|
+
[...parts, rightHandSide],
|
|
134
|
+
node
|
|
135
|
+
);
|
|
136
|
+
const shouldCreateTopLevelGroup = !foundRootAbove && shouldGroupOnTopLevel;
|
|
137
|
+
const isDifferentLogicalOperator =
|
|
138
|
+
isLogicalOperator && node.operator !== parentOperator;
|
|
139
|
+
|
|
140
|
+
const shouldGroupResult =
|
|
141
|
+
shouldCreateTopLevelGroup ||
|
|
142
|
+
!isLogicalOperator ||
|
|
143
|
+
(foundRootAbove && isDifferentLogicalOperator);
|
|
144
|
+
|
|
145
|
+
return shouldGroupResult ? group(result) : result;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const p = (node, path, print, options) => {
|
|
149
|
+
if (Node.isBinaryConcatExpression(node) && node.wasImplicitConcatenation) {
|
|
150
|
+
return printInterpolatedString(node, path, print, options);
|
|
151
|
+
}
|
|
152
|
+
return printBinaryExpression(node, path, print);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export { p as printBinaryExpression };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import { EXPRESSION_NEEDED, printChildBlock } from "../util/index.js";
|
|
4
|
+
|
|
5
|
+
const { hardline, group } = doc.builders;
|
|
6
|
+
|
|
7
|
+
const p = (node, path, print, options) => {
|
|
8
|
+
node[EXPRESSION_NEEDED] = false;
|
|
9
|
+
const hasChildren = Array.isArray(node.body);
|
|
10
|
+
const printEndblockName = options.twigOutputEndblockName === true;
|
|
11
|
+
|
|
12
|
+
if (hasChildren) {
|
|
13
|
+
const blockName = path.call(print, "name");
|
|
14
|
+
const opener = [
|
|
15
|
+
node.trimLeft ? "{%-" : "{%",
|
|
16
|
+
" block ",
|
|
17
|
+
blockName,
|
|
18
|
+
node.trimRightBlock ? " -%}" : " %}"
|
|
19
|
+
];
|
|
20
|
+
const parts = [opener];
|
|
21
|
+
if (node.body.length > 0) {
|
|
22
|
+
const indentedBody = printChildBlock(node, path, print, "body");
|
|
23
|
+
parts.push(indentedBody);
|
|
24
|
+
}
|
|
25
|
+
parts.push(hardline);
|
|
26
|
+
parts.push(
|
|
27
|
+
node.trimLeftEndblock ? "{%-" : "{%",
|
|
28
|
+
" endblock",
|
|
29
|
+
printEndblockName ? [" ", blockName] : "",
|
|
30
|
+
node.trimRight ? " -%}" : " %}"
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const result = group(parts);
|
|
34
|
+
return result;
|
|
35
|
+
} else if (Node.isPrintExpressionStatement(node.body)) {
|
|
36
|
+
const parts = [
|
|
37
|
+
node.trimLeft ? "{%-" : "{%",
|
|
38
|
+
" block ",
|
|
39
|
+
path.call(print, "name"),
|
|
40
|
+
" ",
|
|
41
|
+
path.call(print, "body", "value"),
|
|
42
|
+
node.trimRight ? " -%}" : " %}"
|
|
43
|
+
];
|
|
44
|
+
return parts;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { p as printBlockStatement };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
EXPRESSION_NEEDED,
|
|
5
|
+
STRING_NEEDS_QUOTES,
|
|
6
|
+
wrapExpressionIfNeeded
|
|
7
|
+
} from "../util/index.js";
|
|
8
|
+
|
|
9
|
+
const { group, softline, line, indent, join } = doc.builders;
|
|
10
|
+
|
|
11
|
+
const p = (node, path, print) => {
|
|
12
|
+
node[EXPRESSION_NEEDED] = false;
|
|
13
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
14
|
+
const mappedArguments = path.map(print, "arguments");
|
|
15
|
+
const parts = [path.call(print, "callee"), "("];
|
|
16
|
+
if (node.arguments.length === 0) {
|
|
17
|
+
parts.push(")");
|
|
18
|
+
} else if (
|
|
19
|
+
node.arguments.length === 1 &&
|
|
20
|
+
Node.isObjectExpression(node.arguments[0])
|
|
21
|
+
) {
|
|
22
|
+
// Optimization: No line break between "(" and "{" if
|
|
23
|
+
// there is exactly one object parameter
|
|
24
|
+
parts.push(mappedArguments[0], ")");
|
|
25
|
+
} else {
|
|
26
|
+
parts.push(
|
|
27
|
+
indent([softline, join([",", line], mappedArguments)]),
|
|
28
|
+
softline,
|
|
29
|
+
")"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
34
|
+
|
|
35
|
+
return group(parts);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { p as printCallExpression };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
EXPRESSION_NEEDED,
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
wrapExpressionIfNeeded
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const { line, indent, group } = doc.builders;
|
|
9
|
+
|
|
10
|
+
const p = (node, path, print) => {
|
|
11
|
+
node[EXPRESSION_NEEDED] = false;
|
|
12
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
13
|
+
|
|
14
|
+
const rest = [line, "?"];
|
|
15
|
+
if (node.consequent) {
|
|
16
|
+
rest.push([" ", path.call(print, "consequent")]);
|
|
17
|
+
}
|
|
18
|
+
if (node.alternate) {
|
|
19
|
+
rest.push(line, ": ", path.call(print, "alternate"));
|
|
20
|
+
}
|
|
21
|
+
const parts = [path.call(print, "test"), indent(rest)];
|
|
22
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
23
|
+
|
|
24
|
+
return group(parts);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { p as printConditionalExpression };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES, OVERRIDE_QUOTE_CHAR } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { fill, join } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print) => {
|
|
7
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
8
|
+
node[OVERRIDE_QUOTE_CHAR] = '"';
|
|
9
|
+
const start = "<!" + (node.declarationType || "").toUpperCase();
|
|
10
|
+
const printedParts = path.map(print, "parts");
|
|
11
|
+
|
|
12
|
+
return fill([start, " ", join(" ", printedParts), ">"]);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { p as printDeclaration };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
removeSurroundingWhitespace,
|
|
4
|
+
isInlineElement,
|
|
5
|
+
printChildGroups,
|
|
6
|
+
EXPRESSION_NEEDED,
|
|
7
|
+
STRING_NEEDS_QUOTES
|
|
8
|
+
} from "../util/index.js";
|
|
9
|
+
|
|
10
|
+
const { group, line, hardline, softline, indent, join } = doc.builders;
|
|
11
|
+
|
|
12
|
+
const printOpeningTag = (node, path, print) => {
|
|
13
|
+
const opener = "<" + node.name;
|
|
14
|
+
const printedAttributes = printSeparatedList(path, print, "", "attributes");
|
|
15
|
+
const openingTagEnd = node.selfClosing ? " />" : ">";
|
|
16
|
+
const hasAttributes = node.attributes && node.attributes.length > 0;
|
|
17
|
+
|
|
18
|
+
if (hasAttributes) {
|
|
19
|
+
return [opener, indent([" ", printedAttributes]), openingTagEnd];
|
|
20
|
+
}
|
|
21
|
+
return [opener, openingTagEnd];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const printSeparatedList = (path, print, separator, attrName) => {
|
|
25
|
+
return join([separator, line], path.map(print, attrName));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const p = (node, path, print) => {
|
|
29
|
+
// Set a flag in case attributes contain, e.g., a FilterExpression
|
|
30
|
+
node[EXPRESSION_NEEDED] = true;
|
|
31
|
+
const openingGroup = group(printOpeningTag(node, path, print));
|
|
32
|
+
node[EXPRESSION_NEEDED] = false;
|
|
33
|
+
node[STRING_NEEDS_QUOTES] = false;
|
|
34
|
+
|
|
35
|
+
if (!node.selfClosing) {
|
|
36
|
+
node.children = removeSurroundingWhitespace(node.children);
|
|
37
|
+
|
|
38
|
+
const childGroups = printChildGroups(node, path, print, "children");
|
|
39
|
+
const closingTag = ["</", node.name, ">"];
|
|
40
|
+
const result = [openingGroup];
|
|
41
|
+
const joinedChildren = childGroups;
|
|
42
|
+
if (isInlineElement(node)) {
|
|
43
|
+
result.push(indent([softline, joinedChildren]), softline);
|
|
44
|
+
} else {
|
|
45
|
+
const childBlock = [];
|
|
46
|
+
if (childGroups.length > 0) {
|
|
47
|
+
childBlock.push(hardline);
|
|
48
|
+
}
|
|
49
|
+
childBlock.push(joinedChildren);
|
|
50
|
+
result.push(indent(childBlock));
|
|
51
|
+
if (childGroups.length > 0) {
|
|
52
|
+
result.push(hardline);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
result.push(closingTag);
|
|
56
|
+
|
|
57
|
+
return isInlineElement(node) ? group(result) : result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return openingGroup;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { p as printElement };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
EXPRESSION_NEEDED,
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
printChildBlock
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const { indent, hardline, line, group } = doc.builders;
|
|
9
|
+
|
|
10
|
+
const printOpener = (node, path, print) => {
|
|
11
|
+
node[EXPRESSION_NEEDED] = false;
|
|
12
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
13
|
+
const parts = [
|
|
14
|
+
node.trimLeft ? "{%-" : "{%",
|
|
15
|
+
" embed ",
|
|
16
|
+
path.call(print, "parent")
|
|
17
|
+
];
|
|
18
|
+
if (node.argument) {
|
|
19
|
+
parts.push(indent([line, "with ", path.call(print, "argument")]));
|
|
20
|
+
}
|
|
21
|
+
parts.push([line, node.trimRightEmbed ? "-%}" : "%}"]);
|
|
22
|
+
return group(parts);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const p = (node, path, print) => {
|
|
26
|
+
const children = printChildBlock(node, path, print, "blocks");
|
|
27
|
+
const printedOpener = printOpener(node, path, print);
|
|
28
|
+
const closing = [
|
|
29
|
+
hardline,
|
|
30
|
+
node.trimLeftEndembed ? "{%-" : "{%",
|
|
31
|
+
" endembed ",
|
|
32
|
+
node.trimRight ? "-%}" : "%}"
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
return [printedOpener, children, closing];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { p as printEmbedStatement };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
EXPRESSION_NEEDED,
|
|
5
|
+
STRING_NEEDS_QUOTES,
|
|
6
|
+
isContractableNodeType
|
|
7
|
+
} from "../util/index.js";
|
|
8
|
+
|
|
9
|
+
const { group, indent, line } = doc.builders;
|
|
10
|
+
|
|
11
|
+
const p = (node, path, print) => {
|
|
12
|
+
node[EXPRESSION_NEEDED] = false;
|
|
13
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
14
|
+
const opener = node.trimLeft ? "{{-" : "{{";
|
|
15
|
+
const closing = node.trimRight ? "-}}" : "}}";
|
|
16
|
+
const shouldContractValue =
|
|
17
|
+
isContractableNodeType(node.value) &&
|
|
18
|
+
!Node.isObjectExpression(node.value);
|
|
19
|
+
const padding = shouldContractValue ? " " : line;
|
|
20
|
+
const printedValue = [padding, path.call(print, "value")];
|
|
21
|
+
const value = shouldContractValue ? printedValue : indent(printedValue);
|
|
22
|
+
return group([opener, value, padding, closing]);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { p as printExpressionStatement };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
2
|
+
|
|
3
|
+
const p = (node, path, print) => {
|
|
4
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
5
|
+
return [
|
|
6
|
+
node.trimLeft ? "{%-" : "{%",
|
|
7
|
+
" extends ",
|
|
8
|
+
path.call(print, "parentName"),
|
|
9
|
+
node.trimRight ? " -%}" : " %}"
|
|
10
|
+
];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { p as printExtendsStatement };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { FILTER_BLOCK, printChildBlock } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, line, hardline } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const printOpeningGroup = (node, path, print) => {
|
|
7
|
+
const parts = [node.trimLeft ? "{%- " : "{% "];
|
|
8
|
+
const printedExpression = path.call(print, "filterExpression");
|
|
9
|
+
parts.push(printedExpression, line, node.trimRightFilter ? "-%}" : "%}");
|
|
10
|
+
return group(parts);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const p = (node, path, print) => {
|
|
14
|
+
node[FILTER_BLOCK] = true;
|
|
15
|
+
const openingGroup = printOpeningGroup(node, path, print);
|
|
16
|
+
const body = printChildBlock(node, path, print, "body");
|
|
17
|
+
const closingStatement = [
|
|
18
|
+
hardline,
|
|
19
|
+
node.trimLeftEndfilter ? "{%-" : "{%",
|
|
20
|
+
" endfilter ",
|
|
21
|
+
node.trimRight ? "-%}" : "%}"
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
return [openingGroup, body, closingStatement];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { p as printFilterBlockStatement };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
EXPRESSION_NEEDED,
|
|
5
|
+
INSIDE_OF_STRING,
|
|
6
|
+
STRING_NEEDS_QUOTES,
|
|
7
|
+
FILTER_BLOCK,
|
|
8
|
+
shouldExpressionsBeWrapped,
|
|
9
|
+
wrapInStringInterpolation,
|
|
10
|
+
someParentNode,
|
|
11
|
+
isMultipartExpression,
|
|
12
|
+
getDeepProperty
|
|
13
|
+
} from "../util/index.js";
|
|
14
|
+
|
|
15
|
+
const { group, indent, line, softline, join } = doc.builders;
|
|
16
|
+
|
|
17
|
+
const isInFilterBlock = path =>
|
|
18
|
+
someParentNode(path, node => node[FILTER_BLOCK] === true);
|
|
19
|
+
|
|
20
|
+
const printArguments = (node, path, print, nodePath) => {
|
|
21
|
+
const hasArguments = node.arguments && node.arguments.length > 0;
|
|
22
|
+
if (!hasArguments) {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const printedArguments = path.map(print, ...nodePath, "arguments");
|
|
27
|
+
if (
|
|
28
|
+
node.arguments.length === 1 &&
|
|
29
|
+
Node.isObjectExpression(node.arguments[0])
|
|
30
|
+
) {
|
|
31
|
+
// Optimization: Avoid additional indentation level
|
|
32
|
+
return group(["(", printedArguments[0], ")"]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return group([
|
|
36
|
+
"(",
|
|
37
|
+
indent([softline, join([",", line], printedArguments)]),
|
|
38
|
+
softline,
|
|
39
|
+
")"
|
|
40
|
+
]);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const printOneFilterExpression = (node, path, print, nodePath) => {
|
|
44
|
+
const args = printArguments(node, path, print, nodePath);
|
|
45
|
+
const filterName = path.call(print, ...nodePath, "name");
|
|
46
|
+
return [filterName, args];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const joinFilters = (filterExpressions, space = "") => {
|
|
50
|
+
return join(
|
|
51
|
+
[space === "" ? softline : line, "|", space],
|
|
52
|
+
filterExpressions
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const p = (node, path, print, options) => {
|
|
57
|
+
let currentNode = node;
|
|
58
|
+
node[EXPRESSION_NEEDED] = false;
|
|
59
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
60
|
+
const spaceAroundPipe = options.twigFollowOfficialCodingStandards === false;
|
|
61
|
+
const space = spaceAroundPipe ? " " : "";
|
|
62
|
+
const pathToFinalTarget = ["target"];
|
|
63
|
+
let filterExpressions = [printOneFilterExpression(node, path, print, [])];
|
|
64
|
+
|
|
65
|
+
// Here, we do not do the usual recursion using path.call(), but
|
|
66
|
+
// instead traverse the chain of FilterExpressions ourselves (in
|
|
67
|
+
// case there are multiple chained FilterExpressions, that is).
|
|
68
|
+
// Reason: For a proper layout like this
|
|
69
|
+
// "Some text"
|
|
70
|
+
// | filter1
|
|
71
|
+
// | filter2(arg)
|
|
72
|
+
// | filter3
|
|
73
|
+
// we need all the individual filter expressions in one group. This
|
|
74
|
+
// can only be achieved by collecting them manually in the top-level
|
|
75
|
+
// FilterExpression node.
|
|
76
|
+
while (Node.isFilterExpression(currentNode.target)) {
|
|
77
|
+
filterExpressions.unshift(
|
|
78
|
+
printOneFilterExpression(
|
|
79
|
+
currentNode.target,
|
|
80
|
+
path,
|
|
81
|
+
print,
|
|
82
|
+
pathToFinalTarget
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
pathToFinalTarget.push("target"); // Go one level deeper
|
|
86
|
+
currentNode = currentNode.target;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const finalTarget = path.call(print, ...pathToFinalTarget);
|
|
90
|
+
const isFilterBlock = isInFilterBlock(path); // Special case of FilterBlockStatement
|
|
91
|
+
const targetNeedsParentheses = isMultipartExpression(
|
|
92
|
+
getDeepProperty(node, ...pathToFinalTarget)
|
|
93
|
+
);
|
|
94
|
+
const parts = [];
|
|
95
|
+
if (targetNeedsParentheses) {
|
|
96
|
+
parts.push("(");
|
|
97
|
+
}
|
|
98
|
+
parts.push(finalTarget);
|
|
99
|
+
if (targetNeedsParentheses) {
|
|
100
|
+
parts.push(")");
|
|
101
|
+
}
|
|
102
|
+
if (isFilterBlock) {
|
|
103
|
+
parts.push([" ", filterExpressions[0]]);
|
|
104
|
+
filterExpressions = filterExpressions.slice(1);
|
|
105
|
+
}
|
|
106
|
+
if (filterExpressions.length === 1) {
|
|
107
|
+
// No breaks and indentation for just one expression
|
|
108
|
+
parts.push(`${space}|${space}`, filterExpressions[0]);
|
|
109
|
+
} else if (filterExpressions.length > 1) {
|
|
110
|
+
const indentedFilters = [
|
|
111
|
+
spaceAroundPipe ? line : softline,
|
|
112
|
+
`|${space}`,
|
|
113
|
+
joinFilters(filterExpressions, space)
|
|
114
|
+
];
|
|
115
|
+
parts.push(indent(indentedFilters));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const kindOfWrap = shouldExpressionsBeWrapped(path);
|
|
119
|
+
if (kindOfWrap === EXPRESSION_NEEDED) {
|
|
120
|
+
// Instead of using wrapExpressionIfNeeded(), we manually
|
|
121
|
+
// wrap here, to avoid a line break between the curly braces
|
|
122
|
+
parts.push(" }}");
|
|
123
|
+
parts.unshift("{{ ");
|
|
124
|
+
} else if (kindOfWrap === INSIDE_OF_STRING) {
|
|
125
|
+
wrapInStringInterpolation(parts);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return group(parts);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export { p as printFilterExpression };
|