@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,166 @@
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 Path from "./Path.js";
17
+
18
+ export default class TraversalContext {
19
+ constructor(scope, visitor, state, parentPath) {
20
+ this.parentPath = parentPath;
21
+ this.scope = scope;
22
+ this.state = state;
23
+ this.visitor = visitor;
24
+
25
+ this.queue = null;
26
+ this.priorityQueue = null;
27
+ }
28
+
29
+ /**
30
+ * @returns {Path}
31
+ */
32
+ create(parent, container, key, listKey) {
33
+ return Path.get({
34
+ parentPath: this.parentPath,
35
+ parent,
36
+ container,
37
+ key,
38
+ listKey
39
+ });
40
+ }
41
+
42
+ /**
43
+ * @returns {boolean}
44
+ */
45
+ shouldVisit(node) {
46
+ const visitor = this.visitor;
47
+
48
+ if (visitor[node.type]) {
49
+ return true;
50
+ }
51
+
52
+ /** @type {Array<String>} */
53
+ const keys = node.visitorKeys;
54
+ // this node doesn't have any children
55
+ if (!keys || !keys.length) {
56
+ return false;
57
+ }
58
+
59
+ let i;
60
+ let len;
61
+ for (i = 0, len = keys.length; i < len; i++) {
62
+ // check if some of its visitor keys have a value,
63
+ // if so, we need to visit it
64
+ if (node[keys[i]]) {
65
+ return true;
66
+ }
67
+ }
68
+
69
+ return false;
70
+ }
71
+
72
+ visit(node, key) {
73
+ const nodes = node[key];
74
+ if (!nodes) {
75
+ return false;
76
+ }
77
+
78
+ if (Array.isArray(nodes)) {
79
+ return this.visitMultiple(nodes, node, key);
80
+ }
81
+ return this.visitSingle(node, key);
82
+ }
83
+
84
+ /**
85
+ * @returns {boolean}
86
+ */
87
+ visitSingle(node, key) {
88
+ if (this.shouldVisit(node[key])) {
89
+ return this.visitQueue([this.create(node, node, key)]);
90
+ }
91
+ return false;
92
+ }
93
+
94
+ visitMultiple(container, parent, listKey) {
95
+ if (!container.length) {
96
+ return false;
97
+ }
98
+
99
+ const queue = [];
100
+
101
+ for (let i = 0, len = container.length; i < len; i++) {
102
+ const node = container[i];
103
+ if (node && this.shouldVisit(node)) {
104
+ queue.push(this.create(parent, container, i, listKey));
105
+ }
106
+ }
107
+
108
+ return this.visitQueue(queue);
109
+ }
110
+
111
+ /**
112
+ * @param {Array<Path>} queue
113
+ */
114
+ visitQueue(queue) {
115
+ this.queue = queue;
116
+ this.priorityQueue = [];
117
+
118
+ const visited = [];
119
+ let stop = false;
120
+
121
+ for (const path of queue) {
122
+ path.resync();
123
+ path.pushContext(this);
124
+
125
+ if (visited.indexOf(path.node) >= 0) {
126
+ continue;
127
+ }
128
+ visited.push(path.node);
129
+
130
+ if (path.visit()) {
131
+ stop = true;
132
+ break;
133
+ }
134
+
135
+ if (this.priorityQueue.length) {
136
+ stop = this.visitQueue(this.priorityQueue);
137
+ this.priorityQueue = [];
138
+ this.queue = queue;
139
+ if (stop) {
140
+ break;
141
+ }
142
+ }
143
+ }
144
+
145
+ for (const path of queue) {
146
+ path.popContext();
147
+ }
148
+
149
+ this.queue = null;
150
+
151
+ return stop;
152
+ }
153
+
154
+ /**
155
+ * @param {boolean} [notPriority]
156
+ */
157
+ maybeQueue(path, notPriority) {
158
+ if (this.queue) {
159
+ if (notPriority) {
160
+ this.queue.push(path);
161
+ } else {
162
+ this.priorityQueue.push(path);
163
+ }
164
+ }
165
+ }
166
+ }
@@ -0,0 +1,21 @@
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 Path from "./Path.js";
17
+ import Scope from "./Scope.js";
18
+ export { Scope, Path };
19
+ export * from "./Scope.js";
20
+ export { merge, explode } from "./visitors.js";
21
+ export { traverse, visit } from "./traverse.js";
@@ -0,0 +1,48 @@
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 { explode } from "./visitors.js";
17
+ import TraversalContext from "./TraversalContext.js";
18
+
19
+ /**
20
+ * @param {Object} visitor
21
+ * @param {Object} [scope]
22
+ * @param {Object} [state]
23
+ * @param {Object} [parentPath]
24
+ */
25
+ export function traverse(parentNode, visitor, scope, state = {}, parentPath) {
26
+ if (!parentNode) {
27
+ return;
28
+ }
29
+
30
+ explode(visitor);
31
+ visit(parentNode, visitor, scope, state, parentPath);
32
+ }
33
+
34
+ export function visit(node, visitor, scope, state, parentPath) {
35
+ /** @type {Array<String>} */
36
+ const keys = node.visitorKeys;
37
+ if (!keys || !keys.length) {
38
+ return;
39
+ }
40
+
41
+ const context = new TraversalContext(scope, visitor, state, parentPath);
42
+ for (let i = 0, len = keys.length; i < len; i++) {
43
+ const key = keys[i];
44
+ if (context.visit(node, key)) {
45
+ return;
46
+ }
47
+ }
48
+ }
@@ -0,0 +1,112 @@
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 { ALIAS_TO_TYPE } from "../melody-types/index.js";
17
+
18
+ const EXPLODED = Symbol("EXPLODED");
19
+
20
+ export function explode(visitor) {
21
+ if (visitor[EXPLODED]) {
22
+ return visitor;
23
+ }
24
+ visitor[EXPLODED] = true;
25
+
26
+ for (const key of Object.getOwnPropertyNames(visitor)) {
27
+ // make sure all members are objects with enter and exit methods
28
+ let fns = visitor[key];
29
+ if (typeof fns === "function") {
30
+ fns = visitor[key] = { enter: fns };
31
+ }
32
+
33
+ // make sure enter and exit are arrays
34
+ if (fns.enter && !Array.isArray(fns.enter)) {
35
+ fns.enter = [fns.enter];
36
+ }
37
+ if (fns.exit && !Array.isArray(fns.exit)) {
38
+ fns.exit = [fns.exit];
39
+ }
40
+ }
41
+
42
+ let j = 0;
43
+ const visitorKeys = Object.getOwnPropertyNames(visitor);
44
+ const visitorKeyLength = visitorKeys.length;
45
+ for (; j < visitorKeyLength; j++) {
46
+ const key = visitorKeys[j];
47
+ // manage aliases
48
+ if (ALIAS_TO_TYPE[key]) {
49
+ let i = 0;
50
+ for (
51
+ const types = ALIAS_TO_TYPE[key], len = types.length;
52
+ i < len;
53
+ i++
54
+ ) {
55
+ const type = types[i];
56
+ if (!visitor[type]) {
57
+ visitor[type] = { enter: [] };
58
+ }
59
+ if (visitor[key].enter) {
60
+ visitor[type].enter.push(...visitor[key].enter);
61
+ }
62
+ if (visitor[key].exit) {
63
+ if (!visitor[type].exit) {
64
+ visitor[type].exit = [];
65
+ }
66
+ visitor[type].exit.push(...visitor[key].exit);
67
+ }
68
+ }
69
+ delete visitor[key];
70
+ }
71
+ }
72
+ }
73
+
74
+ /**
75
+ * @param {...Array} visitors
76
+ */
77
+ export function merge(...visitors) {
78
+ const rootVisitor = {};
79
+
80
+ let i = 0;
81
+ for (const len = visitors.length; i < len; i++) {
82
+ const visitor = visitors[i];
83
+ explode(visitor);
84
+
85
+ let j = 0;
86
+ const visitorTypes = Object.getOwnPropertyNames(visitor);
87
+ for (
88
+ const numberOfTypes = visitorTypes.length;
89
+ j < numberOfTypes;
90
+ j++
91
+ ) {
92
+ const key = visitorTypes[j];
93
+ const visitorType = visitor[key];
94
+
95
+ if (!rootVisitor[key]) {
96
+ rootVisitor[key] = {};
97
+ }
98
+
99
+ const nodeVisitor = rootVisitor[key];
100
+ nodeVisitor.enter = [].concat(
101
+ nodeVisitor.enter || [],
102
+ visitorType.enter || []
103
+ );
104
+ nodeVisitor.exit = [].concat(
105
+ nodeVisitor.exit || [],
106
+ visitorType.exit || []
107
+ );
108
+ }
109
+ }
110
+
111
+ return rootVisitor;
112
+ }