/
transform.js
executable file
·464 lines (401 loc) · 13.7 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const recast = require('ember-template-recast');
const logger = require('../../lib/logger');
const KNOWN_HELPERS = require('./known-helpers');
const _EMPTY_STRING_ = `ANGLE_BRACKET_EMPTY_${Date.now()}`;
const { builders: b } = recast;
/**
* List of HTML attributes for which @ should not be appended
*/
const HTML_ATTRIBUTES = ['class', 'placeholder', 'required'];
const BUILT_IN_COMPONENTS = ['link-to', 'input', 'textarea'];
let inAttr = false;
function isAttribute(key) {
return HTML_ATTRIBUTES.includes(key) || isDataAttribute(key);
}
/**
* Returns true if the key matches any of the user provided regex from the
* `skipAttributesThatMatchRegex` array.
* @param {*} key
* @param {*} config
*/
function shouldSkipAttribute(key, config) {
if (config.skipAttributesThatMatchRegex && config.skipAttributesThatMatchRegex.length) {
return config.skipAttributesThatMatchRegex.some(rx => {
// Get the user provided string and convert it to regex.
const match = /^\/(.*)\/([a-z]*)$/.exec(rx);
if (match) {
const regex = new RegExp(match[1], match[2]);
return regex.test(key);
}
});
}
return false;
}
function isDataAttribute(key) {
return key.startsWith('data-');
}
function isBuiltInComponent(key) {
return BUILT_IN_COMPONENTS.includes(key);
}
function isNestedComponentTagName(tagName) {
return tagName && tagName.includes && (tagName.includes('/') || tagName.includes('-'));
}
function isWallStreet(tagName) {
return tagName.includes('$') || tagName.includes('::');
}
/**
* Returns a transformed capitalized tagname for angle brackets syntax
* {{my-component}} => MyComponent
*/
function transformTagName(tagName) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;
if (tagName.includes('.')) {
return tagName;
}
tagName = tagName.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === '/') {
return '::';
}
if (index === 0 || !ALPHA.test(tagName[index - 1])) {
return char.toUpperCase();
}
// Remove all occurrences of '-'s from the tagName that aren't starting with `-`
return char === '-' ? '' : char.toLowerCase();
});
return tagName;
}
function transformNestedSubExpression(subExpression) {
let positionalArgs = subExpression.params.map(param => {
if (param.type === 'SubExpression') {
return transformNestedSubExpression(param);
} else if (param.type === 'StringLiteral') {
return `"${param.original}"`;
} else {
return param.original;
}
});
let namedArgs = [];
if (subExpression.hash.pairs.length > 0) {
namedArgs = subExpression.hash.pairs.map(pair => {
if (pair.value.type === 'SubExpression') {
let nestedValue = transformNestedSubExpression(pair.value);
return `${pair.key}=${nestedValue}`;
} else {
if (pair.value.type === 'StringLiteral') {
return `${pair.key}="${pair.value.original}"`;
}
return `${pair.key}=${pair.value.original}`;
}
});
}
let args = positionalArgs.concat(namedArgs);
return `(${subExpression.path.original} ${args.join(' ')})`;
}
function shouldSkipFile(fileInfo, config) {
let source = fileInfo.source;
if (config.skipFilesThatMatchRegex && config.skipFilesThatMatchRegex.test(source)) {
logger.warn(
`WARNING: ${fileInfo.path} was skipped as its content matches the "skipFilesThatMatchRegex" config setting: ${config.skipFilesThatMatchRegex}`
);
return true;
}
return false;
}
function transformAttrs(tagName, attrs, config) {
return attrs.map(a => {
let _key = a.key;
let _valueType = a.value.type;
let _value;
if (
(!isAttribute(_key) || !isBuiltInComponent(tagName)) &&
!shouldSkipAttribute(_key, config)
) {
_key = `@${_key}`;
}
if (_valueType === 'PathExpression') {
_value = b.mustache(a.value);
} else if (_valueType === 'SubExpression') {
if (a.value.hash.pairs.length > 0) {
a.value.type = 'MustacheStatement';
_value = a.value;
} else {
const params = a.value.params
.map(p => {
if (p.type === 'SubExpression') {
return transformNestedSubExpression(p);
} else if (p.type === 'StringLiteral') {
return `"${p.original}"`;
} else if (p.type === 'NullLiteral') {
return 'null';
} else if (p.type === 'UndefinedLiteral') {
return 'undefined';
} else {
return p.original;
}
})
.join(' ');
_value = b.mustache(b.path(`${a.value.path.original} ${params}`));
}
} else if (_valueType === 'BooleanLiteral') {
_value = b.mustache(b.boolean(a.value.original));
} else if (_valueType === 'NumberLiteral') {
_value = b.mustache(b.number(a.value.original));
} else if (_valueType === 'NullLiteral') {
_value = b.mustache('null');
} else if (_valueType === 'UndefinedLiteral') {
_value = b.mustache('undefined');
} else {
_value = b.text(a.value.original || _EMPTY_STRING_);
}
return b.attr(_key, _value);
});
}
function isQueryParam(param) {
return (
param && param.type === 'SubExpression' && param.path && param.path.original === 'query-params'
);
}
function transformLinkToTextParam(textParam) {
if (textParam.type === 'SubExpression') {
return subExpressionToMustacheStatement(textParam);
} else if (textParam.type.includes('Literal')) {
return b.text(textParam.value);
} else {
return b.mustache(textParam.original);
}
}
function transformModelParams(modelParam) {
let type = modelParam.type;
if (type === 'StringLiteral') {
return b.text(modelParam.value);
} else if (type === 'NumberLiteral') {
return b.mustache(b.number(modelParam.original));
} else {
return b.mustache(modelParam.original);
}
}
function transformLinkToAttrs(params) {
let attributes = [];
let dataAttributes = getDataAttributesFromParams(params);
params = getNonDataAttributesFromParams(params);
let firstParamInput = params[0];
let firstParamOutput;
if (isQueryParam(firstParamInput)) {
firstParamOutput = b.attr('@query', b.mustache(b.path('hash'), [], firstParamInput.hash));
} else if (firstParamInput.type === 'PathExpression') {
firstParamOutput = b.attr('@route', b.mustache(firstParamInput.original));
} else if (firstParamInput.type === 'SubExpression') {
firstParamOutput = b.attr(
'@route',
b.mustache(firstParamInput.path, firstParamInput.params, firstParamInput.hash)
);
} else {
firstParamOutput = b.attr('@route', b.text(firstParamInput.value));
}
if (params.length === 1) {
attributes = [firstParamOutput];
} else if (params.length === 2) {
// @route and @model param
// eslint-disable-next-line no-unused-vars
let [_, secondParamInput] = params;
if (secondParamInput.type === 'SubExpression') {
let _queryParamOrModel;
if (isQueryParam(secondParamInput)) {
_queryParamOrModel = b.attr(
'@query',
b.mustache(b.path('hash'), [], secondParamInput.hash)
);
} else {
_queryParamOrModel = b.attr(
'@model',
b.mustache(secondParamInput.path, secondParamInput.params)
);
}
attributes = [firstParamOutput, _queryParamOrModel];
} else {
let _modelParam = b.attr('@model', transformModelParams(secondParamInput));
attributes = [firstParamOutput, _modelParam];
}
} else if (params.length > 2) {
// @route and @models params
// eslint-disable-next-line no-unused-vars
let [_, ...models] = params;
let hasQueryParamHelper = isQueryParam(models[models.length - 1]);
let _modelsParam;
let _qpParam;
if (hasQueryParamHelper) {
if (models.length < 3) {
_modelsParam = b.attr('@model', transformModelParams(models[0]));
} else {
_modelsParam = b.attr('@models', b.mustache(b.path('array'), models.slice().splice(0, 2)));
}
_qpParam = b.attr('@query', b.mustache(b.path('hash'), [], models[models.length - 1].hash));
} else {
_modelsParam = b.attr('@models', b.mustache(b.path('array'), models));
}
attributes = [firstParamOutput, _modelsParam];
if (_qpParam) {
attributes.push(_qpParam);
}
}
return attributes.concat(dataAttributes);
}
function hasValuelessDataParams(params) {
return getDataAttributesFromParams(params).length > 0;
}
function transformNodeAttributes(tagName, node, config) {
let attributes = transformAttrs(tagName, node.hash.pairs, config);
return node.params.concat(attributes);
}
function isDataAttrPathExpression(node) {
return node.type === 'PathExpression' && node.original.startsWith('data-');
}
function getDataAttributesFromParams(params) {
return params.filter(it => isDataAttrPathExpression(it));
}
function getNonDataAttributesFromParams(params) {
return params.filter(it => !isDataAttrPathExpression(it));
}
function shouldIgnoreMustacheStatement(fullName, config, invokableData) {
let { helpers, components } = invokableData;
let isTelemetryData = !!(helpers || components);
let name = fullName;
// replace `::` with `/`, and ignore the path before $
if (isWallStreet(name)) {
name = name
.split('$')
.pop()
.replace('::', '/');
}
if (isTelemetryData) {
let mergedHelpers = [...KNOWN_HELPERS, ...(helpers || [])];
let isHelper = mergedHelpers.includes(name) || config.helpers.includes(name);
let isComponent = [...(components || []), ...BUILT_IN_COMPONENTS].includes(name);
let strName = `${name}`; // coerce boolean and number to string
return (isHelper || !isComponent) && !strName.includes('.');
} else {
return !(KNOWN_HELPERS.includes(name) && config.helpers.includes(name));
}
}
function nodeHasPositionalParameters(node) {
if (node.params.length > 0) {
let firstParamType = node.params[0].type;
if (['StringLiteral', 'NumberLiteral', 'SubExpression'].includes(firstParamType)) {
return true;
} else if (firstParamType === 'PathExpression') {
if (!isAttribute(node.params[0].original)) {
return true;
}
}
}
return false;
}
function transformNode(node, fileInfo, config) {
if (hasValuelessDataParams(node.params)) {
return;
}
let selfClosing = node.type !== 'BlockStatement';
const tagName = node.path.original;
if (config.skipBuiltInComponents && isBuiltInComponent(tagName)) {
return;
}
if (node.inverse) {
return;
}
const newTagName = transformTagName(tagName, inAttr);
let attributes;
let children = node.program ? node.program.body : undefined;
let blockParams = node.program ? node.program.blockParams : undefined;
if (tagName === 'link-to') {
selfClosing = false;
if (node.type === 'MustacheStatement') {
let params = node.params.slice();
let textParam = params.shift(); //the first param becomes the block content
attributes = transformLinkToAttrs(params);
children = [transformLinkToTextParam(textParam)];
} else {
attributes = transformLinkToAttrs(node.params);
}
let namesParams = transformAttrs(tagName, node.hash.pairs, config);
attributes = attributes.concat(namesParams);
} else {
if (nodeHasPositionalParameters(node)) {
logger.warn(
`WARNING: {{${node.path.original}}} was not converted as it has positional parameters which can't be automatically converted. Source: ${fileInfo.path}`
);
return;
}
if (inAttr) {
return;
}
attributes = transformNodeAttributes(tagName, node, config);
}
return b.element(
{ name: newTagName, selfClosing },
{
attrs: attributes,
children,
blockParams,
}
);
}
function subExpressionToMustacheStatement(subExpression) {
return b.mustache(subExpression.path, subExpression.params, subExpression.hash);
}
module.exports = function transform(fileInfo, config, invokableData = {}) {
config = config || {};
config.helpers = config.helpers || [];
config.skipBuiltInComponents =
'skipBuiltInComponents' in config ? config.skipBuiltInComponents : false;
config.skipFilesThatMatchRegex = config.skipFilesThatMatchRegex || null;
if (shouldSkipFile(fileInfo, config)) {
return fileInfo.source;
}
let { code: toAngleBracket } = recast.transform(fileInfo.source, () =>
transformToAngleBracket(fileInfo, config, invokableData)
);
let attrEqualEmptyString = new RegExp(_EMPTY_STRING_, 'gi');
let dataEqualsNoValue = /(data-\S+)=""/gim;
toAngleBracket = toAngleBracket.replace(attrEqualEmptyString, '');
toAngleBracket = toAngleBracket.replace(dataEqualsNoValue, '$1');
return toAngleBracket;
};
function transformToAngleBracket(fileInfo, config, invokableData) {
/**
* Transform the attributes names & values properly
*/
return {
MustacheStatement(node) {
const tagName = `${node.path && node.path.original}`;
// Don't change attribute statements
const isValidMustache =
node.loc.source !== '(synthetic)' &&
!shouldIgnoreMustacheStatement(tagName, config, invokableData);
const isNestedComponent = isNestedComponentTagName(tagName);
if (
isValidMustache &&
(node.hash.pairs.length > 0 || node.params.length > 0 || isNestedComponent)
) {
return transformNode(node, fileInfo, config);
}
},
BlockStatement(node) {
let tagName = `${node.path.original}`;
if (
!shouldIgnoreMustacheStatement(node.path.original, config, invokableData) ||
isWallStreet(tagName)
) {
return transformNode(node, fileInfo, config);
}
},
AttrNode: {
enter() {
inAttr = true;
},
exit() {
inAttr = false;
},
},
};
}