@zackad/prettier-plugin-twig 0.7.0 → 0.9.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 +1 -1
- package/package.json +1 -2
- package/src/melody/melody-extension-core/operators.js +6 -0
- package/src/melody/melody-extension-core/visitors/filters.js +1 -2
- package/src/melody/melody-extension-core/visitors/for.js +1 -2
- package/src/melody/melody-parser/Lexer.js +8 -0
- package/src/melody/melody-parser/Parser.js +79 -8
- package/src/melody/melody-parser/TokenTypes.js +1 -0
- package/src/melody/melody-types/index.js +20 -5
- package/src/print/ArrowFunction.js +37 -0
- package/src/print/AutoescapeBlock.js +14 -4
- package/src/print/ObjectProperty.js +14 -8
- package/src/printer.js +2 -0
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ Values can be `true` or `false`. If `true`, single quotes will be used for strin
|
|
|
48
48
|
|
|
49
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
50
|
|
|
51
|
-
### twigAlwaysBreakObjects (default: `
|
|
51
|
+
### twigAlwaysBreakObjects (default: `true`)
|
|
52
52
|
|
|
53
53
|
If set to `true`, objects will always be wrapped/broken, even if they would fit on one line:
|
|
54
54
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zackad/prettier-plugin-twig",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Prettier Plugin for Twig",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
17
|
"lint": "DEBUG=eslint:cli-engine eslint .",
|
|
18
|
-
"prettier": "prettier --plugin=./src/index.js --parser=twig",
|
|
19
18
|
"test": "vitest --run"
|
|
20
19
|
},
|
|
21
20
|
"dependencies": {
|
|
@@ -124,6 +124,12 @@ export const BinaryGreaterThanOrEqualExpression = createBinaryOperatorNode({
|
|
|
124
124
|
precedence: 20,
|
|
125
125
|
associativity: LEFT
|
|
126
126
|
});
|
|
127
|
+
export const BinaryThreeWayComparisonExpression = createBinaryOperatorNode({
|
|
128
|
+
text: "<=>",
|
|
129
|
+
type: "BinaryThreeWayComparisonExpression",
|
|
130
|
+
precedence: 20,
|
|
131
|
+
associativity: LEFT
|
|
132
|
+
});
|
|
127
133
|
|
|
128
134
|
export const BinaryNotInExpression = createBinaryOperatorNode({
|
|
129
135
|
text: "not in",
|
|
@@ -14,8 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import * as t from "@babel/types";
|
|
17
|
-
import
|
|
18
|
-
const template = _template.default;
|
|
17
|
+
import template from "@babel/template";
|
|
19
18
|
// use default value if var is null, undefined or an empty string
|
|
20
19
|
// but use var if value is 0, false, an empty array or an empty object
|
|
21
20
|
const defaultFilter = template("VAR != null && VAR !== '' ? VAR : DEFAULT");
|
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { traverse } from "../../melody-traverse/index.js";
|
|
17
17
|
import * as t from "@babel/types";
|
|
18
|
-
import
|
|
19
|
-
const babelTemplate = _template.default;
|
|
18
|
+
import babelTemplate from "@babel/template";
|
|
20
19
|
|
|
21
20
|
// @param template
|
|
22
21
|
// @returns function
|
|
@@ -44,6 +44,7 @@ const CHAR_TO_TOKEN = {
|
|
|
44
44
|
"|": TokenTypes.PIPE,
|
|
45
45
|
",": TokenTypes.COMMA,
|
|
46
46
|
"?": TokenTypes.QUESTION_MARK,
|
|
47
|
+
"=>": TokenTypes.ARROW,
|
|
47
48
|
"=": TokenTypes.ASSIGNMENT,
|
|
48
49
|
//'<': TokenTypes.ELEMENT_START,
|
|
49
50
|
//'>': TokenTypes.ELEMENT_END,
|
|
@@ -345,6 +346,13 @@ export default class Lexer {
|
|
|
345
346
|
this.pushState(State.STRING_DOUBLE);
|
|
346
347
|
input.next();
|
|
347
348
|
return this.createToken(TokenTypes.STRING_START, pos);
|
|
349
|
+
case "=":
|
|
350
|
+
// Lookahead for '>'
|
|
351
|
+
if (input.la(1) === ">") {
|
|
352
|
+
input.next(); // Advance to '='
|
|
353
|
+
input.next(); // Advance to '>'
|
|
354
|
+
return this.createToken(TokenTypes.ARROW, pos);
|
|
355
|
+
}
|
|
348
356
|
default: {
|
|
349
357
|
if (isDigit(input.lac(0))) {
|
|
350
358
|
input.next();
|
|
@@ -52,6 +52,10 @@ const TAG = Symbol("TAG");
|
|
|
52
52
|
const TEST = Symbol("TEST");
|
|
53
53
|
|
|
54
54
|
export default class Parser {
|
|
55
|
+
/**
|
|
56
|
+
* @param {TokenStream} tokenStream
|
|
57
|
+
* @param {Object} options
|
|
58
|
+
*/
|
|
55
59
|
constructor(tokenStream, options) {
|
|
56
60
|
this.tokens = tokenStream;
|
|
57
61
|
this[UNARY] = {};
|
|
@@ -625,7 +629,7 @@ export default class Parser {
|
|
|
625
629
|
if (token.type === Types.LBRACE) {
|
|
626
630
|
node = this.matchArray();
|
|
627
631
|
} else if (token.type === Types.LBRACKET) {
|
|
628
|
-
node = this.
|
|
632
|
+
node = this.matchMapping();
|
|
629
633
|
} else {
|
|
630
634
|
this.error(
|
|
631
635
|
{
|
|
@@ -751,7 +755,7 @@ export default class Parser {
|
|
|
751
755
|
return array;
|
|
752
756
|
}
|
|
753
757
|
|
|
754
|
-
|
|
758
|
+
matchMapping() {
|
|
755
759
|
const tokens = this.tokens;
|
|
756
760
|
let token;
|
|
757
761
|
const obj = new n.ObjectExpression();
|
|
@@ -765,6 +769,9 @@ export default class Parser {
|
|
|
765
769
|
if (!n.is(key, "StringLiteral")) {
|
|
766
770
|
computed = true;
|
|
767
771
|
}
|
|
772
|
+
} else if ((token = tokens.nextIf(Types.EXPRESSION_START))) {
|
|
773
|
+
key = this.matchExpression();
|
|
774
|
+
computed = true;
|
|
768
775
|
} else if ((token = tokens.nextIf(Types.SYMBOL))) {
|
|
769
776
|
key = createNode(n.Identifier, token, token.text);
|
|
770
777
|
} else if ((token = tokens.nextIf(Types.NUMBER))) {
|
|
@@ -781,12 +788,20 @@ export default class Parser {
|
|
|
781
788
|
tokens.next()
|
|
782
789
|
});
|
|
783
790
|
}
|
|
784
|
-
tokens.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
791
|
+
if (tokens.test(Types.COLON)) {
|
|
792
|
+
tokens.expect(Types.COLON);
|
|
793
|
+
const value = this.matchExpression();
|
|
794
|
+
const prop = new n.ObjectProperty(value, computed, key);
|
|
795
|
+
copyStart(prop, key);
|
|
796
|
+
copyEnd(prop, value);
|
|
797
|
+
obj.properties.push(prop);
|
|
798
|
+
} else {
|
|
799
|
+
const value = key;
|
|
800
|
+
const prop = new n.ObjectProperty(value, computed);
|
|
801
|
+
copyStart(prop, key);
|
|
802
|
+
copyEnd(prop, value);
|
|
803
|
+
obj.properties.push(prop);
|
|
804
|
+
}
|
|
790
805
|
if (!tokens.test(Types.RBRACKET)) {
|
|
791
806
|
tokens.expect(Types.COMMA);
|
|
792
807
|
// support trailing comma
|
|
@@ -926,9 +941,18 @@ export default class Parser {
|
|
|
926
941
|
tokens.expect(Types.LPAREN);
|
|
927
942
|
while (!tokens.test(Types.RPAREN) && !tokens.test(Types.EOF)) {
|
|
928
943
|
if (
|
|
944
|
+
tokens.test(Types.LPAREN) ||
|
|
945
|
+
(tokens.test(Types.SYMBOL) && tokens.lat(1) === Types.ARROW)
|
|
946
|
+
) {
|
|
947
|
+
// OPTION 1: filter argument with arrow function
|
|
948
|
+
const arrowFunction = this.matchArrowFunction();
|
|
949
|
+
copyEnd(arrowFunction, arrowFunction.body);
|
|
950
|
+
args.push(arrowFunction);
|
|
951
|
+
} else if (
|
|
929
952
|
tokens.test(Types.SYMBOL) &&
|
|
930
953
|
tokens.lat(1) === Types.ASSIGNMENT
|
|
931
954
|
) {
|
|
955
|
+
// OPTION 2: named filter argument(s)
|
|
932
956
|
const name = tokens.next();
|
|
933
957
|
tokens.next();
|
|
934
958
|
const value = this.matchExpression();
|
|
@@ -939,16 +963,63 @@ export default class Parser {
|
|
|
939
963
|
copyEnd(arg, value);
|
|
940
964
|
args.push(arg);
|
|
941
965
|
} else {
|
|
966
|
+
// OPTION 3: unnamed filter argument(s)
|
|
942
967
|
args.push(this.matchExpression());
|
|
943
968
|
}
|
|
944
969
|
|
|
970
|
+
// No comma means end of filter arguments, return filter arguments to matchFilterExpression()
|
|
945
971
|
if (!tokens.test(Types.COMMA)) {
|
|
946
972
|
tokens.expect(Types.RPAREN);
|
|
947
973
|
return args;
|
|
948
974
|
}
|
|
975
|
+
// Otherwise, expect a comma and run again
|
|
949
976
|
tokens.expect(Types.COMMA);
|
|
950
977
|
}
|
|
978
|
+
// End of arguments
|
|
951
979
|
tokens.expect(Types.RPAREN);
|
|
952
980
|
return args;
|
|
953
981
|
}
|
|
982
|
+
|
|
983
|
+
matchArrowFunction() {
|
|
984
|
+
const tokens = this.tokens;
|
|
985
|
+
|
|
986
|
+
// Arrow arguments
|
|
987
|
+
const arrowArguments = [];
|
|
988
|
+
|
|
989
|
+
if (tokens.test(Types.LPAREN)) {
|
|
990
|
+
// OPTION 1: Multiple arguments in parentheses, e.g. (value, key) => expression
|
|
991
|
+
tokens.next(); // Consume the LPAREN
|
|
992
|
+
|
|
993
|
+
while (!tokens.test(Types.EOF) && !tokens.test(Types.RPAREN)) {
|
|
994
|
+
const arg = this.matchExpression(); // Adjust this line to match arguments properly
|
|
995
|
+
arrowArguments.push(arg);
|
|
996
|
+
if (tokens.test(Types.COMMA)) {
|
|
997
|
+
tokens.next(); // Consume the comma
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
if (tokens.test(Types.RPAREN)) {
|
|
1002
|
+
tokens.next(); // Consume the RPAREN
|
|
1003
|
+
}
|
|
1004
|
+
} else {
|
|
1005
|
+
// OPTION 2: Single argument, e.g. item => expression
|
|
1006
|
+
const arg = this.matchExpression(); // Adjust this line to match arguments properly
|
|
1007
|
+
arrowArguments.push(arg);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// Skip arrow
|
|
1011
|
+
if (tokens.test(Types.ARROW)) {
|
|
1012
|
+
tokens.next();
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// Body
|
|
1016
|
+
const arrowBody = this.matchExpression();
|
|
1017
|
+
|
|
1018
|
+
const result = new n.ArrowFunction(
|
|
1019
|
+
arrowArguments,
|
|
1020
|
+
arrowBody.length === 1 ? arrowBody[0] : arrowBody // If single expression, return it directly
|
|
1021
|
+
);
|
|
1022
|
+
|
|
1023
|
+
return result;
|
|
1024
|
+
}
|
|
954
1025
|
}
|
|
@@ -323,6 +323,21 @@ type(NamedArgumentExpression, "NamedArgumentExpression");
|
|
|
323
323
|
alias(NamedArgumentExpression, "Expression");
|
|
324
324
|
visitor(NamedArgumentExpression, "name", "value");
|
|
325
325
|
|
|
326
|
+
export class ArrowFunction extends Node {
|
|
327
|
+
/**
|
|
328
|
+
* @param {Array<Node>} args
|
|
329
|
+
* @param {Node} body
|
|
330
|
+
*/
|
|
331
|
+
constructor(args, body) {
|
|
332
|
+
super();
|
|
333
|
+
this.args = args;
|
|
334
|
+
this.body = body;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
type(ArrowFunction, "ArrowFunction");
|
|
338
|
+
alias(ArrowFunction, "Expression");
|
|
339
|
+
visitor(ArrowFunction, "args", "body");
|
|
340
|
+
|
|
326
341
|
export class ObjectExpression extends Node {
|
|
327
342
|
/**
|
|
328
343
|
* @param {Array<ObjectProperty>} properties
|
|
@@ -338,14 +353,14 @@ visitor(ObjectExpression, "properties");
|
|
|
338
353
|
|
|
339
354
|
export class ObjectProperty extends Node {
|
|
340
355
|
/**
|
|
341
|
-
* @param {Node}
|
|
342
|
-
* @param {
|
|
343
|
-
* @param {
|
|
356
|
+
* @param {Node} value Actual object property value
|
|
357
|
+
* @param {boolean} computed Whether or not the Node require additional processing
|
|
358
|
+
* @param {Node|null} [key] Optional that would allow omitting key part
|
|
344
359
|
*/
|
|
345
|
-
constructor(
|
|
360
|
+
constructor(value, computed, key = null) {
|
|
346
361
|
super();
|
|
347
|
-
this.key = key;
|
|
348
362
|
this.value = value;
|
|
363
|
+
this.key = key;
|
|
349
364
|
this.computed = computed;
|
|
350
365
|
}
|
|
351
366
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
const { group, indent, join, line, softline } = doc.builders;
|
|
3
|
+
|
|
4
|
+
const p = (node, path, print) => {
|
|
5
|
+
const args = node.args;
|
|
6
|
+
const body = path.call(print, "body");
|
|
7
|
+
|
|
8
|
+
const parts = [];
|
|
9
|
+
|
|
10
|
+
// Args
|
|
11
|
+
if (args.length > 1) {
|
|
12
|
+
// Multiple args
|
|
13
|
+
parts.push(
|
|
14
|
+
group([
|
|
15
|
+
"(",
|
|
16
|
+
join(
|
|
17
|
+
", ",
|
|
18
|
+
args.map(arg => arg.name)
|
|
19
|
+
),
|
|
20
|
+
")"
|
|
21
|
+
])
|
|
22
|
+
);
|
|
23
|
+
} else {
|
|
24
|
+
// Single arg
|
|
25
|
+
parts.push(args[0].name);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Arrow
|
|
29
|
+
parts.push(" => ");
|
|
30
|
+
|
|
31
|
+
// Body
|
|
32
|
+
parts.push(body);
|
|
33
|
+
|
|
34
|
+
return group(parts);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { p as printArrowFunction };
|
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
|
+
import { isBoolean } from "lodash";
|
|
2
3
|
import { printChildBlock, quoteChar } from "../util/index.js";
|
|
3
4
|
|
|
4
5
|
const { hardline } = doc.builders;
|
|
5
6
|
|
|
7
|
+
const printEscapeType = (escapeType, options) => {
|
|
8
|
+
if (escapeType === null) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (isBoolean(escapeType)) {
|
|
13
|
+
return [escapeType ? "true" : "false", " "];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return [quoteChar(options), escapeType, quoteChar(options), " "];
|
|
17
|
+
};
|
|
18
|
+
|
|
6
19
|
const createOpener = (node, options) => {
|
|
7
20
|
return [
|
|
8
21
|
node.trimLeft ? "{%-" : "{%",
|
|
9
22
|
" autoescape ",
|
|
10
|
-
|
|
11
|
-
node.escapeType || "html",
|
|
12
|
-
quoteChar(options),
|
|
13
|
-
" ",
|
|
23
|
+
...printEscapeType(node.escapeType, options),
|
|
14
24
|
node.trimRightAutoescape ? "-%}" : "%}"
|
|
15
25
|
];
|
|
16
26
|
};
|
|
@@ -6,17 +6,23 @@ const p = (node, path, print, options) => {
|
|
|
6
6
|
!node.computed &&
|
|
7
7
|
Node.isStringLiteral(node.key) &&
|
|
8
8
|
!isValidIdentifierName(node.key.value);
|
|
9
|
-
const shouldPrintKeyAsString = node.key
|
|
9
|
+
const shouldPrintKeyAsString = node.key?.wasImplicitConcatenation;
|
|
10
10
|
const needsParentheses = node.computed && !shouldPrintKeyAsString;
|
|
11
11
|
const parts = [];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
|
|
13
|
+
// print "key" part if they exist
|
|
14
|
+
if (node.key !== null) {
|
|
15
|
+
if (needsParentheses) {
|
|
16
|
+
parts.push("(");
|
|
17
|
+
}
|
|
18
|
+
parts.push(path.call(print, "key"));
|
|
19
|
+
if (needsParentheses) {
|
|
20
|
+
parts.push(")");
|
|
21
|
+
}
|
|
22
|
+
parts.push(": ");
|
|
18
23
|
}
|
|
19
|
-
|
|
24
|
+
|
|
25
|
+
// print "value" part
|
|
20
26
|
node[STRING_NEEDS_QUOTES] = true;
|
|
21
27
|
parts.push(path.call(print, "value"));
|
|
22
28
|
return parts;
|
package/src/printer.js
CHANGED
|
@@ -7,6 +7,7 @@ import { printIdentifier } from "./print/Identifier.js";
|
|
|
7
7
|
import { printExpressionStatement } from "./print/ExpressionStatement.js";
|
|
8
8
|
import { printMemberExpression } from "./print/MemberExpression.js";
|
|
9
9
|
import { printFilterExpression } from "./print/FilterExpression.js";
|
|
10
|
+
import { printArrowFunction } from "./print/ArrowFunction.js";
|
|
10
11
|
import { printObjectExpression } from "./print/ObjectExpression.js";
|
|
11
12
|
import { printObjectProperty } from "./print/ObjectProperty.js";
|
|
12
13
|
import { printCallExpression } from "./print/CallExpression.js";
|
|
@@ -194,6 +195,7 @@ printFunctions["PrintTextStatement"] = printTextStatement;
|
|
|
194
195
|
printFunctions["PrintExpressionStatement"] = printExpressionStatement;
|
|
195
196
|
printFunctions["MemberExpression"] = printMemberExpression;
|
|
196
197
|
printFunctions["FilterExpression"] = printFilterExpression;
|
|
198
|
+
printFunctions["ArrowFunction"] = printArrowFunction;
|
|
197
199
|
printFunctions["ObjectExpression"] = printObjectExpression;
|
|
198
200
|
printFunctions["ObjectProperty"] = printObjectProperty;
|
|
199
201
|
|