@zackad/prettier-plugin-twig 0.7.0 → 0.8.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zackad/prettier-plugin-twig",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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": {
|
|
@@ -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();
|
|
@@ -926,9 +926,18 @@ export default class Parser {
|
|
|
926
926
|
tokens.expect(Types.LPAREN);
|
|
927
927
|
while (!tokens.test(Types.RPAREN) && !tokens.test(Types.EOF)) {
|
|
928
928
|
if (
|
|
929
|
+
tokens.test(Types.LPAREN) ||
|
|
930
|
+
(tokens.test(Types.SYMBOL) && tokens.lat(1) === Types.ARROW)
|
|
931
|
+
) {
|
|
932
|
+
// OPTION 1: filter argument with arrow function
|
|
933
|
+
const arrowFunction = this.matchArrowFunction();
|
|
934
|
+
copyEnd(arrowFunction, arrowFunction.body);
|
|
935
|
+
args.push(arrowFunction);
|
|
936
|
+
} else if (
|
|
929
937
|
tokens.test(Types.SYMBOL) &&
|
|
930
938
|
tokens.lat(1) === Types.ASSIGNMENT
|
|
931
939
|
) {
|
|
940
|
+
// OPTION 2: named filter argument(s)
|
|
932
941
|
const name = tokens.next();
|
|
933
942
|
tokens.next();
|
|
934
943
|
const value = this.matchExpression();
|
|
@@ -939,16 +948,63 @@ export default class Parser {
|
|
|
939
948
|
copyEnd(arg, value);
|
|
940
949
|
args.push(arg);
|
|
941
950
|
} else {
|
|
951
|
+
// OPTION 3: unnamed filter argument(s)
|
|
942
952
|
args.push(this.matchExpression());
|
|
943
953
|
}
|
|
944
954
|
|
|
955
|
+
// No comma means end of filter arguments, return filter arguments to matchFilterExpression()
|
|
945
956
|
if (!tokens.test(Types.COMMA)) {
|
|
946
957
|
tokens.expect(Types.RPAREN);
|
|
947
958
|
return args;
|
|
948
959
|
}
|
|
960
|
+
// Otherwise, expect a comma and run again
|
|
949
961
|
tokens.expect(Types.COMMA);
|
|
950
962
|
}
|
|
963
|
+
// End of arguments
|
|
951
964
|
tokens.expect(Types.RPAREN);
|
|
952
965
|
return args;
|
|
953
966
|
}
|
|
967
|
+
|
|
968
|
+
matchArrowFunction() {
|
|
969
|
+
const tokens = this.tokens;
|
|
970
|
+
|
|
971
|
+
// Arrow arguments
|
|
972
|
+
const arrowArguments = [];
|
|
973
|
+
|
|
974
|
+
if (tokens.test(Types.LPAREN)) {
|
|
975
|
+
// OPTION 1: Multiple arguments in parentheses, e.g. (value, key) => expression
|
|
976
|
+
tokens.next(); // Consume the LPAREN
|
|
977
|
+
|
|
978
|
+
while (!tokens.test(Types.EOF) && !tokens.test(Types.RPAREN)) {
|
|
979
|
+
const arg = this.matchExpression(); // Adjust this line to match arguments properly
|
|
980
|
+
arrowArguments.push(arg);
|
|
981
|
+
if (tokens.test(Types.COMMA)) {
|
|
982
|
+
tokens.next(); // Consume the comma
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (tokens.test(Types.RPAREN)) {
|
|
987
|
+
tokens.next(); // Consume the RPAREN
|
|
988
|
+
}
|
|
989
|
+
} else {
|
|
990
|
+
// OPTION 2: Single argument, e.g. item => expression
|
|
991
|
+
const arg = this.matchExpression(); // Adjust this line to match arguments properly
|
|
992
|
+
arrowArguments.push(arg);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// Skip arrow
|
|
996
|
+
if (tokens.test(Types.ARROW)) {
|
|
997
|
+
tokens.next();
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
// Body
|
|
1001
|
+
const arrowBody = this.matchExpression();
|
|
1002
|
+
|
|
1003
|
+
const result = new n.ArrowFunction(
|
|
1004
|
+
arrowArguments,
|
|
1005
|
+
arrowBody.length === 1 ? arrowBody[0] : arrowBody // If single expression, return it directly
|
|
1006
|
+
);
|
|
1007
|
+
|
|
1008
|
+
return result;
|
|
1009
|
+
}
|
|
954
1010
|
}
|
|
@@ -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
|
|
@@ -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 };
|
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
|
|