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