@vobs/compiler 1.2.1 → 1.3.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/dist/compile.cjs +349 -254
- package/dist/compile.cjs.map +1 -1
- package/dist/compile.js +90 -23
- package/dist/compile.js.map +1 -1
- package/dist/i18n-extractor.cjs +45 -16
- package/dist/i18n-extractor.cjs.map +1 -1
- package/dist/i18n-extractor.js +6 -5
- package/dist/i18n-extractor.js.map +1 -1
- package/dist/index.cjs +361 -260
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +99 -28
- package/dist/index.js.map +1 -1
- package/dist/plugin.cjs +18 -2
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +6 -0
- package/dist/plugin.d.ts +6 -0
- package/dist/plugin.js +0 -2
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -2
- package/src/compile.test.ts +74 -0
- package/src/compile.ts +113 -17
- package/src/plugin.ts +6 -0
package/dist/compile.cjs
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var error = require('@vobs/runtime/error');
|
|
5
|
-
|
|
6
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
-
|
|
8
|
-
var ts__default = /*#__PURE__*/_interopDefault(ts);
|
|
9
|
-
|
|
1
|
+
var __VOBS_CJS_FILE_URL = require("url").pathToFileURL(__filename).href;
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
10
4
|
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
9
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
|
|
32
|
+
// packages/compiler/src/compile.ts
|
|
33
|
+
var compile_exports = {};
|
|
34
|
+
__export(compile_exports, {
|
|
35
|
+
compile: () => compile,
|
|
36
|
+
compileWithSourceMap: () => compileWithSourceMap,
|
|
37
|
+
createCompiler: () => createCompiler
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(compile_exports);
|
|
40
|
+
var import_typescript = __toESM(require("typescript"), 1);
|
|
41
|
+
var import_error = require("@vobs/runtime/error");
|
|
12
42
|
function createCompiler(options = {}) {
|
|
13
43
|
const basePlugins = options.plugins ?? [];
|
|
14
44
|
return {
|
|
@@ -31,7 +61,7 @@ function compile(code, options = {}) {
|
|
|
31
61
|
const result = compileWithSourceMap(code, options);
|
|
32
62
|
const firstError = result.diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
33
63
|
if (firstError) {
|
|
34
|
-
throw new
|
|
64
|
+
throw new import_error.VobsError({
|
|
35
65
|
code: firstError.code,
|
|
36
66
|
layer: "compiler",
|
|
37
67
|
message: firstError.message,
|
|
@@ -45,12 +75,12 @@ function compile(code, options = {}) {
|
|
|
45
75
|
__name(compile, "compile");
|
|
46
76
|
function compileWithSourceMap(code, options = {}) {
|
|
47
77
|
const filename = options.filename ?? "component.tsx";
|
|
48
|
-
let sourceFile =
|
|
78
|
+
let sourceFile = import_typescript.default.createSourceFile(
|
|
49
79
|
filename,
|
|
50
80
|
code,
|
|
51
|
-
|
|
81
|
+
import_typescript.default.ScriptTarget.Latest,
|
|
52
82
|
true,
|
|
53
|
-
|
|
83
|
+
import_typescript.default.ScriptKind.TSX
|
|
54
84
|
);
|
|
55
85
|
const state = {
|
|
56
86
|
generatedId: 0,
|
|
@@ -63,21 +93,22 @@ function compileWithSourceMap(code, options = {}) {
|
|
|
63
93
|
sourceLocation: options.sourceLocation ?? true,
|
|
64
94
|
stateAliases: collectStateAliases(sourceFile),
|
|
65
95
|
localBindings: collectLocallyDeclaredNames(sourceFile),
|
|
66
|
-
diagnostics: []
|
|
96
|
+
diagnostics: [],
|
|
97
|
+
hmrModuleId: options.hmrModuleId ?? null
|
|
67
98
|
};
|
|
68
99
|
const cleanFilename = filename.split(/[?#]/u, 1)[0] || filename;
|
|
69
|
-
const diagnostics =
|
|
100
|
+
const diagnostics = import_typescript.default.transpileModule(code, {
|
|
70
101
|
// Vite appends query strings (for example `?direct`) to module IDs;
|
|
71
102
|
// strip them so TypeScript still recognizes TSX syntax for diagnostics.
|
|
72
103
|
fileName: cleanFilename,
|
|
73
104
|
reportDiagnostics: true,
|
|
74
|
-
compilerOptions: { jsx:
|
|
105
|
+
compilerOptions: { jsx: import_typescript.default.JsxEmit.Preserve, target: import_typescript.default.ScriptTarget.Latest }
|
|
75
106
|
}).diagnostics?.map((diagnostic) => toCompilerDiagnostic(diagnostic, sourceFile, cleanFilename)) ?? [];
|
|
76
107
|
const plugins = options.plugins ?? [];
|
|
77
108
|
validatePlugins(plugins);
|
|
78
109
|
const context = {
|
|
79
110
|
filename,
|
|
80
|
-
factory:
|
|
111
|
+
factory: import_typescript.default.factory,
|
|
81
112
|
addRuntimeImport(name) {
|
|
82
113
|
resolveHelperName(state, name);
|
|
83
114
|
},
|
|
@@ -89,15 +120,16 @@ function compileWithSourceMap(code, options = {}) {
|
|
|
89
120
|
}
|
|
90
121
|
for (const plugin of plugins) sourceFile = transformPluginNodes(sourceFile, plugin, context);
|
|
91
122
|
const statements = sourceFile.statements.map(
|
|
92
|
-
(statement) =>
|
|
123
|
+
(statement) => import_typescript.default.isImportDeclaration(statement) ? rebuildImport(state, statement) : transformStatement(state, statement, true)
|
|
93
124
|
);
|
|
94
125
|
const templateDeclarations = createTemplateDeclarations(state);
|
|
95
|
-
|
|
126
|
+
let resultFile = import_typescript.default.factory.updateSourceFile(sourceFile, [
|
|
96
127
|
...createRuntimeImports(state),
|
|
97
128
|
...templateDeclarations,
|
|
98
129
|
...statements
|
|
99
130
|
]);
|
|
100
|
-
|
|
131
|
+
resultFile = transformResidualJsx(state, resultFile);
|
|
132
|
+
const generated = import_typescript.default.createPrinter().printFile(resultFile);
|
|
101
133
|
return {
|
|
102
134
|
code: generated,
|
|
103
135
|
map: buildSourceMap(state, filename, code, generated, resultFile),
|
|
@@ -108,11 +140,11 @@ __name(compileWithSourceMap, "compileWithSourceMap");
|
|
|
108
140
|
function toCompilerDiagnostic(diagnostic, sourceFile, filename) {
|
|
109
141
|
const start = diagnostic.start ?? 0;
|
|
110
142
|
const length = diagnostic.length ?? 1;
|
|
111
|
-
const message =
|
|
143
|
+
const message = import_typescript.default.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
|
112
144
|
const { line, column, codeFrame } = buildCodeFrame(sourceFile, start, length);
|
|
113
145
|
return {
|
|
114
146
|
code: `VOBS_C${String(diagnostic.code).padStart(3, "0")}`,
|
|
115
|
-
severity: diagnostic.category ===
|
|
147
|
+
severity: diagnostic.category === import_typescript.default.DiagnosticCategory.Warning ? "warning" : "error",
|
|
116
148
|
message,
|
|
117
149
|
location: { file: filename, line, column },
|
|
118
150
|
codeFrame
|
|
@@ -135,7 +167,7 @@ function reportUnsupportedTag(state, tagName) {
|
|
|
135
167
|
const sourceFile = tagName.getSourceFile() ?? state.sourceFile;
|
|
136
168
|
if (!sourceFile) return;
|
|
137
169
|
const label = tagName.getText();
|
|
138
|
-
const kindNote = tagName.kind ===
|
|
170
|
+
const kindNote = tagName.kind === import_typescript.default.SyntaxKind.JsxNamespacedName ? "\uFF08JSX \u547D\u540D\u7A7A\u95F4\u6807\u7B7E\uFF09" : "";
|
|
139
171
|
const { line, column, codeFrame } = buildCodeFrame(sourceFile, tagName.getStart(sourceFile), tagName.getWidth(sourceFile));
|
|
140
172
|
state.diagnostics.push({
|
|
141
173
|
code: "VOBS_C101",
|
|
@@ -148,7 +180,7 @@ function reportUnsupportedTag(state, tagName) {
|
|
|
148
180
|
}
|
|
149
181
|
__name(reportUnsupportedTag, "reportUnsupportedTag");
|
|
150
182
|
function buildSourceMap(state, filename, source, generated, resultFile) {
|
|
151
|
-
const reparsed =
|
|
183
|
+
const reparsed = import_typescript.default.createSourceFile(filename, generated, import_typescript.default.ScriptTarget.Latest, true, import_typescript.default.ScriptKind.TSX);
|
|
152
184
|
const segments = [];
|
|
153
185
|
walkPairedTrees(state, resultFile, reparsed, reparsed, segments);
|
|
154
186
|
segments.sort((a, b) => a.genLine - b.genLine || a.genCol - b.genCol);
|
|
@@ -163,8 +195,8 @@ function buildSourceMap(state, filename, source, generated, resultFile) {
|
|
|
163
195
|
}
|
|
164
196
|
__name(buildSourceMap, "buildSourceMap");
|
|
165
197
|
function statementLists(node) {
|
|
166
|
-
if (
|
|
167
|
-
if (
|
|
198
|
+
if (import_typescript.default.isSourceFile(node) || import_typescript.default.isBlock(node) || import_typescript.default.isModuleBlock(node)) return node.statements;
|
|
199
|
+
if (import_typescript.default.isCaseClause(node) || import_typescript.default.isDefaultClause(node)) return node.statements;
|
|
168
200
|
return null;
|
|
169
201
|
}
|
|
170
202
|
__name(statementLists, "statementLists");
|
|
@@ -183,10 +215,10 @@ function walkPairedTrees(state, original, generated, reparsed, segments) {
|
|
|
183
215
|
}
|
|
184
216
|
const originalChildren = [];
|
|
185
217
|
const generatedChildren = [];
|
|
186
|
-
|
|
218
|
+
import_typescript.default.forEachChild(original, (node) => {
|
|
187
219
|
originalChildren.push(node);
|
|
188
220
|
});
|
|
189
|
-
|
|
221
|
+
import_typescript.default.forEachChild(generated, (node) => {
|
|
190
222
|
generatedChildren.push(node);
|
|
191
223
|
});
|
|
192
224
|
if (originalChildren.length !== generatedChildren.length) return;
|
|
@@ -256,8 +288,8 @@ __name(encodeVlq, "encodeVlq");
|
|
|
256
288
|
function validatePlugins(plugins) {
|
|
257
289
|
const names = /* @__PURE__ */ new Set();
|
|
258
290
|
for (const plugin of plugins) {
|
|
259
|
-
if (!plugin.name) throw new
|
|
260
|
-
if (names.has(plugin.name)) throw new
|
|
291
|
+
if (!plugin.name) throw new import_error.VobsError({ code: "VOBS_C007", layer: "compiler", message: "\u7F16\u8BD1\u5668\u63D2\u4EF6\u5FC5\u987B\u63D0\u4F9B name", fix: "\u4E3A\u63D2\u4EF6\u6DFB\u52A0\u7A33\u5B9A\u4E14\u552F\u4E00\u7684 name\u3002" });
|
|
292
|
+
if (names.has(plugin.name)) throw new import_error.VobsError({ code: "VOBS_C007", layer: "compiler", message: `\u68C0\u6D4B\u5230\u91CD\u590D\u63D2\u4EF6: ${plugin.name}`, fix: "\u4E3A\u6BCF\u4E2A\u7F16\u8BD1\u5668\u63D2\u4EF6\u4F7F\u7528\u552F\u4E00\u7684 name\u3002" });
|
|
261
293
|
names.add(plugin.name);
|
|
262
294
|
}
|
|
263
295
|
}
|
|
@@ -269,11 +301,11 @@ function transformPluginNodes(sourceFile, plugin, context) {
|
|
|
269
301
|
const visit = /* @__PURE__ */ __name((node) => {
|
|
270
302
|
const replacement = transformNode(node, context);
|
|
271
303
|
if (replacement === null) return void 0;
|
|
272
|
-
return
|
|
304
|
+
return import_typescript.default.visitEachChild(replacement ?? node, visit, transformContext);
|
|
273
305
|
}, "visit");
|
|
274
|
-
return
|
|
306
|
+
return import_typescript.default.visitNode(root, visit);
|
|
275
307
|
}, "transformer");
|
|
276
|
-
const result =
|
|
308
|
+
const result = import_typescript.default.transform(sourceFile, [transformer]);
|
|
277
309
|
try {
|
|
278
310
|
return result.transformed[0];
|
|
279
311
|
} finally {
|
|
@@ -284,8 +316,8 @@ __name(transformPluginNodes, "transformPluginNodes");
|
|
|
284
316
|
function collectDeclaredNames(sourceFile) {
|
|
285
317
|
const names = /* @__PURE__ */ new Set();
|
|
286
318
|
const visit = /* @__PURE__ */ __name((node) => {
|
|
287
|
-
if (
|
|
288
|
-
|
|
319
|
+
if (import_typescript.default.isIdentifier(node) && isBindingName(node)) names.add(node.text);
|
|
320
|
+
import_typescript.default.forEachChild(node, visit);
|
|
289
321
|
}, "visit");
|
|
290
322
|
visit(sourceFile);
|
|
291
323
|
return names;
|
|
@@ -294,11 +326,11 @@ __name(collectDeclaredNames, "collectDeclaredNames");
|
|
|
294
326
|
function collectStateAliases(sourceFile) {
|
|
295
327
|
const aliases = /* @__PURE__ */ new Set();
|
|
296
328
|
for (const statement of sourceFile.statements) {
|
|
297
|
-
if (!
|
|
298
|
-
const
|
|
299
|
-
if (
|
|
329
|
+
if (!import_typescript.default.isImportDeclaration(statement) || !import_typescript.default.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
330
|
+
const module2 = statement.moduleSpecifier.text;
|
|
331
|
+
if (module2 !== "@vobs/reactivity" && module2 !== "@vobs/vobs") continue;
|
|
300
332
|
const clause = statement.importClause;
|
|
301
|
-
if (!clause?.namedBindings || !
|
|
333
|
+
if (!clause?.namedBindings || !import_typescript.default.isNamedImports(clause.namedBindings)) continue;
|
|
302
334
|
for (const element of clause.namedBindings.elements) {
|
|
303
335
|
if (element.propertyName ? element.propertyName.text === "state" : element.name.text === "state") {
|
|
304
336
|
aliases.add(element.name.text);
|
|
@@ -311,9 +343,9 @@ __name(collectStateAliases, "collectStateAliases");
|
|
|
311
343
|
function collectLocallyDeclaredNames(sourceFile) {
|
|
312
344
|
const names = /* @__PURE__ */ new Set();
|
|
313
345
|
const visit = /* @__PURE__ */ __name((node) => {
|
|
314
|
-
if (
|
|
315
|
-
if (
|
|
316
|
-
|
|
346
|
+
if (import_typescript.default.isImportDeclaration(node)) return;
|
|
347
|
+
if (import_typescript.default.isIdentifier(node) && isBindingName(node)) names.add(node.text);
|
|
348
|
+
import_typescript.default.forEachChild(node, visit);
|
|
317
349
|
}, "visit");
|
|
318
350
|
visit(sourceFile);
|
|
319
351
|
return names;
|
|
@@ -322,9 +354,9 @@ __name(collectLocallyDeclaredNames, "collectLocallyDeclaredNames");
|
|
|
322
354
|
function isBindingName(node) {
|
|
323
355
|
const parent = node.parent;
|
|
324
356
|
if (!parent) return false;
|
|
325
|
-
if (
|
|
326
|
-
if (
|
|
327
|
-
if (
|
|
357
|
+
if (import_typescript.default.isPropertyAccessExpression(parent) && parent.name === node) return false;
|
|
358
|
+
if (import_typescript.default.isQualifiedName(parent) && parent.right === node) return false;
|
|
359
|
+
if (import_typescript.default.isJsxAttribute(parent)) return false;
|
|
328
360
|
return parent.name === node;
|
|
329
361
|
}
|
|
330
362
|
__name(isBindingName, "isBindingName");
|
|
@@ -342,45 +374,45 @@ function resolveHelperName(state, name) {
|
|
|
342
374
|
}
|
|
343
375
|
__name(resolveHelperName, "resolveHelperName");
|
|
344
376
|
function helperRef(state, name) {
|
|
345
|
-
return
|
|
377
|
+
return import_typescript.default.factory.createIdentifier(resolveHelperName(state, name));
|
|
346
378
|
}
|
|
347
379
|
__name(helperRef, "helperRef");
|
|
348
380
|
function createRuntimeImports(state) {
|
|
349
381
|
const modules = /* @__PURE__ */ new Map();
|
|
350
382
|
for (const [name, alias] of state.helperAliases) {
|
|
351
|
-
const
|
|
352
|
-
const imported = modules.get(
|
|
353
|
-
imported.push(
|
|
383
|
+
const module2 = name === "insertResourceBoundary" ? "@vobs/resource" : "@vobs/vobs";
|
|
384
|
+
const imported = modules.get(module2) ?? [];
|
|
385
|
+
imported.push(import_typescript.default.factory.createImportSpecifier(
|
|
354
386
|
false,
|
|
355
|
-
alias === name ? void 0 :
|
|
356
|
-
|
|
387
|
+
alias === name ? void 0 : import_typescript.default.factory.createIdentifier(name),
|
|
388
|
+
import_typescript.default.factory.createIdentifier(alias)
|
|
357
389
|
));
|
|
358
|
-
modules.set(
|
|
390
|
+
modules.set(module2, imported);
|
|
359
391
|
}
|
|
360
|
-
return [...modules.entries()].map(([
|
|
392
|
+
return [...modules.entries()].map(([module2, imported]) => import_typescript.default.factory.createImportDeclaration(
|
|
361
393
|
void 0,
|
|
362
|
-
|
|
394
|
+
import_typescript.default.factory.createImportClause(
|
|
363
395
|
false,
|
|
364
396
|
void 0,
|
|
365
|
-
|
|
397
|
+
import_typescript.default.factory.createNamedImports(imported)
|
|
366
398
|
),
|
|
367
|
-
|
|
399
|
+
import_typescript.default.factory.createStringLiteral(module2)
|
|
368
400
|
));
|
|
369
401
|
}
|
|
370
402
|
__name(createRuntimeImports, "createRuntimeImports");
|
|
371
403
|
function rebuildImport(state, node) {
|
|
372
|
-
if (!
|
|
373
|
-
return tagStatement(state,
|
|
404
|
+
if (!import_typescript.default.isStringLiteral(node.moduleSpecifier)) return node;
|
|
405
|
+
return tagStatement(state, import_typescript.default.factory.createImportDeclaration(
|
|
374
406
|
node.modifiers,
|
|
375
407
|
node.importClause,
|
|
376
|
-
|
|
408
|
+
import_typescript.default.factory.createStringLiteral(node.moduleSpecifier.text),
|
|
377
409
|
node.attributes
|
|
378
410
|
), node);
|
|
379
411
|
}
|
|
380
412
|
__name(rebuildImport, "rebuildImport");
|
|
381
|
-
function transformStatement(state, node) {
|
|
382
|
-
if (
|
|
383
|
-
return tagStatement(state,
|
|
413
|
+
function transformStatement(state, node, moduleScope = false) {
|
|
414
|
+
if (import_typescript.default.isFunctionDeclaration(node) && node.body) {
|
|
415
|
+
return tagStatement(state, import_typescript.default.factory.updateFunctionDeclaration(
|
|
384
416
|
node,
|
|
385
417
|
node.modifiers,
|
|
386
418
|
node.asteriskToken,
|
|
@@ -391,20 +423,20 @@ function transformStatement(state, node) {
|
|
|
391
423
|
transformBlock(state, node.body)
|
|
392
424
|
), node);
|
|
393
425
|
}
|
|
394
|
-
if (
|
|
395
|
-
if (
|
|
396
|
-
return tagStatement(state,
|
|
426
|
+
if (import_typescript.default.isVariableStatement(node)) return transformVariableStatement(state, node, moduleScope);
|
|
427
|
+
if (import_typescript.default.isExportAssignment(node) && containsJsx(node.expression)) {
|
|
428
|
+
return tagStatement(state, import_typescript.default.factory.updateExportAssignment(node, node.modifiers, transformEmbeddedExpression(state, node.expression)), node);
|
|
397
429
|
}
|
|
398
|
-
if (
|
|
399
|
-
return tagStatement(state,
|
|
430
|
+
if (import_typescript.default.isExpressionStatement(node) && containsJsx(node.expression)) {
|
|
431
|
+
return tagStatement(state, import_typescript.default.factory.updateExpressionStatement(node, transformEmbeddedExpression(state, node.expression)), node);
|
|
400
432
|
}
|
|
401
|
-
if (
|
|
402
|
-
return tagStatement(state,
|
|
433
|
+
if (import_typescript.default.isReturnStatement(node) && node.expression && containsJsx(node.expression)) {
|
|
434
|
+
return tagStatement(state, import_typescript.default.factory.updateReturnStatement(node, transformEmbeddedExpression(state, node.expression)), node);
|
|
403
435
|
}
|
|
404
436
|
return node;
|
|
405
437
|
}
|
|
406
438
|
__name(transformStatement, "transformStatement");
|
|
407
|
-
function transformVariableStatement(state, node) {
|
|
439
|
+
function transformVariableStatement(state, node, moduleScope = false) {
|
|
408
440
|
let changed = false;
|
|
409
441
|
const declarations = node.declarationList.declarations.map((declaration) => {
|
|
410
442
|
const initializer = declaration.initializer;
|
|
@@ -412,10 +444,12 @@ function transformVariableStatement(state, node) {
|
|
|
412
444
|
let nextInitializer = inferStateDebugName(state, declaration, initializer) ?? initializer;
|
|
413
445
|
if (containsJsx(nextInitializer)) {
|
|
414
446
|
nextInitializer = transformEmbeddedExpression(state, nextInitializer);
|
|
447
|
+
} else if (moduleScope && state.hmrModuleId !== null) {
|
|
448
|
+
nextInitializer = wrapStateWithHmrRef(state, declaration, nextInitializer) ?? nextInitializer;
|
|
415
449
|
}
|
|
416
450
|
if (nextInitializer === initializer) return declaration;
|
|
417
451
|
changed = true;
|
|
418
|
-
return
|
|
452
|
+
return import_typescript.default.factory.updateVariableDeclaration(
|
|
419
453
|
declaration,
|
|
420
454
|
declaration.name,
|
|
421
455
|
declaration.exclamationToken,
|
|
@@ -424,31 +458,54 @@ function transformVariableStatement(state, node) {
|
|
|
424
458
|
);
|
|
425
459
|
});
|
|
426
460
|
if (!changed) return node;
|
|
427
|
-
return tagStatement(state,
|
|
461
|
+
return tagStatement(state, import_typescript.default.factory.updateVariableStatement(
|
|
428
462
|
node,
|
|
429
463
|
node.modifiers,
|
|
430
|
-
|
|
464
|
+
import_typescript.default.factory.updateVariableDeclarationList(node.declarationList, declarations)
|
|
431
465
|
), node);
|
|
432
466
|
}
|
|
433
467
|
__name(transformVariableStatement, "transformVariableStatement");
|
|
434
468
|
function inferStateDebugName(state, declaration, initializer) {
|
|
435
469
|
if (state.stateAliases.size === 0) return void 0;
|
|
436
|
-
if (!
|
|
470
|
+
if (!import_typescript.default.isIdentifier(declaration.name)) return void 0;
|
|
437
471
|
let call = initializer;
|
|
438
|
-
while (
|
|
472
|
+
while (import_typescript.default.isParenthesizedExpression(call) || import_typescript.default.isAsExpression(call) || import_typescript.default.isTypeAssertionExpression(call) || import_typescript.default.isSatisfiesExpression(call)) {
|
|
439
473
|
call = call.expression;
|
|
440
474
|
}
|
|
441
|
-
if (!
|
|
475
|
+
if (!import_typescript.default.isCallExpression(call)) return void 0;
|
|
442
476
|
const callee = call.expression;
|
|
443
|
-
if (!
|
|
477
|
+
if (!import_typescript.default.isIdentifier(callee) || !state.stateAliases.has(callee.text)) return void 0;
|
|
444
478
|
if (state.localBindings.has(callee.text)) return void 0;
|
|
445
479
|
if (call.arguments.length !== 1) return void 0;
|
|
446
|
-
return
|
|
480
|
+
return import_typescript.default.factory.createCallExpression(callee, call.typeArguments, [
|
|
447
481
|
...call.arguments,
|
|
448
|
-
|
|
482
|
+
import_typescript.default.factory.createStringLiteral(declaration.name.text)
|
|
449
483
|
]);
|
|
450
484
|
}
|
|
451
485
|
__name(inferStateDebugName, "inferStateDebugName");
|
|
486
|
+
function wrapStateWithHmrRef(state, declaration, initializer) {
|
|
487
|
+
let call = initializer;
|
|
488
|
+
while (import_typescript.default.isParenthesizedExpression(call) || import_typescript.default.isAsExpression(call) || import_typescript.default.isTypeAssertionExpression(call) || import_typescript.default.isSatisfiesExpression(call)) {
|
|
489
|
+
call = call.expression;
|
|
490
|
+
}
|
|
491
|
+
if (!import_typescript.default.isCallExpression(call)) return void 0;
|
|
492
|
+
const callee = call.expression;
|
|
493
|
+
if (!import_typescript.default.isIdentifier(callee) || !state.stateAliases.has(callee.text)) return void 0;
|
|
494
|
+
if (state.localBindings.has(callee.text)) return void 0;
|
|
495
|
+
if (!import_typescript.default.isIdentifier(declaration.name)) return void 0;
|
|
496
|
+
return import_typescript.default.factory.createCallExpression(helperRef(state, "hmrStateRef"), void 0, [
|
|
497
|
+
import_typescript.default.factory.createStringLiteral(`${state.hmrModuleId}#${declaration.name.text}`),
|
|
498
|
+
import_typescript.default.factory.createArrowFunction(
|
|
499
|
+
void 0,
|
|
500
|
+
void 0,
|
|
501
|
+
[],
|
|
502
|
+
void 0,
|
|
503
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
504
|
+
initializer
|
|
505
|
+
)
|
|
506
|
+
]);
|
|
507
|
+
}
|
|
508
|
+
__name(wrapStateWithHmrRef, "wrapStateWithHmrRef");
|
|
452
509
|
function containsJsx(expression) {
|
|
453
510
|
let found = false;
|
|
454
511
|
const visit = /* @__PURE__ */ __name((node) => {
|
|
@@ -456,7 +513,7 @@ function containsJsx(expression) {
|
|
|
456
513
|
found = true;
|
|
457
514
|
return;
|
|
458
515
|
}
|
|
459
|
-
|
|
516
|
+
import_typescript.default.forEachChild(node, visit);
|
|
460
517
|
}, "visit");
|
|
461
518
|
visit(expression);
|
|
462
519
|
return found;
|
|
@@ -464,20 +521,20 @@ function containsJsx(expression) {
|
|
|
464
521
|
__name(containsJsx, "containsJsx");
|
|
465
522
|
function transformBlock(state, block) {
|
|
466
523
|
const statements = block.statements.map((statement) => {
|
|
467
|
-
if (!
|
|
468
|
-
const expression =
|
|
469
|
-
return isJsxExpression(expression) ? tagStatement(state,
|
|
524
|
+
if (!import_typescript.default.isReturnStatement(statement) || !statement.expression) return transformStatement(state, statement);
|
|
525
|
+
const expression = import_typescript.default.isParenthesizedExpression(statement.expression) ? statement.expression.expression : statement.expression;
|
|
526
|
+
return isJsxExpression(expression) ? tagStatement(state, import_typescript.default.factory.updateReturnStatement(statement, transformJsxExpression(state, expression)), statement) : statement;
|
|
470
527
|
});
|
|
471
|
-
return
|
|
528
|
+
return import_typescript.default.factory.updateBlock(block, statements);
|
|
472
529
|
}
|
|
473
530
|
__name(transformBlock, "transformBlock");
|
|
474
531
|
function isJsxExpression(node) {
|
|
475
|
-
return
|
|
532
|
+
return import_typescript.default.isJsxElement(node) || import_typescript.default.isJsxSelfClosingElement(node) || import_typescript.default.isJsxFragment(node);
|
|
476
533
|
}
|
|
477
534
|
__name(isJsxExpression, "isJsxExpression");
|
|
478
535
|
function transformJsxExpression(state, node) {
|
|
479
|
-
if (
|
|
480
|
-
if (
|
|
536
|
+
if (import_typescript.default.isJsxFragment(node)) return transformFragment(state, node.children);
|
|
537
|
+
if (import_typescript.default.isJsxElement(node)) {
|
|
481
538
|
return transformElement(state, node, node.openingElement.tagName, node.openingElement.attributes, node.children);
|
|
482
539
|
}
|
|
483
540
|
return transformElement(state, node, node.tagName, node.attributes, []);
|
|
@@ -485,35 +542,35 @@ function transformJsxExpression(state, node) {
|
|
|
485
542
|
__name(transformJsxExpression, "transformJsxExpression");
|
|
486
543
|
function transformElement(state, node, tagName, attributes, children) {
|
|
487
544
|
if (isFragmentTag(tagName)) return transformFragment(state, children);
|
|
488
|
-
if (!
|
|
545
|
+
if (!import_typescript.default.isIdentifier(tagName)) {
|
|
489
546
|
reportUnsupportedTag(state, tagName);
|
|
490
547
|
}
|
|
491
|
-
if (
|
|
548
|
+
if (import_typescript.default.isIdentifier(tagName) && tagName.text === "ResourceBoundary") {
|
|
492
549
|
return transformResourceBoundary(state, node, attributes, children);
|
|
493
550
|
}
|
|
494
|
-
if (
|
|
551
|
+
if (import_typescript.default.isIdentifier(tagName) && tagName.text === "AsyncBoundary") {
|
|
495
552
|
return transformAsyncBoundary(state, node, attributes, children);
|
|
496
553
|
}
|
|
497
|
-
if (
|
|
554
|
+
if (import_typescript.default.isIdentifier(tagName) && tagName.text === "ErrorBoundary") {
|
|
498
555
|
return transformErrorBoundary(state, node, attributes, children);
|
|
499
556
|
}
|
|
500
|
-
if (
|
|
557
|
+
if (import_typescript.default.isIdentifier(tagName) && tagName.text === "Profiler") {
|
|
501
558
|
return transformProfiler(state, node, attributes, children);
|
|
502
559
|
}
|
|
503
|
-
if (
|
|
560
|
+
if (import_typescript.default.isIdentifier(tagName) && /^[A-Z]/.test(tagName.text)) {
|
|
504
561
|
const args = [
|
|
505
|
-
|
|
562
|
+
import_typescript.default.factory.createCallExpression(helperRef(state, "resolveComponent"), void 0, [
|
|
506
563
|
tagName,
|
|
507
|
-
|
|
508
|
-
|
|
564
|
+
import_typescript.default.factory.createStringLiteral(state.filename),
|
|
565
|
+
import_typescript.default.factory.createStringLiteral(tagName.text)
|
|
509
566
|
]),
|
|
510
567
|
createComponentProps(state, attributes, children)
|
|
511
568
|
];
|
|
512
569
|
if (state.sourceLocation) args.push(createSourceLocation(tagName));
|
|
513
|
-
return
|
|
570
|
+
return import_typescript.default.factory.createCallExpression(helperRef(state, "createComponent"), void 0, args);
|
|
514
571
|
}
|
|
515
572
|
if (isStaticElement(tagName, attributes, children)) {
|
|
516
|
-
return
|
|
573
|
+
return import_typescript.default.factory.createCallExpression(
|
|
517
574
|
helperRef(state, "cloneTemplate"),
|
|
518
575
|
void 0,
|
|
519
576
|
[registerTemplate(state, serializeStaticHtml(node))]
|
|
@@ -525,25 +582,25 @@ function transformElement(state, node, tagName, attributes, children) {
|
|
|
525
582
|
createConstStatement(
|
|
526
583
|
state,
|
|
527
584
|
elementId,
|
|
528
|
-
|
|
585
|
+
import_typescript.default.factory.createCallExpression(
|
|
529
586
|
helperRef(state, "createElement"),
|
|
530
587
|
void 0,
|
|
531
|
-
[
|
|
588
|
+
[import_typescript.default.factory.createStringLiteral(elementName)]
|
|
532
589
|
),
|
|
533
590
|
node
|
|
534
591
|
)
|
|
535
592
|
];
|
|
536
593
|
appendAttributes(state, statements, elementId, attributes);
|
|
537
594
|
appendChildren(state, statements, elementId, children);
|
|
538
|
-
statements.push(tagStatement(state,
|
|
539
|
-
return
|
|
540
|
-
|
|
595
|
+
statements.push(tagStatement(state, import_typescript.default.factory.createReturnStatement(elementId), node));
|
|
596
|
+
return import_typescript.default.factory.createCallExpression(
|
|
597
|
+
import_typescript.default.factory.createArrowFunction(
|
|
541
598
|
void 0,
|
|
542
599
|
void 0,
|
|
543
600
|
[],
|
|
544
601
|
void 0,
|
|
545
|
-
|
|
546
|
-
|
|
602
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
603
|
+
import_typescript.default.factory.createBlock(statements, true)
|
|
547
604
|
),
|
|
548
605
|
void 0,
|
|
549
606
|
[]
|
|
@@ -559,29 +616,29 @@ function transformFragment(state, children) {
|
|
|
559
616
|
const anchor = nextIdentifier(state, "_fragmentAnchor");
|
|
560
617
|
const statements = [];
|
|
561
618
|
appendChildren(state, statements, parent, children, anchor);
|
|
562
|
-
return
|
|
619
|
+
return import_typescript.default.factory.createCallExpression(
|
|
563
620
|
helperRef(state, "createFragment"),
|
|
564
621
|
void 0,
|
|
565
|
-
[
|
|
622
|
+
[import_typescript.default.factory.createArrowFunction(
|
|
566
623
|
void 0,
|
|
567
624
|
void 0,
|
|
568
625
|
[
|
|
569
|
-
|
|
570
|
-
|
|
626
|
+
import_typescript.default.factory.createParameterDeclaration(void 0, void 0, parent),
|
|
627
|
+
import_typescript.default.factory.createParameterDeclaration(void 0, void 0, anchor)
|
|
571
628
|
],
|
|
572
629
|
void 0,
|
|
573
|
-
|
|
574
|
-
|
|
630
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
631
|
+
import_typescript.default.factory.createBlock(statements, true)
|
|
575
632
|
)]
|
|
576
633
|
);
|
|
577
634
|
}
|
|
578
635
|
__name(transformFragment, "transformFragment");
|
|
579
636
|
function transformResourceBoundary(state, node, attributes, children) {
|
|
580
637
|
const resource = getAttributeExpression(attributes, "resource");
|
|
581
|
-
if (!resource) throw new
|
|
638
|
+
if (!resource) throw new import_error.VobsError({ code: "VOBS_C002", layer: "compiler", message: "ResourceBoundary \u5FC5\u987B\u63D0\u4F9B resource \u5C5E\u6027", fix: "\u4E3A ResourceBoundary \u6DFB\u52A0 resource={resource}\u3002" });
|
|
582
639
|
const options = [
|
|
583
|
-
|
|
584
|
-
|
|
640
|
+
import_typescript.default.factory.createPropertyAssignment("resource", transformEmbeddedExpression(state, resource)),
|
|
641
|
+
import_typescript.default.factory.createPropertyAssignment("children", createBoundaryFactory(state, children))
|
|
585
642
|
];
|
|
586
643
|
appendBoundaryOptionalProperty(state, options, attributes, "loading");
|
|
587
644
|
appendBoundaryOptionalProperty(state, options, attributes, "empty");
|
|
@@ -591,34 +648,34 @@ function transformResourceBoundary(state, node, attributes, children) {
|
|
|
591
648
|
__name(transformResourceBoundary, "transformResourceBoundary");
|
|
592
649
|
function transformErrorBoundary(state, node, attributes, children) {
|
|
593
650
|
const fallback = getAttributeExpression(attributes, "fallback");
|
|
594
|
-
if (!fallback) throw new
|
|
651
|
+
if (!fallback) throw new import_error.VobsError({ code: "VOBS_C002", layer: "compiler", message: "ErrorBoundary \u5FC5\u987B\u63D0\u4F9B fallback \u5C5E\u6027", fix: "\u4E3A ErrorBoundary \u6DFB\u52A0 fallback={(error, retry) => ...}\u3002" });
|
|
595
652
|
return createBoundaryFragment(state, node, "insertErrorBoundary", [
|
|
596
|
-
|
|
597
|
-
|
|
653
|
+
import_typescript.default.factory.createPropertyAssignment("children", createBoundaryFactory(state, children)),
|
|
654
|
+
import_typescript.default.factory.createPropertyAssignment("fallback", transformEmbeddedExpression(state, fallback))
|
|
598
655
|
]);
|
|
599
656
|
}
|
|
600
657
|
__name(transformErrorBoundary, "transformErrorBoundary");
|
|
601
658
|
function transformAsyncBoundary(state, node, attributes, children) {
|
|
602
659
|
const promise = getAttributeExpression(attributes, "promise");
|
|
603
|
-
if (!promise) throw new
|
|
660
|
+
if (!promise) throw new import_error.VobsError({ code: "VOBS_C002", layer: "compiler", message: "AsyncBoundary \u5FC5\u987B\u63D0\u4F9B promise \u5C5E\u6027", fix: "\u4E3A AsyncBoundary \u6DFB\u52A0 promise={promise}\u3002" });
|
|
604
661
|
const options = [
|
|
605
|
-
|
|
606
|
-
|
|
662
|
+
import_typescript.default.factory.createPropertyAssignment("promise", transformEmbeddedExpression(state, promise)),
|
|
663
|
+
import_typescript.default.factory.createPropertyAssignment("children", createAsyncFactory(state, children))
|
|
607
664
|
];
|
|
608
665
|
appendBoundaryOptionalProperty(state, options, attributes, "loading");
|
|
609
666
|
appendBoundaryOptionalProperty(state, options, attributes, "fallback");
|
|
610
667
|
const resetKey = getAttributeExpression(attributes, "resetKey");
|
|
611
|
-
if (resetKey) options.push(
|
|
668
|
+
if (resetKey) options.push(import_typescript.default.factory.createPropertyAssignment("resetKey", createGetter(resetKey)));
|
|
612
669
|
return createBoundaryFragment(state, node, "insertAsyncBoundary", options);
|
|
613
670
|
}
|
|
614
671
|
__name(transformAsyncBoundary, "transformAsyncBoundary");
|
|
615
672
|
function transformProfiler(state, node, attributes, children) {
|
|
616
|
-
const idAttribute = attributes.properties.find((attribute) =>
|
|
617
|
-
const id = idAttribute &&
|
|
618
|
-
if (!id) throw new
|
|
673
|
+
const idAttribute = attributes.properties.find((attribute) => import_typescript.default.isJsxAttribute(attribute) && attribute.name.getText() === "id");
|
|
674
|
+
const id = idAttribute && import_typescript.default.isJsxAttribute(idAttribute) && idAttribute.initializer && import_typescript.default.isStringLiteral(idAttribute.initializer) ? import_typescript.default.factory.createStringLiteral(idAttribute.initializer.text) : idAttribute && import_typescript.default.isJsxAttribute(idAttribute) && idAttribute.initializer && import_typescript.default.isJsxExpression(idAttribute.initializer) ? idAttribute.initializer.expression : null;
|
|
675
|
+
if (!id) throw new import_error.VobsError({ code: "VOBS_C002", layer: "compiler", message: "Profiler \u5FC5\u987B\u63D0\u4F9B id \u5C5E\u6027", fix: '\u4E3A Profiler \u6DFB\u52A0 id="ComponentName"\u3002' });
|
|
619
676
|
const options = [
|
|
620
|
-
|
|
621
|
-
|
|
677
|
+
import_typescript.default.factory.createPropertyAssignment("id", transformEmbeddedExpression(state, id)),
|
|
678
|
+
import_typescript.default.factory.createPropertyAssignment("children", createBoundaryFactory(state, children))
|
|
622
679
|
];
|
|
623
680
|
appendBoundaryOptionalProperty(state, options, attributes, "onRender");
|
|
624
681
|
return createBoundaryFragment(state, node, "insertProfiler", options);
|
|
@@ -627,22 +684,22 @@ __name(transformProfiler, "transformProfiler");
|
|
|
627
684
|
function createBoundaryFragment(state, node, helper, options) {
|
|
628
685
|
const parent = nextIdentifier(state, "_boundaryParent");
|
|
629
686
|
const anchor = nextIdentifier(state, "_boundaryAnchor");
|
|
630
|
-
return
|
|
687
|
+
return import_typescript.default.factory.createCallExpression(
|
|
631
688
|
helperRef(state, "createFragment"),
|
|
632
689
|
void 0,
|
|
633
|
-
[
|
|
690
|
+
[import_typescript.default.factory.createArrowFunction(
|
|
634
691
|
void 0,
|
|
635
692
|
void 0,
|
|
636
693
|
[
|
|
637
|
-
|
|
638
|
-
|
|
694
|
+
import_typescript.default.factory.createParameterDeclaration(void 0, void 0, parent),
|
|
695
|
+
import_typescript.default.factory.createParameterDeclaration(void 0, void 0, anchor)
|
|
639
696
|
],
|
|
640
697
|
void 0,
|
|
641
|
-
|
|
642
|
-
|
|
698
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
699
|
+
import_typescript.default.factory.createBlock([callStatement(state, helper, [
|
|
643
700
|
parent,
|
|
644
701
|
anchor,
|
|
645
|
-
|
|
702
|
+
import_typescript.default.factory.createObjectLiteralExpression(options, true)
|
|
646
703
|
], node)], true)
|
|
647
704
|
)]
|
|
648
705
|
);
|
|
@@ -650,27 +707,27 @@ function createBoundaryFragment(state, node, helper, options) {
|
|
|
650
707
|
__name(createBoundaryFragment, "createBoundaryFragment");
|
|
651
708
|
function createBoundaryFactory(state, children) {
|
|
652
709
|
const content = transformFragment(state, children);
|
|
653
|
-
return
|
|
710
|
+
return import_typescript.default.factory.createArrowFunction(
|
|
654
711
|
void 0,
|
|
655
712
|
void 0,
|
|
656
713
|
[],
|
|
657
714
|
void 0,
|
|
658
|
-
|
|
715
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
659
716
|
content
|
|
660
717
|
);
|
|
661
718
|
}
|
|
662
719
|
__name(createBoundaryFactory, "createBoundaryFactory");
|
|
663
720
|
function createAsyncFactory(state, children) {
|
|
664
|
-
const value =
|
|
665
|
-
const expressionChild = children.length === 1 && children[0].kind ===
|
|
666
|
-
if (expressionChild &&
|
|
721
|
+
const value = import_typescript.default.factory.createIdentifier("value");
|
|
722
|
+
const expressionChild = children.length === 1 && children[0].kind === import_typescript.default.SyntaxKind.JsxExpression ? children[0].expression : void 0;
|
|
723
|
+
if (expressionChild && import_typescript.default.isArrowFunction(expressionChild)) {
|
|
667
724
|
const transformed = transformEmbeddedExpression(state, expressionChild);
|
|
668
725
|
return transformed;
|
|
669
726
|
}
|
|
670
727
|
const content = transformFragment(state, children);
|
|
671
|
-
return
|
|
672
|
-
|
|
673
|
-
], void 0,
|
|
728
|
+
return import_typescript.default.factory.createArrowFunction(void 0, void 0, [
|
|
729
|
+
import_typescript.default.factory.createParameterDeclaration(void 0, void 0, value)
|
|
730
|
+
], void 0, import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken), content);
|
|
674
731
|
}
|
|
675
732
|
__name(createAsyncFactory, "createAsyncFactory");
|
|
676
733
|
function appendBoundaryOptionalProperty(state, properties, attributes, name) {
|
|
@@ -678,14 +735,14 @@ function appendBoundaryOptionalProperty(state, properties, attributes, name) {
|
|
|
678
735
|
if (expression) {
|
|
679
736
|
const transformed = transformEmbeddedExpression(state, expression);
|
|
680
737
|
const value = isJsxExpression(unwrapExpression(expression)) ? createGetter(transformed) : transformed;
|
|
681
|
-
properties.push(
|
|
738
|
+
properties.push(import_typescript.default.factory.createPropertyAssignment(name, value));
|
|
682
739
|
}
|
|
683
740
|
}
|
|
684
741
|
__name(appendBoundaryOptionalProperty, "appendBoundaryOptionalProperty");
|
|
685
742
|
function getAttributeExpression(attributes, name) {
|
|
686
743
|
for (const attribute of attributes.properties) {
|
|
687
|
-
if (!
|
|
688
|
-
if (attribute.initializer &&
|
|
744
|
+
if (!import_typescript.default.isJsxAttribute(attribute) || attribute.name.getText() !== name) continue;
|
|
745
|
+
if (attribute.initializer && import_typescript.default.isJsxExpression(attribute.initializer)) {
|
|
689
746
|
return attribute.initializer.expression ?? null;
|
|
690
747
|
}
|
|
691
748
|
}
|
|
@@ -693,14 +750,14 @@ function getAttributeExpression(attributes, name) {
|
|
|
693
750
|
}
|
|
694
751
|
__name(getAttributeExpression, "getAttributeExpression");
|
|
695
752
|
function transformEmbeddedExpression(state, expression) {
|
|
696
|
-
const result =
|
|
753
|
+
const result = import_typescript.default.transform(expression, [(context) => (root) => {
|
|
697
754
|
const visit = /* @__PURE__ */ __name((node) => {
|
|
698
|
-
if (
|
|
755
|
+
if (import_typescript.default.isJsxElement(node) || import_typescript.default.isJsxSelfClosingElement(node) || import_typescript.default.isJsxFragment(node)) {
|
|
699
756
|
return transformJsxExpression(state, node);
|
|
700
757
|
}
|
|
701
|
-
return
|
|
758
|
+
return import_typescript.default.visitEachChild(node, visit, context);
|
|
702
759
|
}, "visit");
|
|
703
|
-
return
|
|
760
|
+
return import_typescript.default.visitNode(root, visit);
|
|
704
761
|
}]);
|
|
705
762
|
try {
|
|
706
763
|
return result.transformed[0];
|
|
@@ -709,21 +766,42 @@ function transformEmbeddedExpression(state, expression) {
|
|
|
709
766
|
}
|
|
710
767
|
}
|
|
711
768
|
__name(transformEmbeddedExpression, "transformEmbeddedExpression");
|
|
769
|
+
function transformResidualJsx(state, sourceFile) {
|
|
770
|
+
if (!containsJsx(sourceFile)) return sourceFile;
|
|
771
|
+
const result = import_typescript.default.transform(sourceFile, [(context) => (root) => {
|
|
772
|
+
const visit = /* @__PURE__ */ __name((node) => {
|
|
773
|
+
if (import_typescript.default.isReturnStatement(node) && node.expression && containsJsx(node.expression)) {
|
|
774
|
+
return import_typescript.default.factory.updateReturnStatement(node, transformEmbeddedExpression(state, node.expression));
|
|
775
|
+
}
|
|
776
|
+
if (isJsxExpression(node)) {
|
|
777
|
+
return transformJsxExpression(state, node);
|
|
778
|
+
}
|
|
779
|
+
return import_typescript.default.visitEachChild(node, visit, context);
|
|
780
|
+
}, "visit");
|
|
781
|
+
return import_typescript.default.visitNode(root, visit);
|
|
782
|
+
}]);
|
|
783
|
+
try {
|
|
784
|
+
return result.transformed[0];
|
|
785
|
+
} finally {
|
|
786
|
+
result.dispose();
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
__name(transformResidualJsx, "transformResidualJsx");
|
|
712
790
|
function isStaticElement(tagName, attributes, children) {
|
|
713
|
-
if (!
|
|
791
|
+
if (!import_typescript.default.isIdentifier(tagName) || !/^[a-z]/.test(tagName.text)) return false;
|
|
714
792
|
for (const attribute of attributes.properties) {
|
|
715
|
-
if (
|
|
716
|
-
if (!
|
|
793
|
+
if (import_typescript.default.isJsxSpreadAttribute(attribute)) return false;
|
|
794
|
+
if (!import_typescript.default.isJsxAttribute(attribute)) return false;
|
|
717
795
|
const name = attribute.name.getText();
|
|
718
796
|
if (name === "key" || name === "ref" || name.startsWith("on")) return false;
|
|
719
797
|
if (isPropertyAttribute(name)) return false;
|
|
720
798
|
const initializer = attribute.initializer;
|
|
721
|
-
if (initializer && !
|
|
799
|
+
if (initializer && !import_typescript.default.isStringLiteral(initializer)) return false;
|
|
722
800
|
}
|
|
723
801
|
for (const child of children) {
|
|
724
|
-
if (
|
|
725
|
-
if (
|
|
726
|
-
const nested =
|
|
802
|
+
if (import_typescript.default.isJsxText(child)) continue;
|
|
803
|
+
if (import_typescript.default.isJsxElement(child) || import_typescript.default.isJsxSelfClosingElement(child)) {
|
|
804
|
+
const nested = import_typescript.default.isJsxElement(child) ? { tagName: child.openingElement.tagName, attributes: child.openingElement.attributes, children: child.children } : { tagName: child.tagName, attributes: child.attributes, children: [] };
|
|
727
805
|
if (!isStaticElement(nested.tagName, nested.attributes, nested.children)) return false;
|
|
728
806
|
continue;
|
|
729
807
|
}
|
|
@@ -733,7 +811,7 @@ function isStaticElement(tagName, attributes, children) {
|
|
|
733
811
|
}
|
|
734
812
|
__name(isStaticElement, "isStaticElement");
|
|
735
813
|
function serializeStaticHtml(node) {
|
|
736
|
-
const { tagName, attributes, children } =
|
|
814
|
+
const { tagName, attributes, children } = import_typescript.default.isJsxElement(node) ? { tagName: node.openingElement.tagName, attributes: node.openingElement.attributes, children: node.children } : { tagName: node.tagName, attributes: node.attributes, children: [] };
|
|
737
815
|
return serializeStaticElement(tagName, attributes, children);
|
|
738
816
|
}
|
|
739
817
|
__name(serializeStaticHtml, "serializeStaticHtml");
|
|
@@ -741,25 +819,25 @@ function serializeStaticElement(tagName, attributes, children) {
|
|
|
741
819
|
const name = tagName.getText();
|
|
742
820
|
let html = `<${name}`;
|
|
743
821
|
for (const attribute of attributes.properties) {
|
|
744
|
-
if (!
|
|
822
|
+
if (!import_typescript.default.isJsxAttribute(attribute)) continue;
|
|
745
823
|
const attributeName = attribute.name.getText() === "className" ? "class" : attribute.name.getText();
|
|
746
824
|
const initializer = attribute.initializer;
|
|
747
825
|
if (!initializer) {
|
|
748
826
|
html += ` ${attributeName}=""`;
|
|
749
827
|
continue;
|
|
750
828
|
}
|
|
751
|
-
if (
|
|
829
|
+
if (import_typescript.default.isStringLiteral(initializer)) {
|
|
752
830
|
html += ` ${attributeName}="${escapeHtmlAttribute(initializer.text)}"`;
|
|
753
831
|
}
|
|
754
832
|
}
|
|
755
833
|
html += ">";
|
|
756
834
|
for (const child of children) {
|
|
757
|
-
if (
|
|
835
|
+
if (import_typescript.default.isJsxText(child)) {
|
|
758
836
|
const text = child.text.replace(/\s+/g, " ").trimStart();
|
|
759
837
|
if (text.trim()) html += escapeHtmlText(text);
|
|
760
838
|
continue;
|
|
761
839
|
}
|
|
762
|
-
if (
|
|
840
|
+
if (import_typescript.default.isJsxElement(child)) {
|
|
763
841
|
html += serializeStaticElement(
|
|
764
842
|
child.openingElement.tagName,
|
|
765
843
|
child.openingElement.attributes,
|
|
@@ -767,7 +845,7 @@ function serializeStaticElement(tagName, attributes, children) {
|
|
|
767
845
|
);
|
|
768
846
|
continue;
|
|
769
847
|
}
|
|
770
|
-
if (
|
|
848
|
+
if (import_typescript.default.isJsxSelfClosingElement(child)) {
|
|
771
849
|
html += serializeStaticElement(child.tagName, child.attributes, []);
|
|
772
850
|
}
|
|
773
851
|
}
|
|
@@ -793,103 +871,103 @@ function registerTemplate(state, html) {
|
|
|
793
871
|
__name(registerTemplate, "registerTemplate");
|
|
794
872
|
function createTemplateDeclarations(state) {
|
|
795
873
|
return [...state.templates.entries()].map(
|
|
796
|
-
([html, identifier]) =>
|
|
797
|
-
|
|
874
|
+
([html, identifier]) => import_typescript.default.factory.createVariableStatement(void 0, import_typescript.default.factory.createVariableDeclarationList([
|
|
875
|
+
import_typescript.default.factory.createVariableDeclaration(identifier, void 0, void 0, import_typescript.default.factory.createCallExpression(
|
|
798
876
|
helperRef(state, "createTemplate"),
|
|
799
877
|
void 0,
|
|
800
|
-
[
|
|
878
|
+
[import_typescript.default.factory.createStringLiteral(html)]
|
|
801
879
|
))
|
|
802
|
-
],
|
|
880
|
+
], import_typescript.default.NodeFlags.Const))
|
|
803
881
|
);
|
|
804
882
|
}
|
|
805
883
|
__name(createTemplateDeclarations, "createTemplateDeclarations");
|
|
806
884
|
function appendAttributes(state, statements, element, attributes) {
|
|
807
885
|
const staticProps = [];
|
|
808
|
-
const hasSpread = attributes.properties.some((attribute) =>
|
|
886
|
+
const hasSpread = attributes.properties.some((attribute) => import_typescript.default.isJsxSpreadAttribute(attribute));
|
|
809
887
|
for (const attribute of attributes.properties) {
|
|
810
|
-
if (
|
|
888
|
+
if (import_typescript.default.isJsxSpreadAttribute(attribute)) {
|
|
811
889
|
statements.push(callStatement(state, "spreadProps", [element, transformEmbeddedExpression(state, attribute.expression)], attribute));
|
|
812
890
|
continue;
|
|
813
891
|
}
|
|
814
|
-
if (!
|
|
892
|
+
if (!import_typescript.default.isJsxAttribute(attribute)) continue;
|
|
815
893
|
const name = attribute.name.getText();
|
|
816
894
|
if (name === "key") continue;
|
|
817
895
|
if (name === "ref") {
|
|
818
896
|
const initializer2 = attribute.initializer;
|
|
819
|
-
if (initializer2 &&
|
|
897
|
+
if (initializer2 && import_typescript.default.isJsxExpression(initializer2) && initializer2.expression) {
|
|
820
898
|
statements.push(callStatement(state, "setRef", [element, transformEmbeddedExpression(state, initializer2.expression)], attribute));
|
|
821
899
|
}
|
|
822
900
|
continue;
|
|
823
901
|
}
|
|
824
902
|
const initializer = attribute.initializer;
|
|
825
|
-
if (name.startsWith("on") && initializer &&
|
|
903
|
+
if (name.startsWith("on") && initializer && import_typescript.default.isJsxExpression(initializer) && initializer.expression) {
|
|
826
904
|
statements.push(callStatement(state, "addEventListener", [
|
|
827
905
|
element,
|
|
828
|
-
|
|
906
|
+
import_typescript.default.factory.createStringLiteral(name.slice(2).toLowerCase()),
|
|
829
907
|
initializer.expression
|
|
830
908
|
], attribute));
|
|
831
909
|
continue;
|
|
832
910
|
}
|
|
833
911
|
if (!initializer) {
|
|
834
912
|
if (hasSpread) {
|
|
835
|
-
statements.push(callStatement(state, isPropertyAttribute(name) ? "setProperty" : "setAttribute", [element,
|
|
913
|
+
statements.push(callStatement(state, isPropertyAttribute(name) ? "setProperty" : "setAttribute", [element, import_typescript.default.factory.createStringLiteral(isPropertyAttribute(name) ? name : name === "className" ? "class" : name), isPropertyAttribute(name) ? import_typescript.default.factory.createTrue() : import_typescript.default.factory.createStringLiteral("")], attribute));
|
|
836
914
|
continue;
|
|
837
915
|
}
|
|
838
|
-
if (isPropertyAttribute(name)) staticProps.push(createStaticProperty(name,
|
|
839
|
-
else staticProps.push(createStaticProperty(name === "className" ? "class" : name,
|
|
916
|
+
if (isPropertyAttribute(name)) staticProps.push(createStaticProperty(name, import_typescript.default.factory.createTrue()));
|
|
917
|
+
else staticProps.push(createStaticProperty(name === "className" ? "class" : name, import_typescript.default.factory.createStringLiteral("")));
|
|
840
918
|
continue;
|
|
841
919
|
}
|
|
842
|
-
if (
|
|
920
|
+
if (import_typescript.default.isStringLiteral(initializer)) {
|
|
843
921
|
if (hasSpread) {
|
|
844
|
-
statements.push(callStatement(state, isPropertyAttribute(name) ? "setProperty" : "setAttribute", [element,
|
|
922
|
+
statements.push(callStatement(state, isPropertyAttribute(name) ? "setProperty" : "setAttribute", [element, import_typescript.default.factory.createStringLiteral(isPropertyAttribute(name) ? name : name === "className" ? "class" : name), import_typescript.default.factory.createStringLiteral(initializer.text)], attribute));
|
|
845
923
|
continue;
|
|
846
924
|
}
|
|
847
925
|
staticProps.push(createStaticProperty(
|
|
848
926
|
isPropertyAttribute(name) ? name : name === "className" ? "class" : name,
|
|
849
|
-
|
|
927
|
+
import_typescript.default.factory.createStringLiteral(initializer.text)
|
|
850
928
|
));
|
|
851
929
|
continue;
|
|
852
930
|
}
|
|
853
931
|
const attributeName = name === "className" ? "class" : name;
|
|
854
932
|
const propertyAttribute = isPropertyAttribute(name);
|
|
855
|
-
if (
|
|
933
|
+
if (import_typescript.default.isJsxExpression(initializer) && initializer.expression) {
|
|
856
934
|
statements.push(callStatement(state, propertyAttribute ? "bindProperty" : "bindAttribute", [
|
|
857
935
|
element,
|
|
858
|
-
|
|
936
|
+
import_typescript.default.factory.createStringLiteral(propertyAttribute ? name : attributeName),
|
|
859
937
|
createGetter(initializer.expression)
|
|
860
938
|
], attribute));
|
|
861
939
|
}
|
|
862
940
|
}
|
|
863
941
|
if (staticProps.length) statements.splice(1, 0, callStatement(state, "setStaticProps", [
|
|
864
942
|
element,
|
|
865
|
-
|
|
943
|
+
import_typescript.default.factory.createObjectLiteralExpression(staticProps, true)
|
|
866
944
|
], attributes));
|
|
867
945
|
}
|
|
868
946
|
__name(appendAttributes, "appendAttributes");
|
|
869
947
|
function createStaticProperty(name, value) {
|
|
870
|
-
return
|
|
948
|
+
return import_typescript.default.factory.createPropertyAssignment(import_typescript.default.factory.createStringLiteral(name), value);
|
|
871
949
|
}
|
|
872
950
|
__name(createStaticProperty, "createStaticProperty");
|
|
873
951
|
function isPropertyAttribute(name) {
|
|
874
952
|
return name === "value" || name === "checked" || name === "selected" || name === "disabled" || name === "multiple" || name === "readOnly" || name === "required" || name === "autofocus" || name === "hidden" || name === "tabIndex";
|
|
875
953
|
}
|
|
876
954
|
__name(isPropertyAttribute, "isPropertyAttribute");
|
|
877
|
-
function appendChildren(state, statements, element, children, anchor =
|
|
955
|
+
function appendChildren(state, statements, element, children, anchor = import_typescript.default.factory.createNull()) {
|
|
878
956
|
for (const child of children) {
|
|
879
|
-
if (
|
|
957
|
+
if (import_typescript.default.isJsxText(child)) {
|
|
880
958
|
const text = child.text.replace(/\s+/g, " ").trimStart();
|
|
881
959
|
if (text.trim()) {
|
|
882
960
|
statements.push(callStatement(state, "insertBefore", [
|
|
883
961
|
element,
|
|
884
|
-
|
|
885
|
-
|
|
962
|
+
import_typescript.default.factory.createCallExpression(helperRef(state, "createText"), void 0, [
|
|
963
|
+
import_typescript.default.factory.createStringLiteral(text)
|
|
886
964
|
]),
|
|
887
965
|
anchor
|
|
888
966
|
], child));
|
|
889
967
|
}
|
|
890
968
|
continue;
|
|
891
969
|
}
|
|
892
|
-
if (
|
|
970
|
+
if (import_typescript.default.isJsxElement(child) || import_typescript.default.isJsxSelfClosingElement(child) || import_typescript.default.isJsxFragment(child)) {
|
|
893
971
|
statements.push(callStatement(state, "insertBefore", [
|
|
894
972
|
element,
|
|
895
973
|
transformJsxExpression(state, child),
|
|
@@ -897,7 +975,7 @@ function appendChildren(state, statements, element, children, anchor = ts__defau
|
|
|
897
975
|
], child));
|
|
898
976
|
continue;
|
|
899
977
|
}
|
|
900
|
-
if (child.kind ===
|
|
978
|
+
if (child.kind === import_typescript.default.SyntaxKind.JsxExpression) {
|
|
901
979
|
const expression = child.expression;
|
|
902
980
|
if (!expression) continue;
|
|
903
981
|
const list = transformListExpression(state, element, expression, anchor);
|
|
@@ -910,9 +988,9 @@ function appendChildren(state, statements, element, children, anchor = ts__defau
|
|
|
910
988
|
statements.push(callStatement(state, "insertDynamic", [element, anchor, dynamic], child));
|
|
911
989
|
continue;
|
|
912
990
|
}
|
|
913
|
-
if (!containsJsx(expression) && !
|
|
991
|
+
if (!containsJsx(expression) && !import_typescript.default.isIdentifier(expression)) {
|
|
914
992
|
const textId = nextIdentifier(state, "_text");
|
|
915
|
-
statements.push(createConstStatement(state, textId,
|
|
993
|
+
statements.push(createConstStatement(state, textId, import_typescript.default.factory.createCallExpression(helperRef(state, "createText"), void 0, [import_typescript.default.factory.createStringLiteral("")]), child));
|
|
916
994
|
statements.push(callStatement(state, "insertBefore", [element, textId, anchor], child));
|
|
917
995
|
statements.push(callStatement(state, "bindText", [textId, createGetter(expression)], child));
|
|
918
996
|
continue;
|
|
@@ -926,18 +1004,18 @@ __name(appendChildren, "appendChildren");
|
|
|
926
1004
|
function createComponentProps(state, attributes, children) {
|
|
927
1005
|
const properties = [];
|
|
928
1006
|
for (const attribute of attributes.properties) {
|
|
929
|
-
if (
|
|
930
|
-
properties.push(
|
|
1007
|
+
if (import_typescript.default.isJsxSpreadAttribute(attribute)) {
|
|
1008
|
+
properties.push(import_typescript.default.factory.createSpreadAssignment(attribute.expression));
|
|
931
1009
|
continue;
|
|
932
1010
|
}
|
|
933
1011
|
const name = propertyName(attribute.name.getText());
|
|
934
1012
|
if (attribute.name.getText() === "key") continue;
|
|
935
1013
|
const initializer = attribute.initializer;
|
|
936
1014
|
if (!initializer) {
|
|
937
|
-
properties.push(
|
|
938
|
-
} else if (
|
|
939
|
-
properties.push(
|
|
940
|
-
} else if (
|
|
1015
|
+
properties.push(import_typescript.default.factory.createPropertyAssignment(name, import_typescript.default.factory.createTrue()));
|
|
1016
|
+
} else if (import_typescript.default.isStringLiteral(initializer)) {
|
|
1017
|
+
properties.push(import_typescript.default.factory.createPropertyAssignment(name, import_typescript.default.factory.createStringLiteral(initializer.text)));
|
|
1018
|
+
} else if (import_typescript.default.isJsxExpression(initializer) && initializer.expression) {
|
|
941
1019
|
properties.push(createGetterProperty(name, transformEmbeddedExpression(state, initializer.expression)));
|
|
942
1020
|
}
|
|
943
1021
|
}
|
|
@@ -945,62 +1023,78 @@ function createComponentProps(state, attributes, children) {
|
|
|
945
1023
|
if (childExpressions.length === 1) {
|
|
946
1024
|
properties.push(createGetterProperty("children", childExpressions[0]));
|
|
947
1025
|
} else if (childExpressions.length > 1) {
|
|
948
|
-
properties.push(createGetterProperty("children",
|
|
1026
|
+
properties.push(createGetterProperty("children", import_typescript.default.factory.createArrayLiteralExpression(childExpressions)));
|
|
949
1027
|
}
|
|
950
|
-
return
|
|
1028
|
+
return import_typescript.default.factory.createObjectLiteralExpression(properties, true);
|
|
951
1029
|
}
|
|
952
1030
|
__name(createComponentProps, "createComponentProps");
|
|
953
1031
|
function createSourceLocation(node) {
|
|
954
1032
|
const sourceFile = node.getSourceFile();
|
|
955
1033
|
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
956
|
-
return
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1034
|
+
return import_typescript.default.factory.createObjectLiteralExpression([
|
|
1035
|
+
import_typescript.default.factory.createPropertyAssignment("file", import_typescript.default.factory.createStringLiteral(sourceFile.fileName)),
|
|
1036
|
+
import_typescript.default.factory.createPropertyAssignment("line", import_typescript.default.factory.createNumericLiteral(position.line + 1)),
|
|
1037
|
+
import_typescript.default.factory.createPropertyAssignment("column", import_typescript.default.factory.createNumericLiteral(position.character + 1))
|
|
960
1038
|
], true);
|
|
961
1039
|
}
|
|
962
1040
|
__name(createSourceLocation, "createSourceLocation");
|
|
963
1041
|
function transformDynamicExpression(state, expression) {
|
|
964
|
-
|
|
1042
|
+
const converted = convertDynamicNodeExpression(state, expression);
|
|
1043
|
+
return converted ? createGetter(converted) : null;
|
|
1044
|
+
}
|
|
1045
|
+
__name(transformDynamicExpression, "transformDynamicExpression");
|
|
1046
|
+
function convertDynamicNodeExpression(state, expression) {
|
|
1047
|
+
if (import_typescript.default.isBinaryExpression(expression) && expression.operatorToken.kind === import_typescript.default.SyntaxKind.AmpersandAmpersandToken) {
|
|
965
1048
|
const right = unwrapExpression(expression.right);
|
|
966
|
-
if (
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
ts__default.default.factory.createNull()
|
|
973
|
-
));
|
|
1049
|
+
if (isJsxExpression(right)) {
|
|
1050
|
+
return createNodeConditional(expression.left, transformJsxExpression(state, right), null);
|
|
1051
|
+
}
|
|
1052
|
+
const convertedRight = convertDynamicNodeExpression(state, right);
|
|
1053
|
+
if (convertedRight) return createNodeConditional(expression.left, convertedRight, null);
|
|
1054
|
+
return null;
|
|
974
1055
|
}
|
|
975
|
-
if (
|
|
1056
|
+
if (import_typescript.default.isConditionalExpression(expression)) {
|
|
976
1057
|
const whenTrue = transformDynamicBranch(state, expression.whenTrue);
|
|
977
1058
|
const whenFalse = transformDynamicBranch(state, expression.whenFalse);
|
|
978
1059
|
if (!whenTrue && !whenFalse) return null;
|
|
979
|
-
return
|
|
1060
|
+
return import_typescript.default.factory.createConditionalExpression(
|
|
980
1061
|
expression.condition,
|
|
981
|
-
|
|
982
|
-
whenTrue ??
|
|
983
|
-
|
|
984
|
-
whenFalse ??
|
|
985
|
-
)
|
|
1062
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.QuestionToken),
|
|
1063
|
+
whenTrue ?? import_typescript.default.factory.createNull(),
|
|
1064
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.ColonToken),
|
|
1065
|
+
whenFalse ?? import_typescript.default.factory.createNull()
|
|
1066
|
+
);
|
|
986
1067
|
}
|
|
987
1068
|
return null;
|
|
988
1069
|
}
|
|
989
|
-
__name(
|
|
1070
|
+
__name(convertDynamicNodeExpression, "convertDynamicNodeExpression");
|
|
1071
|
+
function createNodeConditional(condition, whenTrue, whenFalse) {
|
|
1072
|
+
return import_typescript.default.factory.createConditionalExpression(
|
|
1073
|
+
condition,
|
|
1074
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.QuestionToken),
|
|
1075
|
+
whenTrue,
|
|
1076
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.ColonToken),
|
|
1077
|
+
whenFalse ?? import_typescript.default.factory.createNull()
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
__name(createNodeConditional, "createNodeConditional");
|
|
990
1081
|
function transformDynamicBranch(state, expression) {
|
|
991
1082
|
const branch = unwrapExpression(expression);
|
|
992
1083
|
if (isJsxExpression(branch)) return transformJsxExpression(state, branch);
|
|
993
|
-
if (branch.kind ===
|
|
1084
|
+
if (branch.kind === import_typescript.default.SyntaxKind.NullKeyword || branch.kind === import_typescript.default.SyntaxKind.FalseKeyword) return branch;
|
|
1085
|
+
if (import_typescript.default.isConditionalExpression(branch) || import_typescript.default.isBinaryExpression(branch) && branch.operatorToken.kind === import_typescript.default.SyntaxKind.AmpersandAmpersandToken) {
|
|
1086
|
+
return convertDynamicNodeExpression(state, branch);
|
|
1087
|
+
}
|
|
994
1088
|
return null;
|
|
995
1089
|
}
|
|
996
1090
|
__name(transformDynamicBranch, "transformDynamicBranch");
|
|
997
1091
|
function transformListExpression(state, parent, expression, anchor) {
|
|
998
|
-
if (!
|
|
999
|
-
if (!
|
|
1092
|
+
if (!import_typescript.default.isCallExpression(expression) || expression.arguments.length !== 1) return null;
|
|
1093
|
+
if (!import_typescript.default.isPropertyAccessExpression(expression.expression) || expression.expression.name.text !== "map") return null;
|
|
1000
1094
|
const callback = expression.arguments[0];
|
|
1001
|
-
if (!
|
|
1095
|
+
if (!import_typescript.default.isArrowFunction(callback) && !import_typescript.default.isFunctionExpression(callback)) return null;
|
|
1002
1096
|
const body = unwrapExpression(callback.body);
|
|
1003
|
-
if (!isJsxExpression(body) ||
|
|
1097
|
+
if (!isJsxExpression(body) || import_typescript.default.isJsxFragment(body)) return null;
|
|
1004
1098
|
const key = findKeyExpression(body);
|
|
1005
1099
|
const renderItem = transformListCallback(callback, transformJsxExpression(state, body));
|
|
1006
1100
|
const args = [
|
|
@@ -1014,8 +1108,8 @@ function transformListExpression(state, parent, expression, anchor) {
|
|
|
1014
1108
|
}
|
|
1015
1109
|
__name(transformListExpression, "transformListExpression");
|
|
1016
1110
|
function transformListCallback(callback, body) {
|
|
1017
|
-
if (
|
|
1018
|
-
return
|
|
1111
|
+
if (import_typescript.default.isArrowFunction(callback)) {
|
|
1112
|
+
return import_typescript.default.factory.updateArrowFunction(
|
|
1019
1113
|
callback,
|
|
1020
1114
|
callback.modifiers,
|
|
1021
1115
|
callback.typeParameters,
|
|
@@ -1025,7 +1119,7 @@ function transformListCallback(callback, body) {
|
|
|
1025
1119
|
body
|
|
1026
1120
|
);
|
|
1027
1121
|
}
|
|
1028
|
-
return
|
|
1122
|
+
return import_typescript.default.factory.updateFunctionExpression(
|
|
1029
1123
|
callback,
|
|
1030
1124
|
callback.modifiers,
|
|
1031
1125
|
callback.asteriskToken,
|
|
@@ -1033,26 +1127,26 @@ function transformListCallback(callback, body) {
|
|
|
1033
1127
|
callback.typeParameters,
|
|
1034
1128
|
callback.parameters,
|
|
1035
1129
|
callback.type,
|
|
1036
|
-
|
|
1130
|
+
import_typescript.default.factory.createBlock([import_typescript.default.factory.createReturnStatement(body)], true)
|
|
1037
1131
|
);
|
|
1038
1132
|
}
|
|
1039
1133
|
__name(transformListCallback, "transformListCallback");
|
|
1040
1134
|
function createKeyCallback(callback, key) {
|
|
1041
|
-
return
|
|
1135
|
+
return import_typescript.default.factory.createArrowFunction(
|
|
1042
1136
|
void 0,
|
|
1043
1137
|
void 0,
|
|
1044
1138
|
callback.parameters,
|
|
1045
1139
|
void 0,
|
|
1046
|
-
|
|
1140
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
1047
1141
|
key
|
|
1048
1142
|
);
|
|
1049
1143
|
}
|
|
1050
1144
|
__name(createKeyCallback, "createKeyCallback");
|
|
1051
1145
|
function findKeyExpression(node) {
|
|
1052
|
-
const attributes =
|
|
1146
|
+
const attributes = import_typescript.default.isJsxElement(node) ? node.openingElement.attributes : node.attributes;
|
|
1053
1147
|
for (const attribute of attributes.properties) {
|
|
1054
|
-
if (!
|
|
1055
|
-
if (attribute.initializer &&
|
|
1148
|
+
if (!import_typescript.default.isJsxAttribute(attribute) || attribute.name.getText() !== "key") continue;
|
|
1149
|
+
if (attribute.initializer && import_typescript.default.isJsxExpression(attribute.initializer)) {
|
|
1056
1150
|
return attribute.initializer.expression ?? null;
|
|
1057
1151
|
}
|
|
1058
1152
|
}
|
|
@@ -1060,18 +1154,18 @@ function findKeyExpression(node) {
|
|
|
1060
1154
|
}
|
|
1061
1155
|
__name(findKeyExpression, "findKeyExpression");
|
|
1062
1156
|
function unwrapExpression(node) {
|
|
1063
|
-
return
|
|
1157
|
+
return import_typescript.default.isParenthesizedExpression(node) ? node.expression : node;
|
|
1064
1158
|
}
|
|
1065
1159
|
__name(unwrapExpression, "unwrapExpression");
|
|
1066
1160
|
function childToComponentExpression(state, child) {
|
|
1067
|
-
if (
|
|
1161
|
+
if (import_typescript.default.isJsxText(child)) {
|
|
1068
1162
|
const text = child.text.replace(/\s+/g, " ").trim();
|
|
1069
|
-
return text ? [
|
|
1163
|
+
return text ? [import_typescript.default.factory.createStringLiteral(text)] : [];
|
|
1070
1164
|
}
|
|
1071
|
-
if (
|
|
1165
|
+
if (import_typescript.default.isJsxElement(child) || import_typescript.default.isJsxSelfClosingElement(child) || import_typescript.default.isJsxFragment(child)) {
|
|
1072
1166
|
return [transformJsxExpression(state, child)];
|
|
1073
1167
|
}
|
|
1074
|
-
if (child.kind ===
|
|
1168
|
+
if (child.kind === import_typescript.default.SyntaxKind.JsxExpression) {
|
|
1075
1169
|
const expression = child.expression;
|
|
1076
1170
|
return expression ? [transformEmbeddedExpression(state, expression)] : [];
|
|
1077
1171
|
}
|
|
@@ -1079,43 +1173,43 @@ function childToComponentExpression(state, child) {
|
|
|
1079
1173
|
}
|
|
1080
1174
|
__name(childToComponentExpression, "childToComponentExpression");
|
|
1081
1175
|
function propertyName(name) {
|
|
1082
|
-
return /^[$A-Z_a-z][$\w]*$/u.test(name) ?
|
|
1176
|
+
return /^[$A-Z_a-z][$\w]*$/u.test(name) ? import_typescript.default.factory.createIdentifier(name) : import_typescript.default.factory.createStringLiteral(name);
|
|
1083
1177
|
}
|
|
1084
1178
|
__name(propertyName, "propertyName");
|
|
1085
1179
|
function createGetter(expression) {
|
|
1086
|
-
return
|
|
1180
|
+
return import_typescript.default.factory.createArrowFunction(
|
|
1087
1181
|
void 0,
|
|
1088
1182
|
void 0,
|
|
1089
1183
|
[],
|
|
1090
1184
|
void 0,
|
|
1091
|
-
|
|
1185
|
+
import_typescript.default.factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
|
|
1092
1186
|
expression
|
|
1093
1187
|
);
|
|
1094
1188
|
}
|
|
1095
1189
|
__name(createGetter, "createGetter");
|
|
1096
1190
|
function createGetterProperty(name, expression) {
|
|
1097
|
-
return
|
|
1191
|
+
return import_typescript.default.factory.createGetAccessorDeclaration(
|
|
1098
1192
|
void 0,
|
|
1099
1193
|
typeof name === "string" ? propertyName(name) : name,
|
|
1100
1194
|
[],
|
|
1101
1195
|
void 0,
|
|
1102
|
-
|
|
1196
|
+
import_typescript.default.factory.createBlock([import_typescript.default.factory.createReturnStatement(expression)], true)
|
|
1103
1197
|
);
|
|
1104
1198
|
}
|
|
1105
1199
|
__name(createGetterProperty, "createGetterProperty");
|
|
1106
1200
|
function callStatement(state, name, args, source) {
|
|
1107
|
-
const statement =
|
|
1108
|
-
|
|
1201
|
+
const statement = import_typescript.default.factory.createExpressionStatement(
|
|
1202
|
+
import_typescript.default.factory.createCallExpression(helperRef(state, name), void 0, args)
|
|
1109
1203
|
);
|
|
1110
1204
|
return source ? tagStatement(state, statement, source) : statement;
|
|
1111
1205
|
}
|
|
1112
1206
|
__name(callStatement, "callStatement");
|
|
1113
1207
|
function createConstStatement(state, name, initializer, source) {
|
|
1114
|
-
const statement =
|
|
1208
|
+
const statement = import_typescript.default.factory.createVariableStatement(
|
|
1115
1209
|
void 0,
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
],
|
|
1210
|
+
import_typescript.default.factory.createVariableDeclarationList([
|
|
1211
|
+
import_typescript.default.factory.createVariableDeclaration(name, void 0, void 0, initializer)
|
|
1212
|
+
], import_typescript.default.NodeFlags.Const)
|
|
1119
1213
|
);
|
|
1120
1214
|
return source ? tagStatement(state, statement, source) : statement;
|
|
1121
1215
|
}
|
|
@@ -1138,12 +1232,13 @@ function nextIdentifier(state, prefix) {
|
|
|
1138
1232
|
do {
|
|
1139
1233
|
name = `${prefix}${state.generatedId++}`;
|
|
1140
1234
|
} while (state.takenNames.has(name));
|
|
1141
|
-
return
|
|
1235
|
+
return import_typescript.default.factory.createIdentifier(name);
|
|
1142
1236
|
}
|
|
1143
1237
|
__name(nextIdentifier, "nextIdentifier");
|
|
1144
|
-
|
|
1145
|
-
exports
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1238
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1239
|
+
0 && (module.exports = {
|
|
1240
|
+
compile,
|
|
1241
|
+
compileWithSourceMap,
|
|
1242
|
+
createCompiler
|
|
1243
|
+
});
|
|
1149
1244
|
//# sourceMappingURL=compile.cjs.map
|