@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.
- package/README.md +5 -7
- package/package.json +10 -8
- package/src/index.js +53 -53
- package/src/melody/melody-code-frame/index.js +44 -0
- package/src/melody/melody-code-frame/lineNumbers.js +45 -0
- package/src/melody/melody-extension-core/index.js +167 -0
- package/src/melody/melody-extension-core/operators.js +414 -0
- package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
- package/src/melody/melody-extension-core/parser/block.js +93 -0
- package/src/melody/melody-extension-core/parser/do.js +32 -0
- package/src/melody/melody-extension-core/parser/embed.js +76 -0
- package/src/melody/melody-extension-core/parser/extends.js +35 -0
- package/src/melody/melody-extension-core/parser/filter.js +63 -0
- package/src/melody/melody-extension-core/parser/flush.js +33 -0
- package/src/melody/melody-extension-core/parser/for.js +110 -0
- package/src/melody/melody-extension-core/parser/from.js +63 -0
- package/src/melody/melody-extension-core/parser/if.js +100 -0
- package/src/melody/melody-extension-core/parser/import.js +44 -0
- package/src/melody/melody-extension-core/parser/include.js +48 -0
- package/src/melody/melody-extension-core/parser/macro.js +94 -0
- package/src/melody/melody-extension-core/parser/mount.js +147 -0
- package/src/melody/melody-extension-core/parser/set.js +103 -0
- package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
- package/src/melody/melody-extension-core/parser/use.js +58 -0
- package/src/melody/melody-extension-core/types.js +402 -0
- package/src/melody/melody-extension-core/visitors/filters.js +146 -0
- package/src/melody/melody-extension-core/visitors/for.js +391 -0
- package/src/melody/melody-extension-core/visitors/functions.js +100 -0
- package/src/melody/melody-extension-core/visitors/tests.js +127 -0
- package/src/melody/melody-parser/Associativity.js +17 -0
- package/src/melody/melody-parser/CharStream.js +82 -0
- package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
- package/src/melody/melody-parser/GenericTagParser.js +53 -0
- package/src/melody/melody-parser/Lexer.js +708 -0
- package/src/melody/melody-parser/Parser.js +954 -0
- package/src/melody/melody-parser/TokenStream.js +183 -0
- package/src/melody/melody-parser/TokenTypes.js +74 -0
- package/src/melody/melody-parser/elementInfo.js +43 -0
- package/src/melody/melody-parser/index.js +94 -0
- package/src/melody/melody-parser/util.js +100 -0
- package/src/melody/melody-traverse/Binding.js +61 -0
- package/src/melody/melody-traverse/Path.js +427 -0
- package/src/melody/melody-traverse/Scope.js +218 -0
- package/src/melody/melody-traverse/TraversalContext.js +166 -0
- package/src/melody/melody-traverse/index.js +21 -0
- package/src/melody/melody-traverse/traverse.js +48 -0
- package/src/melody/melody-traverse/visitors.js +112 -0
- package/src/melody/melody-types/index.js +506 -0
- package/src/parser.js +7 -2
- package/src/print/Attribute.js +1 -1
- package/src/print/BinaryExpression.js +2 -2
- package/src/print/BlockStatement.js +1 -1
- package/src/print/CallExpression.js +1 -1
- package/src/print/ExpressionStatement.js +1 -1
- package/src/print/FilterExpression.js +1 -1
- package/src/print/GenericTwigTag.js +1 -1
- package/src/print/IfStatement.js +1 -1
- package/src/print/ObjectProperty.js +1 -1
- package/src/print/SetStatement.js +1 -1
- package/src/print/SwitchTag.js +1 -1
- package/src/print/UnarySubclass.js +1 -1
- package/src/util/printFunctions.js +1 -1
- package/src/util/publicFunctions.js +1 -1
|
@@ -0,0 +1,391 @@
|
|
|
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 { traverse } from "../../melody-traverse/index.js";
|
|
17
|
+
import * as t from "@babel/types";
|
|
18
|
+
import _template from "@babel/template";
|
|
19
|
+
const babelTemplate = _template.default;
|
|
20
|
+
|
|
21
|
+
// @param template
|
|
22
|
+
// @returns function
|
|
23
|
+
// @param context context bindings
|
|
24
|
+
// @returns {exprStmt, initDecl, forStmt}
|
|
25
|
+
const template = tpl => {
|
|
26
|
+
return ctx => parseExpr(babelTemplate(tpl)(ctx));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const forWithContext = template(`
|
|
30
|
+
{
|
|
31
|
+
let SEQUENCE = SOURCE,
|
|
32
|
+
KEY_TARGET = 0,
|
|
33
|
+
LENGTH = SEQUENCE.length,
|
|
34
|
+
SUB_CONTEXT = CREATE_SUB_CONTEXT(CONTEXT, {
|
|
35
|
+
VALUE_TARGET: SEQUENCE[0],
|
|
36
|
+
loop: {
|
|
37
|
+
index: 1,
|
|
38
|
+
index0: 0,
|
|
39
|
+
length: LENGTH,
|
|
40
|
+
revindex: LENGTH,
|
|
41
|
+
revindex0: LENGTH - 1,
|
|
42
|
+
first: true,
|
|
43
|
+
last: 1 === LENGTH
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
for (;
|
|
47
|
+
KEY_TARGET < LENGTH;
|
|
48
|
+
KEY_TARGET++
|
|
49
|
+
) {
|
|
50
|
+
SUB_CONTEXT.loop.index0++;
|
|
51
|
+
SUB_CONTEXT.loop.index++;
|
|
52
|
+
SUB_CONTEXT.loop.revindex--;
|
|
53
|
+
SUB_CONTEXT.loop.revindex0--;
|
|
54
|
+
SUB_CONTEXT.loop.first = false;
|
|
55
|
+
SUB_CONTEXT.loop.last = SUB_CONTEXT.loop.revindex === 0;
|
|
56
|
+
SUB_CONTEXT.VALUE_TARGET = _sequence[KEY_TARGET + 1];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
`);
|
|
60
|
+
|
|
61
|
+
const basicFor = template(`
|
|
62
|
+
{
|
|
63
|
+
let SEQUENCE = SOURCE,
|
|
64
|
+
KEY_TARGET = 0,
|
|
65
|
+
LENGTH = SEQUENCE.length,
|
|
66
|
+
VALUE_TARGET = SEQUENCE[0];
|
|
67
|
+
for (;
|
|
68
|
+
KEY_TARGET < LENGTH;
|
|
69
|
+
KEY_TARGET++,
|
|
70
|
+
VALUE_TARGET = SEQUENCE[_index]
|
|
71
|
+
) {
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
|
|
76
|
+
const localFor = template(`
|
|
77
|
+
{
|
|
78
|
+
let SEQUENCE = SOURCE,
|
|
79
|
+
KEY_TARGET = 0,
|
|
80
|
+
LENGTH = SEQUENCE.length,
|
|
81
|
+
VALUE_TARGET = SEQUENCE[0],
|
|
82
|
+
INDEX_BY_1 = 1,
|
|
83
|
+
REVERSE_INDEX_BY_1 = LENGTH,
|
|
84
|
+
REVERSE_INDEX = LENGTH - 1,
|
|
85
|
+
FIRST = true,
|
|
86
|
+
LAST = 1 === LENGTH;
|
|
87
|
+
for (;
|
|
88
|
+
KEY_TARGET < LENGTH;
|
|
89
|
+
KEY_TARGET++,
|
|
90
|
+
VALUE_TARGET = SEQUENCE[_index]
|
|
91
|
+
) {
|
|
92
|
+
INDEX_BY_1++;
|
|
93
|
+
REVERSE_INDEX_BY_1--;
|
|
94
|
+
REVERSE_INDEX--;
|
|
95
|
+
FIRST = false;
|
|
96
|
+
LAST = REVERSE_INDEX === 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
`);
|
|
100
|
+
|
|
101
|
+
// returns an object that has the whole expression, init declarations, for loop
|
|
102
|
+
// statement in respective properties.
|
|
103
|
+
function parseExpr(exprStmt) {
|
|
104
|
+
return {
|
|
105
|
+
exprStmt: exprStmt,
|
|
106
|
+
initDecl: exprStmt.body[0].declarations,
|
|
107
|
+
forStmt: exprStmt.body[1]
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export default {
|
|
112
|
+
analyse: {
|
|
113
|
+
ForStatement: {
|
|
114
|
+
enter(path) {
|
|
115
|
+
const forStmt = path.node;
|
|
116
|
+
const scope = path.scope;
|
|
117
|
+
if (forStmt.keyTarget) {
|
|
118
|
+
scope.registerBinding(
|
|
119
|
+
forStmt.keyTarget.name,
|
|
120
|
+
path.get("keyTarget"),
|
|
121
|
+
"var"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
if (forStmt.valueTarget) {
|
|
125
|
+
scope.registerBinding(
|
|
126
|
+
forStmt.valueTarget.name,
|
|
127
|
+
path.get("valueTarget"),
|
|
128
|
+
"var"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
scope.registerBinding("loop", path, "var");
|
|
132
|
+
},
|
|
133
|
+
exit(path) {
|
|
134
|
+
const sequenceName = path.scope.generateUid("sequence");
|
|
135
|
+
const lenName = path.scope.generateUid("length");
|
|
136
|
+
path.scope.registerBinding(sequenceName, path, "var");
|
|
137
|
+
path.scope.registerBinding(lenName, path, "var");
|
|
138
|
+
let iName;
|
|
139
|
+
if (path.node.keyTarget) {
|
|
140
|
+
iName = path.node.keyTarget.name;
|
|
141
|
+
} else {
|
|
142
|
+
iName = path.scope.generateUid("index0");
|
|
143
|
+
path.scope.registerBinding(iName, path, "var");
|
|
144
|
+
}
|
|
145
|
+
path.setData("forStatement.variableLookup", {
|
|
146
|
+
sequenceName,
|
|
147
|
+
lenName,
|
|
148
|
+
iName
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
if (path.scope.escapesContext) {
|
|
152
|
+
const contextName = path.scope.generateUid("context");
|
|
153
|
+
path.scope.registerBinding(contextName, path, "const");
|
|
154
|
+
path.scope.contextName = contextName;
|
|
155
|
+
path.scope.getBinding("loop").kind = "context";
|
|
156
|
+
if (path.node.valueTarget) {
|
|
157
|
+
path.scope.getBinding(path.node.valueTarget.name).kind =
|
|
158
|
+
"context";
|
|
159
|
+
}
|
|
160
|
+
} else if (path.scope.getBinding("loop").references) {
|
|
161
|
+
const indexName = path.scope.generateUid("index");
|
|
162
|
+
path.scope.registerBinding(indexName, path, "var");
|
|
163
|
+
const revindexName = path.scope.generateUid("revindex");
|
|
164
|
+
path.scope.registerBinding(revindexName, path, "var");
|
|
165
|
+
const revindex0Name = path.scope.generateUid("revindex0");
|
|
166
|
+
path.scope.registerBinding(revindex0Name, path, "var");
|
|
167
|
+
const firstName = path.scope.generateUid("first");
|
|
168
|
+
path.scope.registerBinding(firstName, path, "var");
|
|
169
|
+
const lastName = path.scope.generateUid("last");
|
|
170
|
+
path.scope.registerBinding(lastName, path, "var");
|
|
171
|
+
|
|
172
|
+
const lookupTable = {
|
|
173
|
+
index: indexName,
|
|
174
|
+
index0: iName,
|
|
175
|
+
length: lenName,
|
|
176
|
+
revindex: revindexName,
|
|
177
|
+
revindex0: revindex0Name,
|
|
178
|
+
first: firstName,
|
|
179
|
+
last: lastName
|
|
180
|
+
};
|
|
181
|
+
path.setData("forStatement.loopLookup", lookupTable);
|
|
182
|
+
|
|
183
|
+
const loopBinding = path.scope.getBinding("loop");
|
|
184
|
+
for (const loopPath of loopBinding.referencePaths) {
|
|
185
|
+
const memExpr = loopPath.parentPath;
|
|
186
|
+
|
|
187
|
+
if (memExpr.is("MemberExpression")) {
|
|
188
|
+
const typeName = memExpr.node.property.name;
|
|
189
|
+
if (typeName === "index0") {
|
|
190
|
+
memExpr.replaceWithJS({
|
|
191
|
+
type: "BinaryExpression",
|
|
192
|
+
operator: "-",
|
|
193
|
+
left: {
|
|
194
|
+
type: "Identifier",
|
|
195
|
+
name: indexName
|
|
196
|
+
},
|
|
197
|
+
right: { type: "NumericLiteral", value: 1 },
|
|
198
|
+
extra: {
|
|
199
|
+
parenthesized: true
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
} else {
|
|
203
|
+
memExpr.replaceWithJS({
|
|
204
|
+
type: "Identifier",
|
|
205
|
+
name: lookupTable[typeName]
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
convert: {
|
|
215
|
+
ForStatement: {
|
|
216
|
+
enter(path) {
|
|
217
|
+
if (path.scope.escapesContext) {
|
|
218
|
+
const parentContextName = path.scope.parent.contextName;
|
|
219
|
+
if (path.node.otherwise) {
|
|
220
|
+
const alternate = path.get("otherwise");
|
|
221
|
+
if (alternate.is("Scope")) {
|
|
222
|
+
alternate.scope.contextName = parentContextName;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const sequence = path.get("sequence");
|
|
227
|
+
|
|
228
|
+
if (sequence.is("Identifier")) {
|
|
229
|
+
sequence.setData(
|
|
230
|
+
"Identifier.contextName",
|
|
231
|
+
parentContextName
|
|
232
|
+
);
|
|
233
|
+
} else {
|
|
234
|
+
traverse(path.node.sequence, {
|
|
235
|
+
Identifier(id) {
|
|
236
|
+
id.setData(
|
|
237
|
+
"Identifier.contextName",
|
|
238
|
+
parentContextName
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
exit(path) {
|
|
246
|
+
const node = path.node;
|
|
247
|
+
const { sequenceName, lenName, iName } = path.getData(
|
|
248
|
+
"forStatement.variableLookup"
|
|
249
|
+
);
|
|
250
|
+
let expr;
|
|
251
|
+
if (path.scope.escapesContext) {
|
|
252
|
+
const contextName = path.scope.contextName;
|
|
253
|
+
expr = forWithContext({
|
|
254
|
+
CONTEXT: t.identifier(path.scope.parent.contextName),
|
|
255
|
+
SUB_CONTEXT: t.identifier(contextName),
|
|
256
|
+
CREATE_SUB_CONTEXT: t.identifier(
|
|
257
|
+
this.addImportFrom(
|
|
258
|
+
"melody-runtime",
|
|
259
|
+
"createSubContext"
|
|
260
|
+
)
|
|
261
|
+
),
|
|
262
|
+
KEY_TARGET: t.identifier(iName),
|
|
263
|
+
SOURCE: path.get("sequence").node,
|
|
264
|
+
SEQUENCE: t.identifier(sequenceName),
|
|
265
|
+
LENGTH: t.identifier(lenName),
|
|
266
|
+
VALUE_TARGET: node.valueTarget
|
|
267
|
+
});
|
|
268
|
+
if (node.keyTarget) {
|
|
269
|
+
expr.forStmt.body.body.push({
|
|
270
|
+
type: "ExpressionStatement",
|
|
271
|
+
expression: {
|
|
272
|
+
type: "AssignmentExpression",
|
|
273
|
+
operator: "=",
|
|
274
|
+
left: {
|
|
275
|
+
type: "MemberExpression",
|
|
276
|
+
object: {
|
|
277
|
+
type: "Identifier",
|
|
278
|
+
name: contextName
|
|
279
|
+
},
|
|
280
|
+
property: {
|
|
281
|
+
type: "Identifier",
|
|
282
|
+
name: node.keyTarget.name
|
|
283
|
+
},
|
|
284
|
+
computed: false
|
|
285
|
+
},
|
|
286
|
+
right: {
|
|
287
|
+
type: "Identifier",
|
|
288
|
+
name: iName
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
expr.initDecl[
|
|
293
|
+
expr.initDecl.length - 1
|
|
294
|
+
].init.arguments[1].properties.push({
|
|
295
|
+
type: "ObjectProperty",
|
|
296
|
+
method: false,
|
|
297
|
+
shorthand: false,
|
|
298
|
+
computed: false,
|
|
299
|
+
key: {
|
|
300
|
+
type: "Identifier",
|
|
301
|
+
name: node.keyTarget.name
|
|
302
|
+
},
|
|
303
|
+
value: {
|
|
304
|
+
type: "Identifier",
|
|
305
|
+
name: iName
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
} else if (path.scope.getBinding("loop").references) {
|
|
310
|
+
const {
|
|
311
|
+
index: indexName,
|
|
312
|
+
revindex: revindexName,
|
|
313
|
+
revindex0: revindex0Name,
|
|
314
|
+
first: firstName,
|
|
315
|
+
last: lastName
|
|
316
|
+
} = path.getData("forStatement.loopLookup");
|
|
317
|
+
|
|
318
|
+
expr = localFor({
|
|
319
|
+
KEY_TARGET: t.identifier(iName),
|
|
320
|
+
SOURCE: path.get("sequence").node,
|
|
321
|
+
SEQUENCE: t.identifier(sequenceName),
|
|
322
|
+
LENGTH: t.identifier(lenName),
|
|
323
|
+
VALUE_TARGET: node.valueTarget,
|
|
324
|
+
INDEX_BY_1: t.identifier(indexName),
|
|
325
|
+
REVERSE_INDEX: t.identifier(revindex0Name),
|
|
326
|
+
REVERSE_INDEX_BY_1: t.identifier(revindexName),
|
|
327
|
+
FIRST: t.identifier(firstName),
|
|
328
|
+
LAST: t.identifier(lastName)
|
|
329
|
+
});
|
|
330
|
+
} else {
|
|
331
|
+
expr = basicFor({
|
|
332
|
+
SEQUENCE: t.identifier(sequenceName),
|
|
333
|
+
SOURCE: path.get("sequence").node,
|
|
334
|
+
KEY_TARGET: t.identifier(iName),
|
|
335
|
+
LENGTH: t.identifier(lenName),
|
|
336
|
+
VALUE_TARGET: node.valueTarget
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
expr.forStmt.body.body.unshift(...path.get("body").node.body);
|
|
341
|
+
|
|
342
|
+
let uniteratedName;
|
|
343
|
+
if (node.otherwise) {
|
|
344
|
+
uniteratedName = path.scope.generateUid("uniterated");
|
|
345
|
+
path.scope.parent.registerBinding(
|
|
346
|
+
uniteratedName,
|
|
347
|
+
path,
|
|
348
|
+
"var"
|
|
349
|
+
);
|
|
350
|
+
expr.forStmt.body.body.push(
|
|
351
|
+
t.expressionStatement(
|
|
352
|
+
t.assignmentExpression(
|
|
353
|
+
"=",
|
|
354
|
+
t.identifier(uniteratedName),
|
|
355
|
+
t.booleanLiteral(false)
|
|
356
|
+
)
|
|
357
|
+
)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (node.condition) {
|
|
362
|
+
expr.forStmt.body = t.blockStatement([
|
|
363
|
+
{
|
|
364
|
+
type: "IfStatement",
|
|
365
|
+
test: node.condition,
|
|
366
|
+
consequent: t.blockStatement(expr.forStmt.body.body)
|
|
367
|
+
}
|
|
368
|
+
]);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (uniteratedName) {
|
|
372
|
+
path.replaceWithMultipleJS(
|
|
373
|
+
t.variableDeclaration("let", [
|
|
374
|
+
t.variableDeclarator(
|
|
375
|
+
t.identifier(uniteratedName),
|
|
376
|
+
t.booleanLiteral(true)
|
|
377
|
+
)
|
|
378
|
+
]),
|
|
379
|
+
expr.exprStmt,
|
|
380
|
+
t.ifStatement(
|
|
381
|
+
t.identifier(uniteratedName),
|
|
382
|
+
node.otherwise
|
|
383
|
+
)
|
|
384
|
+
);
|
|
385
|
+
} else {
|
|
386
|
+
path.replaceWithJS(expr.exprStmt);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
|
|
18
|
+
function addOne(expr) {
|
|
19
|
+
return t.binaryExpression("+", expr, t.numericLiteral(1));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default {
|
|
23
|
+
range(path) {
|
|
24
|
+
const args = path.node.arguments;
|
|
25
|
+
const callArgs = [];
|
|
26
|
+
if (args.length === 1) {
|
|
27
|
+
callArgs.push(addOne(args[0]));
|
|
28
|
+
} else if (args.length === 3) {
|
|
29
|
+
callArgs.push(args[0]);
|
|
30
|
+
callArgs.push(addOne(args[1]));
|
|
31
|
+
callArgs.push(args[2]);
|
|
32
|
+
} else if (args.length === 2) {
|
|
33
|
+
callArgs.push(args[0], addOne(args[1]));
|
|
34
|
+
} else {
|
|
35
|
+
path.state.error(
|
|
36
|
+
"Invalid range call",
|
|
37
|
+
path.node.pos,
|
|
38
|
+
`The range function accepts 1 to 3 arguments but you have specified ${args.length} arguments instead.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
path.replaceWithJS(
|
|
43
|
+
t.callExpression(
|
|
44
|
+
t.identifier(path.state.addImportFrom("lodash", "range")),
|
|
45
|
+
callArgs
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
},
|
|
49
|
+
// range: 'lodash',
|
|
50
|
+
dump(path) {
|
|
51
|
+
if (!path.parentPath.is("PrintExpressionStatement")) {
|
|
52
|
+
path.state.error(
|
|
53
|
+
"dump must be used in a lone expression",
|
|
54
|
+
path.node.pos,
|
|
55
|
+
"The dump function does not have a return value. Thus it must be used as the only expression."
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
path.parentPath.replaceWithJS(
|
|
59
|
+
t.expressionStatement(
|
|
60
|
+
t.callExpression(
|
|
61
|
+
t.memberExpression(
|
|
62
|
+
t.identifier("console"),
|
|
63
|
+
t.identifier("log")
|
|
64
|
+
),
|
|
65
|
+
path.node.arguments
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
include(path) {
|
|
71
|
+
if (!path.parentPath.is("PrintExpressionStatement")) {
|
|
72
|
+
path.state.error({
|
|
73
|
+
title: "Include function does not return value",
|
|
74
|
+
pos: path.node.loc.start,
|
|
75
|
+
advice: `The include function currently does not return a value.
|
|
76
|
+
Thus you must use it like a regular include tag.`
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const includeName = path.scope.generateUid("include");
|
|
80
|
+
const importDecl = t.importDeclaration(
|
|
81
|
+
[t.importDefaultSpecifier(t.identifier(includeName))],
|
|
82
|
+
path.node.arguments[0]
|
|
83
|
+
);
|
|
84
|
+
path.state.program.body.splice(0, 0, importDecl);
|
|
85
|
+
path.scope.registerBinding(includeName);
|
|
86
|
+
|
|
87
|
+
const argument = path.node.arguments[1];
|
|
88
|
+
|
|
89
|
+
const includeCall = t.expressionStatement(
|
|
90
|
+
t.callExpression(
|
|
91
|
+
t.memberExpression(
|
|
92
|
+
t.identifier(includeName),
|
|
93
|
+
t.identifier("render")
|
|
94
|
+
),
|
|
95
|
+
argument ? [argument] : []
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
path.replaceWithJS(includeCall);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
|
|
18
|
+
export default {
|
|
19
|
+
convert: {
|
|
20
|
+
TestEvenExpression: {
|
|
21
|
+
exit(path) {
|
|
22
|
+
const expr = t.unaryExpression(
|
|
23
|
+
"!",
|
|
24
|
+
t.binaryExpression(
|
|
25
|
+
"%",
|
|
26
|
+
path.get("expression").node,
|
|
27
|
+
t.numericLiteral(2)
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
expr.extra = { parenthesizedArgument: true };
|
|
31
|
+
path.replaceWithJS(expr);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
TestOddExpression: {
|
|
35
|
+
exit(path) {
|
|
36
|
+
const expr = t.unaryExpression(
|
|
37
|
+
"!",
|
|
38
|
+
t.unaryExpression(
|
|
39
|
+
"!",
|
|
40
|
+
t.binaryExpression(
|
|
41
|
+
"%",
|
|
42
|
+
path.get("expression").node,
|
|
43
|
+
t.numericLiteral(2)
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
expr.extra = { parenthesizedArgument: true };
|
|
48
|
+
path.replaceWithJS(expr);
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
TestDefinedExpression: {
|
|
52
|
+
exit(path) {
|
|
53
|
+
path.replaceWithJS(
|
|
54
|
+
t.binaryExpression(
|
|
55
|
+
"!==",
|
|
56
|
+
t.unaryExpression(
|
|
57
|
+
"typeof",
|
|
58
|
+
path.get("expression").node
|
|
59
|
+
),
|
|
60
|
+
t.stringLiteral("undefined")
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
TestEmptyExpression: {
|
|
66
|
+
exit(path) {
|
|
67
|
+
path.replaceWithJS(
|
|
68
|
+
t.callExpression(
|
|
69
|
+
t.identifier(
|
|
70
|
+
this.addImportFrom("melody-runtime", "isEmpty")
|
|
71
|
+
),
|
|
72
|
+
[path.get("expression").node]
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
TestSameAsExpression: {
|
|
78
|
+
exit(path) {
|
|
79
|
+
path.replaceWithJS(
|
|
80
|
+
t.binaryExpression(
|
|
81
|
+
"===",
|
|
82
|
+
path.get("expression").node,
|
|
83
|
+
path.get("arguments")[0].node
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
TestNullExpression: {
|
|
89
|
+
exit(path) {
|
|
90
|
+
path.replaceWithJS(
|
|
91
|
+
t.binaryExpression(
|
|
92
|
+
"===",
|
|
93
|
+
path.get("expression").node,
|
|
94
|
+
t.nullLiteral()
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
TestDivisibleByExpression: {
|
|
100
|
+
exit(path) {
|
|
101
|
+
path.replaceWithJS(
|
|
102
|
+
t.unaryExpression(
|
|
103
|
+
"!",
|
|
104
|
+
t.binaryExpression(
|
|
105
|
+
"%",
|
|
106
|
+
path.get("expression").node,
|
|
107
|
+
path.node.arguments[0]
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
TestIterableExpression: {
|
|
114
|
+
exit(path) {
|
|
115
|
+
path.replaceWithJS(
|
|
116
|
+
t.callExpression(
|
|
117
|
+
t.memberExpression(
|
|
118
|
+
t.identifier("Array"),
|
|
119
|
+
t.identifier("isArray")
|
|
120
|
+
),
|
|
121
|
+
[path.node.expression]
|
|
122
|
+
)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
export const LEFT = Symbol("LEFT");
|
|
17
|
+
export const RIGHT = Symbol("RIGHT");
|