-
Notifications
You must be signed in to change notification settings - Fork 122
/
utils.js
435 lines (392 loc) · 11.8 KB
/
utils.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
import * as t from "@babel/types";
import { addNamed } from "@babel/helper-module-imports";
export const reservedNameSpaces = new Set([
"class",
"on",
"oncapture",
"style",
"use",
"prop",
"attr"
]);
export const nonSpreadNameSpaces = new Set(["class", "style", "use", "prop", "attr"]);
export function getConfig(path) {
return path.hub.file.metadata.config;
}
export const getRendererConfig = (path, renderer) => {
const config = getConfig(path);
return config?.renderers?.find(r => r.name === renderer) ?? config;
};
export function registerImportMethod(path, name, moduleName) {
const imports =
path.scope.getProgramParent().data.imports ||
(path.scope.getProgramParent().data.imports = new Map());
moduleName = moduleName || getConfig(path).moduleName;
if (!imports.has(`${moduleName}:${name}`)) {
let id = addNamed(path, name, moduleName, {
nameHint: `_$${name}`
});
imports.set(`${moduleName}:${name}`, id);
return id;
} else {
let iden = imports.get(`${moduleName}:${name}`);
// the cloning is required to play well with babel-preset-env which is
// transpiling import as we add them and using the same identifier causes
// problems with the multiple identifiers of the same thing
return t.cloneNode(iden);
}
}
function jsxElementNameToString(node) {
if (t.isJSXMemberExpression(node)) {
return `${jsxElementNameToString(node.object)}.${node.property.name}`;
}
if (t.isJSXIdentifier(node) || t.isIdentifier(node)) {
return node.name;
}
return `${node.namespace.name}:${node.name.name}`;
}
export function tagNameToIdentifier(name) {
const parts = name.split(".");
if (parts.length === 1) return t.identifier(name);
let part;
let base = t.identifier(parts.shift());
while ((part = parts.shift())) {
base = t.memberExpression(base, t.identifier(part));
}
return base;
}
export function getTagName(tag) {
const jsxName = tag.openingElement.name;
return jsxElementNameToString(jsxName);
}
export function isComponent(tagName) {
return (
(tagName[0] && tagName[0].toLowerCase() !== tagName[0]) ||
tagName.includes(".") ||
/[^a-zA-Z]/.test(tagName[0])
);
}
export function isDynamic(path, { checkMember, checkTags, checkCallExpressions = true, native }) {
const config = getConfig(path);
if (config.generate === "ssr" && native) {
checkMember = false;
checkCallExpressions = false;
}
const expr = path.node;
if (t.isFunction(expr)) return false;
if (
expr.leadingComments &&
expr.leadingComments[0] &&
expr.leadingComments[0].value.trim() === config.staticMarker
) {
expr.leadingComments.shift();
return false;
}
if (
checkCallExpressions &&
(t.isCallExpression(expr) || t.isOptionalCallExpression(expr))
) {
return true;
}
if (checkMember && t.isMemberExpression(expr)) {
// Do not assume property access on namespaced imports as dynamic.
const object = path.get("object").node;
if (
t.isIdentifier(object) &&
(!expr.computed ||
!isDynamic(path.get("property"), {
checkMember,
checkTags,
checkCallExpressions,
native,
}))
) {
const binding = path.scope.getBinding(object.name);
if (binding && binding.path.isImportNamespaceSpecifier()) {
return false;
}
}
return true;
}
if (
checkMember &&
(t.isOptionalMemberExpression(expr) ||
t.isSpreadElement(expr) ||
(t.isBinaryExpression(expr) && expr.operator === "in"))
) {
return true;
}
if (checkTags && (t.isJSXElement(expr) || (t.isJSXFragment(expr) && expr.children.length))) {
return true;
}
let dynamic;
path.traverse({
Function(p) {
if (t.isObjectMethod(p.node) && p.node.computed) {
dynamic = isDynamic(p.get("key"), { checkMember, checkTags, checkCallExpressions, native });
}
p.skip();
},
CallExpression(p) {
checkCallExpressions && (dynamic = true) && p.stop();
},
OptionalCallExpression(p) {
checkCallExpressions && (dynamic = true) && p.stop();
},
MemberExpression(p) {
checkMember && (dynamic = true) && p.stop();
},
OptionalMemberExpression(p) {
checkMember && (dynamic = true) && p.stop();
},
SpreadElement(p) {
checkMember && (dynamic = true) && p.stop();
},
BinaryExpression(p) {
checkMember && p.node.operator === "in" && (dynamic = true) && p.stop();
},
JSXElement(p) {
checkTags ? (dynamic = true) && p.stop() : p.skip();
},
JSXFragment(p) {
checkTags && p.node.children.length ? (dynamic = true) && p.stop() : p.skip();
}
});
return dynamic;
}
export function getStaticExpression(path) {
const node = path.node;
let value, type;
return (
t.isJSXExpressionContainer(node) &&
t.isJSXElement(path.parent) &&
!isComponent(getTagName(path.parent)) &&
!t.isSequenceExpression(node.expression) &&
(value = path.get("expression").evaluate().value) !== undefined &&
((type = typeof value) === "string" || type === "number") &&
value
);
}
// remove unnecessary JSX Text nodes
export function filterChildren(children) {
return children.filter(
({ node: child }) =>
!(t.isJSXExpressionContainer(child) && t.isJSXEmptyExpression(child.expression)) &&
(!t.isJSXText(child) || !/^[\r\n]\s*$/.test(child.extra.raw))
);
}
export function checkLength(children) {
let i = 0;
children.forEach(path => {
const child = path.node;
!(t.isJSXExpressionContainer(child) && t.isJSXEmptyExpression(child.expression)) &&
(!t.isJSXText(child) || !/^\s*$/.test(child.extra.raw) || /^ *$/.test(child.extra.raw)) &&
i++;
});
return i > 1;
}
export function trimWhitespace(text) {
text = text.replace(/\r/g, "");
if (/\n/g.test(text)) {
text = text
.split("\n")
.map((t, i) => (i ? t.replace(/^\s*/g, "") : t))
.filter(s => !/^\s*$/.test(s))
.join(" ");
}
return text.replace(/\s+/g, " ");
}
export function toEventName(name) {
return name.slice(2).toLowerCase();
}
export function toAttributeName(name) {
return name.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`);
}
export function toPropertyName(name) {
return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
}
export function wrappedByText(list, startIndex) {
let index = startIndex,
wrapped;
while (--index >= 0) {
const node = list[index];
if (!node) continue;
if (node.text) {
wrapped = true;
break;
}
if (node.id) return false;
}
if (!wrapped) return false;
index = startIndex;
while (++index < list.length) {
const node = list[index];
if (!node) continue;
if (node.text) return true;
if (node.id) return false;
}
return false;
}
export function transformCondition(path, inline, deep) {
const config = getConfig(path);
const expr = path.node;
const memo = registerImportMethod(path, config.memoWrapper);
let dTest, cond, id;
if (
t.isConditionalExpression(expr) &&
(isDynamic(path.get("consequent"), {
checkTags: true
}) ||
isDynamic(path.get("alternate"), { checkTags: true }))
) {
dTest = isDynamic(path.get("test"), { checkMember: true });
if (dTest) {
cond = expr.test;
if (!t.isBinaryExpression(cond))
cond = t.unaryExpression("!", t.unaryExpression("!", cond, true), true);
id = inline
? t.callExpression(memo, [t.arrowFunctionExpression([], cond)])
: path.scope.generateUidIdentifier("_c$");
expr.test = t.callExpression(id, []);
if (t.isConditionalExpression(expr.consequent) || t.isLogicalExpression(expr.consequent)) {
expr.consequent = transformCondition(path.get("consequent"), inline, true);
}
if (t.isConditionalExpression(expr.alternate) || t.isLogicalExpression(expr.alternate)) {
expr.alternate = transformCondition(path.get("alternate"), inline, true);
}
}
} else if (t.isLogicalExpression(expr)) {
let nextPath = path;
// handle top-level or, ie cond && <A/> || <B/>
while (nextPath.node.operator !== "&&" && t.isLogicalExpression(nextPath.node.left)) {
nextPath = nextPath.get("left");
}
nextPath.node.operator === "&&" &&
isDynamic(nextPath.get("right"), { checkTags: true }) &&
(dTest = isDynamic(nextPath.get("left"), {
checkMember: true
}));
if (dTest) {
cond = nextPath.node.left;
if (!t.isBinaryExpression(cond))
cond = t.unaryExpression("!", t.unaryExpression("!", cond, true), true);
id = inline
? t.callExpression(memo, [t.arrowFunctionExpression([], cond)])
: path.scope.generateUidIdentifier("_c$");
nextPath.node.left = t.callExpression(id, []);
}
}
if (dTest && !inline) {
const statements = [
t.variableDeclaration("var", [
t.variableDeclarator(
id,
config.memoWrapper
? t.callExpression(memo, [t.arrowFunctionExpression([], cond)])
: t.arrowFunctionExpression([], cond)
)
]),
t.arrowFunctionExpression([], expr)
];
return deep
? t.callExpression(
t.arrowFunctionExpression(
[],
t.blockStatement([statements[0], t.returnStatement(statements[1])])
),
[]
)
: statements;
}
return deep ? expr : t.arrowFunctionExpression([], expr);
}
export function escapeHTML(s, attr) {
if (typeof s !== "string") return s;
const delim = attr ? '"' : "<";
const escDelim = attr ? """ : "<";
let iDelim = s.indexOf(delim);
let iAmp = s.indexOf("&");
if (iDelim < 0 && iAmp < 0) return s;
let left = 0,
out = "";
while (iDelim >= 0 && iAmp >= 0) {
if (iDelim < iAmp) {
if (left < iDelim) out += s.substring(left, iDelim);
out += escDelim;
left = iDelim + 1;
iDelim = s.indexOf(delim, left);
} else {
if (left < iAmp) out += s.substring(left, iAmp);
out += "&";
left = iAmp + 1;
iAmp = s.indexOf("&", left);
}
}
if (iDelim >= 0) {
do {
if (left < iDelim) out += s.substring(left, iDelim);
out += escDelim;
left = iDelim + 1;
iDelim = s.indexOf(delim, left);
} while (iDelim >= 0);
} else {
while (iAmp >= 0) {
if (left < iAmp) out += s.substring(left, iAmp);
out += "&";
left = iAmp + 1;
iAmp = s.indexOf("&", left);
}
}
return left < s.length ? out + s.substring(left) : out;
}
export function convertJSXIdentifier(node) {
if (t.isJSXIdentifier(node)) {
if (t.isValidIdentifier(node.name)) {
node.type = "Identifier";
} else {
return t.stringLiteral(node.name);
}
} else if (t.isJSXMemberExpression(node)) {
return t.memberExpression(
convertJSXIdentifier(node.object),
convertJSXIdentifier(node.property)
);
} else if (t.isJSXNamespacedName(node)) {
return t.stringLiteral(`${node.namespace.name}:${node.name.name}`);
}
return node;
}
export function canNativeSpread(key, { checkNameSpaces } = {}) {
if (checkNameSpaces && key.includes(":") && nonSpreadNameSpaces.has(key.split(":")[0]))
return false;
// TODO: figure out how to detect definitely function ref
if (key === "ref") return false;
return true;
}
const chars = "etaoinshrdlucwmfygpbTAOISWCBvkxjqzPHFMDRELNGUKVYJQZX_$";
const base = chars.length;
export function getNumberedId(num) {
let out = "";
do {
const digit = num % base;
num = Math.floor(num / base);
out = chars[digit] + out;
} while (num !== 0);
return out;
}
export function escapeStringForTemplate(str) {
return str.replace(/[{\\`\n\t\b\f\v\r\u2028\u2029]/g, ch => templateEscapes.get(ch))
}
const templateEscapes = new Map([
['{', '\\{'],
['`', '\\`'],
['\\', '\\\\'],
['\n', '\\n'],
['\t', '\\t'],
['\b', '\\b'],
['\f', '\\f'],
['\v', '\\v'],
['\r', '\\r'],
['\u2028', '\\u2028'],
['\u2029', '\\u2029']
])