| 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 |
1x
1x
31x
18x
18x
1x
46x
46x
31x
31x
31x
31x
7x
24x
46x
46x
46x
2x
44x
44x
26x
26x
1x
25x
6x
4x
21x
21x
21x
24x
18x
10x
10x
10x
10x
39x
38x
38x
9x
29x
29x
9x
29x
8x
29x
8x
29x
8x
29x
55x
55x
8x
47x
6x
6x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
32x
32x
26x
8x
18x
4x
14x
2x
12x
3x
2x
2x
10x
1x
9x
1x
8x
6x
3x
34x
34x
34x
34x
2x
2x
1x
47x
32x
5x
1x
32x
8x
165x
4x
1x
2x
1x
61x
14x
6x
26x
1x
1x
26x
34x
1x
3x
3x
3x
1x
2x
1x
1x
1x
1x
| /* global module */
// builders ref https://github.com/glimmerjs/glimmer-vm/blob/master/packages/%40glimmer/syntax/lib/builders.ts
const HOT_LOAD_HELPER_NAME = "hot-load";
// for cases like {{#let (component 'foo-bar') as |Boo|}} <Boo />a {{/let}}
var nestedNames = [];
function createSubExpression(params, b) {
return b.sexpr(b.path(HOT_LOAD_HELPER_NAME), params);
}
function looksLikeComponent(nodePath, appHelpers) {
Iif (typeof nodePath.includes !== "function") {
// eslint-disable-next-line no-console
console.log(nodePath, typeof nodePath);
return false;
}
// const isCapitalized = nodePath.charAt(0) === nodePath.charAt(0).toUpperCase();
// (isCapitalized || nodePath.includes("-"))
return (
!appHelpers.includes(nodePath) &&
!helperNames.includes(nodePath) &&
nodePath.includes("-") && !nodePath.includes('.')
);
}
const helperNames = [
"identity", // glimmer blocks
"render-inverse", // glimmer blocks
"-get-dynamic-var", // glimmer internal helper
"-lf-get-outlet-state", // dunno
"action",
"component",
"link-to",
HOT_LOAD_HELPER_NAME,
"hot-content",
"hot-placeholder",
"if",
"if-unless",
"each",
"each-in",
"format-date",
"format-message",
"format-relative",
"format-time",
"unless",
"in-element",
"query-params",
"-in-element",
"-class",
"-html-safe",
"-input-type",
"-normalize-class",
"concat",
"get",
"mut",
"readonly",
"unbound",
"debugger",
"else",
"let",
"log",
"loc",
"hash",
"input",
"partial",
"yield",
"textarea",
"t",
"t-for",
"transition-to",
"get-meta",
"get-attr",
"index-of",
//ember-moment
"moment-format",
"moment-from-now",
"moment-to",
"moment-to-now",
"moment-duration",
"moment-calendar",
"moment-diff",
"outlet",
"is-before",
"is-after",
"is-same",
"is-same-or-before",
"is-same-or-after",
"is-between",
"now",
"unix",
//cp-validations
"v-get",
//route-action
"route-action",
// composable-helpers
"map-by",
"sort-by",
"filter-by",
"reject-by",
"find-by",
"object-at",
"hasBlock",
"has-block",
"has-next",
"has-previous",
"group-by",
"not-eq",
"is-array",
"is-empty",
"is-equal",
// liquid
"liquid-unless",
"liquid-container",
"liquid-outlet",
"liquid-versions",
"liquid-bind",
"liquid-spacer",
"liquid-sync",
"liquid-measured",
"liquid-child",
"liquid-if",
//app-version
"app-version"
];
// For compatibility with pre- and post-glimmer
function unwrapNode(node) {
Iif (node.sexpr) {
return node.sexpr;
} else {
return node;
}
}
function buildNodeHashAndStats(node, componentName, b) {
let hotReloadCUSTOMHasParams = node.params && node.params.length !== 0;
let hotReloadCustomHasHash = node.hash && (node.hash.pairs || []).length !== 0;
let hashPairs = [
b.pair("hotReloadCUSTOMhlContext", b.path('this')),
b.pair("hotReloadCUSTOMName", b.string(componentName.original || componentName)),
b.pair("hotReloadCUSTOMhlProperty", b.path(componentName)),
b.pair("hotReloadCUSTOMHasParams", b.boolean(hotReloadCUSTOMHasParams)),
b.pair("hotReloadCUSTOMHasHash", b.boolean(hotReloadCustomHasHash))
];
if (node.hash && node.hash.pairs && node.hash.pairs.length) {
node.hash = b.hash(hashPairs.concat(node.hash.pairs));
} else {
node.hash = b.hash(hashPairs);
}
}
function convertComponent(input, b, {
helpers
} = {
helpers: []
}) {
const node = unwrapNode(input);
Iif (typeof node !== "object") {
return;
}
if (node.__ignore && (node.path && node.path.original !== "component")) {
return;
}
const nodePath = node.path.original;
if (nodePath === "component") {
let firstParam = node.params[0];
if (firstParam.value === HOT_LOAD_HELPER_NAME) {
return;
}
if (firstParam.path) {
if (firstParam.path.original === HOT_LOAD_HELPER_NAME) {
return;
}
}
let componentName = firstParam || firstParam.path.original;
buildNodeHashAndStats(node, componentName, b);
node.params = [createSubExpression([firstParam, b.path('this'), b.path(componentName), b.string(componentName.original || componentName) ], b)].concat(
node.params.filter(n => n !== firstParam)
);
} else if (looksLikeComponent(nodePath, helpers)) {
let componentName = node.path.original;
node.path = b.path("component");
buildNodeHashAndStats(node, componentName, b);
node.params = [createSubExpression([b.string(componentName), b.path('this'), b.path(componentName), b.string(componentName.original || componentName)], b)].concat(
node.params
);
}
return node;
}
function markNodeAsIgnored(node) {
Iif (!node) {
return;
}
if (typeof node !== "object") {
return;
}
node.__ignore = true;
if (node.value) {
markNodeAsIgnored(node.value);
}
if (node.path) {
markNodeAsIgnored(node.path);
}
if (node.params) {
node.params.forEach(markNodeAsIgnored);
}
if (node.hash) {
node.hash.pairs.forEach(markNodeAsIgnored);
}
return node;
}
function captureBlockParams(node) {
const params = node.blockParams;
if (node.__ignore) {
return;
}
params.forEach((param)=>{
Eif (!nestedNames.includes(param)) {
nestedNames.push(param);
}
});
}
function wrapAngeBrackedComponentWithLetHelper(b, node, options = {}) {
const tagName = node.tag;
const newNode = JSON.parse(JSON.stringify(node));
captureBlockParams(node);
newNode.cloned = true;
node.type = "BlockStatement";
node.params = [
b.sexpr(b.path('component'), [b.string(tagName)])
];
node.path = b.path('let');
node.hash = b.hash([]);
const salt = (options && options.salt) || Math.random().toString(36).slice(2);
newNode.tag = 'HotLoad' + tagName + salt;
node.program = b.program([newNode], [newNode.tag]);
node.program['__ignore'] = true;
node.inverse = null;
node.loc = null;
}
function isAngleBrackedComponent(node) {
const tagName = node.tag;
if (tagName.charAt(0) === tagName.charAt(0).toUpperCase()) {
if (node.cloned) {
return false;
}
if (nestedNames.includes(tagName)) {
return false;
}
if (tagName === 'Input' || tagName === 'Textarea') {
return false;
}
// https://github.com/emberjs/ember.js/pull/17146
// https://github.com/emberjs/ember.js/pull/17160
// https://github.com/glimmerjs/glimmer-vm/pull/892
// dunno how to detect in transformation time
if (node.attributes && node.attributes.length) {
// looks like ember can't handle ...attributes inside `let'ed` component.
let attrs = node.attributes.filter((attr)=>attr.name === '...attributes');
Eif (attrs.length) {
return false;
}
}
if (tagName.indexOf('.') !== -1) {
return false;
}
if (tagName.charAt(0) === '@') {
return false;
}
return true;
}
return false;
}
class BaseASTHotLoadTransform {
constructor() {
this.syntax = null;
}
static createASTPlugin(config, syntax) {
let b = syntax.builders;
let options = config || { helpers: [] };
// console.log('config', config.addonContext._OPTIONS);
nestedNames = [];
const visitor = {
ElementModifierStatement(node) {
markNodeAsIgnored(node.path);
node.params.forEach(param => {
markNodeAsIgnored(param);
});
},
Program(node) {
captureBlockParams(node);
},
ElementNode(node) {
node.attributes.forEach(attr => {
if (attr.value && attr.value.type === "MustacheStatement") {
markNodeAsIgnored(attr.value);
}
});
if (isAngleBrackedComponent(node)) {
wrapAngeBrackedComponentWithLetHelper(b, node, options);
}
},
HashPair(node) {
if (
node.value &&
node.value.path &&
node.value.path.original !== "component"
) {
markNodeAsIgnored(node.value);
}
},
ConcatStatement(node) {
node.parts
.filter(part => {
return part.type === "MustacheStatement";
})
.forEach(item => {
markNodeAsIgnored(item);
});
},
SubExpression(node) {
if (node.path.original === "component") {
convertComponent(node, b, options);
}
},
BlockStatement(node) {
convertComponent(node, b, options);
},
MustacheStatement(node) {
if (node.path.original === "concat") {
node.params.forEach(param => {
markNodeAsIgnored(param);
});
}
convertComponent(node, b, options);
}
};
return {
name: 'ember-ast-hot-load',
visitor
}
}
}
// module.exports = ASTHotLoadTransform;
module.exports = function(config) {
return class ASTHotLoadTransform extends BaseASTHotLoadTransform {
transform(ast) {
// let startLoc = ast.loc ? ast.loc.start : {};
// /*
// Checking for line and column to avoid registering the plugin for ProgramNode inside a BlockStatement since transform is called for all ProgramNodes in Ember 2.15.X. Removing this would result in minifying all the TextNodes.
// */
// if (startLoc.line !== 1 || startLoc.column !== 0) {
// return ast;
// }
const astConfig = config.addonContext._OPTIONS || {};
if (!astConfig.enabled) {
return ast;
}
// cover case for blacklisted addon
if (!astConfig.initialized) {
return ast;
}
let plugin = ASTHotLoadTransform.createASTPlugin(astConfig, this.syntax);
this.syntax.traverse(ast, plugin.visitor);
return ast;
}
};
}; |