@zackad/prettier-plugin-twig 0.7.0-alpha.2 → 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/README.md +0 -3
- package/package.json +1 -2
- package/src/melody/melody-parser/CharStream.js +1 -1
- package/src/melody/melody-parser/Lexer.js +57 -6
- package/src/melody/melody-parser/Parser.js +56 -0
- package/src/melody/melody-parser/TokenTypes.js +1 -0
- package/src/melody/melody-types/index.js +15 -0
- package/src/print/ArrowFunction.js +37 -0
- package/src/printer.js +2 -0
package/README.md
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
# Prettier Plugin for Twig
|
|
2
2
|
|
|
3
|
-
> [!WARNING]
|
|
4
|
-
> Not ready. DON'T USE!
|
|
5
|
-
|
|
6
3
|
Forked from [trivago/prettier-plugin-twig-melody](https://github.com/trivago/prettier-plugin-twig-melody) with focus on twig template only.
|
|
7
4
|
|
|
8
5
|

|
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();
|
|
@@ -497,7 +505,7 @@ export default class Lexer {
|
|
|
497
505
|
(c === 95 ||
|
|
498
506
|
isAlpha(c) ||
|
|
499
507
|
isDigit(c) ||
|
|
500
|
-
(inElement && (c
|
|
508
|
+
(inElement && isValidAttributeName(c)))
|
|
501
509
|
) {
|
|
502
510
|
input.next();
|
|
503
511
|
}
|
|
@@ -561,17 +569,15 @@ export default class Lexer {
|
|
|
561
569
|
matchAttributeValue(pos) {
|
|
562
570
|
const input = this.input;
|
|
563
571
|
const start = this.state === State.STRING_SINGLE ? "'" : '"';
|
|
564
|
-
let c;
|
|
565
|
-
|
|
572
|
+
let c = input.la(0);
|
|
573
|
+
const c2 = input.la(1);
|
|
574
|
+
if (c === "{" && (c2 === "{" || c2 === "#" || c2 === "%")) {
|
|
566
575
|
return this.matchExpressionToken(pos);
|
|
567
576
|
}
|
|
568
577
|
while ((c = input.la(0)) !== start && c !== EOF) {
|
|
569
578
|
if (c === "\\" && input.la(1) === start) {
|
|
570
579
|
input.next();
|
|
571
580
|
input.next();
|
|
572
|
-
} else if (c === "{") {
|
|
573
|
-
// interpolation start
|
|
574
|
-
break;
|
|
575
581
|
} else if (c === start) {
|
|
576
582
|
break;
|
|
577
583
|
} else {
|
|
@@ -663,3 +669,48 @@ function isAlpha(c) {
|
|
|
663
669
|
function isDigit(c) {
|
|
664
670
|
return c >= 48 && c <= 57;
|
|
665
671
|
}
|
|
672
|
+
|
|
673
|
+
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
|
|
674
|
+
function isValidAttributeName(c) {
|
|
675
|
+
const is_c0_control = c >= 0 && c <= 31;
|
|
676
|
+
const is_control = is_c0_control || (c >= 127 && c <= 159);
|
|
677
|
+
const is_invalid =
|
|
678
|
+
c === 32 || c === 34 || c === 39 || c === 62 || c === 47 || c === 61;
|
|
679
|
+
const is_noncharacter =
|
|
680
|
+
(c >= 64976 && c <= 65007) ||
|
|
681
|
+
c === 65534 ||
|
|
682
|
+
c === 65535 ||
|
|
683
|
+
c === 131070 ||
|
|
684
|
+
c === 131071 ||
|
|
685
|
+
c === 196606 ||
|
|
686
|
+
c === 196607 ||
|
|
687
|
+
c === 262142 ||
|
|
688
|
+
c === 262143 ||
|
|
689
|
+
c === 327678 ||
|
|
690
|
+
c === 327679 ||
|
|
691
|
+
c === 393214 ||
|
|
692
|
+
c === 393215 ||
|
|
693
|
+
c === 458750 ||
|
|
694
|
+
c === 458751 ||
|
|
695
|
+
c === 524286 ||
|
|
696
|
+
c === 524287 ||
|
|
697
|
+
c === 589822 ||
|
|
698
|
+
c === 589823 ||
|
|
699
|
+
c === 655358 ||
|
|
700
|
+
c === 655359 ||
|
|
701
|
+
c === 720894 ||
|
|
702
|
+
c === 720895 ||
|
|
703
|
+
c === 786430 ||
|
|
704
|
+
c === 786431 ||
|
|
705
|
+
c === 851966 ||
|
|
706
|
+
c === 851967 ||
|
|
707
|
+
c === 917502 ||
|
|
708
|
+
c === 917503 ||
|
|
709
|
+
c === 983038 ||
|
|
710
|
+
c === 983039 ||
|
|
711
|
+
c === 1048574 ||
|
|
712
|
+
c === 1048575 ||
|
|
713
|
+
c === 1114106 ||
|
|
714
|
+
c === 1114107;
|
|
715
|
+
return !is_control && !is_invalid && !is_noncharacter;
|
|
716
|
+
}
|
|
@@ -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
|
|