@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.0-alpha.2

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 -4
  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 +665 -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,402 @@
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
+ Node,
18
+ Identifier,
19
+ SequenceExpression,
20
+ type,
21
+ alias,
22
+ visitor,
23
+ StringLiteral
24
+ } from "../melody-types/index.js";
25
+
26
+ export class AutoescapeBlock extends Node {
27
+ /**
28
+ *
29
+ * @param {String | boolean} type
30
+ * @param {Array<Node>} [expressions]
31
+ */
32
+ constructor(type, expressions = []) {
33
+ super();
34
+ this.escapeType = type;
35
+ this.expressions = expressions;
36
+ }
37
+ }
38
+ type(AutoescapeBlock, "AutoescapeBlock");
39
+ alias(AutoescapeBlock, "Block", "Escape");
40
+ visitor(AutoescapeBlock, "expressions");
41
+
42
+ export class BlockStatement extends Node {
43
+ /**
44
+ * @param {Identifier} name
45
+ * @param {Node} body
46
+ */
47
+ constructor(name, body) {
48
+ super();
49
+ this.name = name;
50
+ this.body = body;
51
+ }
52
+ }
53
+ type(BlockStatement, "BlockStatement");
54
+ alias(BlockStatement, "Statement", "Scope", "RootScope");
55
+ visitor(BlockStatement, "body");
56
+
57
+ export class BlockCallExpression extends Node {
58
+ /**
59
+ * @param {StringLiteral} callee
60
+ * @param {Array<Node>} args
61
+ */
62
+ constructor(callee, args = []) {
63
+ super();
64
+ this.callee = callee;
65
+ this.arguments = args;
66
+ }
67
+ }
68
+ type(BlockCallExpression, "BlockCallExpression");
69
+ alias(BlockCallExpression, "Expression", "FunctionInvocation");
70
+ visitor(BlockCallExpression, "arguments");
71
+
72
+ export class MountStatement extends Node {
73
+ /**
74
+ * Constructs an instance with the given parameters.
75
+ *
76
+ * @param {Identifier} [name]
77
+ * @param {string} [source]
78
+ * @param {Node} [key]
79
+ * @param {Node} [argument]
80
+ * @param {boolean} [async]
81
+ * @param {number} [delayBy]
82
+ */
83
+ constructor(name, source, key, argument, async, delayBy) {
84
+ super();
85
+ this.name = name;
86
+ this.source = source;
87
+ this.key = key;
88
+ this.argument = argument;
89
+ this.async = async;
90
+ this.delayBy = delayBy;
91
+ this.errorVariableName = null;
92
+ this.body = null;
93
+ this.otherwise = null;
94
+ }
95
+ }
96
+ type(MountStatement, "MountStatement");
97
+ alias(MountStatement, "Statement", "Scope");
98
+ visitor(
99
+ MountStatement,
100
+ "name",
101
+ "source",
102
+ "key",
103
+ "argument",
104
+ "body",
105
+ "otherwise"
106
+ );
107
+
108
+ export class DoStatement extends Node {
109
+ /**
110
+ * @param {Node} expression
111
+ */
112
+ constructor(expression) {
113
+ super();
114
+ this.value = expression;
115
+ }
116
+ }
117
+ type(DoStatement, "DoStatement");
118
+ alias(DoStatement, "Statement");
119
+ visitor(DoStatement, "value");
120
+
121
+ export class EmbedStatement extends Node {
122
+ /**
123
+ * @param {Node} parent
124
+ */
125
+ constructor(parent) {
126
+ super();
127
+ this.parent = parent;
128
+ this.argument = null;
129
+ this.contextFree = false;
130
+ // when `true`, missing templates will be ignored
131
+ this.ignoreMissing = false;
132
+ this.blocks = null;
133
+ }
134
+ }
135
+ type(EmbedStatement, "EmbedStatement");
136
+ alias(EmbedStatement, "Statement", "Include");
137
+ visitor(EmbedStatement, "argument", "blocks");
138
+
139
+ export class ExtendsStatement extends Node {
140
+ /**
141
+ * @param {Node} parentName
142
+ */
143
+ constructor(parentName) {
144
+ super();
145
+ this.parentName = parentName;
146
+ }
147
+ }
148
+ type(ExtendsStatement, "ExtendsStatement");
149
+ alias(ExtendsStatement, "Statement", "Include");
150
+ visitor(ExtendsStatement, "parentName");
151
+
152
+ export class FilterBlockStatement extends Node {
153
+ /**
154
+ * @param {Node} filterExpression
155
+ * @param {Node} body
156
+ */
157
+ constructor(filterExpression, body) {
158
+ super();
159
+ this.filterExpression = filterExpression;
160
+ this.body = body;
161
+ }
162
+ }
163
+ type(FilterBlockStatement, "FilterBlockStatement");
164
+ alias(FilterBlockStatement, "Statement", "Block");
165
+ visitor(FilterBlockStatement, "filterExpression", "body");
166
+
167
+ export class FlushStatement extends Node {
168
+ constructor() {
169
+ super();
170
+ }
171
+ }
172
+ type(FlushStatement, "FlushStatement");
173
+ alias(FlushStatement, "Statement");
174
+
175
+ export class ForStatement extends Node {
176
+ /**
177
+ * @param {Identifier} keyTarget
178
+ * @param {Identifier} valueTarget
179
+ * @param {Node} sequence
180
+ * @param {Node} condition
181
+ * @param {Node} body
182
+ * @param {Node} otherwise
183
+ */
184
+ constructor(
185
+ keyTarget = null,
186
+ valueTarget = null,
187
+ sequence = null,
188
+ condition = null,
189
+ body = null,
190
+ otherwise = null
191
+ ) {
192
+ super();
193
+ this.keyTarget = keyTarget;
194
+ this.valueTarget = valueTarget;
195
+ this.sequence = sequence;
196
+ this.condition = condition;
197
+ this.body = body;
198
+ this.otherwise = otherwise;
199
+ }
200
+ }
201
+ type(ForStatement, "ForStatement");
202
+ alias(ForStatement, "Statement", "Scope", "Loop");
203
+ visitor(
204
+ ForStatement,
205
+ "keyTarget",
206
+ "valueTarget",
207
+ "sequence",
208
+ "condition",
209
+ "body",
210
+ "otherwise"
211
+ );
212
+
213
+ export class ImportDeclaration extends Node {
214
+ /**
215
+ * @param {Node} key
216
+ * @param {Identifier} alias
217
+ */
218
+ constructor(key, alias) {
219
+ super();
220
+ this.key = key;
221
+ this.alias = alias;
222
+ }
223
+ }
224
+ type(ImportDeclaration, "ImportDeclaration");
225
+ alias(ImportDeclaration, "VariableDeclaration");
226
+ visitor(ImportDeclaration, "key", "value");
227
+
228
+ export class FromStatement extends Node {
229
+ /**
230
+ * @param {Node} source
231
+ * @param {Array<ImportDeclaration>} imports
232
+ */
233
+ constructor(source, imports) {
234
+ super();
235
+ this.source = source;
236
+ this.imports = imports;
237
+ }
238
+ }
239
+ type(FromStatement, "FromStatement");
240
+ alias(FromStatement, "Statement");
241
+ visitor(FromStatement, "source", "imports");
242
+
243
+ export class IfStatement extends Node {
244
+ /**
245
+ * @param {Node} test
246
+ * @param {Node} [consequent]
247
+ * @param {Node} [alternate]
248
+ */
249
+ constructor(test, consequent = null, alternate = null) {
250
+ super();
251
+ this.test = test;
252
+ this.consequent = consequent;
253
+ this.alternate = alternate;
254
+ }
255
+ }
256
+ type(IfStatement, "IfStatement");
257
+ alias(IfStatement, "Statement", "Conditional");
258
+ visitor(IfStatement, "test", "consequent", "alternate");
259
+
260
+ export class IncludeStatement extends Node {
261
+ /**
262
+ * @param {Node} source
263
+ */
264
+ constructor(source) {
265
+ super();
266
+ this.source = source;
267
+ this.argument = null;
268
+ this.contextFree = false;
269
+ // when `true`, missing templates will be ignored
270
+ this.ignoreMissing = false;
271
+ }
272
+ }
273
+ type(IncludeStatement, "IncludeStatement");
274
+ alias(IncludeStatement, "Statement", "Include");
275
+ visitor(IncludeStatement, "source", "argument");
276
+
277
+ export class MacroDeclarationStatement extends Node {
278
+ /**
279
+ * @param {Identifier} name
280
+ * @param {Array<Node>} args
281
+ * @param {SequenceExpression} body
282
+ */
283
+ constructor(name, args, body) {
284
+ super();
285
+ this.name = name;
286
+ this.arguments = args;
287
+ this.body = body;
288
+ }
289
+ }
290
+ type(MacroDeclarationStatement, "MacroDeclarationStatement");
291
+ alias(MacroDeclarationStatement, "Statement", "Scope", "RootScope");
292
+ visitor(MacroDeclarationStatement, "name", "arguments", "body");
293
+
294
+ export class VariableDeclarationStatement extends Node {
295
+ /**
296
+ * @param {Identifier} name
297
+ * @param {Node} value
298
+ */
299
+ constructor(name, value) {
300
+ super();
301
+ this.name = name;
302
+ this.value = value;
303
+ }
304
+ }
305
+ type(VariableDeclarationStatement, "VariableDeclarationStatement");
306
+ alias(VariableDeclarationStatement, "Statement");
307
+ visitor(VariableDeclarationStatement, "name", "value");
308
+
309
+ export class SetStatement extends Node {
310
+ /**
311
+ * @param {Array<VariableDeclarationStatement>} assignments
312
+ */
313
+ constructor(assignments) {
314
+ super();
315
+ this.assignments = assignments;
316
+ }
317
+ }
318
+ type(SetStatement, "SetStatement");
319
+ alias(SetStatement, "Statement", "ContextMutation");
320
+ visitor(SetStatement, "assignments");
321
+
322
+ export class SpacelessBlock extends Node {
323
+ /**
324
+ * @param {Node} [body]
325
+ */
326
+ constructor(body = null) {
327
+ super();
328
+ this.body = body;
329
+ }
330
+ }
331
+ type(SpacelessBlock, "SpacelessBlock");
332
+ alias(SpacelessBlock, "Statement", "Block");
333
+ visitor(SpacelessBlock, "body");
334
+
335
+ export class AliasExpression extends Node {
336
+ /**
337
+ * @param {Identifier} name
338
+ * @param {Identifier} alias
339
+ */
340
+ constructor(name, alias) {
341
+ super();
342
+ this.name = name;
343
+ this.alias = alias;
344
+ }
345
+ }
346
+ type(AliasExpression, "AliasExpression");
347
+ alias(AliasExpression, "Expression");
348
+ visitor(AliasExpression, "name", "alias");
349
+
350
+ export class UseStatement extends Node {
351
+ /**
352
+ * @param {Node} source
353
+ * @param {Array<AliasExpression>} aliases
354
+ */
355
+ constructor(source, aliases) {
356
+ super();
357
+ this.source = source;
358
+ this.aliases = aliases;
359
+ }
360
+ }
361
+ type(UseStatement, "UseStatement");
362
+ alias(UseStatement, "Statement", "Include");
363
+ visitor(UseStatement, "source", "aliases");
364
+
365
+ export {
366
+ UnaryNotExpression,
367
+ UnaryNeqExpression,
368
+ UnaryPosExpression,
369
+ BinaryOrExpression,
370
+ BinaryAndExpression,
371
+ BitwiseOrExpression,
372
+ BitwiseXorExpression,
373
+ BitwiseAndExpression,
374
+ BinaryEqualsExpression,
375
+ BinaryNotEqualsExpression,
376
+ BinaryLessThanExpression,
377
+ BinaryGreaterThanExpression,
378
+ BinaryLessThanOrEqualExpression,
379
+ BinaryGreaterThanOrEqualExpression,
380
+ BinaryNotInExpression,
381
+ BinaryInExpression,
382
+ BinaryMatchesExpression,
383
+ BinaryStartsWithExpression,
384
+ BinaryEndsWithExpression,
385
+ BinaryRangeExpression,
386
+ BinaryAddExpression,
387
+ BinaryMulExpression,
388
+ BinaryDivExpression,
389
+ BinaryFloorDivExpression,
390
+ BinaryModExpression,
391
+ BinaryPowerExpression,
392
+ BinaryNullCoalesceExpression,
393
+ TestEvenExpression,
394
+ TestOddExpression,
395
+ TestDefinedExpression,
396
+ TestSameAsExpression,
397
+ TestNullExpression,
398
+ TestDivisibleByExpression,
399
+ TestConstantExpression,
400
+ TestEmptyExpression,
401
+ TestIterableExpression
402
+ } from "./operators.js";
@@ -0,0 +1,146 @@
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 * as t from "@babel/types";
17
+ import _template from "@babel/template";
18
+ const template = _template.default;
19
+ // use default value if var is null, undefined or an empty string
20
+ // but use var if value is 0, false, an empty array or an empty object
21
+ const defaultFilter = template("VAR != null && VAR !== '' ? VAR : DEFAULT");
22
+
23
+ export default {
24
+ capitalize: "lodash",
25
+ first: "lodash",
26
+ last: "lodash",
27
+ keys: "lodash",
28
+ default(path) {
29
+ // babel-template transforms it to an expression statement
30
+ // but we really need an expression here, so unwrap it
31
+ path.replaceWithJS(
32
+ defaultFilter({
33
+ VAR: path.node.target,
34
+ DEFAULT: path.node.arguments[0] || t.stringLiteral("")
35
+ }).expression
36
+ );
37
+ },
38
+ abs(path) {
39
+ // todo throw error if arguments exist
40
+ path.replaceWithJS(
41
+ t.callExpression(
42
+ t.memberExpression(t.identifier("Math"), t.identifier("abs")),
43
+ [path.node.target]
44
+ )
45
+ );
46
+ },
47
+ join(path) {
48
+ path.replaceWithJS(
49
+ t.callExpression(
50
+ t.memberExpression(path.node.target, t.identifier("join")),
51
+ path.node.arguments
52
+ )
53
+ );
54
+ },
55
+ json_encode(path) {
56
+ // todo: handle arguments
57
+ path.replaceWithJS(
58
+ t.callExpression(
59
+ t.memberExpression(
60
+ t.identifier("JSON"),
61
+ t.identifier("stringify")
62
+ ),
63
+ [path.node.target]
64
+ )
65
+ );
66
+ },
67
+ length(path) {
68
+ path.replaceWithJS(
69
+ t.memberExpression(path.node.target, t.identifier("length"))
70
+ );
71
+ },
72
+ lower(path) {
73
+ path.replaceWithJS(
74
+ t.callExpression(
75
+ t.memberExpression(
76
+ path.node.target,
77
+ t.identifier("toLowerCase")
78
+ ),
79
+ []
80
+ )
81
+ );
82
+ },
83
+ upper(path) {
84
+ path.replaceWithJS(
85
+ t.callExpression(
86
+ t.memberExpression(
87
+ path.node.target,
88
+ t.identifier("toUpperCase")
89
+ ),
90
+ []
91
+ )
92
+ );
93
+ },
94
+ slice(path) {
95
+ path.replaceWithJS(
96
+ t.callExpression(
97
+ t.memberExpression(path.node.target, t.identifier("slice")),
98
+ path.node.arguments
99
+ )
100
+ );
101
+ },
102
+ sort(path) {
103
+ path.replaceWithJS(
104
+ t.callExpression(
105
+ t.memberExpression(path.node.target, t.identifier("sort")),
106
+ path.node.arguments
107
+ )
108
+ );
109
+ },
110
+ split(path) {
111
+ path.replaceWithJS(
112
+ t.callExpression(
113
+ t.memberExpression(path.node.target, t.identifier("split")),
114
+ path.node.arguments
115
+ )
116
+ );
117
+ },
118
+ convert_encoding(path) {
119
+ // encoding conversion is not supported
120
+ path.replaceWith(path.node.target);
121
+ },
122
+ date_modify(path) {
123
+ path.replaceWithJS(
124
+ t.callExpression(
125
+ t.identifier(
126
+ path.state.addImportFrom("melody-runtime", "strtotime")
127
+ ),
128
+ [path.node.arguments[0], path.node.target]
129
+ )
130
+ );
131
+ },
132
+ date(path) {
133
+ // Not really happy about this since moment.js could well be incompatible with
134
+ // the default twig behaviour
135
+ // might need to switch to an actual strftime implementation
136
+ path.replaceWithJS(
137
+ t.callExpression(
138
+ t.callExpression(
139
+ t.identifier(path.state.addDefaultImportFrom("moment")),
140
+ [path.node.target]
141
+ ),
142
+ [path.node.arguments[0]]
143
+ )
144
+ );
145
+ }
146
+ };