@litsx/babel-preset-litsx 0.1.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 +37 -0
- package/dist/index.cjs +30 -0
- package/dist/index.cjs.map +1 -0
- package/dist/internal/transform-litsx-components.cjs +2518 -0
- package/dist/internal/transform-litsx-components.cjs.map +1 -0
- package/dist/internal/transform-litsx-dom-refs.cjs +16 -0
- package/dist/internal/transform-litsx-dom-refs.cjs.map +1 -0
- package/dist/internal/transform-litsx-hooks.cjs +45 -0
- package/dist/internal/transform-litsx-hooks.cjs.map +1 -0
- package/dist/internal/transform-litsx-properties.cjs +1254 -0
- package/dist/internal/transform-litsx-properties.cjs.map +1 -0
- package/dist/pipeline.cjs +97 -0
- package/dist/pipeline.cjs.map +1 -0
- package/package.json +80 -0
- package/src/index.js +15 -0
- package/src/internal/transform-litsx-class-generation.js +109 -0
- package/src/internal/transform-litsx-components.js +549 -0
- package/src/internal/transform-litsx-dom-refs.js +9 -0
- package/src/internal/transform-litsx-handlers.js +220 -0
- package/src/internal/transform-litsx-hooks.js +38 -0
- package/src/internal/transform-litsx-param-rewrites.js +227 -0
- package/src/internal/transform-litsx-program.js +229 -0
- package/src/internal/transform-litsx-properties.js +1246 -0
- package/src/internal/transform-litsx-refs.js +231 -0
- package/src/internal/transform-litsx-static-hoists.js +754 -0
- package/src/internal/transform-litsx-wrapper-utils.js +243 -0
- package/src/pipeline.js +90 -0
|
@@ -0,0 +1,2518 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var jsxSyntaxPlugin = require('@babel/plugin-syntax-jsx');
|
|
6
|
+
var internal_transformLitsxProperties = require('./transform-litsx-properties.cjs');
|
|
7
|
+
require('module');
|
|
8
|
+
require('@litsx/typescript-session');
|
|
9
|
+
|
|
10
|
+
let t$7;
|
|
11
|
+
|
|
12
|
+
function setStaticHoistsBabelTypes(types) {
|
|
13
|
+
t$7 = types;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isLightDomHoist(statement) {
|
|
17
|
+
if (!t$7.isExpressionStatement(statement)) return false;
|
|
18
|
+
if (!t$7.isCallExpression(statement.expression)) return false;
|
|
19
|
+
if (!t$7.isIdentifier(statement.expression.callee, { name: "__litsx_static_lightDom" })) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
if (statement.expression.arguments.length !== 0) {
|
|
23
|
+
throw new Error("^lightDom() does not accept arguments.");
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function createStaticHoistGetter(name, symbolId, expression) {
|
|
29
|
+
const getter = t$7.classMethod(
|
|
30
|
+
"get",
|
|
31
|
+
t$7.identifier(name),
|
|
32
|
+
[],
|
|
33
|
+
t$7.blockStatement([
|
|
34
|
+
t$7.returnStatement(
|
|
35
|
+
t$7.callExpression(
|
|
36
|
+
t$7.memberExpression(t$7.thisExpression(), t$7.identifier("__litsxStatic")),
|
|
37
|
+
[
|
|
38
|
+
t$7.cloneNode(symbolId),
|
|
39
|
+
t$7.arrowFunctionExpression([], expression),
|
|
40
|
+
]
|
|
41
|
+
)
|
|
42
|
+
),
|
|
43
|
+
])
|
|
44
|
+
);
|
|
45
|
+
getter.static = true;
|
|
46
|
+
return getter;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function resolveStaticHoistExpression(expression) {
|
|
50
|
+
return t$7.callExpression(
|
|
51
|
+
t$7.memberExpression(t$7.thisExpression(), t$7.identifier("__litsxResolveStaticValue")),
|
|
52
|
+
[t$7.cloneNode(expression)]
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function createPropertiesHoistResolver(propertiesStatic, staticProps, expression) {
|
|
57
|
+
const mergedProperties = propertiesStatic.map((property) => t$7.cloneNode(property));
|
|
58
|
+
if (staticProps.length > 0) {
|
|
59
|
+
mergeStaticPropsIntoProperties(mergedProperties, staticProps);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return t$7.callExpression(
|
|
63
|
+
t$7.memberExpression(t$7.thisExpression(), t$7.identifier("__litsxMergeProperties")),
|
|
64
|
+
[
|
|
65
|
+
t$7.objectExpression(mergedProperties),
|
|
66
|
+
resolveStaticHoistExpression(expression),
|
|
67
|
+
]
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function createStylesHoistResolver(staticStyles, expression) {
|
|
72
|
+
const resolvedExpression = resolveStaticHoistExpression(expression);
|
|
73
|
+
if (staticStyles.length === 0) {
|
|
74
|
+
return resolvedExpression;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const baseStyles =
|
|
78
|
+
staticStyles.length === 1
|
|
79
|
+
? t$7.cloneNode(staticStyles[0])
|
|
80
|
+
: t$7.arrayExpression(staticStyles.map((style) => t$7.cloneNode(style)));
|
|
81
|
+
|
|
82
|
+
return t$7.logicalExpression("||", resolvedExpression, baseStyles);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getStaticPropsExpression(statement) {
|
|
86
|
+
if (!t$7.isExpressionStatement(statement)) return null;
|
|
87
|
+
if (!t$7.isCallExpression(statement.expression)) return null;
|
|
88
|
+
const isLegacyStaticProps = t$7.isIdentifier(statement.expression.callee, { name: "staticProps" });
|
|
89
|
+
const isHoistedProperties = t$7.isIdentifier(
|
|
90
|
+
statement.expression.callee,
|
|
91
|
+
{ name: "__litsx_static_properties" }
|
|
92
|
+
);
|
|
93
|
+
if (
|
|
94
|
+
!isLegacyStaticProps &&
|
|
95
|
+
!isHoistedProperties
|
|
96
|
+
) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (statement.expression.arguments.length !== 1) return null;
|
|
100
|
+
|
|
101
|
+
const [argument] = statement.expression.arguments;
|
|
102
|
+
if (isHoistedProperties && (t$7.isFunctionExpression(argument) || t$7.isArrowFunctionExpression(argument))) {
|
|
103
|
+
throw new Error("^properties(...) only accepts an object literal with static Lit property options.");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!t$7.isObjectExpression(argument)) {
|
|
107
|
+
throw new Error("^properties(...) only accepts an object literal with static Lit property options.");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return isHoistedProperties ? {
|
|
111
|
+
__litsxHoistedProperties: true,
|
|
112
|
+
expression: t$7.cloneNode(argument),
|
|
113
|
+
} : t$7.cloneNode(argument);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function getStaticPropertyName(node) {
|
|
117
|
+
if (t$7.isIdentifier(node)) return node.name;
|
|
118
|
+
if (t$7.isStringLiteral(node)) return node.value;
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function normalizeStaticPropOverrideValue(value) {
|
|
123
|
+
if (
|
|
124
|
+
t$7.isIdentifier(value) &&
|
|
125
|
+
["String", "Number", "Boolean", "Object", "Array", "Date"].includes(value.name)
|
|
126
|
+
) {
|
|
127
|
+
return internal_transformLitsxProperties.createPropertyConfig(t$7.identifier(value.name));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (t$7.isObjectExpression(value)) {
|
|
131
|
+
const typeProperty = value.properties.find(
|
|
132
|
+
(prop) =>
|
|
133
|
+
t$7.isObjectProperty(prop) &&
|
|
134
|
+
t$7.isIdentifier(prop.key, { name: "type" }) &&
|
|
135
|
+
t$7.isIdentifier(prop.value)
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const attributeProperty = value.properties.find(
|
|
139
|
+
(prop) =>
|
|
140
|
+
t$7.isObjectProperty(prop) &&
|
|
141
|
+
t$7.isIdentifier(prop.key, { name: "attribute" }) &&
|
|
142
|
+
t$7.isBooleanLiteral(prop.value) &&
|
|
143
|
+
prop.value.value === false
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return internal_transformLitsxProperties.createPropertyConfig(typeProperty ? typeProperty.value : null, {
|
|
147
|
+
attribute: attributeProperty ? false : undefined,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
throw new Error(
|
|
152
|
+
"^properties(...) values must be Lit property option objects or constructor references."
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function mergeStaticPropertyObject(targetNode, overrideObject) {
|
|
157
|
+
if (!t$7.isObjectProperty(targetNode) || !t$7.isObjectExpression(targetNode.value)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
overrideObject.properties.forEach((property) => {
|
|
162
|
+
if (!t$7.isObjectProperty(property) && !t$7.isObjectMethod(property)) {
|
|
163
|
+
throw new Error("^properties(...) only accepts plain object members.");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const keyName = getStaticPropertyName(property.key);
|
|
167
|
+
if (!keyName) {
|
|
168
|
+
throw new Error("^properties(...) property option names must be static identifiers or strings.");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const existing = targetNode.value.properties.find(
|
|
172
|
+
(candidate) =>
|
|
173
|
+
(t$7.isObjectProperty(candidate) || t$7.isObjectMethod(candidate)) &&
|
|
174
|
+
getStaticPropertyName(candidate.key) === keyName
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (existing) {
|
|
178
|
+
const nextNode = t$7.cloneNode(property);
|
|
179
|
+
const index = targetNode.value.properties.indexOf(existing);
|
|
180
|
+
targetNode.value.properties.splice(index, 1, nextNode);
|
|
181
|
+
} else {
|
|
182
|
+
targetNode.value.properties.push(t$7.cloneNode(property));
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function mergeStaticPropsIntoProperties(propertiesStatic, staticProps) {
|
|
188
|
+
const propertyMap = new Map();
|
|
189
|
+
|
|
190
|
+
propertiesStatic.forEach((propertyNode) => {
|
|
191
|
+
if (!t$7.isObjectProperty(propertyNode)) return;
|
|
192
|
+
const keyName = getStaticPropertyName(propertyNode.key);
|
|
193
|
+
if (!keyName) return;
|
|
194
|
+
propertyMap.set(keyName, propertyNode);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
staticProps.forEach((optionsObject) => {
|
|
198
|
+
optionsObject.properties.forEach((property) => {
|
|
199
|
+
if (!t$7.isObjectProperty(property)) {
|
|
200
|
+
throw new Error("^properties(...) only accepts plain object properties.");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const keyName = getStaticPropertyName(property.key);
|
|
204
|
+
if (!keyName) {
|
|
205
|
+
throw new Error("^properties(...) property names must be static identifiers or strings.");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const existing = propertyMap.get(keyName);
|
|
209
|
+
const normalized = normalizeStaticPropOverrideValue(property.value);
|
|
210
|
+
|
|
211
|
+
if (!existing) {
|
|
212
|
+
const node = t$7.objectProperty(
|
|
213
|
+
t$7.identifier(keyName),
|
|
214
|
+
internal_transformLitsxProperties.createPropertyValue(normalized, false)
|
|
215
|
+
);
|
|
216
|
+
if (t$7.isObjectExpression(property.value)) {
|
|
217
|
+
mergeStaticPropertyObject(node, property.value);
|
|
218
|
+
}
|
|
219
|
+
propertiesStatic.push(node);
|
|
220
|
+
propertyMap.set(keyName, node);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
internal_transformLitsxProperties.mergePropertyConfig(
|
|
225
|
+
{ node: existing },
|
|
226
|
+
normalized,
|
|
227
|
+
false
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
if (t$7.isObjectExpression(property.value)) {
|
|
231
|
+
mergeStaticPropertyObject(existing, property.value);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function normalizeStylesTemplate(argument, functionPath) {
|
|
238
|
+
if (t$7.isTemplateLiteral(argument)) {
|
|
239
|
+
if (
|
|
240
|
+
!argument.expressions.every((expression) =>
|
|
241
|
+
isStaticStylesExpression(expression, functionPath)
|
|
242
|
+
)
|
|
243
|
+
) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return t$7.templateLiteral(
|
|
247
|
+
argument.quasis,
|
|
248
|
+
argument.expressions.map((expression) =>
|
|
249
|
+
wrapStaticStylesInterpolation(expression)
|
|
250
|
+
)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (t$7.isStringLiteral(argument)) {
|
|
255
|
+
return t$7.templateLiteral(
|
|
256
|
+
[t$7.templateElement({ raw: argument.value, cooked: argument.value }, true)],
|
|
257
|
+
[]
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (isStaticStylesExpression(argument, functionPath)) {
|
|
262
|
+
return t$7.templateLiteral(
|
|
263
|
+
[
|
|
264
|
+
t$7.templateElement({ raw: "", cooked: "" }, false),
|
|
265
|
+
t$7.templateElement({ raw: "", cooked: "" }, true),
|
|
266
|
+
],
|
|
267
|
+
[wrapStaticStylesInterpolation(argument)]
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function wrapStaticStylesInterpolation(expression) {
|
|
275
|
+
if (
|
|
276
|
+
t$7.isTaggedTemplateExpression(expression) &&
|
|
277
|
+
t$7.isIdentifier(expression.tag, { name: "css" })
|
|
278
|
+
) {
|
|
279
|
+
return expression;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (t$7.isNumericLiteral(expression)) {
|
|
283
|
+
return expression;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return t$7.callExpression(
|
|
287
|
+
t$7.identifier("unsafeCSS"),
|
|
288
|
+
[expression]
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function getStaticStylesExpression(statement, functionPath) {
|
|
293
|
+
if (!t$7.isExpressionStatement(statement)) return null;
|
|
294
|
+
if (!t$7.isCallExpression(statement.expression)) return null;
|
|
295
|
+
const isLegacyStaticStyles = t$7.isIdentifier(statement.expression.callee, { name: "staticStyles" });
|
|
296
|
+
const isHoistedStyles = t$7.isIdentifier(statement.expression.callee, { name: "__litsx_static_styles" });
|
|
297
|
+
if (
|
|
298
|
+
!isLegacyStaticStyles &&
|
|
299
|
+
!isHoistedStyles
|
|
300
|
+
) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
if (statement.expression.arguments.length !== 1) return null;
|
|
304
|
+
|
|
305
|
+
const [argument] = statement.expression.arguments;
|
|
306
|
+
|
|
307
|
+
if (isHoistedStyles && (t$7.isFunctionExpression(argument) || t$7.isArrowFunctionExpression(argument))) {
|
|
308
|
+
throw new Error("^styles(...) only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const template = normalizeStylesTemplate(
|
|
312
|
+
argument,
|
|
313
|
+
functionPath
|
|
314
|
+
);
|
|
315
|
+
if (!template) {
|
|
316
|
+
throw new Error("^styles(...) only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const expression = t$7.taggedTemplateExpression(t$7.identifier("css"), template);
|
|
320
|
+
return isHoistedStyles
|
|
321
|
+
? { __litsxHoistedStyles: true, expression }
|
|
322
|
+
: expression;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function getStaticHoistExpression(statement, functionPath) {
|
|
326
|
+
if (!t$7.isExpressionStatement(statement)) return null;
|
|
327
|
+
if (!t$7.isCallExpression(statement.expression)) return null;
|
|
328
|
+
if (!t$7.isIdentifier(statement.expression.callee)) return null;
|
|
329
|
+
|
|
330
|
+
const calleeName = statement.expression.callee.name;
|
|
331
|
+
if (!calleeName.startsWith("__litsx_static_")) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const name = calleeName.slice("__litsx_static_".length);
|
|
336
|
+
if (!name || name === "properties" || name === "styles") {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (statement.expression.arguments.length !== 1) {
|
|
341
|
+
throw new Error(`^${name}(...) expects exactly one argument.`);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const [argument] = statement.expression.arguments;
|
|
345
|
+
if (name === "expose") {
|
|
346
|
+
if (t$7.isObjectExpression(argument)) {
|
|
347
|
+
return {
|
|
348
|
+
name,
|
|
349
|
+
expression: t$7.cloneNode(argument),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
throw new Error("^expose(...) only accepts an object literal.");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (t$7.isFunctionExpression(argument) || t$7.isArrowFunctionExpression(argument)) {
|
|
357
|
+
throw new Error(`^${name}(...) only accepts a direct static value.`);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (!isStaticStylesExpression(argument, functionPath)) {
|
|
361
|
+
throw new Error(`^${name}(...) only accepts a direct static value.`);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return {
|
|
365
|
+
name,
|
|
366
|
+
expression: t$7.cloneNode(argument),
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function createExposeHoistMembers(expression) {
|
|
371
|
+
const { methodsExpression } = normalizeExposeHoistExpression(expression);
|
|
372
|
+
|
|
373
|
+
return methodsExpression.properties.map((property) =>
|
|
374
|
+
createExposeClassMethod(property)
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function normalizeExposeHoistExpression(expression) {
|
|
379
|
+
if (t$7.isObjectExpression(expression)) {
|
|
380
|
+
return {
|
|
381
|
+
methodsExpression: t$7.cloneNode(expression),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
throw new Error("^expose(...) only accepts an object literal.");
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function createExposeClassMethod(property) {
|
|
389
|
+
const method = normalizeExposePropertyToClassMethod(property);
|
|
390
|
+
method.static = true;
|
|
391
|
+
return method;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function normalizeExposePropertyToClassMethod(property) {
|
|
395
|
+
if (t$7.isSpreadElement(property)) {
|
|
396
|
+
throw new Error("^expose(...) does not accept spread elements.");
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (t$7.isObjectMethod(property)) {
|
|
400
|
+
if (property.kind !== "method") {
|
|
401
|
+
throw new Error("^expose(...) only accepts plain methods.");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return t$7.classMethod(
|
|
405
|
+
"method",
|
|
406
|
+
t$7.cloneNode(property.key),
|
|
407
|
+
property.params.map((param) => t$7.cloneNode(param)),
|
|
408
|
+
t$7.cloneNode(property.body),
|
|
409
|
+
property.computed
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (!t$7.isObjectProperty(property)) {
|
|
414
|
+
throw new Error("^expose(...) only accepts plain methods.");
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const value = property.value;
|
|
418
|
+
if (!t$7.isFunctionExpression(value) && !t$7.isArrowFunctionExpression(value)) {
|
|
419
|
+
throw new Error("^expose(...) values must be functions.");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const body = t$7.isBlockStatement(value.body)
|
|
423
|
+
? t$7.cloneNode(value.body)
|
|
424
|
+
: t$7.blockStatement([t$7.returnStatement(t$7.cloneNode(value.body))]);
|
|
425
|
+
|
|
426
|
+
const method = t$7.classMethod(
|
|
427
|
+
"method",
|
|
428
|
+
t$7.cloneNode(property.key),
|
|
429
|
+
value.params.map((param) => t$7.cloneNode(param)),
|
|
430
|
+
body,
|
|
431
|
+
property.computed
|
|
432
|
+
);
|
|
433
|
+
method.async = value.async;
|
|
434
|
+
method.generator = value.generator || false;
|
|
435
|
+
return method;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function assertStaticHoistsStayTopLevel(functionPath) {
|
|
439
|
+
functionPath.traverse({
|
|
440
|
+
CallExpression(callPath) {
|
|
441
|
+
if (!t$7.isIdentifier(callPath.node.callee)) return;
|
|
442
|
+
if (!callPath.node.callee.name.startsWith("__litsx_static_")) return;
|
|
443
|
+
|
|
444
|
+
const statementParent = callPath.parentPath;
|
|
445
|
+
const blockParent = statementParent?.parentPath;
|
|
446
|
+
|
|
447
|
+
if (
|
|
448
|
+
statementParent?.isExpressionStatement() &&
|
|
449
|
+
blockParent?.isBlockStatement() &&
|
|
450
|
+
blockParent.node === functionPath.node.body
|
|
451
|
+
) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const macroName = callPath.node.callee.name.slice("__litsx_static_".length);
|
|
456
|
+
throw callPath.buildCodeFrameError(
|
|
457
|
+
`^${macroName}(...) must appear as a top-level statement in the component body.`
|
|
458
|
+
);
|
|
459
|
+
},
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function containsUnsafeCssCall(node) {
|
|
464
|
+
if (!node || typeof node !== "object") return false;
|
|
465
|
+
if (
|
|
466
|
+
t$7.isCallExpression(node) &&
|
|
467
|
+
t$7.isIdentifier(node.callee, { name: "unsafeCSS" })
|
|
468
|
+
) {
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return Object.values(node).some((value) => {
|
|
473
|
+
if (Array.isArray(value)) {
|
|
474
|
+
return value.some((entry) => containsUnsafeCssCall(entry));
|
|
475
|
+
}
|
|
476
|
+
return containsUnsafeCssCall(value);
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function isStaticStylesExpression(node, functionPath, seenBindings = new Set()) {
|
|
481
|
+
if (
|
|
482
|
+
t$7.isStringLiteral(node) ||
|
|
483
|
+
t$7.isNumericLiteral(node) ||
|
|
484
|
+
t$7.isBooleanLiteral(node) ||
|
|
485
|
+
t$7.isNullLiteral(node) ||
|
|
486
|
+
t$7.isBigIntLiteral?.(node)
|
|
487
|
+
) {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (t$7.isTemplateLiteral(node)) {
|
|
492
|
+
return node.expressions.every((expression) =>
|
|
493
|
+
isStaticStylesExpression(expression, functionPath, seenBindings)
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (t$7.isIdentifier(node)) {
|
|
498
|
+
return isStaticStylesIdentifier(node, functionPath, seenBindings);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (t$7.isUnaryExpression(node)) {
|
|
502
|
+
return isStaticStylesExpression(node.argument, functionPath, seenBindings);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (t$7.isBinaryExpression(node) || t$7.isLogicalExpression(node)) {
|
|
506
|
+
return (
|
|
507
|
+
isStaticStylesExpression(node.left, functionPath, seenBindings) &&
|
|
508
|
+
isStaticStylesExpression(node.right, functionPath, seenBindings)
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (t$7.isConditionalExpression(node)) {
|
|
513
|
+
return (
|
|
514
|
+
isStaticStylesExpression(node.test, functionPath, seenBindings) &&
|
|
515
|
+
isStaticStylesExpression(node.consequent, functionPath, seenBindings) &&
|
|
516
|
+
isStaticStylesExpression(node.alternate, functionPath, seenBindings)
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (t$7.isArrayExpression(node)) {
|
|
521
|
+
return node.elements.every((element) =>
|
|
522
|
+
element == null || isStaticStylesExpression(element, functionPath, seenBindings)
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
if (t$7.isObjectExpression(node)) {
|
|
527
|
+
return node.properties.every((property) => {
|
|
528
|
+
if (t$7.isObjectProperty(property)) {
|
|
529
|
+
return (
|
|
530
|
+
(!property.computed ||
|
|
531
|
+
isStaticStylesExpression(property.key, functionPath, seenBindings)) &&
|
|
532
|
+
isStaticStylesExpression(property.value, functionPath, seenBindings)
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
return false;
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (t$7.isMemberExpression(node)) {
|
|
540
|
+
return (
|
|
541
|
+
isStaticStylesExpression(node.object, functionPath, seenBindings) &&
|
|
542
|
+
(!node.computed ||
|
|
543
|
+
isStaticStylesExpression(node.property, functionPath, seenBindings))
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (t$7.isCallExpression(node)) {
|
|
548
|
+
return (
|
|
549
|
+
isStaticStylesExpression(node.callee, functionPath, seenBindings) &&
|
|
550
|
+
node.arguments.every((argument) =>
|
|
551
|
+
t$7.isSpreadElement(argument)
|
|
552
|
+
? false
|
|
553
|
+
: isStaticStylesExpression(argument, functionPath, seenBindings)
|
|
554
|
+
)
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if (t$7.isTaggedTemplateExpression(node)) {
|
|
559
|
+
return (
|
|
560
|
+
isStaticStylesExpression(node.tag, functionPath, seenBindings) &&
|
|
561
|
+
isStaticStylesExpression(node.quasi, functionPath, seenBindings)
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function isStaticStylesIdentifier(node, functionPath, seenBindings) {
|
|
569
|
+
const binding = functionPath?.scope
|
|
570
|
+
? functionPath.scope.getBinding(node.name)
|
|
571
|
+
: null;
|
|
572
|
+
|
|
573
|
+
if (!binding) {
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (binding.path.findParent((parent) => parent === functionPath)) {
|
|
578
|
+
return false;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (
|
|
582
|
+
binding.path.isImportSpecifier() ||
|
|
583
|
+
binding.path.isImportDefaultSpecifier() ||
|
|
584
|
+
binding.path.isImportNamespaceSpecifier()
|
|
585
|
+
) {
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (binding.path.isVariableDeclarator()) {
|
|
590
|
+
if (binding.kind !== "const" || !binding.path.node.init) {
|
|
591
|
+
return false;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (seenBindings.has(binding)) {
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
seenBindings.add(binding);
|
|
599
|
+
return isStaticStylesExpression(binding.path.node.init, functionPath, seenBindings);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (binding.path.isFunctionDeclaration() || binding.path.isClassDeclaration()) {
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function processStaticHoists({
|
|
610
|
+
functionPath,
|
|
611
|
+
node,
|
|
612
|
+
renderStatements,
|
|
613
|
+
programPath,
|
|
614
|
+
propertiesStatic,
|
|
615
|
+
classMembers,
|
|
616
|
+
options = {},
|
|
617
|
+
getOrCreateModuleStaticHoistSymbol,
|
|
618
|
+
}) {
|
|
619
|
+
const staticStyles = [];
|
|
620
|
+
const staticProps = [];
|
|
621
|
+
const staticHoists = [];
|
|
622
|
+
let lightDomRequested = options.defaultDomMode === "light";
|
|
623
|
+
|
|
624
|
+
if (t$7.isBlockStatement(node.body)) {
|
|
625
|
+
for (let index = renderStatements.length - 1; index >= 0; index -= 1) {
|
|
626
|
+
const propertyOptions = getStaticPropsExpression(renderStatements[index]);
|
|
627
|
+
if (propertyOptions) {
|
|
628
|
+
if (propertyOptions.__litsxHoistedProperties) {
|
|
629
|
+
staticHoists.unshift({
|
|
630
|
+
name: "properties",
|
|
631
|
+
expression: propertyOptions.expression,
|
|
632
|
+
});
|
|
633
|
+
} else {
|
|
634
|
+
staticProps.unshift(propertyOptions);
|
|
635
|
+
}
|
|
636
|
+
renderStatements.splice(index, 1);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const cssExpression = getStaticStylesExpression(renderStatements[index], functionPath);
|
|
641
|
+
if (!cssExpression) continue;
|
|
642
|
+
if (cssExpression.__litsxHoistedStyles) {
|
|
643
|
+
staticHoists.unshift({
|
|
644
|
+
name: "styles",
|
|
645
|
+
expression: cssExpression.expression,
|
|
646
|
+
});
|
|
647
|
+
} else {
|
|
648
|
+
staticStyles.unshift(cssExpression);
|
|
649
|
+
}
|
|
650
|
+
renderStatements.splice(index, 1);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
for (let index = renderStatements.length - 1; index >= 0; index -= 1) {
|
|
654
|
+
if (isLightDomHoist(renderStatements[index])) {
|
|
655
|
+
lightDomRequested = true;
|
|
656
|
+
renderStatements.splice(index, 1);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
for (let index = renderStatements.length - 1; index >= 0; index -= 1) {
|
|
661
|
+
const hoistExpression = getStaticHoistExpression(renderStatements[index], functionPath);
|
|
662
|
+
if (!hoistExpression) continue;
|
|
663
|
+
staticHoists.unshift(hoistExpression);
|
|
664
|
+
renderStatements.splice(index, 1);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (lightDomRequested && staticHoists.some((entry) => entry.name === "shadowRootOptions")) {
|
|
669
|
+
throw new Error("^lightDom() cannot be combined with ^shadowRootOptions(...).");
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (staticProps.length > 0) {
|
|
673
|
+
mergeStaticPropsIntoProperties(propertiesStatic, staticProps);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const hasHoistedProperties = staticHoists.some((entry) => entry.name === "properties");
|
|
677
|
+
if (propertiesStatic.length > 0 && !hasHoistedProperties) {
|
|
678
|
+
const classProperties = t$7.classProperty(
|
|
679
|
+
t$7.identifier("properties"),
|
|
680
|
+
t$7.objectExpression(propertiesStatic),
|
|
681
|
+
null,
|
|
682
|
+
[],
|
|
683
|
+
false
|
|
684
|
+
);
|
|
685
|
+
|
|
686
|
+
classProperties.static = true;
|
|
687
|
+
classMembers.push(classProperties);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const hasHoistedStyles = staticHoists.some((entry) => entry.name === "styles");
|
|
691
|
+
if (staticStyles.length > 0 && !hasHoistedStyles) {
|
|
692
|
+
const stylesProperty = t$7.classProperty(
|
|
693
|
+
t$7.identifier("styles"),
|
|
694
|
+
staticStyles.length === 1 ? staticStyles[0] : t$7.arrayExpression(staticStyles),
|
|
695
|
+
null,
|
|
696
|
+
[],
|
|
697
|
+
false
|
|
698
|
+
);
|
|
699
|
+
stylesProperty.static = true;
|
|
700
|
+
classMembers.push(stylesProperty);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
const hoistSymbolDeclarations = [];
|
|
704
|
+
let needsStaticHoistsMixin = false;
|
|
705
|
+
const hoistMembers = staticHoists.flatMap((hoist) => {
|
|
706
|
+
if (hoist.name === "expose") {
|
|
707
|
+
return createExposeHoistMembers(hoist.expression);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
needsStaticHoistsMixin = true;
|
|
711
|
+
const { symbolId, declaration } = getOrCreateModuleStaticHoistSymbol(programPath, hoist.name);
|
|
712
|
+
if (declaration) {
|
|
713
|
+
hoistSymbolDeclarations.push(declaration);
|
|
714
|
+
const symbolMap = programPath.getData("__litsxStaticHoistSymbols");
|
|
715
|
+
if (symbolMap?.has(hoist.name)) {
|
|
716
|
+
symbolMap.set(hoist.name, { symbolId, declaration: null });
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (hoist.name === "properties") {
|
|
721
|
+
return createStaticHoistGetter(
|
|
722
|
+
"properties",
|
|
723
|
+
symbolId,
|
|
724
|
+
createPropertiesHoistResolver(propertiesStatic, staticProps, hoist.expression)
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (hoist.name === "styles") {
|
|
729
|
+
return createStaticHoistGetter(
|
|
730
|
+
"styles",
|
|
731
|
+
symbolId,
|
|
732
|
+
createStylesHoistResolver(staticStyles, hoist.expression)
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return createStaticHoistGetter(
|
|
737
|
+
hoist.name,
|
|
738
|
+
symbolId,
|
|
739
|
+
resolveStaticHoistExpression(hoist.expression)
|
|
740
|
+
);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
lightDomRequested,
|
|
745
|
+
hoistMembers,
|
|
746
|
+
hoistSymbolDeclarations,
|
|
747
|
+
needsStaticHoistsMixin,
|
|
748
|
+
needsCss:
|
|
749
|
+
staticStyles.length > 0 ||
|
|
750
|
+
staticHoists.some((entry) => entry.name === "styles"),
|
|
751
|
+
needsUnsafeCss:
|
|
752
|
+
staticStyles.some(containsUnsafeCssCall) ||
|
|
753
|
+
staticHoists.some(
|
|
754
|
+
(entry) => entry.name === "styles" && containsUnsafeCssCall(entry.expression)
|
|
755
|
+
),
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
let t$6;
|
|
760
|
+
|
|
761
|
+
function setHandlersBabelTypes(types) {
|
|
762
|
+
t$6 = types;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function isHoistableHandler(exprPath, componentPath) {
|
|
766
|
+
let hoistable = true;
|
|
767
|
+
|
|
768
|
+
exprPath.traverse({
|
|
769
|
+
Identifier(identifierPath) {
|
|
770
|
+
if (!identifierPath.isReferencedIdentifier()) return;
|
|
771
|
+
|
|
772
|
+
const name = identifierPath.node.name;
|
|
773
|
+
const ownBinding = exprPath.scope.getOwnBinding(name);
|
|
774
|
+
if (ownBinding) return;
|
|
775
|
+
|
|
776
|
+
const binding = identifierPath.scope.getBinding(name);
|
|
777
|
+
if (!binding) return;
|
|
778
|
+
|
|
779
|
+
if (binding.scope === componentPath.scope) {
|
|
780
|
+
hoistable = false;
|
|
781
|
+
identifierPath.stop();
|
|
782
|
+
}
|
|
783
|
+
},
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
return hoistable;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
function ensureUniqueHandlerName(baseName, usedNames) {
|
|
790
|
+
let candidate = baseName;
|
|
791
|
+
let suffix = 2;
|
|
792
|
+
|
|
793
|
+
while (usedNames.has(candidate)) {
|
|
794
|
+
candidate = `${baseName}${suffix}`;
|
|
795
|
+
suffix += 1;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
usedNames.add(candidate);
|
|
799
|
+
return candidate;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
function generateHandlerName(attrName, usedNames) {
|
|
803
|
+
const eventSegment = attrName.slice(2) || "Event";
|
|
804
|
+
const base = `handle${eventSegment}`;
|
|
805
|
+
return ensureUniqueHandlerName(base, usedNames);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function normalizeHandlerBody(body) {
|
|
809
|
+
if (t$6.isBlockStatement(body)) {
|
|
810
|
+
return t$6.cloneNode(body, true);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
return t$6.blockStatement([t$6.returnStatement(t$6.cloneNode(body, true))]);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function hoistDeclaredHandlers(functionPath, usedNames) {
|
|
817
|
+
const handlerInfos = [];
|
|
818
|
+
const bodyPath = functionPath.get("body");
|
|
819
|
+
if (!bodyPath.isBlockStatement()) {
|
|
820
|
+
return handlerInfos;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
functionPath.traverse({
|
|
824
|
+
VariableDeclarator(path) {
|
|
825
|
+
if (path.getFunctionParent() !== functionPath) return;
|
|
826
|
+
|
|
827
|
+
const id = path.node.id;
|
|
828
|
+
if (!t$6.isIdentifier(id)) return;
|
|
829
|
+
|
|
830
|
+
const initPath = path.get("init");
|
|
831
|
+
if (!initPath.isArrowFunctionExpression() && !initPath.isFunctionExpression()) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
const statementParent = path.getStatementParent();
|
|
836
|
+
if (!statementParent || statementParent.parentPath !== bodyPath) {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
if (!isHoistableHandler(initPath, functionPath)) return;
|
|
841
|
+
|
|
842
|
+
const originalName = id.name;
|
|
843
|
+
usedNames.delete(originalName);
|
|
844
|
+
const handlerName = ensureUniqueHandlerName(originalName, usedNames);
|
|
845
|
+
const binding = path.scope.getBinding(originalName);
|
|
846
|
+
|
|
847
|
+
if (!binding) return;
|
|
848
|
+
|
|
849
|
+
binding.referencePaths.slice().forEach((refPath) => {
|
|
850
|
+
if (!refPath.node || refPath.removed) return;
|
|
851
|
+
refPath.replaceWith(
|
|
852
|
+
t$6.memberExpression(t$6.thisExpression(), t$6.identifier(handlerName))
|
|
853
|
+
);
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
handlerInfos.push({
|
|
857
|
+
name: handlerName,
|
|
858
|
+
params: initPath.node.params.map((param) => t$6.cloneNode(param, true)),
|
|
859
|
+
body: normalizeHandlerBody(initPath.node.body),
|
|
860
|
+
async: initPath.node.async,
|
|
861
|
+
generator: initPath.node.generator,
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
path.remove();
|
|
865
|
+
|
|
866
|
+
if (
|
|
867
|
+
statementParent.isVariableDeclaration() &&
|
|
868
|
+
statementParent.node.declarations.length === 0
|
|
869
|
+
) {
|
|
870
|
+
statementParent.remove();
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
binding.scope.removeBinding(originalName);
|
|
874
|
+
},
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
return handlerInfos;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function hoistEventHandlers(functionPath, usedNames) {
|
|
881
|
+
const handlerInfos = [];
|
|
882
|
+
|
|
883
|
+
functionPath.traverse({
|
|
884
|
+
JSXAttribute(attrPath) {
|
|
885
|
+
if (attrPath.getFunctionParent() !== functionPath) return;
|
|
886
|
+
|
|
887
|
+
const { node } = attrPath;
|
|
888
|
+
if (!t$6.isJSXIdentifier(node.name)) return;
|
|
889
|
+
|
|
890
|
+
const attrName = node.name.name;
|
|
891
|
+
if (!/^on[A-Z]/.test(attrName)) return;
|
|
892
|
+
|
|
893
|
+
const valuePath = attrPath.get("value");
|
|
894
|
+
if (!valuePath.isJSXExpressionContainer()) return;
|
|
895
|
+
|
|
896
|
+
const exprPath = valuePath.get("expression");
|
|
897
|
+
if (
|
|
898
|
+
!exprPath.isArrowFunctionExpression() &&
|
|
899
|
+
!exprPath.isFunctionExpression()
|
|
900
|
+
) {
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
if (!isHoistableHandler(exprPath, functionPath)) return;
|
|
905
|
+
|
|
906
|
+
const handlerName = generateHandlerName(attrName, usedNames);
|
|
907
|
+
|
|
908
|
+
handlerInfos.push({
|
|
909
|
+
name: handlerName,
|
|
910
|
+
params: exprPath.node.params.map((param) => t$6.cloneNode(param, true)),
|
|
911
|
+
body: normalizeHandlerBody(exprPath.node.body),
|
|
912
|
+
async: exprPath.node.async,
|
|
913
|
+
generator: exprPath.node.generator,
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
valuePath.replaceWith(
|
|
917
|
+
t$6.jsxExpressionContainer(
|
|
918
|
+
t$6.memberExpression(t$6.thisExpression(), t$6.identifier(handlerName))
|
|
919
|
+
)
|
|
920
|
+
);
|
|
921
|
+
},
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
return handlerInfos;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function isNativeIntrinsicJsxElement(nameNode) {
|
|
928
|
+
return t$6.isJSXIdentifier(nameNode) && /^[a-z]/.test(nameNode.name);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function collectNativeClassNameWarnings(functionPath, warn, options = {}) {
|
|
932
|
+
if (typeof warn !== "function" || options.suppressNativeClassNameWarning === true) {
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
functionPath.traverse({
|
|
937
|
+
JSXAttribute(attrPath) {
|
|
938
|
+
if (attrPath.getFunctionParent() !== functionPath) return;
|
|
939
|
+
|
|
940
|
+
const openingElement = attrPath.parentPath;
|
|
941
|
+
if (!openingElement?.isJSXOpeningElement()) return;
|
|
942
|
+
if (!isNativeIntrinsicJsxElement(openingElement.node.name)) return;
|
|
943
|
+
|
|
944
|
+
const { node } = attrPath;
|
|
945
|
+
if (!t$6.isJSXIdentifier(node.name, { name: "className" })) return;
|
|
946
|
+
|
|
947
|
+
warn({
|
|
948
|
+
code: "LITSX_NATIVE_CLASSNAME",
|
|
949
|
+
message:
|
|
950
|
+
'`className` is not native LitSX syntax. Use `class` in native LitSX, or add the React compatibility layer to rewrite `className`.',
|
|
951
|
+
attributeName: "className",
|
|
952
|
+
tagName: openingElement.node.name.name,
|
|
953
|
+
line: node.loc?.start?.line ?? null,
|
|
954
|
+
column: node.loc?.start?.column ?? null,
|
|
955
|
+
});
|
|
956
|
+
},
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
function processHandlers(functionPath, usedNames) {
|
|
961
|
+
const declaredHandlers = hoistDeclaredHandlers(functionPath, usedNames);
|
|
962
|
+
const inlineHandlers = hoistEventHandlers(functionPath, usedNames);
|
|
963
|
+
return [...declaredHandlers, ...inlineHandlers];
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function createHandlerClassMember({ name, params, body, async, generator }) {
|
|
967
|
+
const method = t$6.classMethod(
|
|
968
|
+
"method",
|
|
969
|
+
t$6.identifier(name),
|
|
970
|
+
params.map((param) => t$6.cloneNode(param, true)),
|
|
971
|
+
t$6.cloneNode(body, true)
|
|
972
|
+
);
|
|
973
|
+
|
|
974
|
+
method.async = Boolean(async);
|
|
975
|
+
method.generator = Boolean(generator);
|
|
976
|
+
|
|
977
|
+
return method;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
let t$5;
|
|
981
|
+
|
|
982
|
+
function setWrapperUtilsBabelTypes(nextTypes) {
|
|
983
|
+
t$5 = nextTypes;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
function emitWrapperWarnings(meta, warn) {
|
|
987
|
+
if (!meta || typeof warn !== "function" || !Array.isArray(meta.warnings)) {
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
meta.warnings.forEach((warning) => warn(warning));
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
function pruneWrapperImports(meta) {
|
|
995
|
+
if (!meta || !Array.isArray(meta.cleanups)) {
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
meta.cleanups.forEach((cleanup) => {
|
|
1000
|
+
if (!cleanup || !cleanup.shouldRemoveImport || !cleanup.importSpecifierPath) {
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
const importDecl = cleanup.importSpecifierPath.parentPath;
|
|
1004
|
+
cleanup.importSpecifierPath.remove();
|
|
1005
|
+
if (importDecl.node.specifiers.length === 0) {
|
|
1006
|
+
importDecl.remove();
|
|
1007
|
+
}
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
function maybeTransformWrappedVariableDeclarator({
|
|
1012
|
+
varPath,
|
|
1013
|
+
resolvedPluginOptions,
|
|
1014
|
+
state,
|
|
1015
|
+
transformFunction,
|
|
1016
|
+
updateTransformState,
|
|
1017
|
+
getWrapperMetadata,
|
|
1018
|
+
}) {
|
|
1019
|
+
if (typeof getWrapperMetadata !== "function") {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
const initPath = varPath.get("init");
|
|
1024
|
+
if (!initPath || !initPath.isCallExpression()) {
|
|
1025
|
+
return false;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const wrapperMeta = getWrapperMetadata(initPath);
|
|
1029
|
+
if (!wrapperMeta) {
|
|
1030
|
+
return false;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
1034
|
+
state?.__litsxWarnings?.push(warning);
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
const programPath = varPath.findParent((p) => p.isProgram());
|
|
1038
|
+
const localName = t$5.isIdentifier(varPath.node.id)
|
|
1039
|
+
? varPath.node.id.name
|
|
1040
|
+
: undefined;
|
|
1041
|
+
|
|
1042
|
+
const classNode = transformFunction(
|
|
1043
|
+
wrapperMeta.functionPath,
|
|
1044
|
+
programPath,
|
|
1045
|
+
localName,
|
|
1046
|
+
{
|
|
1047
|
+
...resolvedPluginOptions,
|
|
1048
|
+
...wrapperMeta.options,
|
|
1049
|
+
typeResolver: state?.__litsxTypeResolver,
|
|
1050
|
+
warn: (warning) => {
|
|
1051
|
+
state?.__litsxWarnings?.push(warning);
|
|
1052
|
+
},
|
|
1053
|
+
}
|
|
1054
|
+
);
|
|
1055
|
+
|
|
1056
|
+
if (!classNode) {
|
|
1057
|
+
return true;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
if (t$5.isIdentifier(varPath.node.id)) {
|
|
1061
|
+
varPath.scope.removeBinding(varPath.node.id.name);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
const declarationPath = varPath.parentPath;
|
|
1065
|
+
if (!declarationPath.isVariableDeclaration()) {
|
|
1066
|
+
return true;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
declarationPath.replaceWith(classNode);
|
|
1070
|
+
declarationPath.requeue();
|
|
1071
|
+
pruneWrapperImports(wrapperMeta);
|
|
1072
|
+
updateTransformState?.(state, classNode);
|
|
1073
|
+
return true;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
function handlePotentialComponentExport({
|
|
1077
|
+
exportPath,
|
|
1078
|
+
state,
|
|
1079
|
+
isDefault = false,
|
|
1080
|
+
transformFunction,
|
|
1081
|
+
isInsideFunctionOrClass,
|
|
1082
|
+
updateTransformState,
|
|
1083
|
+
getWrapperMetadata,
|
|
1084
|
+
}) {
|
|
1085
|
+
const declaration = exportPath.node.declaration;
|
|
1086
|
+
const typeResolver = state?.__litsxTypeResolver || null;
|
|
1087
|
+
if (!declaration || isInsideFunctionOrClass(exportPath)) {
|
|
1088
|
+
return false;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
if (
|
|
1092
|
+
t$5.isFunctionDeclaration(declaration) ||
|
|
1093
|
+
(t$5.isVariableDeclaration(declaration) &&
|
|
1094
|
+
declaration.declarations.length === 1 &&
|
|
1095
|
+
t$5.isArrowFunctionExpression(declaration.declarations[0].init))
|
|
1096
|
+
) {
|
|
1097
|
+
const funcPath = exportPath.get("declaration");
|
|
1098
|
+
const declarationPath = funcPath.isVariableDeclaration()
|
|
1099
|
+
? funcPath.get("declarations.0.init")
|
|
1100
|
+
: funcPath;
|
|
1101
|
+
let exportName;
|
|
1102
|
+
if (t$5.isFunctionDeclaration(declaration) && declaration.id) {
|
|
1103
|
+
exportName = declaration.id.name;
|
|
1104
|
+
} else if (
|
|
1105
|
+
t$5.isVariableDeclaration(declaration) &&
|
|
1106
|
+
t$5.isIdentifier(declaration.declarations[0].id)
|
|
1107
|
+
) {
|
|
1108
|
+
exportName = declaration.declarations[0].id.name;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
const classNode = transformFunction(
|
|
1112
|
+
declarationPath,
|
|
1113
|
+
exportPath.findParent((p) => p.isProgram()),
|
|
1114
|
+
exportName,
|
|
1115
|
+
{
|
|
1116
|
+
...state?.__litsxResolvedPluginOptions,
|
|
1117
|
+
typeResolver,
|
|
1118
|
+
warn: (warning) => {
|
|
1119
|
+
state?.__litsxWarnings?.push(warning);
|
|
1120
|
+
},
|
|
1121
|
+
}
|
|
1122
|
+
);
|
|
1123
|
+
|
|
1124
|
+
if (!classNode) return true;
|
|
1125
|
+
|
|
1126
|
+
if (exportName) {
|
|
1127
|
+
exportPath.scope.removeBinding(exportName);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
exportPath.insertBefore(
|
|
1131
|
+
isDefault
|
|
1132
|
+
? t$5.exportDefaultDeclaration(classNode)
|
|
1133
|
+
: t$5.exportNamedDeclaration(classNode, [])
|
|
1134
|
+
);
|
|
1135
|
+
exportPath.remove();
|
|
1136
|
+
updateTransformState?.(state, classNode);
|
|
1137
|
+
return true;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
if (
|
|
1141
|
+
typeof getWrapperMetadata === "function" &&
|
|
1142
|
+
t$5.isVariableDeclaration(declaration) &&
|
|
1143
|
+
declaration.declarations.length === 1
|
|
1144
|
+
) {
|
|
1145
|
+
const declaratorPath = exportPath.get("declaration.declarations.0");
|
|
1146
|
+
const initPath = declaratorPath.get("init");
|
|
1147
|
+
const exportName = t$5.isIdentifier(declaratorPath.node.id)
|
|
1148
|
+
? declaratorPath.node.id.name
|
|
1149
|
+
: undefined;
|
|
1150
|
+
const programPath = exportPath.findParent((p) => p.isProgram());
|
|
1151
|
+
|
|
1152
|
+
if (initPath.isCallExpression()) {
|
|
1153
|
+
const wrapperMeta = getWrapperMetadata(initPath);
|
|
1154
|
+
if (!wrapperMeta) return false;
|
|
1155
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
1156
|
+
state?.__litsxWarnings?.push(warning);
|
|
1157
|
+
});
|
|
1158
|
+
|
|
1159
|
+
const classNode = transformFunction(
|
|
1160
|
+
wrapperMeta.functionPath,
|
|
1161
|
+
programPath,
|
|
1162
|
+
exportName,
|
|
1163
|
+
{
|
|
1164
|
+
...state?.__litsxResolvedPluginOptions,
|
|
1165
|
+
...wrapperMeta.options,
|
|
1166
|
+
typeResolver,
|
|
1167
|
+
warn: (warning) => {
|
|
1168
|
+
state?.__litsxWarnings?.push(warning);
|
|
1169
|
+
},
|
|
1170
|
+
}
|
|
1171
|
+
);
|
|
1172
|
+
if (!classNode) return true;
|
|
1173
|
+
|
|
1174
|
+
if (exportName) {
|
|
1175
|
+
exportPath.scope.removeBinding(exportName);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
exportPath.replaceWith(t$5.exportNamedDeclaration(classNode, []));
|
|
1179
|
+
exportPath.requeue();
|
|
1180
|
+
pruneWrapperImports(wrapperMeta);
|
|
1181
|
+
updateTransformState?.(state, classNode);
|
|
1182
|
+
return true;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
if (typeof getWrapperMetadata === "function" && isDefault && t$5.isCallExpression(declaration)) {
|
|
1187
|
+
const callPath = exportPath.get("declaration");
|
|
1188
|
+
const wrapperMeta = getWrapperMetadata(callPath);
|
|
1189
|
+
if (!wrapperMeta) return false;
|
|
1190
|
+
emitWrapperWarnings(wrapperMeta, (warning) => {
|
|
1191
|
+
state?.__litsxWarnings?.push(warning);
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
const programPath = exportPath.findParent((p) => p.isProgram());
|
|
1195
|
+
const inferredName = wrapperMeta.functionPath.node.id
|
|
1196
|
+
? wrapperMeta.functionPath.node.id.name
|
|
1197
|
+
: undefined;
|
|
1198
|
+
|
|
1199
|
+
const classNode = transformFunction(
|
|
1200
|
+
wrapperMeta.functionPath,
|
|
1201
|
+
programPath,
|
|
1202
|
+
inferredName,
|
|
1203
|
+
{
|
|
1204
|
+
...state?.__litsxResolvedPluginOptions,
|
|
1205
|
+
...wrapperMeta.options,
|
|
1206
|
+
typeResolver,
|
|
1207
|
+
warn: (warning) => {
|
|
1208
|
+
state?.__litsxWarnings?.push(warning);
|
|
1209
|
+
},
|
|
1210
|
+
}
|
|
1211
|
+
);
|
|
1212
|
+
if (!classNode) return true;
|
|
1213
|
+
|
|
1214
|
+
exportPath.replaceWith(t$5.exportDefaultDeclaration(classNode));
|
|
1215
|
+
exportPath.requeue();
|
|
1216
|
+
pruneWrapperImports(wrapperMeta);
|
|
1217
|
+
updateTransformState?.(state, classNode);
|
|
1218
|
+
return true;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
return false;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
let t$4;
|
|
1225
|
+
|
|
1226
|
+
function setRefsBabelTypes(nextTypes) {
|
|
1227
|
+
t$4 = nextTypes;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
function createThisMemberExpression$3(propName) {
|
|
1231
|
+
return t$4.memberExpression(t$4.thisExpression(), t$4.identifier(propName));
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
function createManagedRefLookupExpression(refName) {
|
|
1235
|
+
const selectorLiteral = t$4.stringLiteral(`[data-ref="${refName}"]`);
|
|
1236
|
+
|
|
1237
|
+
const renderRootQuery = t$4.optionalCallExpression(
|
|
1238
|
+
t$4.optionalMemberExpression(
|
|
1239
|
+
t$4.memberExpression(t$4.thisExpression(), t$4.identifier("renderRoot")),
|
|
1240
|
+
t$4.identifier("querySelector"),
|
|
1241
|
+
false,
|
|
1242
|
+
true
|
|
1243
|
+
),
|
|
1244
|
+
[selectorLiteral],
|
|
1245
|
+
false
|
|
1246
|
+
);
|
|
1247
|
+
|
|
1248
|
+
const hostQuery = t$4.callExpression(
|
|
1249
|
+
t$4.memberExpression(t$4.thisExpression(), t$4.identifier("querySelector")),
|
|
1250
|
+
[t$4.cloneNode(selectorLiteral)]
|
|
1251
|
+
);
|
|
1252
|
+
|
|
1253
|
+
return t$4.logicalExpression("??", renderRootQuery, hostQuery);
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
function createForwardedTargetRefSyncStatement(propName, refName) {
|
|
1257
|
+
return t$4.expressionStatement(
|
|
1258
|
+
t$4.callExpression(t$4.identifier("useCallbackRef"), [
|
|
1259
|
+
t$4.thisExpression(),
|
|
1260
|
+
t$4.arrowFunctionExpression([], createManagedRefLookupExpression(refName)),
|
|
1261
|
+
t$4.arrowFunctionExpression(
|
|
1262
|
+
[t$4.identifier("node")],
|
|
1263
|
+
t$4.blockStatement([
|
|
1264
|
+
t$4.variableDeclaration("const", [
|
|
1265
|
+
t$4.variableDeclarator(
|
|
1266
|
+
t$4.identifier("componentRef"),
|
|
1267
|
+
createThisMemberExpression$3(propName)
|
|
1268
|
+
),
|
|
1269
|
+
]),
|
|
1270
|
+
t$4.ifStatement(
|
|
1271
|
+
t$4.binaryExpression(
|
|
1272
|
+
"===",
|
|
1273
|
+
t$4.unaryExpression("typeof", t$4.identifier("componentRef")),
|
|
1274
|
+
t$4.stringLiteral("function")
|
|
1275
|
+
),
|
|
1276
|
+
t$4.blockStatement([
|
|
1277
|
+
t$4.expressionStatement(
|
|
1278
|
+
t$4.callExpression(t$4.identifier("componentRef"), [t$4.identifier("node")])
|
|
1279
|
+
),
|
|
1280
|
+
]),
|
|
1281
|
+
t$4.ifStatement(
|
|
1282
|
+
t$4.logicalExpression(
|
|
1283
|
+
"&&",
|
|
1284
|
+
t$4.identifier("componentRef"),
|
|
1285
|
+
t$4.binaryExpression(
|
|
1286
|
+
"===",
|
|
1287
|
+
t$4.unaryExpression("typeof", t$4.identifier("componentRef")),
|
|
1288
|
+
t$4.stringLiteral("object")
|
|
1289
|
+
)
|
|
1290
|
+
),
|
|
1291
|
+
t$4.blockStatement([
|
|
1292
|
+
t$4.expressionStatement(
|
|
1293
|
+
t$4.assignmentExpression(
|
|
1294
|
+
"=",
|
|
1295
|
+
t$4.memberExpression(t$4.identifier("componentRef"), t$4.identifier("current")),
|
|
1296
|
+
t$4.identifier("node")
|
|
1297
|
+
)
|
|
1298
|
+
),
|
|
1299
|
+
])
|
|
1300
|
+
)
|
|
1301
|
+
),
|
|
1302
|
+
])
|
|
1303
|
+
),
|
|
1304
|
+
t$4.arrayExpression([createThisMemberExpression$3(propName)]),
|
|
1305
|
+
])
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
function isStandardElementJsxName(nameNode) {
|
|
1310
|
+
if (!t$4.isJSXIdentifier(nameNode)) {
|
|
1311
|
+
return false;
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
const name = nameNode.name || "";
|
|
1315
|
+
return Boolean(name) && name[0] === name[0].toLowerCase() && !name.includes("-");
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
function createComponentInstanceRefSyncStatement() {
|
|
1319
|
+
return t$4.expressionStatement(
|
|
1320
|
+
t$4.callExpression(t$4.identifier("useCallbackRef"), [
|
|
1321
|
+
t$4.thisExpression(),
|
|
1322
|
+
t$4.arrowFunctionExpression([], t$4.thisExpression()),
|
|
1323
|
+
t$4.arrowFunctionExpression(
|
|
1324
|
+
[t$4.identifier("node")],
|
|
1325
|
+
t$4.blockStatement([
|
|
1326
|
+
t$4.variableDeclaration("const", [
|
|
1327
|
+
t$4.variableDeclarator(
|
|
1328
|
+
t$4.identifier("componentRef"),
|
|
1329
|
+
createThisMemberExpression$3("ref")
|
|
1330
|
+
),
|
|
1331
|
+
]),
|
|
1332
|
+
t$4.ifStatement(
|
|
1333
|
+
t$4.binaryExpression(
|
|
1334
|
+
"===",
|
|
1335
|
+
t$4.unaryExpression("typeof", t$4.identifier("componentRef")),
|
|
1336
|
+
t$4.stringLiteral("function")
|
|
1337
|
+
),
|
|
1338
|
+
t$4.blockStatement([
|
|
1339
|
+
t$4.expressionStatement(
|
|
1340
|
+
t$4.callExpression(t$4.identifier("componentRef"), [t$4.identifier("node")])
|
|
1341
|
+
),
|
|
1342
|
+
]),
|
|
1343
|
+
t$4.ifStatement(
|
|
1344
|
+
t$4.logicalExpression(
|
|
1345
|
+
"&&",
|
|
1346
|
+
t$4.identifier("componentRef"),
|
|
1347
|
+
t$4.binaryExpression(
|
|
1348
|
+
"===",
|
|
1349
|
+
t$4.unaryExpression("typeof", t$4.identifier("componentRef")),
|
|
1350
|
+
t$4.stringLiteral("object")
|
|
1351
|
+
)
|
|
1352
|
+
),
|
|
1353
|
+
t$4.blockStatement([
|
|
1354
|
+
t$4.expressionStatement(
|
|
1355
|
+
t$4.assignmentExpression(
|
|
1356
|
+
"=",
|
|
1357
|
+
t$4.memberExpression(t$4.identifier("componentRef"), t$4.identifier("current")),
|
|
1358
|
+
t$4.identifier("node")
|
|
1359
|
+
)
|
|
1360
|
+
),
|
|
1361
|
+
])
|
|
1362
|
+
)
|
|
1363
|
+
),
|
|
1364
|
+
])
|
|
1365
|
+
),
|
|
1366
|
+
t$4.arrayExpression([createThisMemberExpression$3("ref")]),
|
|
1367
|
+
])
|
|
1368
|
+
);
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
function hasRefProp(functionPath) {
|
|
1372
|
+
const [firstParam] = functionPath.node.params || [];
|
|
1373
|
+
if (!firstParam) {
|
|
1374
|
+
return false;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
if (t$4.isObjectPattern(firstParam)) {
|
|
1378
|
+
return firstParam.properties.some((property) => {
|
|
1379
|
+
if (!t$4.isObjectProperty(property)) return false;
|
|
1380
|
+
return (
|
|
1381
|
+
t$4.isIdentifier(property.key, { name: "ref" }) ||
|
|
1382
|
+
t$4.isStringLiteral(property.key, { value: "ref" })
|
|
1383
|
+
);
|
|
1384
|
+
});
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
if (t$4.isAssignmentPattern(firstParam) && t$4.isObjectPattern(firstParam.left)) {
|
|
1388
|
+
return firstParam.left.properties.some((property) => {
|
|
1389
|
+
if (!t$4.isObjectProperty(property)) return false;
|
|
1390
|
+
return (
|
|
1391
|
+
t$4.isIdentifier(property.key, { name: "ref" }) ||
|
|
1392
|
+
t$4.isStringLiteral(property.key, { value: "ref" })
|
|
1393
|
+
);
|
|
1394
|
+
});
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
if (t$4.isIdentifier(firstParam)) {
|
|
1398
|
+
const binding = functionPath.scope.getBinding(firstParam.name);
|
|
1399
|
+
if (!binding) return false;
|
|
1400
|
+
return binding.referencePaths.some((refPath) => {
|
|
1401
|
+
const parentPath = refPath.parentPath;
|
|
1402
|
+
return Boolean(
|
|
1403
|
+
parentPath &&
|
|
1404
|
+
parentPath.isMemberExpression() &&
|
|
1405
|
+
parentPath.node.object === refPath.node &&
|
|
1406
|
+
t$4.isIdentifier(parentPath.node.property, { name: "ref" }) &&
|
|
1407
|
+
!parentPath.node.computed
|
|
1408
|
+
);
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
return false;
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
function lowerForwardedElementRefs(functionPath, propName) {
|
|
1416
|
+
if (!propName) {
|
|
1417
|
+
return [];
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
const callbackStatements = [];
|
|
1421
|
+
const seenRefNames = new Set();
|
|
1422
|
+
|
|
1423
|
+
functionPath.traverse({
|
|
1424
|
+
JSXAttribute(attrPath) {
|
|
1425
|
+
if (!t$4.isJSXIdentifier(attrPath.node.name, { name: "ref" })) return;
|
|
1426
|
+
|
|
1427
|
+
const value = attrPath.node.value;
|
|
1428
|
+
if (!t$4.isJSXExpressionContainer(value)) return;
|
|
1429
|
+
if (
|
|
1430
|
+
!t$4.isMemberExpression(value.expression) ||
|
|
1431
|
+
!t$4.isThisExpression(value.expression.object) ||
|
|
1432
|
+
!t$4.isIdentifier(value.expression.property, { name: propName }) ||
|
|
1433
|
+
value.expression.computed
|
|
1434
|
+
) {
|
|
1435
|
+
return;
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
const openingElement = attrPath.parentPath;
|
|
1439
|
+
if (!openingElement?.isJSXOpeningElement()) return;
|
|
1440
|
+
if (!isStandardElementJsxName(openingElement.node.name)) return;
|
|
1441
|
+
|
|
1442
|
+
const managedRefName = functionPath.scope.generateUidIdentifier(`${propName}Element`).name;
|
|
1443
|
+
attrPath.replaceWith(
|
|
1444
|
+
t$4.jsxAttribute(t$4.jsxIdentifier("data-ref"), t$4.stringLiteral(managedRefName))
|
|
1445
|
+
);
|
|
1446
|
+
|
|
1447
|
+
if (seenRefNames.has(managedRefName)) return;
|
|
1448
|
+
seenRefNames.add(managedRefName);
|
|
1449
|
+
callbackStatements.push(createForwardedTargetRefSyncStatement(propName, managedRefName));
|
|
1450
|
+
},
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
return callbackStatements;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
let t$3;
|
|
1457
|
+
|
|
1458
|
+
function setClassGenerationBabelTypes(nextTypes) {
|
|
1459
|
+
t$3 = nextTypes;
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
function createThisMemberExpression$2(propName) {
|
|
1463
|
+
return t$3.memberExpression(t$3.thisExpression(), t$3.identifier(propName));
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
function buildClassMembers({
|
|
1467
|
+
classMembers = [],
|
|
1468
|
+
defaults,
|
|
1469
|
+
renderStatements,
|
|
1470
|
+
handlerInfos,
|
|
1471
|
+
createHandlerClassMember,
|
|
1472
|
+
}) {
|
|
1473
|
+
if (defaults.size > 0) {
|
|
1474
|
+
const constructorStatements = [
|
|
1475
|
+
t$3.expressionStatement(t$3.callExpression(t$3.super(), [])),
|
|
1476
|
+
];
|
|
1477
|
+
|
|
1478
|
+
defaults.forEach((defaultExpression, propName) => {
|
|
1479
|
+
constructorStatements.push(
|
|
1480
|
+
t$3.expressionStatement(
|
|
1481
|
+
t$3.assignmentExpression(
|
|
1482
|
+
"??=",
|
|
1483
|
+
createThisMemberExpression$2(propName),
|
|
1484
|
+
t$3.cloneNode(defaultExpression)
|
|
1485
|
+
)
|
|
1486
|
+
)
|
|
1487
|
+
);
|
|
1488
|
+
});
|
|
1489
|
+
|
|
1490
|
+
const constructorMethod = t$3.classMethod(
|
|
1491
|
+
"constructor",
|
|
1492
|
+
t$3.identifier("constructor"),
|
|
1493
|
+
[],
|
|
1494
|
+
t$3.blockStatement(constructorStatements)
|
|
1495
|
+
);
|
|
1496
|
+
|
|
1497
|
+
const insertionIndex = classMembers.findIndex((member) => !member.static);
|
|
1498
|
+
if (insertionIndex === -1) {
|
|
1499
|
+
classMembers.push(constructorMethod);
|
|
1500
|
+
} else {
|
|
1501
|
+
classMembers.splice(insertionIndex, 0, constructorMethod);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
const handlerMembers = handlerInfos.map((handler) =>
|
|
1506
|
+
createHandlerClassMember(handler)
|
|
1507
|
+
);
|
|
1508
|
+
|
|
1509
|
+
const renderMethod = t$3.classMethod(
|
|
1510
|
+
"method",
|
|
1511
|
+
t$3.identifier("render"),
|
|
1512
|
+
[],
|
|
1513
|
+
t$3.blockStatement(renderStatements)
|
|
1514
|
+
);
|
|
1515
|
+
|
|
1516
|
+
classMembers.push(...handlerMembers, renderMethod);
|
|
1517
|
+
return classMembers;
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
function createComponentClass({
|
|
1521
|
+
className,
|
|
1522
|
+
classMembers,
|
|
1523
|
+
hoistMembers,
|
|
1524
|
+
hoistSymbolDeclarations,
|
|
1525
|
+
needsStaticHoistsMixin,
|
|
1526
|
+
lightDomRequested,
|
|
1527
|
+
needsCss,
|
|
1528
|
+
needsUnsafeCss,
|
|
1529
|
+
needsCallbackRef = false,
|
|
1530
|
+
}) {
|
|
1531
|
+
const classNode = t$3.classDeclaration(
|
|
1532
|
+
t$3.identifier(className),
|
|
1533
|
+
t$3.identifier("LitElement"),
|
|
1534
|
+
t$3.classBody(classMembers)
|
|
1535
|
+
);
|
|
1536
|
+
|
|
1537
|
+
if (hoistMembers.length > 0) {
|
|
1538
|
+
classNode.body.body.unshift(...hoistMembers);
|
|
1539
|
+
if (hoistSymbolDeclarations.length > 0) {
|
|
1540
|
+
classNode._litsxStaticSymbolDeclarations = hoistSymbolDeclarations;
|
|
1541
|
+
}
|
|
1542
|
+
if (needsStaticHoistsMixin) {
|
|
1543
|
+
classNode.superClass = t$3.callExpression(
|
|
1544
|
+
t$3.identifier("LitsxStaticHoistsMixin"),
|
|
1545
|
+
[classNode.superClass]
|
|
1546
|
+
);
|
|
1547
|
+
classNode._needsStaticHoistsMixin = true;
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
if (lightDomRequested) {
|
|
1552
|
+
classNode.superClass = t$3.callExpression(
|
|
1553
|
+
t$3.identifier("LightDomMixin"),
|
|
1554
|
+
[classNode.superClass]
|
|
1555
|
+
);
|
|
1556
|
+
classNode._needsLightDomMixin = true;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
classNode._needsCss = needsCss;
|
|
1560
|
+
classNode._needsUnsafeCss = needsUnsafeCss;
|
|
1561
|
+
classNode._litsxLightDom = lightDomRequested;
|
|
1562
|
+
classNode._needsCallbackRef = needsCallbackRef;
|
|
1563
|
+
return classNode;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
let t$2;
|
|
1567
|
+
|
|
1568
|
+
function setParamRewriteBabelTypes(nextTypes) {
|
|
1569
|
+
t$2 = nextTypes;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
function createThisMemberExpression$1(propName) {
|
|
1573
|
+
return t$2.memberExpression(t$2.thisExpression(), t$2.identifier(propName));
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
function transformJSXExpressions(jsxPath, bindings) {
|
|
1577
|
+
const localNames = Array.from(bindings.keys());
|
|
1578
|
+
|
|
1579
|
+
jsxPath.traverse({
|
|
1580
|
+
JSXExpressionContainer(expressionPath) {
|
|
1581
|
+
if (t$2.isIdentifier(expressionPath.node.expression)) {
|
|
1582
|
+
const name = expressionPath.node.expression.name;
|
|
1583
|
+
if (localNames.includes(name)) {
|
|
1584
|
+
const propName = bindings.get(name) || name;
|
|
1585
|
+
expressionPath.node.expression = t$2.memberExpression(
|
|
1586
|
+
t$2.thisExpression(),
|
|
1587
|
+
t$2.identifier(propName)
|
|
1588
|
+
);
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
},
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
function registerLocalPropAliases(functionPath, bindings) {
|
|
1596
|
+
let changed = true;
|
|
1597
|
+
|
|
1598
|
+
while (changed) {
|
|
1599
|
+
changed = false;
|
|
1600
|
+
|
|
1601
|
+
functionPath.traverse({
|
|
1602
|
+
VariableDeclarator(path) {
|
|
1603
|
+
if (path.getFunctionParent() !== functionPath) return;
|
|
1604
|
+
|
|
1605
|
+
const { id, init } = path.node;
|
|
1606
|
+
if (!t$2.isIdentifier(init)) return;
|
|
1607
|
+
|
|
1608
|
+
if (t$2.isIdentifier(id)) {
|
|
1609
|
+
if (bindings.has(id.name) || !bindings.has(init.name)) return;
|
|
1610
|
+
bindings.set(id.name, bindings.get(init.name));
|
|
1611
|
+
changed = true;
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
if (!t$2.isObjectPattern(id)) return;
|
|
1616
|
+
|
|
1617
|
+
const bindingInfo = bindings.get(init.name);
|
|
1618
|
+
if (!bindingInfo || typeof bindingInfo !== "object" || bindingInfo.kind !== "alias") {
|
|
1619
|
+
return;
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
id.properties.forEach((property) => {
|
|
1623
|
+
if (!t$2.isObjectProperty(property)) return;
|
|
1624
|
+
|
|
1625
|
+
const keyName = t$2.isIdentifier(property.key)
|
|
1626
|
+
? property.key.name
|
|
1627
|
+
: t$2.isStringLiteral(property.key)
|
|
1628
|
+
? property.key.value
|
|
1629
|
+
: null;
|
|
1630
|
+
|
|
1631
|
+
if (!keyName || !bindingInfo.properties.has(keyName)) return;
|
|
1632
|
+
|
|
1633
|
+
if (t$2.isIdentifier(property.value)) {
|
|
1634
|
+
if (!bindings.has(property.value.name)) {
|
|
1635
|
+
bindings.set(property.value.name, keyName);
|
|
1636
|
+
changed = true;
|
|
1637
|
+
}
|
|
1638
|
+
return;
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
if (
|
|
1642
|
+
t$2.isAssignmentPattern(property.value) &&
|
|
1643
|
+
t$2.isIdentifier(property.value.left) &&
|
|
1644
|
+
!bindings.has(property.value.left.name)
|
|
1645
|
+
) {
|
|
1646
|
+
bindings.set(property.value.left.name, keyName);
|
|
1647
|
+
changed = true;
|
|
1648
|
+
}
|
|
1649
|
+
});
|
|
1650
|
+
},
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
function shouldCapturePropReference(refPath, functionPath) {
|
|
1656
|
+
const functionParent = refPath.getFunctionParent();
|
|
1657
|
+
if (!functionParent || functionParent === functionPath) {
|
|
1658
|
+
return false;
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
return !functionParent.isArrowFunctionExpression();
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
function replaceParamReferences(functionPath, bindings, propertyMap = new Map()) {
|
|
1665
|
+
registerLocalPropAliases(functionPath, bindings);
|
|
1666
|
+
|
|
1667
|
+
const capturedPropAliases = new Map();
|
|
1668
|
+
|
|
1669
|
+
function getReplacementForProp(propName, refPath) {
|
|
1670
|
+
if (shouldCapturePropReference(refPath, functionPath)) {
|
|
1671
|
+
let aliasId = capturedPropAliases.get(propName);
|
|
1672
|
+
if (!aliasId) {
|
|
1673
|
+
aliasId = functionPath.scope.generateUidIdentifier(propName);
|
|
1674
|
+
capturedPropAliases.set(propName, aliasId);
|
|
1675
|
+
}
|
|
1676
|
+
return t$2.cloneNode(aliasId);
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
return t$2.memberExpression(t$2.thisExpression(), t$2.identifier(propName));
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
bindings.forEach((bindingInfo, localName) => {
|
|
1683
|
+
if (!localName) return;
|
|
1684
|
+
const binding = functionPath.scope.getBinding(localName);
|
|
1685
|
+
if (!binding) return;
|
|
1686
|
+
|
|
1687
|
+
binding.referencePaths.slice().forEach((refPath) => {
|
|
1688
|
+
if (!refPath.node) return;
|
|
1689
|
+
|
|
1690
|
+
if (
|
|
1691
|
+
bindingInfo &&
|
|
1692
|
+
typeof bindingInfo === "object" &&
|
|
1693
|
+
bindingInfo.kind === "alias" &&
|
|
1694
|
+
(!refPath.parentPath || !refPath.parentPath.isMemberExpression())
|
|
1695
|
+
) {
|
|
1696
|
+
if (shouldCapturePropReference(refPath, functionPath)) {
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
if (
|
|
1701
|
+
refPath.parentPath &&
|
|
1702
|
+
refPath.parentPath.isObjectProperty({ shorthand: true }) &&
|
|
1703
|
+
refPath.parentKey === "value"
|
|
1704
|
+
) {
|
|
1705
|
+
return;
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
refPath.replaceWith(t$2.thisExpression());
|
|
1709
|
+
return;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
if (
|
|
1713
|
+
bindingInfo &&
|
|
1714
|
+
typeof bindingInfo === "object" &&
|
|
1715
|
+
bindingInfo.kind === "alias" &&
|
|
1716
|
+
refPath.parentPath &&
|
|
1717
|
+
refPath.parentPath.isMemberExpression() &&
|
|
1718
|
+
refPath.parentKey === "object" &&
|
|
1719
|
+
t$2.isIdentifier(refPath.parentPath.node.property) &&
|
|
1720
|
+
!refPath.parentPath.node.computed
|
|
1721
|
+
) {
|
|
1722
|
+
const propName = refPath.parentPath.node.property.name;
|
|
1723
|
+
if (bindingInfo.properties.has(propName)) {
|
|
1724
|
+
refPath.parentPath.replaceWith(getReplacementForProp(propName, refPath));
|
|
1725
|
+
return;
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
let targetProp;
|
|
1730
|
+
if (typeof bindingInfo === "string") {
|
|
1731
|
+
targetProp = bindingInfo;
|
|
1732
|
+
} else if (bindingInfo && typeof bindingInfo === "object") {
|
|
1733
|
+
targetProp = bindingInfo.bindKey;
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
if (
|
|
1737
|
+
typeof bindingInfo === "string" &&
|
|
1738
|
+
refPath.parentPath &&
|
|
1739
|
+
refPath.parentPath.isMemberExpression() &&
|
|
1740
|
+
refPath.parentKey === "object" &&
|
|
1741
|
+
t$2.isIdentifier(refPath.parentPath.node.property) &&
|
|
1742
|
+
!refPath.parentPath.node.computed
|
|
1743
|
+
) {
|
|
1744
|
+
const propName = refPath.parentPath.node.property.name;
|
|
1745
|
+
if (localName === "props" || propertyMap.has(propName)) {
|
|
1746
|
+
refPath.parentPath.replaceWith(getReplacementForProp(propName, refPath));
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
if (
|
|
1752
|
+
refPath.parentPath &&
|
|
1753
|
+
refPath.parentPath.isObjectProperty({ shorthand: true }) &&
|
|
1754
|
+
refPath.parentKey === "value"
|
|
1755
|
+
) {
|
|
1756
|
+
refPath.parentPath.node.shorthand = false;
|
|
1757
|
+
refPath.replaceWith(getReplacementForProp(targetProp || localName, refPath));
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
if (
|
|
1762
|
+
refPath.parentPath &&
|
|
1763
|
+
refPath.parentPath.isJSXAttribute() &&
|
|
1764
|
+
refPath.parentKey === "value"
|
|
1765
|
+
) {
|
|
1766
|
+
refPath.replaceWith(
|
|
1767
|
+
t$2.jsxExpressionContainer(
|
|
1768
|
+
getReplacementForProp(targetProp || localName, refPath)
|
|
1769
|
+
)
|
|
1770
|
+
);
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
if (refPath.parentPath && refPath.parentPath.isMemberExpression()) {
|
|
1775
|
+
if (
|
|
1776
|
+
refPath.parentKey === "property" &&
|
|
1777
|
+
!refPath.parentPath.node.computed
|
|
1778
|
+
) {
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
refPath.replaceWith(getReplacementForProp(targetProp || localName, refPath));
|
|
1784
|
+
});
|
|
1785
|
+
});
|
|
1786
|
+
|
|
1787
|
+
return Array.from(capturedPropAliases.entries()).map(([propName, aliasId]) =>
|
|
1788
|
+
t$2.variableDeclaration("const", [
|
|
1789
|
+
t$2.variableDeclarator(t$2.cloneNode(aliasId), createThisMemberExpression$1(propName)),
|
|
1790
|
+
])
|
|
1791
|
+
);
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
let t$1;
|
|
1795
|
+
|
|
1796
|
+
function setProgramBabelTypes(nextTypes) {
|
|
1797
|
+
t$1 = nextTypes;
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
function createLitElementImport() {
|
|
1801
|
+
return t$1.importDeclaration(
|
|
1802
|
+
[
|
|
1803
|
+
t$1.importSpecifier(t$1.identifier("LitElement"), t$1.identifier("LitElement")),
|
|
1804
|
+
],
|
|
1805
|
+
t$1.stringLiteral("lit")
|
|
1806
|
+
);
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
function createLitsxInfrastructureImport(importedName) {
|
|
1810
|
+
return t$1.importDeclaration(
|
|
1811
|
+
[
|
|
1812
|
+
t$1.importSpecifier(t$1.identifier(importedName), t$1.identifier(importedName)),
|
|
1813
|
+
],
|
|
1814
|
+
t$1.stringLiteral("litsx/runtime-infrastructure")
|
|
1815
|
+
);
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
function createLitsxImport(importedName) {
|
|
1819
|
+
return t$1.importDeclaration(
|
|
1820
|
+
[
|
|
1821
|
+
t$1.importSpecifier(t$1.identifier(importedName), t$1.identifier(importedName)),
|
|
1822
|
+
],
|
|
1823
|
+
t$1.stringLiteral("litsx")
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
function ensureNamedImport(importPath, importedName) {
|
|
1828
|
+
const specifiers = importPath.node.specifiers;
|
|
1829
|
+
|
|
1830
|
+
if (
|
|
1831
|
+
specifiers.some(
|
|
1832
|
+
(specifier) =>
|
|
1833
|
+
t$1.isImportSpecifier(specifier) &&
|
|
1834
|
+
t$1.isIdentifier(specifier.imported, { name: importedName })
|
|
1835
|
+
)
|
|
1836
|
+
) {
|
|
1837
|
+
return true;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
if (specifiers.some((specifier) => t$1.isImportNamespaceSpecifier(specifier))) {
|
|
1841
|
+
return false;
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
specifiers.push(
|
|
1845
|
+
t$1.importSpecifier(t$1.identifier(importedName), t$1.identifier(importedName))
|
|
1846
|
+
);
|
|
1847
|
+
return true;
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
function pruneUnusedLitsxStaticImports(programPath) {
|
|
1851
|
+
programPath.scope.crawl();
|
|
1852
|
+
|
|
1853
|
+
const bodyPaths = programPath.get("body");
|
|
1854
|
+
const litsxImports = bodyPaths.filter(
|
|
1855
|
+
(path) => path.isImportDeclaration() && path.node.source.value === "litsx"
|
|
1856
|
+
);
|
|
1857
|
+
|
|
1858
|
+
litsxImports.forEach((importPath) => {
|
|
1859
|
+
const removableSpecifiers = importPath.get("specifiers").filter((specifierPath) => {
|
|
1860
|
+
if (!specifierPath.isImportSpecifier()) return false;
|
|
1861
|
+
if (!t$1.isIdentifier(specifierPath.node.imported)) return false;
|
|
1862
|
+
|
|
1863
|
+
const importedName = specifierPath.node.imported.name;
|
|
1864
|
+
if (importedName !== "staticStyles" && importedName !== "staticProps") {
|
|
1865
|
+
return false;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
const localName = t$1.isIdentifier(specifierPath.node.local)
|
|
1869
|
+
? specifierPath.node.local.name
|
|
1870
|
+
: importedName;
|
|
1871
|
+
const binding = specifierPath.scope.getBinding(localName);
|
|
1872
|
+
return !binding || binding.referencePaths.length === 0;
|
|
1873
|
+
});
|
|
1874
|
+
|
|
1875
|
+
removableSpecifiers.forEach((specifierPath) => {
|
|
1876
|
+
specifierPath.remove();
|
|
1877
|
+
});
|
|
1878
|
+
|
|
1879
|
+
if (importPath.node.specifiers.length === 0) {
|
|
1880
|
+
importPath.remove();
|
|
1881
|
+
}
|
|
1882
|
+
});
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function finalizeProgram(programPath, state) {
|
|
1886
|
+
if (!state?.__litsxTransformCount) {
|
|
1887
|
+
return;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
const hoistDeclarations = [];
|
|
1891
|
+
for (const bodyPath of programPath.get("body")) {
|
|
1892
|
+
const node = bodyPath.isClassDeclaration()
|
|
1893
|
+
? bodyPath.node
|
|
1894
|
+
: bodyPath.isVariableDeclaration()
|
|
1895
|
+
? bodyPath.node
|
|
1896
|
+
: bodyPath.isExportNamedDeclaration() || bodyPath.isExportDefaultDeclaration()
|
|
1897
|
+
? bodyPath.node.declaration
|
|
1898
|
+
: null;
|
|
1899
|
+
|
|
1900
|
+
if (!node) {
|
|
1901
|
+
continue;
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
if (t$1.isClassDeclaration(node) && Array.isArray(node._litsxStaticSymbolDeclarations)) {
|
|
1905
|
+
hoistDeclarations.push(...node._litsxStaticSymbolDeclarations);
|
|
1906
|
+
continue;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
if (t$1.isVariableDeclaration(node)) {
|
|
1910
|
+
node.declarations.forEach((declarator) => {
|
|
1911
|
+
if (Array.isArray(declarator.init?._litsxStaticSymbolDeclarations)) {
|
|
1912
|
+
hoistDeclarations.push(...declarator.init._litsxStaticSymbolDeclarations);
|
|
1913
|
+
}
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
if (hoistDeclarations.length > 0) {
|
|
1919
|
+
programPath.unshiftContainer("body", hoistDeclarations);
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
const bodyPaths = programPath.get("body");
|
|
1923
|
+
const litImports = bodyPaths.filter(
|
|
1924
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "lit"
|
|
1925
|
+
);
|
|
1926
|
+
|
|
1927
|
+
let litElementImported = false;
|
|
1928
|
+
|
|
1929
|
+
litImports.some((importPath) => {
|
|
1930
|
+
if (ensureNamedImport(importPath, "LitElement")) {
|
|
1931
|
+
litElementImported = true;
|
|
1932
|
+
return true;
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
return false;
|
|
1936
|
+
});
|
|
1937
|
+
|
|
1938
|
+
if (!litElementImported) {
|
|
1939
|
+
programPath.unshiftContainer("body", createLitElementImport());
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
if (state.__litsxNeedsCss) {
|
|
1943
|
+
const nextBodyPaths = programPath.get("body");
|
|
1944
|
+
const nextLitImports = nextBodyPaths.filter(
|
|
1945
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "lit"
|
|
1946
|
+
);
|
|
1947
|
+
nextLitImports.some((importPath) => ensureNamedImport(importPath, "css"));
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
if (state.__litsxNeedsUnsafeCss) {
|
|
1951
|
+
const nextBodyPaths = programPath.get("body");
|
|
1952
|
+
const nextLitImports = nextBodyPaths.filter(
|
|
1953
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "lit"
|
|
1954
|
+
);
|
|
1955
|
+
nextLitImports.some((importPath) => ensureNamedImport(importPath, "unsafeCSS"));
|
|
1956
|
+
}
|
|
1957
|
+
|
|
1958
|
+
if (state.__litsxNeedsStaticHoistsMixin) {
|
|
1959
|
+
const bodyPathsWithInternal = programPath.get("body");
|
|
1960
|
+
const internalImports = bodyPathsWithInternal.filter(
|
|
1961
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "litsx/runtime-infrastructure"
|
|
1962
|
+
);
|
|
1963
|
+
|
|
1964
|
+
let internalImported = false;
|
|
1965
|
+
internalImports.some((importPath) => {
|
|
1966
|
+
if (ensureNamedImport(importPath, "LitsxStaticHoistsMixin")) {
|
|
1967
|
+
internalImported = true;
|
|
1968
|
+
return true;
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
return false;
|
|
1972
|
+
});
|
|
1973
|
+
|
|
1974
|
+
if (!internalImported) {
|
|
1975
|
+
programPath.unshiftContainer("body", createLitsxInfrastructureImport("LitsxStaticHoistsMixin"));
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
if (state.__litsxNeedsLightDomMixin) {
|
|
1980
|
+
const bodyPathsWithInternal = programPath.get("body");
|
|
1981
|
+
const internalImports = bodyPathsWithInternal.filter(
|
|
1982
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "litsx/runtime-infrastructure"
|
|
1983
|
+
);
|
|
1984
|
+
|
|
1985
|
+
let internalImported = false;
|
|
1986
|
+
internalImports.some((importPath) => {
|
|
1987
|
+
if (ensureNamedImport(importPath, "LightDomMixin")) {
|
|
1988
|
+
internalImported = true;
|
|
1989
|
+
return true;
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
return false;
|
|
1993
|
+
});
|
|
1994
|
+
|
|
1995
|
+
if (!internalImported) {
|
|
1996
|
+
programPath.unshiftContainer("body", createLitsxInfrastructureImport("LightDomMixin"));
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
if (state.__litsxNeedsCallbackRef) {
|
|
2001
|
+
const bodyPathsWithLitsx = programPath.get("body");
|
|
2002
|
+
const litsxImports = bodyPathsWithLitsx.filter(
|
|
2003
|
+
(n) => n.isImportDeclaration() && n.node.source.value === "litsx"
|
|
2004
|
+
);
|
|
2005
|
+
|
|
2006
|
+
let litsxImported = false;
|
|
2007
|
+
litsxImports.some((importPath) => {
|
|
2008
|
+
if (ensureNamedImport(importPath, "useCallbackRef")) {
|
|
2009
|
+
litsxImported = true;
|
|
2010
|
+
return true;
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
return false;
|
|
2014
|
+
});
|
|
2015
|
+
|
|
2016
|
+
if (!litsxImported) {
|
|
2017
|
+
programPath.unshiftContainer("body", createLitsxImport("useCallbackRef"));
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
pruneUnusedLitsxStaticImports(programPath);
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
let t;
|
|
2025
|
+
|
|
2026
|
+
function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
2027
|
+
return function transformFunctionToClassPlugin(_api, pluginOptions = {}) {
|
|
2028
|
+
internal_transformLitsxProperties.ensureTypescriptModule();
|
|
2029
|
+
t = _api.types;
|
|
2030
|
+
internal_transformLitsxProperties.setPropertyBabelTypes(t);
|
|
2031
|
+
setStaticHoistsBabelTypes(t);
|
|
2032
|
+
setHandlersBabelTypes(t);
|
|
2033
|
+
setWrapperUtilsBabelTypes(t);
|
|
2034
|
+
setRefsBabelTypes(t);
|
|
2035
|
+
setClassGenerationBabelTypes(t);
|
|
2036
|
+
setParamRewriteBabelTypes(t);
|
|
2037
|
+
setProgramBabelTypes(t);
|
|
2038
|
+
const resolvedPluginOptions = {
|
|
2039
|
+
...defaultPluginOptions,
|
|
2040
|
+
...pluginOptions,
|
|
2041
|
+
};
|
|
2042
|
+
|
|
2043
|
+
const getWrapperMetadata =
|
|
2044
|
+
typeof resolvedPluginOptions.getWrapperMetadata === "function"
|
|
2045
|
+
? resolvedPluginOptions.getWrapperMetadata
|
|
2046
|
+
: null;
|
|
2047
|
+
|
|
2048
|
+
return {
|
|
2049
|
+
name: "transform-function-to-class",
|
|
2050
|
+
inherits: jsxSyntaxPlugin.default || jsxSyntaxPlugin,
|
|
2051
|
+
pre() {
|
|
2052
|
+
this.__litsxTransformCount = 0;
|
|
2053
|
+
this.__litsxNeedsCss = false;
|
|
2054
|
+
this.__litsxNeedsUnsafeCss = false;
|
|
2055
|
+
this.__litsxNeedsStaticHoistsMixin = false;
|
|
2056
|
+
this.__litsxNeedsLightDomMixin = false;
|
|
2057
|
+
this.__litsxNeedsCallbackRef = false;
|
|
2058
|
+
this.__litsxWarnings = [];
|
|
2059
|
+
this.__litsxResolvedPluginOptions = resolvedPluginOptions;
|
|
2060
|
+
this.__litsxTypeResolver = fileLikelyNeedsTypeResolver(this)
|
|
2061
|
+
? internal_transformLitsxProperties.createTypeResolver(
|
|
2062
|
+
this.file?.opts?.filename,
|
|
2063
|
+
this.file?.code,
|
|
2064
|
+
resolvedPluginOptions
|
|
2065
|
+
)
|
|
2066
|
+
: undefined;
|
|
2067
|
+
},
|
|
2068
|
+
post() {
|
|
2069
|
+
if (!this.file) return;
|
|
2070
|
+
this.file.metadata ||= {};
|
|
2071
|
+
this.file.metadata.litsxWarnings = this.__litsxWarnings || [];
|
|
2072
|
+
},
|
|
2073
|
+
visitor: {
|
|
2074
|
+
Program: {
|
|
2075
|
+
exit(programPath) {
|
|
2076
|
+
finalizeProgram(programPath, this);
|
|
2077
|
+
},
|
|
2078
|
+
},
|
|
2079
|
+
ExportNamedDeclaration(exportPath) {
|
|
2080
|
+
handlePotentialComponentExport({
|
|
2081
|
+
exportPath,
|
|
2082
|
+
state: this,
|
|
2083
|
+
transformFunction,
|
|
2084
|
+
isInsideFunctionOrClass,
|
|
2085
|
+
updateTransformState,
|
|
2086
|
+
getWrapperMetadata,
|
|
2087
|
+
});
|
|
2088
|
+
},
|
|
2089
|
+
ExportDefaultDeclaration(exportPath) {
|
|
2090
|
+
handlePotentialComponentExport({
|
|
2091
|
+
exportPath,
|
|
2092
|
+
state: this,
|
|
2093
|
+
isDefault: true,
|
|
2094
|
+
transformFunction,
|
|
2095
|
+
isInsideFunctionOrClass,
|
|
2096
|
+
updateTransformState,
|
|
2097
|
+
getWrapperMetadata,
|
|
2098
|
+
});
|
|
2099
|
+
},
|
|
2100
|
+
VariableDeclarator(varPath) {
|
|
2101
|
+
if (
|
|
2102
|
+
varPath.findParent(
|
|
2103
|
+
(p) => p.isExportNamedDeclaration?.() || p.isExportDefaultDeclaration?.()
|
|
2104
|
+
)
|
|
2105
|
+
) {
|
|
2106
|
+
return;
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
const initPath = varPath.get("init");
|
|
2110
|
+
|
|
2111
|
+
if (
|
|
2112
|
+
maybeTransformWrappedVariableDeclarator({
|
|
2113
|
+
varPath,
|
|
2114
|
+
resolvedPluginOptions,
|
|
2115
|
+
state: this,
|
|
2116
|
+
transformFunction,
|
|
2117
|
+
updateTransformState,
|
|
2118
|
+
getWrapperMetadata,
|
|
2119
|
+
})
|
|
2120
|
+
) {
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
if (initPath && initPath.isArrowFunctionExpression() && !isInsideFunctionOrClass(varPath)) {
|
|
2125
|
+
const programPath = varPath.findParent((p) => p.isProgram());
|
|
2126
|
+
const elementCandidates = collectElementCandidates(initPath, programPath);
|
|
2127
|
+
const classNode = transformFunction(
|
|
2128
|
+
initPath,
|
|
2129
|
+
programPath,
|
|
2130
|
+
varPath.node.id.name,
|
|
2131
|
+
{
|
|
2132
|
+
...resolvedPluginOptions,
|
|
2133
|
+
typeResolver: getTypeResolverForFunction(initPath, this),
|
|
2134
|
+
warn: (warning) => {
|
|
2135
|
+
this.__litsxWarnings.push(warning);
|
|
2136
|
+
},
|
|
2137
|
+
}
|
|
2138
|
+
);
|
|
2139
|
+
|
|
2140
|
+
if (!classNode) return;
|
|
2141
|
+
|
|
2142
|
+
if (elementCandidates.size) {
|
|
2143
|
+
classNode._litsxElementCandidates &&= new Set(classNode._litsxElementCandidates);
|
|
2144
|
+
const elementSet = classNode._litsxElementCandidates ||= new Set();
|
|
2145
|
+
elementCandidates.forEach((candidate) => elementSet.add(candidate));
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
const declarationPath = varPath.parentPath;
|
|
2149
|
+
if (!declarationPath.isVariableDeclaration()) return;
|
|
2150
|
+
|
|
2151
|
+
varPath.scope.removeBinding(varPath.node.id.name);
|
|
2152
|
+
declarationPath.replaceWith(classNode);
|
|
2153
|
+
declarationPath.requeue();
|
|
2154
|
+
updateTransformState(this, classNode);
|
|
2155
|
+
}
|
|
2156
|
+
},
|
|
2157
|
+
FunctionDeclaration(funcPath) {
|
|
2158
|
+
if (!isInsideFunctionOrClass(funcPath)) {
|
|
2159
|
+
const programPath = funcPath.findParent((p) => p.isProgram());
|
|
2160
|
+
const elementCandidates = collectElementCandidates(funcPath, programPath);
|
|
2161
|
+
const classNode = transformFunction(
|
|
2162
|
+
funcPath,
|
|
2163
|
+
programPath,
|
|
2164
|
+
undefined,
|
|
2165
|
+
{
|
|
2166
|
+
...resolvedPluginOptions,
|
|
2167
|
+
typeResolver: getTypeResolverForFunction(funcPath, this),
|
|
2168
|
+
warn: (warning) => {
|
|
2169
|
+
this.__litsxWarnings.push(warning);
|
|
2170
|
+
},
|
|
2171
|
+
}
|
|
2172
|
+
);
|
|
2173
|
+
|
|
2174
|
+
if (!classNode) return;
|
|
2175
|
+
|
|
2176
|
+
if (funcPath.node.id) {
|
|
2177
|
+
funcPath.scope.removeBinding(funcPath.node.id.name);
|
|
2178
|
+
}
|
|
2179
|
+
if (elementCandidates.size) {
|
|
2180
|
+
classNode._litsxElementCandidates &&= new Set(classNode._litsxElementCandidates);
|
|
2181
|
+
const elementSet = classNode._litsxElementCandidates ||= new Set();
|
|
2182
|
+
elementCandidates.forEach((candidate) => elementSet.add(candidate));
|
|
2183
|
+
}
|
|
2184
|
+
funcPath.replaceWith(classNode);
|
|
2185
|
+
funcPath.requeue();
|
|
2186
|
+
updateTransformState(this, classNode);
|
|
2187
|
+
}
|
|
2188
|
+
},
|
|
2189
|
+
},
|
|
2190
|
+
};
|
|
2191
|
+
};
|
|
2192
|
+
}
|
|
2193
|
+
|
|
2194
|
+
var transformLitsxComponents = createTransformFunctionToClassPlugin();
|
|
2195
|
+
|
|
2196
|
+
function getOrCreateModuleStaticHoistSymbol(programPath, hoistName) {
|
|
2197
|
+
let symbolMap = programPath.getData("__litsxStaticHoistSymbols");
|
|
2198
|
+
if (!symbolMap) {
|
|
2199
|
+
symbolMap = new Map();
|
|
2200
|
+
programPath.setData("__litsxStaticHoistSymbols", symbolMap);
|
|
2201
|
+
}
|
|
2202
|
+
|
|
2203
|
+
if (symbolMap.has(hoistName)) {
|
|
2204
|
+
return symbolMap.get(hoistName);
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
const symbolId = programPath.scope.generateUidIdentifier(`litsx_static_${hoistName}`);
|
|
2208
|
+
const declaration = t.variableDeclaration("const", [
|
|
2209
|
+
t.variableDeclarator(
|
|
2210
|
+
symbolId,
|
|
2211
|
+
t.callExpression(t.identifier("Symbol"), [t.stringLiteral(`litsx.static.${hoistName}`)])
|
|
2212
|
+
),
|
|
2213
|
+
]);
|
|
2214
|
+
|
|
2215
|
+
const entry = { symbolId, declaration };
|
|
2216
|
+
symbolMap.set(hoistName, entry);
|
|
2217
|
+
return entry;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
|
|
2221
|
+
function updateTransformState(state, classNode) {
|
|
2222
|
+
if (!state || !classNode) {
|
|
2223
|
+
return;
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
state.__litsxTransformCount = (state.__litsxTransformCount || 0) + 1;
|
|
2227
|
+
state.__litsxNeedsCss ||= Boolean(classNode._needsCss);
|
|
2228
|
+
state.__litsxNeedsUnsafeCss ||= Boolean(classNode._needsUnsafeCss);
|
|
2229
|
+
state.__litsxNeedsStaticHoistsMixin ||= Boolean(
|
|
2230
|
+
classNode._needsStaticHoistsMixin
|
|
2231
|
+
);
|
|
2232
|
+
state.__litsxNeedsLightDomMixin ||= Boolean(
|
|
2233
|
+
classNode._needsLightDomMixin
|
|
2234
|
+
);
|
|
2235
|
+
state.__litsxNeedsCallbackRef ||= Boolean(
|
|
2236
|
+
classNode._needsCallbackRef
|
|
2237
|
+
);
|
|
2238
|
+
}
|
|
2239
|
+
|
|
2240
|
+
// Verifica si el nodo está dentro de otra función o clase
|
|
2241
|
+
function isInsideFunctionOrClass(path) {
|
|
2242
|
+
return path.findParent(
|
|
2243
|
+
(p) => p.isFunctionDeclaration() || p.isFunctionExpression() || p.isArrowFunctionExpression() || p.isClassDeclaration()
|
|
2244
|
+
);
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
function getOrCreateTypeResolver(state) {
|
|
2248
|
+
if (state.__litsxTypeResolver !== undefined) {
|
|
2249
|
+
return state.__litsxTypeResolver;
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
state.__litsxTypeResolver = internal_transformLitsxProperties.createTypeResolver(
|
|
2253
|
+
state.file?.opts?.filename,
|
|
2254
|
+
state.file?.code,
|
|
2255
|
+
state.__litsxResolvedPluginOptions
|
|
2256
|
+
);
|
|
2257
|
+
return state.__litsxTypeResolver;
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
function fileLikelyNeedsTypeResolver(state) {
|
|
2261
|
+
const filename = state?.file?.opts?.filename || "";
|
|
2262
|
+
if (/\.(?:[cm]?ts|tsx)$/i.test(filename)) {
|
|
2263
|
+
return true;
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
const source = state?.file?.code || "";
|
|
2267
|
+
return /\b(?:type|interface|enum)\b/.test(source);
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
function functionNeedsTypeResolver(functionPath, state) {
|
|
2271
|
+
const params = functionPath.get("params");
|
|
2272
|
+
if (!Array.isArray(params) || params.length === 0) {
|
|
2273
|
+
return false;
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
if (fileLikelyNeedsTypeResolver(state)) {
|
|
2277
|
+
return true;
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
return params.some((paramPath) => containsTypeResolutionSyntax(paramPath));
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
function containsTypeResolutionSyntax(path) {
|
|
2284
|
+
if (!path?.node) {
|
|
2285
|
+
return false;
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2288
|
+
if (
|
|
2289
|
+
path.isIdentifier?.() ||
|
|
2290
|
+
path.isObjectPattern?.() ||
|
|
2291
|
+
path.isArrayPattern?.() ||
|
|
2292
|
+
path.isAssignmentPattern?.()
|
|
2293
|
+
) {
|
|
2294
|
+
if (path.node.typeAnnotation) {
|
|
2295
|
+
return true;
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
if (path.isAssignmentPattern?.()) {
|
|
2300
|
+
return containsTypeResolutionSyntax(path.get("left"));
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
if (path.isObjectPattern?.()) {
|
|
2304
|
+
return path.get("properties").some((propertyPath) => {
|
|
2305
|
+
if (propertyPath.isRestElement()) {
|
|
2306
|
+
return containsTypeResolutionSyntax(propertyPath.get("argument"));
|
|
2307
|
+
}
|
|
2308
|
+
if (propertyPath.isObjectProperty()) {
|
|
2309
|
+
return containsTypeResolutionSyntax(propertyPath.get("value"));
|
|
2310
|
+
}
|
|
2311
|
+
return false;
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
if (path.isArrayPattern?.()) {
|
|
2316
|
+
return path.get("elements").some((elementPath) => {
|
|
2317
|
+
if (!elementPath?.node) return false;
|
|
2318
|
+
return containsTypeResolutionSyntax(elementPath);
|
|
2319
|
+
});
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
return false;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
function getTypeResolverForFunction(functionPath, state) {
|
|
2326
|
+
if (!functionNeedsTypeResolver(functionPath, state)) {
|
|
2327
|
+
return null;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
return getOrCreateTypeResolver(state);
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
function transformFunction(functionPath, programPath, className, options = {}) {
|
|
2334
|
+
const { node } = functionPath;
|
|
2335
|
+
const forwardRefOptions = options.forwardRef || null;
|
|
2336
|
+
let resolvedName = className;
|
|
2337
|
+
if (!resolvedName && node && node.id && t.isIdentifier(node.id)) {
|
|
2338
|
+
resolvedName = node.id.name;
|
|
2339
|
+
}
|
|
2340
|
+
if (!resolvedName) {
|
|
2341
|
+
resolvedName = "AnonymousComponent";
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
className = resolvedName;
|
|
2345
|
+
|
|
2346
|
+
const {
|
|
2347
|
+
properties: propertiesStatic,
|
|
2348
|
+
propertyNames,
|
|
2349
|
+
bindings,
|
|
2350
|
+
defaults,
|
|
2351
|
+
nestedInitializers,
|
|
2352
|
+
} = internal_transformLitsxProperties.extractProperties(
|
|
2353
|
+
functionPath,
|
|
2354
|
+
programPath,
|
|
2355
|
+
options
|
|
2356
|
+
);
|
|
2357
|
+
|
|
2358
|
+
assertStaticHoistsStayTopLevel(functionPath);
|
|
2359
|
+
collectNativeClassNameWarnings(functionPath, options.warn, options);
|
|
2360
|
+
|
|
2361
|
+
let returnStatement;
|
|
2362
|
+
functionPath.traverse({
|
|
2363
|
+
ReturnStatement(returnPath) {
|
|
2364
|
+
if (t.isJSXElement(returnPath.node.argument)) {
|
|
2365
|
+
returnStatement = returnPath.node;
|
|
2366
|
+
transformJSXExpressions(returnPath, bindings);
|
|
2367
|
+
}
|
|
2368
|
+
},
|
|
2369
|
+
});
|
|
2370
|
+
|
|
2371
|
+
if (!returnStatement) return;
|
|
2372
|
+
|
|
2373
|
+
const capturedPropAliasStatements = replaceParamReferences(functionPath, bindings, propertyNames);
|
|
2374
|
+
|
|
2375
|
+
const usedNames = new Set([
|
|
2376
|
+
...Object.keys(functionPath.scope.bindings || {}),
|
|
2377
|
+
"render",
|
|
2378
|
+
"properties",
|
|
2379
|
+
"constructor",
|
|
2380
|
+
]);
|
|
2381
|
+
|
|
2382
|
+
const handlerInfos = processHandlers(functionPath, usedNames);
|
|
2383
|
+
|
|
2384
|
+
const renderStatements = t.isBlockStatement(node.body)
|
|
2385
|
+
? [...node.body.body]
|
|
2386
|
+
: [t.returnStatement(node.body)];
|
|
2387
|
+
|
|
2388
|
+
const resolvedRefPropName = forwardRefOptions?.propName ||
|
|
2389
|
+
(propertyNames.has("ref") || hasRefProp(functionPath) ? "ref" : null);
|
|
2390
|
+
let needsCallbackRef = false;
|
|
2391
|
+
|
|
2392
|
+
if (resolvedRefPropName) {
|
|
2393
|
+
renderStatements.unshift(
|
|
2394
|
+
...lowerForwardedElementRefs(functionPath, resolvedRefPropName)
|
|
2395
|
+
);
|
|
2396
|
+
needsCallbackRef = renderStatements.some(
|
|
2397
|
+
(statement) =>
|
|
2398
|
+
t.isExpressionStatement(statement) &&
|
|
2399
|
+
t.isCallExpression(statement.expression) &&
|
|
2400
|
+
t.isIdentifier(statement.expression.callee, { name: "useCallbackRef" })
|
|
2401
|
+
) || needsCallbackRef;
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
if (resolvedRefPropName && !forwardRefOptions) {
|
|
2405
|
+
renderStatements.unshift(createComponentInstanceRefSyncStatement());
|
|
2406
|
+
needsCallbackRef = true;
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
if (capturedPropAliasStatements.length > 0) {
|
|
2410
|
+
renderStatements.unshift(...capturedPropAliasStatements);
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
if (nestedInitializers.length > 0) {
|
|
2414
|
+
const initializerStatements = nestedInitializers.map(({ pattern, root, defaultValue }) =>
|
|
2415
|
+
createNestedInitializerStatement(pattern, root, defaultValue, t)
|
|
2416
|
+
);
|
|
2417
|
+
renderStatements.unshift(...initializerStatements);
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
const classMembers = [];
|
|
2421
|
+
|
|
2422
|
+
const {
|
|
2423
|
+
lightDomRequested,
|
|
2424
|
+
hoistMembers,
|
|
2425
|
+
hoistSymbolDeclarations,
|
|
2426
|
+
needsStaticHoistsMixin,
|
|
2427
|
+
needsCss,
|
|
2428
|
+
needsUnsafeCss,
|
|
2429
|
+
} = processStaticHoists({
|
|
2430
|
+
functionPath,
|
|
2431
|
+
node,
|
|
2432
|
+
renderStatements,
|
|
2433
|
+
programPath,
|
|
2434
|
+
propertiesStatic,
|
|
2435
|
+
classMembers,
|
|
2436
|
+
options,
|
|
2437
|
+
getOrCreateModuleStaticHoistSymbol,
|
|
2438
|
+
});
|
|
2439
|
+
|
|
2440
|
+
buildClassMembers({
|
|
2441
|
+
classMembers,
|
|
2442
|
+
defaults,
|
|
2443
|
+
renderStatements,
|
|
2444
|
+
handlerInfos,
|
|
2445
|
+
createHandlerClassMember,
|
|
2446
|
+
});
|
|
2447
|
+
|
|
2448
|
+
return createComponentClass({
|
|
2449
|
+
className,
|
|
2450
|
+
classMembers,
|
|
2451
|
+
hoistMembers,
|
|
2452
|
+
hoistSymbolDeclarations,
|
|
2453
|
+
needsStaticHoistsMixin,
|
|
2454
|
+
lightDomRequested,
|
|
2455
|
+
needsCss,
|
|
2456
|
+
needsUnsafeCss,
|
|
2457
|
+
needsCallbackRef,
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
function createThisMemberExpression(propName) {
|
|
2462
|
+
return t.memberExpression(t.thisExpression(), t.identifier(propName));
|
|
2463
|
+
}
|
|
2464
|
+
|
|
2465
|
+
function createNestedInitializerStatement(pattern, root, defaultValue, t) {
|
|
2466
|
+
const rootAccess = createThisMemberExpression(root);
|
|
2467
|
+
let sourceExpression = rootAccess;
|
|
2468
|
+
|
|
2469
|
+
if (defaultValue) {
|
|
2470
|
+
sourceExpression = t.logicalExpression(
|
|
2471
|
+
"??",
|
|
2472
|
+
t.cloneNode(rootAccess),
|
|
2473
|
+
t.cloneNode(defaultValue)
|
|
2474
|
+
);
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
return t.variableDeclaration("const", [
|
|
2478
|
+
t.variableDeclarator(t.cloneNode(pattern), sourceExpression),
|
|
2479
|
+
]);
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
function collectElementCandidates(functionPath, programPath) {
|
|
2483
|
+
const candidates = new Set();
|
|
2484
|
+
if (!programPath) return candidates;
|
|
2485
|
+
|
|
2486
|
+
const importNames = new Set();
|
|
2487
|
+
programPath.get("body").forEach((nodePath) => {
|
|
2488
|
+
if (!nodePath.isImportDeclaration()) return;
|
|
2489
|
+
nodePath.node.specifiers.forEach((specifier) => {
|
|
2490
|
+
if (specifier.local) {
|
|
2491
|
+
importNames.add(specifier.local.name);
|
|
2492
|
+
}
|
|
2493
|
+
});
|
|
2494
|
+
});
|
|
2495
|
+
|
|
2496
|
+
functionPath.traverse({
|
|
2497
|
+
JSXOpeningElement(path) {
|
|
2498
|
+
if (!path.node.name || path.node.name.type !== "JSXIdentifier") return;
|
|
2499
|
+
const originalName = path.node.name.name;
|
|
2500
|
+
if (!importNames.has(originalName)) return;
|
|
2501
|
+
|
|
2502
|
+
path.node.__scopedOriginal = originalName;
|
|
2503
|
+
candidates.add(originalName);
|
|
2504
|
+
},
|
|
2505
|
+
JSXClosingElement(path) {
|
|
2506
|
+
if (!path.node.name || path.node.name.type !== "JSXIdentifier") return;
|
|
2507
|
+
const originalName = path.node.name.name;
|
|
2508
|
+
if (!importNames.has(originalName)) return;
|
|
2509
|
+
path.node.__scopedOriginal = originalName;
|
|
2510
|
+
},
|
|
2511
|
+
});
|
|
2512
|
+
|
|
2513
|
+
return candidates;
|
|
2514
|
+
}
|
|
2515
|
+
|
|
2516
|
+
exports.createTransformFunctionToClassPlugin = createTransformFunctionToClassPlugin;
|
|
2517
|
+
exports.default = transformLitsxComponents;
|
|
2518
|
+
//# sourceMappingURL=transform-litsx-components.cjs.map
|