@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,63 @@
|
|
|
1
|
+
import {
|
|
2
|
+
firstValueInAncestorChain,
|
|
3
|
+
quoteChar,
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
OVERRIDE_QUOTE_CHAR
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const isUnmaskedOccurrence = (s, pos) => {
|
|
9
|
+
return pos === 0 || s[pos - 1] !== "\\";
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const containsUnmasked = char => s => {
|
|
13
|
+
let pos = s.indexOf(char);
|
|
14
|
+
while (pos >= 0) {
|
|
15
|
+
if (isUnmaskedOccurrence(s, pos)) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
pos = s.indexOf(char, pos + 1);
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const containsUnmaskedSingleQuote = containsUnmasked("'");
|
|
24
|
+
const containsUnmaskedDoubleQuote = containsUnmasked('"');
|
|
25
|
+
|
|
26
|
+
const getQuoteChar = (s, options) => {
|
|
27
|
+
if (containsUnmaskedSingleQuote(s)) {
|
|
28
|
+
return '"';
|
|
29
|
+
}
|
|
30
|
+
if (containsUnmaskedDoubleQuote(s)) {
|
|
31
|
+
return "'";
|
|
32
|
+
}
|
|
33
|
+
return quoteChar(options);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const p = (node, path, print, options) => {
|
|
37
|
+
// The structure this string literal is part of
|
|
38
|
+
// determines if we need quotes or not
|
|
39
|
+
const needsQuotes = firstValueInAncestorChain(
|
|
40
|
+
path,
|
|
41
|
+
STRING_NEEDS_QUOTES,
|
|
42
|
+
false
|
|
43
|
+
);
|
|
44
|
+
// In case of a string with interpolations, only double quotes
|
|
45
|
+
// are allowed. This is then indicated by OVERRIDE_QUOTE_CHAR
|
|
46
|
+
// in an ancestor.
|
|
47
|
+
const overridingQuoteChar = firstValueInAncestorChain(
|
|
48
|
+
path,
|
|
49
|
+
OVERRIDE_QUOTE_CHAR,
|
|
50
|
+
null
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (needsQuotes) {
|
|
54
|
+
const quote = overridingQuoteChar
|
|
55
|
+
? overridingQuoteChar
|
|
56
|
+
: getQuoteChar(node.value, options);
|
|
57
|
+
return quote + node.value + quote;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return node.value;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { p as printStringLiteral };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
STRING_NEEDS_QUOTES,
|
|
5
|
+
printSingleTwigTag,
|
|
6
|
+
indentWithHardline,
|
|
7
|
+
isEmptySequence
|
|
8
|
+
} from "../util/index.js";
|
|
9
|
+
|
|
10
|
+
const { indent, 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
|
+
if (section.tagName === "endswitch") {
|
|
20
|
+
parts.push([hardline, printedSections[i]]);
|
|
21
|
+
} else {
|
|
22
|
+
parts.push(indentWithHardline(printedSections[i]));
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
if (!isEmptySequence(section)) {
|
|
26
|
+
// Indent twice
|
|
27
|
+
parts.push(indent(indentWithHardline(printedSections[i])));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return parts;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { p as printSwitchTag };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { findParentNode } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { softline, line, group, join, indent } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const textMap = {
|
|
7
|
+
TestNullExpression: "null",
|
|
8
|
+
TestDivisibleByExpression: "divisible by",
|
|
9
|
+
TestDefinedExpression: "defined",
|
|
10
|
+
TestEmptyExpression: "empty",
|
|
11
|
+
TestEvenExpression: "even",
|
|
12
|
+
TestOddExpression: "odd",
|
|
13
|
+
TestIterableExpression: "iterable",
|
|
14
|
+
TestSameAsExpression: "same as"
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const isNegator = node =>
|
|
18
|
+
node.constructor.name === "UnarySubclass" && node.operator === "not";
|
|
19
|
+
|
|
20
|
+
const p = (node, path, print) => {
|
|
21
|
+
const expressionType = node.__proto__.type;
|
|
22
|
+
const parts = [path.call(print, "expression"), " is "];
|
|
23
|
+
const parent = findParentNode(path);
|
|
24
|
+
const hasArguments =
|
|
25
|
+
Array.isArray(node.arguments) && node.arguments.length > 0;
|
|
26
|
+
if (isNegator(parent)) {
|
|
27
|
+
parts.push("not ");
|
|
28
|
+
}
|
|
29
|
+
if (!textMap[expressionType]) {
|
|
30
|
+
console.error(
|
|
31
|
+
"TestExpression: No text for " + expressionType + " defined"
|
|
32
|
+
);
|
|
33
|
+
} else {
|
|
34
|
+
parts.push(textMap[expressionType]);
|
|
35
|
+
}
|
|
36
|
+
if (hasArguments) {
|
|
37
|
+
const printedArguments = path.map(print, "arguments");
|
|
38
|
+
const joinedArguments = join([",", line], printedArguments);
|
|
39
|
+
parts.push(
|
|
40
|
+
group(["(", indent([softline, joinedArguments]), softline, ")"])
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return parts;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { p as printTestExpression };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import {
|
|
3
|
+
isWhitespaceOnly,
|
|
4
|
+
countNewlines,
|
|
5
|
+
createTextGroups,
|
|
6
|
+
PRESERVE_LEADING_WHITESPACE,
|
|
7
|
+
PRESERVE_TRAILING_WHITESPACE,
|
|
8
|
+
NEWLINES_ONLY
|
|
9
|
+
} from "../util/index.js";
|
|
10
|
+
|
|
11
|
+
const { line, join, hardline } = doc.builders;
|
|
12
|
+
|
|
13
|
+
const newlinesOnly = (s, preserveWhitespace = true) => {
|
|
14
|
+
const numNewlines = countNewlines(s);
|
|
15
|
+
if (numNewlines === 0) {
|
|
16
|
+
return preserveWhitespace ? line : "";
|
|
17
|
+
} else if (numNewlines === 1) {
|
|
18
|
+
return hardline;
|
|
19
|
+
}
|
|
20
|
+
return [hardline, hardline];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const p = (node, path, print) => {
|
|
24
|
+
// Check for special values that might have been
|
|
25
|
+
// computed during preprocessing
|
|
26
|
+
const preserveLeadingWhitespace =
|
|
27
|
+
node[PRESERVE_LEADING_WHITESPACE] === true;
|
|
28
|
+
const preserveTrailingWhitespace =
|
|
29
|
+
node[PRESERVE_TRAILING_WHITESPACE] === true;
|
|
30
|
+
|
|
31
|
+
const rawString = path.call(print, "value");
|
|
32
|
+
if (isWhitespaceOnly(rawString) && node[NEWLINES_ONLY]) {
|
|
33
|
+
return newlinesOnly(rawString);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const textGroups = createTextGroups(
|
|
37
|
+
rawString,
|
|
38
|
+
preserveLeadingWhitespace,
|
|
39
|
+
preserveTrailingWhitespace
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return join([hardline, hardline], textGroups);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { p as printTextStatement };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTextGroups,
|
|
3
|
+
stripTwigCommentChars,
|
|
4
|
+
normalizeTwigComment,
|
|
5
|
+
countNewlines
|
|
6
|
+
} from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
const p = node => {
|
|
9
|
+
const originalText = node.value.value || "";
|
|
10
|
+
const commentText = stripTwigCommentChars(originalText);
|
|
11
|
+
const trimLeft = originalText.length >= 3 ? originalText[2] === "-" : false;
|
|
12
|
+
const trimRight =
|
|
13
|
+
originalText.length >= 3 ? originalText.slice(-3, -2) === "-" : false;
|
|
14
|
+
|
|
15
|
+
const numNewlines = countNewlines(commentText);
|
|
16
|
+
if (numNewlines === 0) {
|
|
17
|
+
return normalizeTwigComment(commentText, trimLeft, trimRight);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return [trimLeft ? "{#-" : "{#", commentText, trimRight ? "-#}" : "#}"];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { p as printTwigComment };
|
|
@@ -0,0 +1,18 @@
|
|
|
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 = [node.operator, path.call(print, "argument")];
|
|
14
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
15
|
+
return group(parts);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { p as printUnaryExpression };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
firstValueInAncestorChain,
|
|
5
|
+
findParentNode,
|
|
6
|
+
isMultipartExpression,
|
|
7
|
+
IS_ROOT_LOGICAL_EXPRESSION,
|
|
8
|
+
GROUP_TOP_LEVEL_LOGICAL
|
|
9
|
+
} from "../util/index.js";
|
|
10
|
+
|
|
11
|
+
const { softline, indent, group } = doc.builders;
|
|
12
|
+
|
|
13
|
+
const argumentNeedsParentheses = node => isMultipartExpression(node);
|
|
14
|
+
|
|
15
|
+
const isLogicalOperator = operator => operator === "not";
|
|
16
|
+
|
|
17
|
+
const printLogicalExpression = (node, path, print) => {
|
|
18
|
+
const foundRootAbove = firstValueInAncestorChain(
|
|
19
|
+
path,
|
|
20
|
+
IS_ROOT_LOGICAL_EXPRESSION,
|
|
21
|
+
false
|
|
22
|
+
);
|
|
23
|
+
if (!foundRootAbove) {
|
|
24
|
+
node[IS_ROOT_LOGICAL_EXPRESSION] = true;
|
|
25
|
+
}
|
|
26
|
+
const parentNode = findParentNode(path);
|
|
27
|
+
const shouldGroupOnTopLevel = parentNode[GROUP_TOP_LEVEL_LOGICAL] !== false;
|
|
28
|
+
|
|
29
|
+
const parts = [node.operator, " "];
|
|
30
|
+
const needsParentheses = argumentNeedsParentheses(node.argument);
|
|
31
|
+
const printedArgument = path.call(print, "argument");
|
|
32
|
+
if (needsParentheses) {
|
|
33
|
+
parts.push("(", indent([softline, printedArgument]), [softline, ")"]);
|
|
34
|
+
} else {
|
|
35
|
+
parts.push(printedArgument);
|
|
36
|
+
}
|
|
37
|
+
const result = parts;
|
|
38
|
+
const shouldCreateTopLevelGroup = !foundRootAbove && shouldGroupOnTopLevel;
|
|
39
|
+
|
|
40
|
+
return shouldCreateTopLevelGroup ? group(result) : result;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const p = (node, path, print) => {
|
|
44
|
+
const parts = [];
|
|
45
|
+
// Example: a is not same as ... => Here, the "not" is printed "inline"
|
|
46
|
+
// Therefore, we do not output it here
|
|
47
|
+
const hasTestExpressionArgument = Node.isTestExpression(node.argument);
|
|
48
|
+
if (isLogicalOperator(node.operator) && !hasTestExpressionArgument) {
|
|
49
|
+
return printLogicalExpression(node, path, print);
|
|
50
|
+
}
|
|
51
|
+
if (!hasTestExpressionArgument) {
|
|
52
|
+
parts.push(node.operator, " ");
|
|
53
|
+
}
|
|
54
|
+
parts.push(path.call(print, "argument"));
|
|
55
|
+
return parts;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { p as printUnarySubclass };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
|
|
3
|
+
const { group, indent, join, line } = doc.builders;
|
|
4
|
+
|
|
5
|
+
const p = (node, path, print) => {
|
|
6
|
+
const docs = [
|
|
7
|
+
node.trimLeft ? "{%-" : "{%",
|
|
8
|
+
' use "',
|
|
9
|
+
path.call(print, "source"),
|
|
10
|
+
'"'
|
|
11
|
+
];
|
|
12
|
+
const hasAliases = node.aliases && node.aliases.length > 0;
|
|
13
|
+
if (hasAliases) {
|
|
14
|
+
docs.push(" with");
|
|
15
|
+
const mappedAliases = path.map(print, "aliases");
|
|
16
|
+
docs.push(indent([line, join([",", line], mappedAliases)]));
|
|
17
|
+
docs.push(line);
|
|
18
|
+
} else {
|
|
19
|
+
docs.push(" ");
|
|
20
|
+
}
|
|
21
|
+
docs.push(node.trimRight ? "-%}" : "%}");
|
|
22
|
+
return group(docs);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { p as printUseStatement };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { STRING_NEEDS_QUOTES, isContractableNodeType } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { line, indent } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const p = (node, path, print) => {
|
|
7
|
+
const printedName = path.call(print, "name");
|
|
8
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
9
|
+
const printedValue = path.call(print, "value");
|
|
10
|
+
const shouldCondenseLayout = isContractableNodeType(node.value);
|
|
11
|
+
const rightHandSide = shouldCondenseLayout
|
|
12
|
+
? [" ", printedValue]
|
|
13
|
+
: indent([line, printedValue]);
|
|
14
|
+
|
|
15
|
+
// We are explicitly not returning a group here, because
|
|
16
|
+
// a VariableDeclarationStatement is - currently - always
|
|
17
|
+
// embedded in a group created by SetStatement.
|
|
18
|
+
return [printedName, " =", rightHandSide];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { p as printVariableDeclarationStatement };
|
package/src/printer.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { printSequenceExpression } from "./print/SequenceExpression.js";
|
|
2
|
+
import { printBinaryExpression } from "./print/BinaryExpression.js";
|
|
3
|
+
import { printConditionalExpression } from "./print/ConditionalExpression.js";
|
|
4
|
+
import { printElement } from "./print/Element.js";
|
|
5
|
+
import { printAttribute } from "./print/Attribute.js";
|
|
6
|
+
import { printIdentifier } from "./print/Identifier.js";
|
|
7
|
+
import { printExpressionStatement } from "./print/ExpressionStatement.js";
|
|
8
|
+
import { printMemberExpression } from "./print/MemberExpression.js";
|
|
9
|
+
import { printFilterExpression } from "./print/FilterExpression.js";
|
|
10
|
+
import { printObjectExpression } from "./print/ObjectExpression.js";
|
|
11
|
+
import { printObjectProperty } from "./print/ObjectProperty.js";
|
|
12
|
+
import { printCallExpression } from "./print/CallExpression.js";
|
|
13
|
+
import { printTestExpression } from "./print/TestExpression.js";
|
|
14
|
+
import { printUnaryExpression } from "./print/UnaryExpression.js";
|
|
15
|
+
import { printUnarySubclass } from "./print/UnarySubclass.js";
|
|
16
|
+
import { printTextStatement } from "./print/TextStatement.js";
|
|
17
|
+
import { printStringLiteral } from "./print/StringLiteral.js";
|
|
18
|
+
import { printArrayExpression } from "./print/ArrayExpression.js";
|
|
19
|
+
import { printSliceExpression } from "./print/SliceExpression.js";
|
|
20
|
+
import { printUseStatement } from "./print/UseStatement.js";
|
|
21
|
+
import { printAliasExpression } from "./print/AliasExpression.js";
|
|
22
|
+
import { printBlockStatement } from "./print/BlockStatement.js";
|
|
23
|
+
import { printSpacelessBlock } from "./print/SpacelessBlock.js";
|
|
24
|
+
import { printAutoescapeBlock } from "./print/AutoescapeBlock.js";
|
|
25
|
+
import { printFlushStatement } from "./print/FlushStatement.js";
|
|
26
|
+
import { printIncludeStatement } from "./print/IncludeStatement.js";
|
|
27
|
+
import { printIfStatement } from "./print/IfStatement.js";
|
|
28
|
+
import { printMountStatement } from "./print/MountStatement.js";
|
|
29
|
+
import { printForStatement } from "./print/ForStatement.js";
|
|
30
|
+
import { printSetStatement } from "./print/SetStatement.js";
|
|
31
|
+
import { printDoStatement } from "./print/DoStatement.js";
|
|
32
|
+
import { printExtendsStatement } from "./print/ExtendsStatement.js";
|
|
33
|
+
import { printEmbedStatement } from "./print/EmbedStatement.js";
|
|
34
|
+
import { printImportDeclaration } from "./print/ImportDeclaration.js";
|
|
35
|
+
import { printFromStatement } from "./print/FromStatement.js";
|
|
36
|
+
import { printTwigComment } from "./print/TwigComment.js";
|
|
37
|
+
import { printHtmlComment } from "./print/HtmlComment.js";
|
|
38
|
+
import { printDeclaration } from "./print/Declaration.js";
|
|
39
|
+
import { printGenericTwigTag } from "./print/GenericTwigTag.js";
|
|
40
|
+
import { printGenericToken } from "./print/GenericToken.js";
|
|
41
|
+
import { printMacroDeclarationStatement } from "./print/MacroDeclarationStatement.js";
|
|
42
|
+
import { printFilterBlockStatement } from "./print/FilterBlockStatement.js";
|
|
43
|
+
import { printVariableDeclarationStatement } from "./print/VariableDeclarationStatement.js";
|
|
44
|
+
import { printNamedArgumentExpression } from "./print/NamedArgumentExpression.js";
|
|
45
|
+
import { printSwitchTag } from "./print/SwitchTag.js";
|
|
46
|
+
import {
|
|
47
|
+
isWhitespaceNode,
|
|
48
|
+
isHtmlCommentEqualTo,
|
|
49
|
+
isTwigCommentEqualTo
|
|
50
|
+
} from "./util/index.js";
|
|
51
|
+
import { ORIGINAL_SOURCE } from "./parser.js";
|
|
52
|
+
|
|
53
|
+
const printFunctions = {};
|
|
54
|
+
|
|
55
|
+
const isHtmlIgnoreNextComment = isHtmlCommentEqualTo("prettier-ignore");
|
|
56
|
+
const isHtmlIgnoreStartComment = isHtmlCommentEqualTo("prettier-ignore-start");
|
|
57
|
+
const isHtmlIgnoreEndComment = isHtmlCommentEqualTo("prettier-ignore-end");
|
|
58
|
+
const isTwigIgnoreNextComment = isTwigCommentEqualTo("prettier-ignore");
|
|
59
|
+
const isTwigIgnoreStartComment = isTwigCommentEqualTo("prettier-ignore-start");
|
|
60
|
+
const isTwigIgnoreEndComment = isTwigCommentEqualTo("prettier-ignore-end");
|
|
61
|
+
|
|
62
|
+
const isIgnoreNextComment = s =>
|
|
63
|
+
isHtmlIgnoreNextComment(s) || isTwigIgnoreNextComment(s);
|
|
64
|
+
const isIgnoreRegionStartComment = s =>
|
|
65
|
+
isHtmlIgnoreStartComment(s) || isTwigIgnoreStartComment(s);
|
|
66
|
+
const isIgnoreRegionEndComment = s =>
|
|
67
|
+
isHtmlIgnoreEndComment(s) || isTwigIgnoreEndComment(s);
|
|
68
|
+
|
|
69
|
+
let originalSource = "";
|
|
70
|
+
let ignoreRegion = false;
|
|
71
|
+
let ignoreNext = false;
|
|
72
|
+
|
|
73
|
+
const checkForIgnoreStart = node => {
|
|
74
|
+
// Keep current "ignoreNext" value if it's true,
|
|
75
|
+
// but is not applied in this step yet
|
|
76
|
+
ignoreNext =
|
|
77
|
+
(ignoreNext && !shouldApplyIgnoreNext(node)) ||
|
|
78
|
+
isIgnoreNextComment(node);
|
|
79
|
+
ignoreRegion = ignoreRegion || isIgnoreRegionStartComment(node);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const checkForIgnoreEnd = node => {
|
|
83
|
+
if (ignoreRegion && isIgnoreRegionEndComment(node)) {
|
|
84
|
+
ignoreRegion = false;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const shouldApplyIgnoreNext = node => !isWhitespaceNode(node);
|
|
89
|
+
|
|
90
|
+
const print = (path, options, print) => {
|
|
91
|
+
const node = path.getValue();
|
|
92
|
+
const nodeType = node.constructor.name;
|
|
93
|
+
|
|
94
|
+
// Try to get the entire original source from AST root
|
|
95
|
+
if (node[ORIGINAL_SOURCE]) {
|
|
96
|
+
originalSource = node[ORIGINAL_SOURCE];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (options.twigPrintWidth) {
|
|
100
|
+
options.printWidth = options.twigPrintWidth;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
checkForIgnoreEnd(node);
|
|
104
|
+
const useOriginalSource =
|
|
105
|
+
(shouldApplyIgnoreNext(node) && ignoreNext) || ignoreRegion;
|
|
106
|
+
const hasPrintFunction = printFunctions[nodeType];
|
|
107
|
+
|
|
108
|
+
// Happy path: We have a formatting function, and the user wants the
|
|
109
|
+
// node formatted
|
|
110
|
+
if (!useOriginalSource && hasPrintFunction) {
|
|
111
|
+
checkForIgnoreStart(node);
|
|
112
|
+
return printFunctions[nodeType](node, path, print, options);
|
|
113
|
+
} else if (!hasPrintFunction) {
|
|
114
|
+
console.warn(`No print function available for node type "${nodeType}"`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
checkForIgnoreStart(node);
|
|
118
|
+
|
|
119
|
+
// Fallback: Use the node's loc property with the
|
|
120
|
+
// originalSource property on the AST root
|
|
121
|
+
if (canGetSubstringForNode(node)) {
|
|
122
|
+
return getSubstringForNode(node);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return "";
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const getSubstringForNode = node =>
|
|
129
|
+
originalSource.substring(node.loc.start.index, node.loc.end.index);
|
|
130
|
+
|
|
131
|
+
const canGetSubstringForNode = node =>
|
|
132
|
+
originalSource &&
|
|
133
|
+
node.loc &&
|
|
134
|
+
node.loc.start &&
|
|
135
|
+
node.loc.end &&
|
|
136
|
+
node.loc.start.index &&
|
|
137
|
+
node.loc.end.index;
|
|
138
|
+
/**
|
|
139
|
+
* Prettier printing works with a so-called FastPath object, which is
|
|
140
|
+
* passed into many of the following methods through a "path" argument.
|
|
141
|
+
* This is basically a stack, and the way to do do recursion in Prettier
|
|
142
|
+
* is through this path object.
|
|
143
|
+
*
|
|
144
|
+
* For example, you might expect to write something like this:
|
|
145
|
+
*
|
|
146
|
+
* BinaryExpression.prototype.prettyPrint = _ => {
|
|
147
|
+
* return concat([
|
|
148
|
+
* this.left.prettyPrint(),
|
|
149
|
+
* " ",
|
|
150
|
+
* this.operator,
|
|
151
|
+
* " ",
|
|
152
|
+
* this.right.prettyPrint()
|
|
153
|
+
* ]);
|
|
154
|
+
* };
|
|
155
|
+
*
|
|
156
|
+
* Here, the prettyPrint() method of BinaryExpression calls the prettyPrint()
|
|
157
|
+
* methods of the left and right operands. However, it actually has to be
|
|
158
|
+
* done like this in Prettier plugins:
|
|
159
|
+
*
|
|
160
|
+
* BinaryExpression.prototype.prettyPrint = (path, print) => {
|
|
161
|
+
* const docs = [
|
|
162
|
+
* path.call(print, "left"),
|
|
163
|
+
* " ",
|
|
164
|
+
* this.operator,
|
|
165
|
+
* " ",
|
|
166
|
+
* path.call(print, "right")
|
|
167
|
+
* ];
|
|
168
|
+
* return concat(docs);
|
|
169
|
+
* };
|
|
170
|
+
*
|
|
171
|
+
* The first argument to path.call() seems to always be the print function
|
|
172
|
+
* that is passed in (a case of bad interface design and over-complication?),
|
|
173
|
+
* at least I have not found any other instance. The arguments after that are
|
|
174
|
+
* field names that are pulled from the node and put on the stack for the
|
|
175
|
+
* next processing step(s) => this is how recursion is done.
|
|
176
|
+
*
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
printFunctions["SequenceExpression"] = printSequenceExpression;
|
|
180
|
+
printFunctions["ConstantValue"] = node => {
|
|
181
|
+
return node.value;
|
|
182
|
+
};
|
|
183
|
+
printFunctions["StringLiteral"] = printStringLiteral;
|
|
184
|
+
printFunctions["Identifier"] = printIdentifier;
|
|
185
|
+
printFunctions["UnaryExpression"] = printUnaryExpression;
|
|
186
|
+
printFunctions["BinaryExpression"] = printBinaryExpression;
|
|
187
|
+
printFunctions["BinarySubclass"] = printBinaryExpression;
|
|
188
|
+
printFunctions["UnarySubclass"] = printUnarySubclass;
|
|
189
|
+
printFunctions["TestExpression"] = printTestExpression;
|
|
190
|
+
printFunctions["ConditionalExpression"] = printConditionalExpression;
|
|
191
|
+
printFunctions["Element"] = printElement;
|
|
192
|
+
printFunctions["Attribute"] = printAttribute;
|
|
193
|
+
printFunctions["PrintTextStatement"] = printTextStatement;
|
|
194
|
+
printFunctions["PrintExpressionStatement"] = printExpressionStatement;
|
|
195
|
+
printFunctions["MemberExpression"] = printMemberExpression;
|
|
196
|
+
printFunctions["FilterExpression"] = printFilterExpression;
|
|
197
|
+
printFunctions["ObjectExpression"] = printObjectExpression;
|
|
198
|
+
printFunctions["ObjectProperty"] = printObjectProperty;
|
|
199
|
+
|
|
200
|
+
// Return value has to be a string
|
|
201
|
+
const returnNodeValue = node => "" + node.value;
|
|
202
|
+
|
|
203
|
+
printFunctions["Fragment"] = (node, path, print) => {
|
|
204
|
+
return path.call(print, "value");
|
|
205
|
+
};
|
|
206
|
+
printFunctions["switchTag"] = printSwitchTag;
|
|
207
|
+
printFunctions["NumericLiteral"] = returnNodeValue;
|
|
208
|
+
printFunctions["BooleanLiteral"] = returnNodeValue;
|
|
209
|
+
printFunctions["NullLiteral"] = () => "null";
|
|
210
|
+
printFunctions["ArrayExpression"] = printArrayExpression;
|
|
211
|
+
printFunctions["CallExpression"] = printCallExpression;
|
|
212
|
+
printFunctions["NamedArgumentExpression"] = printNamedArgumentExpression;
|
|
213
|
+
printFunctions["SliceExpression"] = printSliceExpression;
|
|
214
|
+
printFunctions["UseStatement"] = printUseStatement;
|
|
215
|
+
printFunctions["AliasExpression"] = printAliasExpression;
|
|
216
|
+
printFunctions["BlockStatement"] = printBlockStatement;
|
|
217
|
+
printFunctions["SpacelessBlock"] = printSpacelessBlock;
|
|
218
|
+
printFunctions["AutoescapeBlock"] = printAutoescapeBlock;
|
|
219
|
+
printFunctions["FlushStatement"] = printFlushStatement;
|
|
220
|
+
printFunctions["IncludeStatement"] = printIncludeStatement;
|
|
221
|
+
printFunctions["IfStatement"] = printIfStatement;
|
|
222
|
+
printFunctions["MountStatement"] = printMountStatement;
|
|
223
|
+
printFunctions["ForStatement"] = printForStatement;
|
|
224
|
+
printFunctions["BinaryConcatExpression"] = printBinaryExpression;
|
|
225
|
+
printFunctions["SetStatement"] = printSetStatement;
|
|
226
|
+
printFunctions["VariableDeclarationStatement"] =
|
|
227
|
+
printVariableDeclarationStatement;
|
|
228
|
+
printFunctions["DoStatement"] = printDoStatement;
|
|
229
|
+
printFunctions["ExtendsStatement"] = printExtendsStatement;
|
|
230
|
+
printFunctions["EmbedStatement"] = printEmbedStatement;
|
|
231
|
+
printFunctions["FilterBlockStatement"] = printFilterBlockStatement;
|
|
232
|
+
printFunctions["ImportDeclaration"] = printImportDeclaration;
|
|
233
|
+
printFunctions["FromStatement"] = printFromStatement;
|
|
234
|
+
printFunctions["MacroDeclarationStatement"] = printMacroDeclarationStatement;
|
|
235
|
+
printFunctions["TwigComment"] = printTwigComment;
|
|
236
|
+
printFunctions["HtmlComment"] = printHtmlComment;
|
|
237
|
+
printFunctions["Declaration"] = printDeclaration;
|
|
238
|
+
printFunctions["GenericTwigTag"] = (node, path, print, options) => {
|
|
239
|
+
const tagName = node.tagName;
|
|
240
|
+
if (printFunctions[tagName + "Tag"]) {
|
|
241
|
+
// Give the user the chance to implement a custom
|
|
242
|
+
// print function for certain generic Twig tags
|
|
243
|
+
return printFunctions[tagName + "Tag"](node, path, print, options);
|
|
244
|
+
}
|
|
245
|
+
return printGenericTwigTag(node, path, print, options);
|
|
246
|
+
};
|
|
247
|
+
printFunctions["GenericToken"] = printGenericToken;
|
|
248
|
+
|
|
249
|
+
// Fallbacks
|
|
250
|
+
printFunctions["String"] = s => s;
|
|
251
|
+
|
|
252
|
+
export { print };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
|
|
4
|
+
const { line, indent, group } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const noSpaceBeforeToken = {
|
|
7
|
+
",": true
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const printSingleTwigTag = (node, path, print) => {
|
|
11
|
+
const opener = node.trimLeft ? "{%-" : "{%";
|
|
12
|
+
const parts = [opener, " ", node.tagName];
|
|
13
|
+
const printedParts = path.map(print, "parts");
|
|
14
|
+
if (printedParts.length > 0) {
|
|
15
|
+
parts.push(" ", printedParts[0]);
|
|
16
|
+
}
|
|
17
|
+
const indentedParts = [];
|
|
18
|
+
for (let i = 1; i < node.parts.length; i++) {
|
|
19
|
+
const part = node.parts[i];
|
|
20
|
+
const isToken = Node.isGenericToken(part);
|
|
21
|
+
const separator =
|
|
22
|
+
isToken && noSpaceBeforeToken[part.tokenText] ? "" : line;
|
|
23
|
+
indentedParts.push(separator, printedParts[i]);
|
|
24
|
+
}
|
|
25
|
+
if (node.parts.length > 1) {
|
|
26
|
+
parts.push(indent(indentedParts));
|
|
27
|
+
}
|
|
28
|
+
const closing = node.trimRight ? "-%}" : "%}";
|
|
29
|
+
parts.push(line, closing);
|
|
30
|
+
return group(parts);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { printSingleTwigTag };
|