@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,58 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
EXPRESSION_NEEDED,
|
|
4
|
+
isWhitespaceNode,
|
|
5
|
+
indentWithHardline
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const { group, indent, line, hardline } = doc.builders;
|
|
9
|
+
|
|
10
|
+
const printFor = (node, path, print) => {
|
|
11
|
+
const parts = [node.trimLeft ? "{%-" : "{%", " for "];
|
|
12
|
+
if (node.keyTarget) {
|
|
13
|
+
parts.push(path.call(print, "keyTarget"), ", ");
|
|
14
|
+
}
|
|
15
|
+
parts.push(
|
|
16
|
+
path.call(print, "valueTarget"),
|
|
17
|
+
" in ",
|
|
18
|
+
path.call(print, "sequence")
|
|
19
|
+
);
|
|
20
|
+
if (node.condition) {
|
|
21
|
+
parts.push(indent([line, "if ", path.call(print, "condition")]));
|
|
22
|
+
}
|
|
23
|
+
parts.push([" ", node.trimRightFor ? "-%}" : "%}"]);
|
|
24
|
+
return group(parts);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const p = (node, path, print) => {
|
|
28
|
+
node[EXPRESSION_NEEDED] = false;
|
|
29
|
+
const parts = [printFor(node, path, print)];
|
|
30
|
+
const isBodyEmpty =
|
|
31
|
+
node.body.expressions.length === 0 ||
|
|
32
|
+
(node.body.expressions.length === 1 &&
|
|
33
|
+
isWhitespaceNode(node.body.expressions[0]));
|
|
34
|
+
const printedChildren = path.call(print, "body");
|
|
35
|
+
if (!isBodyEmpty || node.otherwise) {
|
|
36
|
+
parts.push(indentWithHardline(printedChildren));
|
|
37
|
+
}
|
|
38
|
+
if (node.otherwise) {
|
|
39
|
+
parts.push(
|
|
40
|
+
hardline,
|
|
41
|
+
node.trimLeftElse ? "{%-" : "{%",
|
|
42
|
+
" else ",
|
|
43
|
+
node.trimRightElse ? "-%}" : "%}"
|
|
44
|
+
);
|
|
45
|
+
const printedOtherwise = path.call(print, "otherwise");
|
|
46
|
+
parts.push(indentWithHardline(printedOtherwise));
|
|
47
|
+
}
|
|
48
|
+
parts.push(
|
|
49
|
+
isBodyEmpty ? "" : hardline,
|
|
50
|
+
node.trimLeftEndfor ? "{%-" : "{%",
|
|
51
|
+
" endfor ",
|
|
52
|
+
node.trimRight ? "-%}" : "%}"
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return parts;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { p as printForStatement };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, join, line, indent } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const printImportDeclaration = node => {
|
|
7
|
+
const parts = [node.key.name];
|
|
8
|
+
if (node.key.name !== node.alias.name) {
|
|
9
|
+
parts.push(" as ", node.alias.name);
|
|
10
|
+
}
|
|
11
|
+
return parts;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const p = (node, path, print) => {
|
|
15
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
16
|
+
// Unfortunately, ImportDeclaration has different
|
|
17
|
+
// formatting needs here compared to when used
|
|
18
|
+
// standalone. Therefore, we collect them manually.
|
|
19
|
+
const mappedImports = node.imports.map(printImportDeclaration);
|
|
20
|
+
const indentedParts = indent([line, join([",", line], mappedImports)]);
|
|
21
|
+
return group([
|
|
22
|
+
node.trimLeft ? "{%-" : "{%",
|
|
23
|
+
" from ",
|
|
24
|
+
path.call(print, "source"),
|
|
25
|
+
" import",
|
|
26
|
+
indentedParts,
|
|
27
|
+
line,
|
|
28
|
+
node.trimRight ? "-%}" : "%}"
|
|
29
|
+
]);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { p as printFromStatement };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
indentWithHardline,
|
|
6
|
+
printSingleTwigTag,
|
|
7
|
+
isEmptySequence
|
|
8
|
+
} from "../util/index.js";
|
|
9
|
+
|
|
10
|
+
const { hardline } = doc.builders;
|
|
11
|
+
|
|
12
|
+
const p = (node, path, print) => {
|
|
13
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
14
|
+
const openingTag = printSingleTwigTag(node, path, print);
|
|
15
|
+
const parts = [openingTag];
|
|
16
|
+
const printedSections = path.map(print, "sections");
|
|
17
|
+
node.sections.forEach((section, i) => {
|
|
18
|
+
if (Node.isGenericTwigTag(section)) {
|
|
19
|
+
parts.push([hardline, printedSections[i]]);
|
|
20
|
+
} else {
|
|
21
|
+
if (!isEmptySequence(section)) {
|
|
22
|
+
// Indent
|
|
23
|
+
parts.push(indentWithHardline(printedSections[i]));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return parts;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { p as printGenericTwigTag };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
createTextGroups,
|
|
4
|
+
stripHtmlCommentChars,
|
|
5
|
+
normalizeHtmlComment,
|
|
6
|
+
countNewlines
|
|
7
|
+
} from "../util/index.js";
|
|
8
|
+
|
|
9
|
+
const { join, indent, hardline } = doc.builders;
|
|
10
|
+
|
|
11
|
+
const p = (node, path, print) => {
|
|
12
|
+
const commentText = stripHtmlCommentChars(node.value.value || "");
|
|
13
|
+
|
|
14
|
+
const numNewlines = countNewlines(commentText);
|
|
15
|
+
if (numNewlines === 0) {
|
|
16
|
+
return normalizeHtmlComment(commentText);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return ["<!-- ", commentText, " -->"];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { p as printHtmlComment };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { EXPRESSION_NEEDED, wrapExpressionIfNeeded } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path) => {
|
|
7
|
+
node[EXPRESSION_NEEDED] = false;
|
|
8
|
+
|
|
9
|
+
const parts = [node.name];
|
|
10
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
11
|
+
const result = parts;
|
|
12
|
+
return parts.length === 1 ? result : group(result);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { p as printIdentifier };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
printChildBlock,
|
|
5
|
+
hasNoNewlines,
|
|
6
|
+
EXPRESSION_NEEDED,
|
|
7
|
+
PRESERVE_LEADING_WHITESPACE,
|
|
8
|
+
PRESERVE_TRAILING_WHITESPACE
|
|
9
|
+
} from "../util/index.js";
|
|
10
|
+
|
|
11
|
+
const { group, indent, line, hardline } = doc.builders;
|
|
12
|
+
|
|
13
|
+
const IS_ELSEIF = Symbol("IS_ELSEIF");
|
|
14
|
+
|
|
15
|
+
const p = (node, path, print) => {
|
|
16
|
+
node[EXPRESSION_NEEDED] = false;
|
|
17
|
+
const hasElseBranch =
|
|
18
|
+
Array.isArray(node.alternate) && node.alternate.length > 0;
|
|
19
|
+
const hasElseIfBranch = Node.isIfStatement(node.alternate);
|
|
20
|
+
const isElseIf = node[IS_ELSEIF] === true;
|
|
21
|
+
const isEmptyIf = node.consequent.length === 0;
|
|
22
|
+
const hasOneChild = node.consequent.length === 1;
|
|
23
|
+
const firstChild = node.consequent[0];
|
|
24
|
+
const printInline =
|
|
25
|
+
!isElseIf &&
|
|
26
|
+
!node.alternate &&
|
|
27
|
+
(isEmptyIf ||
|
|
28
|
+
(hasOneChild &&
|
|
29
|
+
!Node.isElement(firstChild) &&
|
|
30
|
+
(!Node.isPrintTextStatement(firstChild) ||
|
|
31
|
+
hasNoNewlines(firstChild.value.value))));
|
|
32
|
+
|
|
33
|
+
// Preserve no-newline white space in single text node child
|
|
34
|
+
if (
|
|
35
|
+
hasOneChild &&
|
|
36
|
+
Node.isPrintTextStatement(firstChild) &&
|
|
37
|
+
hasNoNewlines(firstChild.value.value)
|
|
38
|
+
) {
|
|
39
|
+
firstChild[PRESERVE_LEADING_WHITESPACE] = true;
|
|
40
|
+
firstChild[PRESERVE_TRAILING_WHITESPACE] = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ifClause = group([
|
|
44
|
+
node.trimLeft ? "{%- " : "{% ",
|
|
45
|
+
isElseIf ? "elseif" : "if",
|
|
46
|
+
indent([line, path.call(print, "test")]),
|
|
47
|
+
" ",
|
|
48
|
+
node.trimRightIf ? "-%}" : "%}"
|
|
49
|
+
]);
|
|
50
|
+
const ifBody = printInline
|
|
51
|
+
? isEmptyIf
|
|
52
|
+
? ""
|
|
53
|
+
: path.call(print, "consequent", "0")
|
|
54
|
+
: printChildBlock(node, path, print, "consequent");
|
|
55
|
+
const parts = [ifClause, ifBody];
|
|
56
|
+
if (hasElseBranch) {
|
|
57
|
+
parts.push(
|
|
58
|
+
hardline,
|
|
59
|
+
node.trimLeftElse ? "{%-" : "{%",
|
|
60
|
+
" else ",
|
|
61
|
+
node.trimRightElse ? "-%}" : "%}"
|
|
62
|
+
);
|
|
63
|
+
parts.push(printChildBlock(node, path, print, "alternate"));
|
|
64
|
+
} else if (hasElseIfBranch) {
|
|
65
|
+
node.alternate[IS_ELSEIF] = true;
|
|
66
|
+
parts.push(hardline);
|
|
67
|
+
parts.push(path.call(print, "alternate"));
|
|
68
|
+
}
|
|
69
|
+
// The {% endif %} will be taken care of by the "root" if statement
|
|
70
|
+
if (!isElseIf) {
|
|
71
|
+
parts.push(
|
|
72
|
+
printInline ? "" : hardline,
|
|
73
|
+
node.trimLeftEndif ? "{%-" : "{%",
|
|
74
|
+
" endif ",
|
|
75
|
+
node.trimRight ? "-%}" : "%}"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return parts;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export { p as printIfStatement };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, line, indent } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print) => {
|
|
7
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
8
|
+
return group([
|
|
9
|
+
node.trimLeft ? "{%-" : "{%",
|
|
10
|
+
" import ",
|
|
11
|
+
path.call(print, "key"),
|
|
12
|
+
indent([line, "as ", path.call(print, "alias")]),
|
|
13
|
+
line,
|
|
14
|
+
node.trimRight ? "-%}" : "%}"
|
|
15
|
+
]);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { p as printImportDeclaration };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print) => {
|
|
7
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
8
|
+
const parts = [
|
|
9
|
+
node.trimLeft ? "{%-" : "{%",
|
|
10
|
+
" include ",
|
|
11
|
+
path.call(print, "source")
|
|
12
|
+
];
|
|
13
|
+
if (node.argument) {
|
|
14
|
+
const printedArguments = path.call(print, "argument");
|
|
15
|
+
parts.push(" with ");
|
|
16
|
+
parts.push(printedArguments);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (node.contextFree) {
|
|
20
|
+
parts.push(" only");
|
|
21
|
+
}
|
|
22
|
+
parts.push(node.trimRight ? " -%}" : " %}");
|
|
23
|
+
return group(parts);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { p as printIncludeStatement };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
|
|
3
|
+
const { group, join, line, softline, hardline, indent } = doc.builders;
|
|
4
|
+
|
|
5
|
+
const printOpener = (node, path, print) => {
|
|
6
|
+
const parts = [
|
|
7
|
+
node.trimLeft ? "{%-" : "{%",
|
|
8
|
+
" macro ",
|
|
9
|
+
path.call(print, "name"),
|
|
10
|
+
"("
|
|
11
|
+
];
|
|
12
|
+
const mappedArguments = path.map(print, "arguments");
|
|
13
|
+
const joinedArguments = join([",", line], mappedArguments);
|
|
14
|
+
parts.push(indent([softline, joinedArguments]));
|
|
15
|
+
parts.push(")", line, node.trimRightMacro ? "-%}" : "%}");
|
|
16
|
+
return group(parts);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const p = (node, path, print) => {
|
|
20
|
+
const parts = [printOpener(node, path, print)];
|
|
21
|
+
parts.push(indent([hardline, path.call(print, "body")]));
|
|
22
|
+
parts.push(
|
|
23
|
+
hardline,
|
|
24
|
+
node.trimLeftEndmacro ? "{%-" : "{%",
|
|
25
|
+
" endmacro ",
|
|
26
|
+
node.trimRight ? "-%}" : "%}"
|
|
27
|
+
);
|
|
28
|
+
return parts;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { p as printMacroDeclarationStatement };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
EXPRESSION_NEEDED,
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
wrapExpressionIfNeeded
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const { group } = doc.builders;
|
|
9
|
+
|
|
10
|
+
const p = (node, path, print) => {
|
|
11
|
+
node[EXPRESSION_NEEDED] = false;
|
|
12
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
13
|
+
const parts = [path.call(print, "object")];
|
|
14
|
+
parts.push(node.computed ? "[" : ".");
|
|
15
|
+
parts.push(path.call(print, "property"));
|
|
16
|
+
if (node.computed) {
|
|
17
|
+
parts.push("]");
|
|
18
|
+
}
|
|
19
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
20
|
+
return group(parts);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { p as printMemberExpression };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { EXPRESSION_NEEDED, STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, indent, line, hardline } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const formatDelay = delay => {
|
|
7
|
+
return "" + delay / 1000 + "s";
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const buildOpener = (node, path, print) => {
|
|
11
|
+
const result = [];
|
|
12
|
+
const firstGroup = [node.trimLeft ? "{%-" : "{%", " mount"];
|
|
13
|
+
if (node.async === true) {
|
|
14
|
+
firstGroup.push(" async");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (node.name) {
|
|
18
|
+
firstGroup.push(" ", path.call(print, "name"));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (node.name && node.source) {
|
|
22
|
+
firstGroup.push(" from");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (node.source) {
|
|
26
|
+
firstGroup.push(" ", path.call(print, "source"));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (node.key) {
|
|
30
|
+
firstGroup.push(indent([line, "as ", path.call(print, "key")]));
|
|
31
|
+
}
|
|
32
|
+
result.push(group(firstGroup));
|
|
33
|
+
if (node.argument) {
|
|
34
|
+
result.push(indent([" with ", path.call(print, "argument")]));
|
|
35
|
+
}
|
|
36
|
+
if (node.delayBy) {
|
|
37
|
+
result.push(
|
|
38
|
+
indent([line, "delay placeholder by ", formatDelay(node.delayBy)])
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const trimRightMount =
|
|
42
|
+
node.body || node.otherwise ? node.trimRightMount : node.trimRight;
|
|
43
|
+
result.push([line, trimRightMount ? "-%}" : "%}"]);
|
|
44
|
+
return group(result);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const buildBody = (path, print) => {
|
|
48
|
+
return indent([hardline, path.call(print, "body")]);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const buildErrorHandling = (node, path, print) => {
|
|
52
|
+
const parts = [];
|
|
53
|
+
parts.push([hardline, node.trimLeftCatch ? "{%-" : "{%", " catch "]);
|
|
54
|
+
if (node.errorVariableName) {
|
|
55
|
+
parts.push(path.call(print, "errorVariableName"), " ");
|
|
56
|
+
}
|
|
57
|
+
parts.push(node.trimRightCatch ? "-%}" : "%}");
|
|
58
|
+
parts.push(indent([hardline, path.call(print, "otherwise")]));
|
|
59
|
+
return parts;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const p = (node, path, print) => {
|
|
63
|
+
node[EXPRESSION_NEEDED] = false;
|
|
64
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
65
|
+
const parts = [buildOpener(node, path, print)];
|
|
66
|
+
if (node.body) {
|
|
67
|
+
parts.push(buildBody(path, print));
|
|
68
|
+
}
|
|
69
|
+
if (node.otherwise) {
|
|
70
|
+
parts.push(buildErrorHandling(node, path, print));
|
|
71
|
+
}
|
|
72
|
+
if (node.body || node.otherwise) {
|
|
73
|
+
parts.push([
|
|
74
|
+
hardline,
|
|
75
|
+
node.trimLeftEndmount ? "{%-" : "{%",
|
|
76
|
+
" endmount ",
|
|
77
|
+
node.trimRight ? "-%}" : "%}"
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return parts;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { p as printMountStatement };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
2
|
+
|
|
3
|
+
const p = (node, path, print) => {
|
|
4
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
5
|
+
const printedName = path.call(print, "name");
|
|
6
|
+
const printedValue = path.call(print, "value");
|
|
7
|
+
return [printedName, " = ", printedValue];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { p as printNamedArgumentExpression };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { EXPRESSION_NEEDED, wrapExpressionIfNeeded } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, line, hardline, indent, join } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print, options) => {
|
|
7
|
+
if (node.properties.length === 0) {
|
|
8
|
+
return "{}";
|
|
9
|
+
}
|
|
10
|
+
node[EXPRESSION_NEEDED] = false;
|
|
11
|
+
const mappedElements = path.map(print, "properties");
|
|
12
|
+
const separator = options.twigAlwaysBreakObjects ? hardline : line;
|
|
13
|
+
const indentedContent = [line, join([",", separator], mappedElements)];
|
|
14
|
+
|
|
15
|
+
const parts = ["{", indent(indentedContent), separator, "}"];
|
|
16
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
17
|
+
|
|
18
|
+
return group(parts);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { p as printObjectExpression };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Node } from "melody-types";
|
|
2
|
+
import { isValidIdentifierName, STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const p = (node, path, print, options) => {
|
|
5
|
+
node[STRING_NEEDS_QUOTES] =
|
|
6
|
+
!node.computed &&
|
|
7
|
+
Node.isStringLiteral(node.key) &&
|
|
8
|
+
!isValidIdentifierName(node.key.value);
|
|
9
|
+
const shouldPrintKeyAsString = node.key.wasImplicitConcatenation;
|
|
10
|
+
const needsParentheses = node.computed && !shouldPrintKeyAsString;
|
|
11
|
+
const parts = [];
|
|
12
|
+
if (needsParentheses) {
|
|
13
|
+
parts.push("(");
|
|
14
|
+
}
|
|
15
|
+
parts.push(path.call(print, "key"));
|
|
16
|
+
if (needsParentheses) {
|
|
17
|
+
parts.push(")");
|
|
18
|
+
}
|
|
19
|
+
parts.push(": ");
|
|
20
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
21
|
+
parts.push(path.call(print, "value"));
|
|
22
|
+
return parts;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { p as printObjectProperty };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
removeSurroundingWhitespace,
|
|
4
|
+
printChildGroups,
|
|
5
|
+
isRootNode,
|
|
6
|
+
STRING_NEEDS_QUOTES
|
|
7
|
+
} from "../util/index.js";
|
|
8
|
+
|
|
9
|
+
const { hardline } = doc.builders;
|
|
10
|
+
|
|
11
|
+
const p = (node, path, print) => {
|
|
12
|
+
node[STRING_NEEDS_QUOTES] = false;
|
|
13
|
+
node.expressions = removeSurroundingWhitespace(node.expressions);
|
|
14
|
+
const items = printChildGroups(node, path, print, "expressions");
|
|
15
|
+
if (isRootNode(path)) {
|
|
16
|
+
return [...items, hardline];
|
|
17
|
+
}
|
|
18
|
+
return items;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { p as printSequenceExpression };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
printChildBlock,
|
|
5
|
+
isNotExpression,
|
|
6
|
+
STRING_NEEDS_QUOTES,
|
|
7
|
+
GROUP_TOP_LEVEL_LOGICAL
|
|
8
|
+
} from "../util/index.js";
|
|
9
|
+
|
|
10
|
+
const { group, line, hardline } = doc.builders;
|
|
11
|
+
|
|
12
|
+
const shouldAvoidBreakBeforeClosing = valueNode =>
|
|
13
|
+
Node.isObjectExpression(valueNode) ||
|
|
14
|
+
isNotExpression(valueNode) ||
|
|
15
|
+
Node.isArrayExpression(valueNode);
|
|
16
|
+
|
|
17
|
+
const buildSetStatement = (node, path, print, assignmentIndex) => {
|
|
18
|
+
const varDeclaration = node.assignments[assignmentIndex];
|
|
19
|
+
varDeclaration[GROUP_TOP_LEVEL_LOGICAL] = false;
|
|
20
|
+
const avoidBreakBeforeClosing = shouldAvoidBreakBeforeClosing(
|
|
21
|
+
varDeclaration.value
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return group([
|
|
25
|
+
node.trimLeft ? "{%-" : "{%",
|
|
26
|
+
" set ",
|
|
27
|
+
path.call(print, "assignments", assignmentIndex),
|
|
28
|
+
avoidBreakBeforeClosing ? " " : line,
|
|
29
|
+
node.trimRight ? "-%}" : "%}"
|
|
30
|
+
]);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const isEmbracingSet = node => {
|
|
34
|
+
return (
|
|
35
|
+
Array.isArray(node.assignments) &&
|
|
36
|
+
node.assignments.length === 1 &&
|
|
37
|
+
Array.isArray(node.assignments[0].value)
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const printRegularSet = (node, path, print) => {
|
|
42
|
+
const parts = [];
|
|
43
|
+
const hasAssignments =
|
|
44
|
+
Array.isArray(node.assignments) && node.assignments.length > 0;
|
|
45
|
+
if (hasAssignments) {
|
|
46
|
+
node.assignments.forEach((_, index) => {
|
|
47
|
+
if (parts.length > 0) {
|
|
48
|
+
parts.push(hardline);
|
|
49
|
+
}
|
|
50
|
+
parts.push(buildSetStatement(node, path, print, index));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return parts;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const printEmbracingSet = (node, path, print) => {
|
|
57
|
+
const parts = [
|
|
58
|
+
node.trimLeft ? "{%-" : "{%",
|
|
59
|
+
" set ",
|
|
60
|
+
path.call(print, "assignments", "0", "name"),
|
|
61
|
+
node.trimRightSet ? " -%}" : " %}"
|
|
62
|
+
];
|
|
63
|
+
node[STRING_NEEDS_QUOTES] = false;
|
|
64
|
+
const printedContents = printChildBlock(
|
|
65
|
+
node,
|
|
66
|
+
path,
|
|
67
|
+
print,
|
|
68
|
+
"assignments",
|
|
69
|
+
"0",
|
|
70
|
+
"value"
|
|
71
|
+
);
|
|
72
|
+
// const printedContents = path.map(print, "assignments", "0", "value");
|
|
73
|
+
parts.push(printedContents);
|
|
74
|
+
parts.push(
|
|
75
|
+
hardline,
|
|
76
|
+
node.trimLeftEndset ? "{%-" : "{%",
|
|
77
|
+
" endset ",
|
|
78
|
+
node.trimRight ? "-%}" : "%}"
|
|
79
|
+
);
|
|
80
|
+
return parts;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const p = (node, path, print) => {
|
|
84
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
85
|
+
if (isEmbracingSet(node)) {
|
|
86
|
+
return printEmbracingSet(node, path, print);
|
|
87
|
+
}
|
|
88
|
+
return printRegularSet(node, path, print);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { p as printSetStatement };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const p = (node, path, print) => {
|
|
2
|
+
const printedTarget = path.call(print, "target");
|
|
3
|
+
const printedStart = node.start ? path.call(print, "start") : "";
|
|
4
|
+
const printedEnd = node.end ? path.call(print, "end") : "";
|
|
5
|
+
return [printedTarget, "[", printedStart, ":", printedEnd, "]"];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export { p as printSliceExpression };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { printChildBlock } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { hardline, group } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print) => {
|
|
7
|
+
const parts = [
|
|
8
|
+
node.trimLeft ? "{%-" : "{%",
|
|
9
|
+
" spaceless ",
|
|
10
|
+
node.trimRightSpaceless ? "-%}" : "%}"
|
|
11
|
+
];
|
|
12
|
+
parts.push(printChildBlock(node, path, print, "body"));
|
|
13
|
+
parts.push(hardline);
|
|
14
|
+
parts.push(
|
|
15
|
+
node.trimLeftEndspaceless ? "{%-" : "{%",
|
|
16
|
+
" endspaceless ",
|
|
17
|
+
node.trimRight ? "-%}" : "%}"
|
|
18
|
+
);
|
|
19
|
+
const result = group(parts);
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { p as printSpacelessBlock };
|