@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.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.
Files changed (63) hide show
  1. package/README.md +5 -7
  2. package/package.json +10 -8
  3. package/src/index.js +53 -53
  4. package/src/melody/melody-code-frame/index.js +44 -0
  5. package/src/melody/melody-code-frame/lineNumbers.js +45 -0
  6. package/src/melody/melody-extension-core/index.js +167 -0
  7. package/src/melody/melody-extension-core/operators.js +414 -0
  8. package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
  9. package/src/melody/melody-extension-core/parser/block.js +93 -0
  10. package/src/melody/melody-extension-core/parser/do.js +32 -0
  11. package/src/melody/melody-extension-core/parser/embed.js +76 -0
  12. package/src/melody/melody-extension-core/parser/extends.js +35 -0
  13. package/src/melody/melody-extension-core/parser/filter.js +63 -0
  14. package/src/melody/melody-extension-core/parser/flush.js +33 -0
  15. package/src/melody/melody-extension-core/parser/for.js +110 -0
  16. package/src/melody/melody-extension-core/parser/from.js +63 -0
  17. package/src/melody/melody-extension-core/parser/if.js +100 -0
  18. package/src/melody/melody-extension-core/parser/import.js +44 -0
  19. package/src/melody/melody-extension-core/parser/include.js +48 -0
  20. package/src/melody/melody-extension-core/parser/macro.js +94 -0
  21. package/src/melody/melody-extension-core/parser/mount.js +147 -0
  22. package/src/melody/melody-extension-core/parser/set.js +103 -0
  23. package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
  24. package/src/melody/melody-extension-core/parser/use.js +58 -0
  25. package/src/melody/melody-extension-core/types.js +402 -0
  26. package/src/melody/melody-extension-core/visitors/filters.js +146 -0
  27. package/src/melody/melody-extension-core/visitors/for.js +391 -0
  28. package/src/melody/melody-extension-core/visitors/functions.js +100 -0
  29. package/src/melody/melody-extension-core/visitors/tests.js +127 -0
  30. package/src/melody/melody-parser/Associativity.js +17 -0
  31. package/src/melody/melody-parser/CharStream.js +82 -0
  32. package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
  33. package/src/melody/melody-parser/GenericTagParser.js +53 -0
  34. package/src/melody/melody-parser/Lexer.js +708 -0
  35. package/src/melody/melody-parser/Parser.js +954 -0
  36. package/src/melody/melody-parser/TokenStream.js +183 -0
  37. package/src/melody/melody-parser/TokenTypes.js +74 -0
  38. package/src/melody/melody-parser/elementInfo.js +43 -0
  39. package/src/melody/melody-parser/index.js +94 -0
  40. package/src/melody/melody-parser/util.js +100 -0
  41. package/src/melody/melody-traverse/Binding.js +61 -0
  42. package/src/melody/melody-traverse/Path.js +427 -0
  43. package/src/melody/melody-traverse/Scope.js +218 -0
  44. package/src/melody/melody-traverse/TraversalContext.js +166 -0
  45. package/src/melody/melody-traverse/index.js +21 -0
  46. package/src/melody/melody-traverse/traverse.js +48 -0
  47. package/src/melody/melody-traverse/visitors.js +112 -0
  48. package/src/melody/melody-types/index.js +506 -0
  49. package/src/parser.js +7 -2
  50. package/src/print/Attribute.js +1 -1
  51. package/src/print/BinaryExpression.js +2 -2
  52. package/src/print/BlockStatement.js +1 -1
  53. package/src/print/CallExpression.js +1 -1
  54. package/src/print/ExpressionStatement.js +1 -1
  55. package/src/print/FilterExpression.js +1 -1
  56. package/src/print/GenericTwigTag.js +1 -1
  57. package/src/print/IfStatement.js +1 -1
  58. package/src/print/ObjectProperty.js +1 -1
  59. package/src/print/SetStatement.js +1 -1
  60. package/src/print/SwitchTag.js +1 -1
  61. package/src/print/UnarySubclass.js +1 -1
  62. package/src/util/printFunctions.js +1 -1
  63. package/src/util/publicFunctions.js +1 -1
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { Identifier } from "../../melody-types/index.js";
17
+ import { MountStatement } from "../types.js";
18
+ import {
19
+ Types,
20
+ setStartFromToken,
21
+ setEndFromToken,
22
+ createNode,
23
+ hasTagStartTokenTrimLeft,
24
+ hasTagEndTokenTrimRight
25
+ } from "../../melody-parser/index.js";
26
+
27
+ export const MountParser = {
28
+ name: "mount",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+
32
+ let name = null;
33
+ let source = null;
34
+ let key = null;
35
+ let async = false;
36
+ let delayBy = 0;
37
+ let argument = null;
38
+
39
+ if (tokens.test(Types.SYMBOL, "async")) {
40
+ // we might be looking at an async mount
41
+ const nextToken = tokens.la(1);
42
+ if (nextToken.type === Types.STRING_START) {
43
+ async = true;
44
+ tokens.next();
45
+ }
46
+ }
47
+
48
+ if (tokens.test(Types.STRING_START)) {
49
+ source = parser.matchStringExpression();
50
+ } else {
51
+ const nameToken = tokens.expect(Types.SYMBOL);
52
+ name = createNode(Identifier, nameToken, nameToken.text);
53
+ if (tokens.nextIf(Types.SYMBOL, "from")) {
54
+ source = parser.matchStringExpression();
55
+ }
56
+ }
57
+
58
+ if (tokens.nextIf(Types.SYMBOL, "as")) {
59
+ key = parser.matchExpression();
60
+ }
61
+
62
+ if (tokens.nextIf(Types.SYMBOL, "with")) {
63
+ argument = parser.matchExpression();
64
+ }
65
+
66
+ if (async) {
67
+ if (tokens.nextIf(Types.SYMBOL, "delay")) {
68
+ tokens.expect(Types.SYMBOL, "placeholder");
69
+ tokens.expect(Types.SYMBOL, "by");
70
+ delayBy = Number.parseInt(tokens.expect(Types.NUMBER).text, 10);
71
+ if (tokens.nextIf(Types.SYMBOL, "s")) {
72
+ delayBy *= 1000;
73
+ } else {
74
+ tokens.expect(Types.SYMBOL, "ms");
75
+ }
76
+ }
77
+ }
78
+
79
+ const mountStatement = new MountStatement(
80
+ name,
81
+ source,
82
+ key,
83
+ argument,
84
+ async,
85
+ delayBy
86
+ );
87
+
88
+ let openingTagEndToken;
89
+ let catchTagStartToken;
90
+ let catchTagEndToken;
91
+ let endmountTagStartToken;
92
+
93
+ if (async) {
94
+ tokens.expect(Types.TAG_END);
95
+ openingTagEndToken = tokens.la(-1);
96
+
97
+ mountStatement.body = parser.parse((tokenText, token, tokens) => {
98
+ return (
99
+ token.type === Types.TAG_START &&
100
+ (tokens.test(Types.SYMBOL, "catch") ||
101
+ tokens.test(Types.SYMBOL, "endmount"))
102
+ );
103
+ });
104
+
105
+ if (tokens.nextIf(Types.SYMBOL, "catch")) {
106
+ catchTagStartToken = tokens.la(-2);
107
+ const errorVariableName = tokens.expect(Types.SYMBOL);
108
+ mountStatement.errorVariableName = createNode(
109
+ Identifier,
110
+ errorVariableName,
111
+ errorVariableName.text
112
+ );
113
+ tokens.expect(Types.TAG_END);
114
+ catchTagEndToken = tokens.la(-1);
115
+ mountStatement.otherwise = parser.parse(
116
+ (tokenText, token, tokens) => {
117
+ return (
118
+ token.type === Types.TAG_START &&
119
+ tokens.test(Types.SYMBOL, "endmount")
120
+ );
121
+ }
122
+ );
123
+ }
124
+ tokens.expect(Types.SYMBOL, "endmount");
125
+ endmountTagStartToken = tokens.la(-2);
126
+ }
127
+
128
+ setStartFromToken(mountStatement, token);
129
+ setEndFromToken(mountStatement, tokens.expect(Types.TAG_END));
130
+
131
+ mountStatement.trimRightMount = !!(
132
+ openingTagEndToken && hasTagEndTokenTrimRight(openingTagEndToken)
133
+ );
134
+ mountStatement.trimLeftCatch = !!(
135
+ catchTagStartToken && hasTagStartTokenTrimLeft(catchTagStartToken)
136
+ );
137
+ mountStatement.trimRightCatch = !!(
138
+ catchTagEndToken && hasTagEndTokenTrimRight(catchTagEndToken)
139
+ );
140
+ mountStatement.trimLeftEndmount = !!(
141
+ endmountTagStartToken &&
142
+ hasTagStartTokenTrimLeft(endmountTagStartToken)
143
+ );
144
+
145
+ return mountStatement;
146
+ }
147
+ };
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode,
22
+ hasTagStartTokenTrimLeft,
23
+ hasTagEndTokenTrimRight
24
+ } from "../../melody-parser/index.js";
25
+ import { VariableDeclarationStatement, SetStatement } from "./../types.js";
26
+
27
+ export const SetParser = {
28
+ name: "set",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+ const names = [];
32
+ const values = [];
33
+
34
+ let openingTagEndToken;
35
+ let closingTagStartToken;
36
+
37
+ do {
38
+ const name = tokens.expect(Types.SYMBOL);
39
+ names.push(createNode(Identifier, name, name.text));
40
+ } while (tokens.nextIf(Types.COMMA));
41
+
42
+ if (tokens.nextIf(Types.ASSIGNMENT)) {
43
+ do {
44
+ values.push(parser.matchExpression());
45
+ } while (tokens.nextIf(Types.COMMA));
46
+ } else {
47
+ if (names.length !== 1) {
48
+ parser.error({
49
+ title: "Illegal multi-set",
50
+ pos: tokens.la(0).pos,
51
+ advice: "When using set with a block, you cannot have multiple targets."
52
+ });
53
+ }
54
+ tokens.expect(Types.TAG_END);
55
+ openingTagEndToken = tokens.la(-1);
56
+
57
+ values[0] = parser.parse((tokenText, token, tokens) => {
58
+ const result = !!(
59
+ token.type === Types.TAG_START &&
60
+ tokens.nextIf(Types.SYMBOL, "endset")
61
+ );
62
+ if (result) {
63
+ closingTagStartToken = token;
64
+ }
65
+ return result;
66
+ }).expressions;
67
+ }
68
+
69
+ if (names.length !== values.length) {
70
+ parser.error({
71
+ title: "Mismatch of set names and values",
72
+ pos: token.pos,
73
+ advice: `When using set, you must ensure that the number of
74
+ assigned variable names is identical to the supplied values. However, here I've found
75
+ ${names.length} variable names and ${values.length} values.`
76
+ });
77
+ }
78
+
79
+ // now join names and values
80
+ const assignments = [];
81
+ for (let i = 0, len = names.length; i < len; i++) {
82
+ assignments[i] = new VariableDeclarationStatement(
83
+ names[i],
84
+ values[i]
85
+ );
86
+ }
87
+
88
+ const setStatement = new SetStatement(assignments);
89
+
90
+ setStartFromToken(setStatement, token);
91
+ setEndFromToken(setStatement, tokens.expect(Types.TAG_END));
92
+
93
+ setStatement.trimRightSet = !!(
94
+ openingTagEndToken && hasTagEndTokenTrimRight(openingTagEndToken)
95
+ );
96
+ setStatement.trimLeftEndset = !!(
97
+ closingTagStartToken &&
98
+ hasTagStartTokenTrimLeft(closingTagStartToken)
99
+ );
100
+
101
+ return setStatement;
102
+ }
103
+ };
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import {
17
+ Types,
18
+ setStartFromToken,
19
+ setEndFromToken,
20
+ hasTagStartTokenTrimLeft,
21
+ hasTagEndTokenTrimRight
22
+ } from "../../melody-parser/index.js";
23
+ import { SpacelessBlock } from "./../types.js";
24
+
25
+ export const SpacelessParser = {
26
+ name: "spaceless",
27
+ parse(parser, token) {
28
+ const tokens = parser.tokens;
29
+
30
+ tokens.expect(Types.TAG_END);
31
+ const openingTagEndToken = tokens.la(-1);
32
+ let closingTagStartToken;
33
+
34
+ const body = parser.parse((tokenText, token, tokens) => {
35
+ const result = !!(
36
+ token.type === Types.TAG_START &&
37
+ tokens.nextIf(Types.SYMBOL, "endspaceless")
38
+ );
39
+ closingTagStartToken = token;
40
+ return result;
41
+ }).expressions;
42
+
43
+ const spacelessBlock = new SpacelessBlock(body);
44
+ setStartFromToken(spacelessBlock, token);
45
+ setEndFromToken(spacelessBlock, tokens.expect(Types.TAG_END));
46
+
47
+ spacelessBlock.trimRightSpaceless =
48
+ hasTagEndTokenTrimRight(openingTagEndToken);
49
+ spacelessBlock.trimLeftEndspaceless = !!(
50
+ closingTagStartToken &&
51
+ hasTagStartTokenTrimLeft(closingTagStartToken)
52
+ );
53
+
54
+ return spacelessBlock;
55
+ }
56
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ copyStart,
22
+ copyEnd,
23
+ createNode
24
+ } from "../../melody-parser/index.js";
25
+ import { AliasExpression, UseStatement } from "./../types.js";
26
+
27
+ export const UseParser = {
28
+ name: "use",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+
32
+ const source = parser.matchExpression();
33
+ const aliases = [];
34
+
35
+ if (tokens.nextIf(Types.SYMBOL, "with")) {
36
+ do {
37
+ const nameToken = tokens.expect(Types.SYMBOL);
38
+ const name = createNode(Identifier, nameToken, nameToken.text);
39
+ let alias = name;
40
+ if (tokens.nextIf(Types.SYMBOL, "as")) {
41
+ const aliasToken = tokens.expect(Types.SYMBOL);
42
+ alias = createNode(Identifier, aliasToken, aliasToken.text);
43
+ }
44
+ const aliasExpression = new AliasExpression(name, alias);
45
+ copyStart(aliasExpression, name);
46
+ copyEnd(aliasExpression, alias);
47
+ aliases.push(aliasExpression);
48
+ } while (tokens.nextIf(Types.COMMA));
49
+ }
50
+
51
+ const useStatement = new UseStatement(source, aliases);
52
+
53
+ setStartFromToken(useStatement, token);
54
+ setEndFromToken(useStatement, tokens.expect(Types.TAG_END));
55
+
56
+ return useStatement;
57
+ }
58
+ };