@zackad/prettier-plugin-twig 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -4
- package/package.json +1 -1
- package/src/index.js +0 -6
- package/src/melody/melody-extension-core/operators.js +7 -0
- package/src/melody/melody-extension-core/parser/macro.js +10 -3
- package/src/melody/melody-parser/Parser.js +12 -7
- package/src/print/BlockStatement.js +7 -4
- package/src/print/UnarySubclass.js +18 -2
- package/src/printer.js +0 -4
package/README.md
CHANGED
|
@@ -44,10 +44,6 @@ This Prettier plugin comes with some options that you can add to your Prettier c
|
|
|
44
44
|
|
|
45
45
|
Values can be `true` or `false`. If `true`, single quotes will be used for string literals in Twig files.
|
|
46
46
|
|
|
47
|
-
### twigPrintWidth (default: `80`)
|
|
48
|
-
|
|
49
|
-
Because Twig files might have a lot of nesting, it can be useful to define a separate print width for Twig files. This can be done with this option. If it is not set, the standard `printWidth` option is used.
|
|
50
|
-
|
|
51
47
|
### twigAlwaysBreakObjects (default: `true`)
|
|
52
48
|
|
|
53
49
|
If set to `true`, objects will always be wrapped/broken, even if they would fit on one line:
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -91,12 +91,6 @@ const options = {
|
|
|
91
91
|
default: true,
|
|
92
92
|
description: "Should objects always break in Twig files?"
|
|
93
93
|
},
|
|
94
|
-
twigPrintWidth: {
|
|
95
|
-
type: "int",
|
|
96
|
-
category: "Global",
|
|
97
|
-
default: 80,
|
|
98
|
-
description: "Print width for Twig files"
|
|
99
|
-
},
|
|
100
94
|
twigFollowOfficialCodingStandards: {
|
|
101
95
|
type: "boolean",
|
|
102
96
|
category: "Global",
|
|
@@ -38,6 +38,13 @@ export const binaryOperators = [];
|
|
|
38
38
|
export const tests = [];
|
|
39
39
|
|
|
40
40
|
//region Unary Expressions
|
|
41
|
+
export const SpreadExpression = createUnaryOperator(
|
|
42
|
+
"...",
|
|
43
|
+
"SpreadExpression",
|
|
44
|
+
// I can't find precedence reference for spread operator
|
|
45
|
+
// Ref: https://twig.symfony.com/doc/3.x/templates.html#operators
|
|
46
|
+
20
|
|
47
|
+
);
|
|
41
48
|
export const UnaryNotExpression = createUnaryOperator(
|
|
42
49
|
"not",
|
|
43
50
|
"UnaryNotExpression",
|
|
@@ -49,9 +49,16 @@ export const MacroParser = {
|
|
|
49
49
|
let value;
|
|
50
50
|
let node;
|
|
51
51
|
if (tokens.nextIf(Types.STRING_START)) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
if (tokens.test(Types.STRING_END)) {
|
|
53
|
+
// handle empty string as default value
|
|
54
|
+
value = tokens.expect(Types.STRING_END);
|
|
55
|
+
node = createNode(StringLiteral, value, "");
|
|
56
|
+
} else {
|
|
57
|
+
// handle non-empty string as default value
|
|
58
|
+
value = tokens.expect(Types.STRING);
|
|
59
|
+
tokens.expect(Types.STRING_END);
|
|
60
|
+
node = createNode(StringLiteral, value, value.text);
|
|
61
|
+
}
|
|
55
62
|
}
|
|
56
63
|
if (tokens.test(Types.NUMBER)) {
|
|
57
64
|
value = tokens.expect(Types.NUMBER);
|
|
@@ -764,6 +764,8 @@ export default class Parser {
|
|
|
764
764
|
while (!tokens.test(Types.RBRACKET) && !tokens.test(Types.EOF)) {
|
|
765
765
|
let computed = false;
|
|
766
766
|
let key;
|
|
767
|
+
|
|
768
|
+
// find the key part of object property
|
|
767
769
|
if (tokens.test(Types.STRING_START)) {
|
|
768
770
|
key = this.matchStringExpression();
|
|
769
771
|
if (!n.is(key, "StringLiteral")) {
|
|
@@ -780,14 +782,11 @@ export default class Parser {
|
|
|
780
782
|
key = this.matchExpression();
|
|
781
783
|
computed = true;
|
|
782
784
|
} else {
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
pos: tokens.la(0).pos,
|
|
786
|
-
advice:
|
|
787
|
-
"Key must be a string, symbol or a number but was " +
|
|
788
|
-
tokens.next()
|
|
789
|
-
});
|
|
785
|
+
// if none above check is matches, we can assume that key part is being omitted
|
|
786
|
+
// noop
|
|
790
787
|
}
|
|
788
|
+
|
|
789
|
+
// find the value part of object property
|
|
791
790
|
if (tokens.test(Types.COLON)) {
|
|
792
791
|
tokens.expect(Types.COLON);
|
|
793
792
|
const value = this.matchExpression();
|
|
@@ -796,12 +795,18 @@ export default class Parser {
|
|
|
796
795
|
copyEnd(prop, value);
|
|
797
796
|
obj.properties.push(prop);
|
|
798
797
|
} else {
|
|
798
|
+
// when the part is missing, we can assume that it is being omitted
|
|
799
|
+
if (key === undefined) {
|
|
800
|
+
computed = true;
|
|
801
|
+
key = this.matchExpression();
|
|
802
|
+
}
|
|
799
803
|
const value = key;
|
|
800
804
|
const prop = new n.ObjectProperty(value, computed);
|
|
801
805
|
copyStart(prop, key);
|
|
802
806
|
copyEnd(prop, value);
|
|
803
807
|
obj.properties.push(prop);
|
|
804
808
|
}
|
|
809
|
+
|
|
805
810
|
if (!tokens.test(Types.RBRACKET)) {
|
|
806
811
|
tokens.expect(Types.COMMA);
|
|
807
812
|
// support trailing comma
|
|
@@ -18,6 +18,11 @@ const p = (node, path, print, options) => {
|
|
|
18
18
|
node.trimRightBlock ? " -%}" : " %}"
|
|
19
19
|
];
|
|
20
20
|
const parts = [opener];
|
|
21
|
+
|
|
22
|
+
if (node.body.length === 0) {
|
|
23
|
+
parts.push(hardline);
|
|
24
|
+
}
|
|
25
|
+
|
|
21
26
|
if (node.body.length > 0) {
|
|
22
27
|
const indentedBody = printChildBlock(node, path, print, "body");
|
|
23
28
|
parts.push(indentedBody);
|
|
@@ -30,10 +35,9 @@ const p = (node, path, print, options) => {
|
|
|
30
35
|
node.trimRight ? " -%}" : " %}"
|
|
31
36
|
);
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
return result;
|
|
38
|
+
return group(parts);
|
|
35
39
|
} else if (Node.isPrintExpressionStatement(node.body)) {
|
|
36
|
-
|
|
40
|
+
return [
|
|
37
41
|
node.trimLeft ? "{%-" : "{%",
|
|
38
42
|
" block ",
|
|
39
43
|
path.call(print, "name"),
|
|
@@ -41,7 +45,6 @@ const p = (node, path, print, options) => {
|
|
|
41
45
|
path.call(print, "body", "value"),
|
|
42
46
|
node.trimRight ? " -%}" : " %}"
|
|
43
47
|
];
|
|
44
|
-
return parts;
|
|
45
48
|
}
|
|
46
49
|
};
|
|
47
50
|
|
|
@@ -5,7 +5,9 @@ import {
|
|
|
5
5
|
findParentNode,
|
|
6
6
|
isMultipartExpression,
|
|
7
7
|
IS_ROOT_LOGICAL_EXPRESSION,
|
|
8
|
-
GROUP_TOP_LEVEL_LOGICAL
|
|
8
|
+
GROUP_TOP_LEVEL_LOGICAL,
|
|
9
|
+
wrapExpressionIfNeeded,
|
|
10
|
+
EXPRESSION_NEEDED
|
|
9
11
|
} from "../util/index.js";
|
|
10
12
|
|
|
11
13
|
const { softline, indent, group } = doc.builders;
|
|
@@ -41,6 +43,16 @@ const printLogicalExpression = (node, path, print) => {
|
|
|
41
43
|
};
|
|
42
44
|
|
|
43
45
|
const p = (node, path, print) => {
|
|
46
|
+
// We need this part to prevent argument being wrapped into expression separated from operator
|
|
47
|
+
// Example:
|
|
48
|
+
// with node[EXPRESSION_NEEDED] = true; // default value
|
|
49
|
+
// input: {{ ...vars }}
|
|
50
|
+
// output: ...{{ vars }}
|
|
51
|
+
// with node[EXPRESSION_NEEDED] = false;
|
|
52
|
+
// input: {{ ...vars }}
|
|
53
|
+
// output: {{ ...vars }}
|
|
54
|
+
node[EXPRESSION_NEEDED] = false;
|
|
55
|
+
|
|
44
56
|
const parts = [];
|
|
45
57
|
// Example: a is not same as ... => Here, the "not" is printed "inline"
|
|
46
58
|
// Therefore, we do not output it here
|
|
@@ -48,10 +60,14 @@ const p = (node, path, print) => {
|
|
|
48
60
|
if (isLogicalOperator(node.operator) && !hasTestExpressionArgument) {
|
|
49
61
|
return printLogicalExpression(node, path, print);
|
|
50
62
|
}
|
|
51
|
-
if (
|
|
63
|
+
if (node.operator === "...") {
|
|
64
|
+
parts.push(node.operator);
|
|
65
|
+
} else if (!hasTestExpressionArgument) {
|
|
52
66
|
parts.push(node.operator, " ");
|
|
53
67
|
}
|
|
54
68
|
parts.push(path.call(print, "argument"));
|
|
69
|
+
wrapExpressionIfNeeded(path, parts, node);
|
|
70
|
+
|
|
55
71
|
return parts;
|
|
56
72
|
};
|
|
57
73
|
|
package/src/printer.js
CHANGED
|
@@ -97,10 +97,6 @@ const print = (path, options, print) => {
|
|
|
97
97
|
originalSource = node[ORIGINAL_SOURCE];
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
if (options.twigPrintWidth) {
|
|
101
|
-
options.printWidth = options.twigPrintWidth;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
100
|
checkForIgnoreEnd(node);
|
|
105
101
|
const useOriginalSource =
|
|
106
102
|
(shouldApplyIgnoreNext(node) && ignoreNext) || ignoreRegion;
|