@esportsplus/template 0.32.2 → 0.32.3
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.
|
@@ -4,9 +4,9 @@ type CodegenResult = {
|
|
|
4
4
|
changed: boolean;
|
|
5
5
|
code: string;
|
|
6
6
|
};
|
|
7
|
-
declare const
|
|
7
|
+
declare const addImport: (code: string) => string;
|
|
8
8
|
declare const generateCode: (templates: TemplateInfo[], originalCode: string, sourceFile: ts.SourceFile, checker?: ts.TypeChecker) => CodegenResult;
|
|
9
9
|
declare const generateReactiveInlining: (calls: ReactiveCallInfo[], code: string, sourceFile: ts.SourceFile) => string;
|
|
10
10
|
declare const needsArraySlotImport: (sourceFile: ts.SourceFile) => boolean;
|
|
11
|
-
export {
|
|
11
|
+
export { addImport, generateCode, generateReactiveInlining, needsArraySlotImport };
|
|
12
12
|
export type { CodegenResult };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import { code as c, uid } from '@esportsplus/typescript/transformer';
|
|
2
|
+
import { ast, code as c, uid } from '@esportsplus/typescript/transformer';
|
|
3
3
|
import { analyzeExpression, generateAttributeBinding, generateSpreadBindings } from './type-analyzer.js';
|
|
4
4
|
import { COMPILER_ENTRYPOINT, COMPILER_NAMESPACE, COMPILER_TYPES, PACKAGE } from '../constants.js';
|
|
5
5
|
import parser from './parser.js';
|
|
@@ -140,42 +140,21 @@ function hasArraySlotImport(sourceFile) {
|
|
|
140
140
|
}
|
|
141
141
|
return false;
|
|
142
142
|
}
|
|
143
|
-
function hasMatch(node, predicate) {
|
|
144
|
-
if (predicate(node)) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
let found = false;
|
|
148
|
-
ts.forEachChild(node, child => {
|
|
149
|
-
if (!found) {
|
|
150
|
-
found = hasMatch(child, predicate);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
return found;
|
|
154
|
-
}
|
|
155
143
|
function isNestedHtmlTemplate(expr) {
|
|
156
144
|
return ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === COMPILER_ENTRYPOINT;
|
|
157
145
|
}
|
|
158
|
-
function isNestedTemplate(template, exprRanges) {
|
|
159
|
-
for (let i = 0, n = exprRanges.length; i < n; i++) {
|
|
160
|
-
let range = exprRanges[i];
|
|
161
|
-
if (template.start >= range.start && template.end <= range.end) {
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
146
|
function rewriteExpression(ctx, expr) {
|
|
168
147
|
if (isNestedHtmlTemplate(expr)) {
|
|
169
148
|
return generateNestedTemplateCode(ctx, expr);
|
|
170
149
|
}
|
|
171
|
-
if (!hasMatch(expr, n => isNestedHtmlTemplate(n))) {
|
|
150
|
+
if (!ast.hasMatch(expr, n => isNestedHtmlTemplate(n))) {
|
|
172
151
|
return ctx.printer.printNode(ts.EmitHint.Expression, expr, ctx.sourceFile);
|
|
173
152
|
}
|
|
174
153
|
let replacements = [];
|
|
175
154
|
collectNestedTemplateReplacements(ctx, expr, expr.getStart(), replacements);
|
|
176
155
|
return c.replaceReverse(expr.getText(ctx.sourceFile), replacements);
|
|
177
156
|
}
|
|
178
|
-
const
|
|
157
|
+
const addImport = (code) => {
|
|
179
158
|
return `import * as ${COMPILER_NAMESPACE} from '${PACKAGE}';\n` + code;
|
|
180
159
|
};
|
|
181
160
|
const generateCode = (templates, originalCode, sourceFile, checker) => {
|
|
@@ -189,7 +168,7 @@ const generateCode = (templates, originalCode, sourceFile, checker) => {
|
|
|
189
168
|
ranges.push({ end: exprs[j].end, start: exprs[j].getStart() });
|
|
190
169
|
}
|
|
191
170
|
}
|
|
192
|
-
let rootTemplates = templates.filter(t => !
|
|
171
|
+
let rootTemplates = templates.filter(t => !ast.inRange(ranges, t.start, t.end));
|
|
193
172
|
if (rootTemplates.length === 0) {
|
|
194
173
|
return { changed: false, code: originalCode };
|
|
195
174
|
}
|
|
@@ -229,7 +208,7 @@ const generateCode = (templates, originalCode, sourceFile, checker) => {
|
|
|
229
208
|
for (let [id, html] of ctx.hoistedFactories) {
|
|
230
209
|
factories.push(`const ${id} = ${COMPILER_NAMESPACE}.template(\`${html}\`);`);
|
|
231
210
|
}
|
|
232
|
-
code =
|
|
211
|
+
code = addImport(factories.join('\n') + code);
|
|
233
212
|
}
|
|
234
213
|
return { changed, code };
|
|
235
214
|
};
|
|
@@ -252,8 +231,8 @@ const generateReactiveInlining = (calls, code, sourceFile) => {
|
|
|
252
231
|
return c.replaceReverse(code, replacements);
|
|
253
232
|
};
|
|
254
233
|
const needsArraySlotImport = (sourceFile) => {
|
|
255
|
-
return hasMatch(sourceFile, n => ts.isNewExpression(n) &&
|
|
234
|
+
return ast.hasMatch(sourceFile, n => ts.isNewExpression(n) &&
|
|
256
235
|
ts.isPropertyAccessExpression(n.expression) &&
|
|
257
236
|
n.expression.name.text === 'ArraySlot') && !hasArraySlotImport(sourceFile);
|
|
258
237
|
};
|
|
259
|
-
export {
|
|
238
|
+
export { addImport, generateCode, generateReactiveInlining, needsArraySlotImport };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { code as c } from '@esportsplus/typescript/transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { addImport, generateCode, generateReactiveInlining, needsArraySlotImport } from './codegen.js';
|
|
3
3
|
import { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY } from '../constants.js';
|
|
4
4
|
import { findHtmlTemplates, findReactiveCalls } from './ts-parser.js';
|
|
5
5
|
import { ts } from '@esportsplus/typescript';
|
|
@@ -36,7 +36,7 @@ const transform = (sourceFile, program) => {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
if (needsImport && !codegenChanged) {
|
|
39
|
-
result =
|
|
39
|
+
result = addImport(result);
|
|
40
40
|
}
|
|
41
41
|
if (changed) {
|
|
42
42
|
sourceFile = ts.createSourceFile(sourceFile.fileName, result, sourceFile.languageVersion, true);
|
package/package.json
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
4
|
"@esportsplus/queue": "^0.2.0",
|
|
5
|
-
"@esportsplus/reactivity": "^0.25.
|
|
5
|
+
"@esportsplus/reactivity": "^0.25.14",
|
|
6
6
|
"@esportsplus/utilities": "^0.27.2",
|
|
7
7
|
"serve": "^14.2.5"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@esportsplus/typescript": "^0.
|
|
10
|
+
"@esportsplus/typescript": "^0.20.0",
|
|
11
11
|
"@types/node": "^25.0.3",
|
|
12
12
|
"vite": "^7.3.0",
|
|
13
13
|
"vite-tsconfig-paths": "^6.0.3"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"types": "./build/index.d.ts",
|
|
43
|
-
"version": "0.32.
|
|
43
|
+
"version": "0.32.3",
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsc",
|
|
46
46
|
"build:test": "vite build --config test/vite.config.ts",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
|
-
import { code as c, uid, type Replacement } from '@esportsplus/typescript/transformer';
|
|
2
|
+
import { ast, code as c, uid, type Replacement } from '@esportsplus/typescript/transformer';
|
|
3
3
|
import type { ReactiveCallInfo, TemplateInfo } from './ts-parser';
|
|
4
4
|
import { analyzeExpression, generateAttributeBinding, generateSpreadBindings } from './type-analyzer';
|
|
5
5
|
import {
|
|
@@ -273,44 +273,16 @@ function hasArraySlotImport(sourceFile: ts.SourceFile): boolean {
|
|
|
273
273
|
return false;
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
-
function hasMatch(node: ts.Node, predicate: (n: ts.Node) => boolean): boolean {
|
|
277
|
-
if (predicate(node)) {
|
|
278
|
-
return true;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
let found = false;
|
|
282
|
-
|
|
283
|
-
ts.forEachChild(node, child => {
|
|
284
|
-
if (!found) {
|
|
285
|
-
found = hasMatch(child, predicate);
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
return found;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
276
|
function isNestedHtmlTemplate(expr: ts.Expression): expr is ts.TaggedTemplateExpression {
|
|
293
277
|
return ts.isTaggedTemplateExpression(expr) && ts.isIdentifier(expr.tag) && expr.tag.text === COMPILER_ENTRYPOINT;
|
|
294
278
|
}
|
|
295
279
|
|
|
296
|
-
function isNestedTemplate(template: TemplateInfo, exprRanges: { end: number; start: number }[]): boolean {
|
|
297
|
-
for (let i = 0, n = exprRanges.length; i < n; i++) {
|
|
298
|
-
let range = exprRanges[i];
|
|
299
|
-
|
|
300
|
-
if (template.start >= range.start && template.end <= range.end) {
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
return false;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
280
|
function rewriteExpression(ctx: CodegenContext, expr: ts.Expression): string {
|
|
309
281
|
if (isNestedHtmlTemplate(expr)) {
|
|
310
282
|
return generateNestedTemplateCode(ctx, expr);
|
|
311
283
|
}
|
|
312
284
|
|
|
313
|
-
if (!hasMatch(expr, n => isNestedHtmlTemplate(n as ts.Expression))) {
|
|
285
|
+
if (!ast.hasMatch(expr, n => isNestedHtmlTemplate(n as ts.Expression))) {
|
|
314
286
|
return ctx.printer.printNode(ts.EmitHint.Expression, expr, ctx.sourceFile);
|
|
315
287
|
}
|
|
316
288
|
|
|
@@ -322,7 +294,7 @@ function rewriteExpression(ctx: CodegenContext, expr: ts.Expression): string {
|
|
|
322
294
|
}
|
|
323
295
|
|
|
324
296
|
|
|
325
|
-
const
|
|
297
|
+
const addImport = (code: string): string => {
|
|
326
298
|
return `import * as ${COMPILER_NAMESPACE} from '${PACKAGE}';\n` + code;
|
|
327
299
|
};
|
|
328
300
|
|
|
@@ -342,7 +314,7 @@ const generateCode = (templates: TemplateInfo[], originalCode: string, sourceFil
|
|
|
342
314
|
}
|
|
343
315
|
}
|
|
344
316
|
|
|
345
|
-
let rootTemplates = templates.filter(t => !
|
|
317
|
+
let rootTemplates = templates.filter(t => !ast.inRange(ranges, t.start, t.end));
|
|
346
318
|
|
|
347
319
|
if (rootTemplates.length === 0) {
|
|
348
320
|
return { changed: false, code: originalCode };
|
|
@@ -406,7 +378,7 @@ const generateCode = (templates: TemplateInfo[], originalCode: string, sourceFil
|
|
|
406
378
|
factories.push(`const ${id} = ${COMPILER_NAMESPACE}.template(\`${html}\`);`);
|
|
407
379
|
}
|
|
408
380
|
|
|
409
|
-
code =
|
|
381
|
+
code = addImport(factories.join('\n') + code);
|
|
410
382
|
}
|
|
411
383
|
|
|
412
384
|
return { changed, code };
|
|
@@ -436,7 +408,7 @@ const generateReactiveInlining = (calls: ReactiveCallInfo[], code: string, sourc
|
|
|
436
408
|
};
|
|
437
409
|
|
|
438
410
|
const needsArraySlotImport = (sourceFile: ts.SourceFile): boolean => {
|
|
439
|
-
return hasMatch(sourceFile, n =>
|
|
411
|
+
return ast.hasMatch(sourceFile, n =>
|
|
440
412
|
ts.isNewExpression(n) &&
|
|
441
413
|
ts.isPropertyAccessExpression(n.expression) &&
|
|
442
414
|
n.expression.name.text === 'ArraySlot'
|
|
@@ -444,5 +416,5 @@ const needsArraySlotImport = (sourceFile: ts.SourceFile): boolean => {
|
|
|
444
416
|
};
|
|
445
417
|
|
|
446
418
|
|
|
447
|
-
export {
|
|
419
|
+
export { addImport, generateCode, generateReactiveInlining, needsArraySlotImport };
|
|
448
420
|
export type { CodegenResult };
|
package/src/transformer/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { code as c } from '@esportsplus/typescript/transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { addImport, generateCode, generateReactiveInlining, needsArraySlotImport } from './codegen';
|
|
3
3
|
import { COMPILER_ENTRYPOINT, COMPILER_ENTRYPOINT_REACTIVITY } from '../constants';
|
|
4
4
|
import { findHtmlTemplates, findReactiveCalls } from './ts-parser';
|
|
5
5
|
import { ts } from '@esportsplus/typescript';
|
|
@@ -64,7 +64,7 @@ const transform = (sourceFile: ts.SourceFile, program: ts.Program): TransformRes
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
if (needsImport && !codegenChanged) {
|
|
67
|
-
result =
|
|
67
|
+
result = addImport(result);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
if (changed) {
|