@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,427 @@
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_CACHE_KEY, Node, is } from "../melody-types/index.js";
17
+ import { visit } from "./traverse.js";
18
+ import Scope from "./Scope.js";
19
+
20
+ export default class Path {
21
+ //region Path creation
22
+ constructor(parent) {
23
+ this.parent = parent;
24
+
25
+ this.inList = false;
26
+ this.listKey = null;
27
+ this.parentKey = null;
28
+ this.container = null;
29
+ this.parentPath = null;
30
+
31
+ this.key = null;
32
+ this.node = null;
33
+ this.type = null;
34
+
35
+ this.state = null;
36
+
37
+ this.data = Object.create(null);
38
+ this.contexts = [];
39
+ this.scope = null;
40
+ this.visitor = null;
41
+
42
+ this.shouldSkip = false;
43
+ this.shouldStop = false;
44
+ this.removed = false;
45
+ }
46
+
47
+ /**
48
+ * @returns {Path}
49
+ */
50
+ static get({ parentPath, parent, container, listKey, key }) {
51
+ const targetNode = container[key];
52
+ const paths =
53
+ (parent && parent[PATH_CACHE_KEY]) ||
54
+ (parent ? (parent[PATH_CACHE_KEY] = []) : []);
55
+ let path;
56
+
57
+ for (let i = 0, len = paths.length; i < len; i++) {
58
+ const candidate = paths[i];
59
+ if (candidate.node === targetNode) {
60
+ path = candidate;
61
+ break;
62
+ }
63
+ }
64
+
65
+ if (!path) {
66
+ path = new Path(parent);
67
+ }
68
+
69
+ path.inList = !!listKey;
70
+ path.listKey = listKey;
71
+ path.parentKey = listKey || key;
72
+ path.container = container;
73
+ path.parentPath = parentPath || path.parentPath;
74
+
75
+ path.key = key;
76
+ path.node = path.container[path.key];
77
+ path.type = path.node && path.node.type;
78
+
79
+ if (!path.node) {
80
+ /*eslint no-console: off*/
81
+ console.log(
82
+ "Path has no node " + path.parentKey + " > " + path.key
83
+ );
84
+ }
85
+ paths.push(path);
86
+
87
+ return path;
88
+ }
89
+
90
+ //endregion
91
+
92
+ //region Generic data
93
+ /**
94
+ * @param {string} key
95
+ */
96
+ setData(key, val) {
97
+ return (this.data[key] = val);
98
+ }
99
+
100
+ /**
101
+ * @param {string} key
102
+ */
103
+ getData(key, def) {
104
+ const val = this.data[key];
105
+ if (!val && def) {
106
+ return (this.data[key] = def);
107
+ }
108
+ return val;
109
+ }
110
+ //endregion
111
+
112
+ //region Context
113
+ pushContext(context) {
114
+ this.contexts.push(context);
115
+ this.setContext(context);
116
+ }
117
+
118
+ popContext() {
119
+ this.contexts.pop();
120
+ this.setContext(this.contexts[this.contexts.length - 1]);
121
+ }
122
+
123
+ setContext(context) {
124
+ this.shouldSkip = false;
125
+ this.shouldStop = false;
126
+ this.removed = false;
127
+ //this.skipKeys = {};
128
+
129
+ if (context) {
130
+ this.context = context;
131
+ this.state = context.state;
132
+ this.visitor = context.visitor;
133
+ }
134
+
135
+ this.setScope();
136
+ return this;
137
+ }
138
+
139
+ /**
140
+ * @param {Scope} scope
141
+ */
142
+ getScope(scope) {
143
+ if (Node.isScope(this.node)) {
144
+ if (this.node.type === "BlockStatement") {
145
+ return Scope.get(this, scope.getRootScope());
146
+ }
147
+ return Scope.get(this, scope);
148
+ }
149
+ return scope;
150
+ }
151
+
152
+ setScope() {
153
+ let target = this.context && this.context.scope;
154
+
155
+ if (!target) {
156
+ let path = this.parentPath;
157
+ while (path && !target) {
158
+ target = path.scope;
159
+ path = path.parentPath;
160
+ }
161
+ }
162
+
163
+ this.scope = this.getScope(target);
164
+ }
165
+
166
+ /**
167
+ * @returns {boolean}
168
+ */
169
+ visit() {
170
+ if (!this.node) {
171
+ return false;
172
+ }
173
+
174
+ if (call(this, "enter") || this.shouldSkip) {
175
+ return this.shouldStop;
176
+ }
177
+
178
+ visit(this.node, this.visitor, this.scope, this.state, this);
179
+
180
+ call(this, "exit");
181
+
182
+ return this.shouldStop;
183
+ }
184
+
185
+ skip() {
186
+ this.shouldSkip = true;
187
+ }
188
+
189
+ stop() {
190
+ this.shouldStop = true;
191
+ this.shouldSkip = true;
192
+ }
193
+
194
+ resync() {
195
+ if (this.removed) {
196
+ return;
197
+ }
198
+
199
+ if (this.parentPath) {
200
+ this.parent = this.parentPath.node;
201
+ }
202
+
203
+ if (this.parent && this.inList) {
204
+ const newContainer = this.parent[this.listKey];
205
+ if (this.container !== newContainer) {
206
+ this.container = newContainer || null;
207
+ }
208
+ }
209
+
210
+ if (this.container && this.node !== this.container[this.key]) {
211
+ this.key = null;
212
+ if (Array.isArray(this.container)) {
213
+ let i;
214
+ let len;
215
+ for (i = 0, len = this.container.length; i < len; i++) {
216
+ if (this.container[i] === this.node) {
217
+ this.setKey(i);
218
+ break;
219
+ }
220
+ }
221
+ } else {
222
+ let key;
223
+ for (key in this.container) {
224
+ if (this.container[key] === this.node) {
225
+ this.setKey(key);
226
+ break;
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+
233
+ setKey(key) {
234
+ this.key = key;
235
+ this.node = this.container[this.key];
236
+ this.type = this.node && this.node.type;
237
+ }
238
+
239
+ requeue(path = this) {
240
+ if (path.removed) {
241
+ return;
242
+ }
243
+
244
+ for (const context of this.contexts) {
245
+ context.maybeQueue(path);
246
+ }
247
+ }
248
+ //endregion
249
+
250
+ //region Modification
251
+ replaceWith(value) {
252
+ this.resync();
253
+
254
+ const replacement = value instanceof Path ? value.node : value;
255
+
256
+ if (this.node === replacement) {
257
+ return;
258
+ }
259
+
260
+ replaceWith(this, replacement);
261
+ this.type = replacement.type;
262
+ this.resync();
263
+ this.setScope();
264
+ this.requeue();
265
+ }
266
+
267
+ replaceWithJS(replacement) {
268
+ this.resync();
269
+ replaceWith(this, replacement);
270
+ this.type = replacement.type;
271
+ this.resync();
272
+ this.setScope();
273
+ }
274
+
275
+ replaceWithMultipleJS(...replacements) {
276
+ this.resync();
277
+
278
+ if (!this.container) {
279
+ throw new Error("Path does not have a container");
280
+ }
281
+ if (!Array.isArray(this.container)) {
282
+ throw new Error("Container of path is not an array");
283
+ }
284
+
285
+ this.container.splice(this.key, 1, ...replacements);
286
+ this.resync();
287
+ this.updateSiblingKeys(this.key, replacements.length - 1);
288
+ markRemoved(this);
289
+ //this.node = replacements[0];
290
+ }
291
+
292
+ remove() {
293
+ this.resync();
294
+
295
+ if (Array.isArray(this.container)) {
296
+ this.container.splice(this.key, 1);
297
+ this.updateSiblingKeys(this.key, -1);
298
+ } else {
299
+ replaceWith(this, null);
300
+ }
301
+
302
+ markRemoved(this);
303
+ }
304
+
305
+ updateSiblingKeys(fromIndex, incrementBy) {
306
+ if (!this.parent) {
307
+ return;
308
+ }
309
+
310
+ /** @type {Array<Path>} */
311
+ const paths = this.parent[PATH_CACHE_KEY];
312
+ for (const path of paths) {
313
+ if (path.key >= fromIndex) {
314
+ path.key += incrementBy;
315
+ }
316
+ }
317
+ }
318
+ //endregion
319
+
320
+ /**
321
+ * @param {String} type
322
+ */
323
+ is(type) {
324
+ return is(this.node, type);
325
+ }
326
+ /**
327
+ * @param {String} type
328
+ */
329
+ findParentPathOfType(type) {
330
+ let path = this.parentPath;
331
+ while (path && !path.is(type)) {
332
+ path = path.parentPath;
333
+ }
334
+ return path && path.type === type ? path : null;
335
+ }
336
+
337
+ get(key) {
338
+ /** @type {Array} */
339
+ const parts = key.split(".");
340
+ const context = this.context;
341
+ if (parts.length === 1) {
342
+ const node = this.node;
343
+ const container = node[key];
344
+ if (Array.isArray(container)) {
345
+ return container.map((_, i) =>
346
+ Path.get({
347
+ listKey: key,
348
+ parentPath: this,
349
+ parent: node,
350
+ container,
351
+ key: i
352
+ }).setContext(context)
353
+ );
354
+ }
355
+ return Path.get({
356
+ parentPath: this,
357
+ parent: node,
358
+ container: node,
359
+ key
360
+ }).setContext(context);
361
+ }
362
+ let path = this;
363
+ for (const part of parts) {
364
+ if (Array.isArray(path)) {
365
+ path = path[part];
366
+ } else {
367
+ path = path.get(part);
368
+ }
369
+ }
370
+ return path;
371
+ }
372
+ }
373
+
374
+ function markRemoved(path) {
375
+ path.shouldSkip = true;
376
+ path.removed = true;
377
+ path.node = null;
378
+ }
379
+
380
+ function replaceWith(path, node) {
381
+ if (!path.container) {
382
+ throw new Error("Path does not have a container");
383
+ }
384
+
385
+ path.node = path.container[path.key] = node;
386
+ }
387
+
388
+ /**
389
+ * @returns {boolean}
390
+ */
391
+ function call(path, key) {
392
+ if (!path.node) {
393
+ return false;
394
+ }
395
+
396
+ const visitor = path.visitor[path.node.type];
397
+ if (!visitor || !visitor[key]) {
398
+ return false;
399
+ }
400
+
401
+ /** @type {Array<Function>} */
402
+ const fns = visitor[key];
403
+ for (let i = 0, len = fns.length; i < len; i++) {
404
+ const fn = fns[i];
405
+ if (!fn) {
406
+ continue;
407
+ }
408
+
409
+ const node = path.node;
410
+ if (!node) {
411
+ return true;
412
+ }
413
+
414
+ fn.call(path.state, path, path.state);
415
+
416
+ // node has been replaced, requeue
417
+ if (path.node !== node) {
418
+ return true;
419
+ }
420
+
421
+ if (path.shouldStop || path.shouldSkip || path.removed) {
422
+ return true;
423
+ }
424
+ }
425
+
426
+ return false;
427
+ }
@@ -0,0 +1,218 @@
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 { Binding } from "./Binding.js";
17
+ import Path from "./Path.js";
18
+ const CACHE_KEY = Symbol("CACHE_KEY");
19
+ let uid = 0;
20
+
21
+ export default class Scope {
22
+ /**
23
+ * @param {Path} path
24
+ * @param {Scope} [parentScope]
25
+ */
26
+ constructor(path, parentScope) {
27
+ this.uid = uid++;
28
+ this.parent = parentScope;
29
+
30
+ this.parentBlock = path.parent;
31
+ this.block = path.node;
32
+ this.path = path;
33
+
34
+ this.references = Object.create(null);
35
+ this.bindings = Object.create(null);
36
+ this.globals = Object.create(null);
37
+ this.uids = Object.create(null);
38
+ this.escapesContext = false;
39
+ this._contextName = null;
40
+ this.mutated = false;
41
+ //this.contextName = parentScope && parentScope.contextName || '_context';
42
+ }
43
+
44
+ set contextName(val) {
45
+ this._contextName = val;
46
+ }
47
+
48
+ get contextName() {
49
+ if (this._contextName) {
50
+ return this._contextName;
51
+ }
52
+ if (this.parent) {
53
+ return this.parent.contextName || "_context";
54
+ }
55
+ return "_context";
56
+ }
57
+
58
+ /**
59
+ * @param {Path} path
60
+ * @param Scope} parentScope
61
+ */
62
+ static get(path, parentScope) {
63
+ if (parentScope && parentScope.block == path.node) {
64
+ return parentScope;
65
+ }
66
+
67
+ const cached = getCache(path.node);
68
+ if (cached) {
69
+ return cached;
70
+ }
71
+
72
+ const scope = new Scope(path, parentScope);
73
+ path.node[CACHE_KEY] = scope;
74
+ return scope;
75
+ }
76
+
77
+ get needsSubContext() {
78
+ return this.escapesContext && this.hasCustomBindings;
79
+ }
80
+
81
+ get hasCustomBindings() {
82
+ return !!Object.keys(this.bindings).length;
83
+ }
84
+
85
+ /**
86
+ * @param {string} name
87
+ */
88
+ getBinding(name) {
89
+ let scope = this;
90
+
91
+ do {
92
+ const binding = scope.getOwnBinding(name);
93
+ if (binding) {
94
+ return binding;
95
+ }
96
+ if (scope.path.is("RootScope")) {
97
+ return;
98
+ }
99
+ } while ((scope = scope.parent));
100
+ }
101
+
102
+ /**
103
+ * @param {string} name
104
+ */
105
+ getOwnBinding(name) {
106
+ return this.bindings[name];
107
+ }
108
+
109
+ /**
110
+ * @param {string} name
111
+ */
112
+ hasOwnBinding(name) {
113
+ return !!this.getOwnBinding(name);
114
+ }
115
+
116
+ /**
117
+ * @param {string} name
118
+ */
119
+ hasBinding(name) {
120
+ return !name
121
+ ? false
122
+ : !!(this.hasOwnBinding(name) || this.parentHasBinding(name));
123
+ }
124
+
125
+ getRootScope() {
126
+ let scope = this;
127
+ while (scope.parent) {
128
+ scope = scope.parent;
129
+ }
130
+ return scope;
131
+ }
132
+
133
+ /**
134
+ * @param {string} name
135
+ * @param {Path} path
136
+ * @param {string} kind
137
+ */
138
+ registerBinding(name, path = null, kind = "context") {
139
+ let scope = this;
140
+ if (kind === "global" && path === null) {
141
+ scope = this.getRootScope();
142
+ } else if (kind === "const") {
143
+ while (scope.parent) {
144
+ scope = scope.parent;
145
+ if (scope.path.is("RootScope")) {
146
+ break;
147
+ }
148
+ }
149
+ }
150
+ // todo identify if we need to be able to differentiate between binding kinds
151
+ // if (scope.bindings[name]) {
152
+ // todo: warn about colliding binding or fix it
153
+ // }
154
+ if (this.path.state) {
155
+ this.path.state.markIdentifier(name);
156
+ }
157
+ return (scope.bindings[name] = new Binding(name, this, path, kind));
158
+ }
159
+
160
+ /**
161
+ * @param {string} name
162
+ * @param {Path} path
163
+ */
164
+ reference(name, path) {
165
+ let binding = this.getBinding(name);
166
+ if (!binding) {
167
+ binding = this.registerBinding(name);
168
+ }
169
+ binding.reference(path);
170
+ }
171
+
172
+ /**
173
+ * @param {string} name
174
+ */
175
+ parentHasBinding(name) {
176
+ return this.parent && this.parent.hasBinding(name);
177
+ }
178
+
179
+ /**
180
+ * @param {string} nameHint
181
+ */
182
+ generateUid(nameHint = "temp") {
183
+ const name = toIdentifier(nameHint);
184
+
185
+ let uid;
186
+ let i = 0;
187
+ do {
188
+ uid = generateUid(name, i);
189
+ i++;
190
+ } while (this.hasBinding(uid));
191
+
192
+ return uid;
193
+ }
194
+ }
195
+
196
+ function getCache(node) {
197
+ return node[CACHE_KEY];
198
+ }
199
+
200
+ function toIdentifier(nameHint) {
201
+ let name = nameHint + "";
202
+ name = name.replace(/[^a-zA-Z0-9$_]/g, "");
203
+
204
+ name = name.replace(/^[-0-9]+/, "");
205
+ name = name.replace(/[-\s]+(.)?/, (match, c) => {
206
+ return c ? c.toUpperCase() : "";
207
+ });
208
+
209
+ name = name.replace(/^_+/, "").replace(/[0-9]+$/, "");
210
+ return name;
211
+ }
212
+
213
+ function generateUid(name, i) {
214
+ if (i > 0) {
215
+ return `_${name}$${i}`;
216
+ }
217
+ return `_${name}`;
218
+ }