jslike 1.8.0 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/interpreter/interpreter.js +229 -23
- package/dist/index.cjs +212 -20
- package/dist/index.d.cts +231 -24
- package/dist/index.d.ts +231 -24
- package/dist/index.js +212 -20
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -11527,6 +11527,165 @@ function getPatternName(pattern) {
|
|
|
11527
11527
|
if (pattern.type === "TSParameterProperty") return getPatternName(pattern.parameter);
|
|
11528
11528
|
return pattern.name;
|
|
11529
11529
|
}
|
|
11530
|
+
function collectRuntimeIdentifierReferences(node) {
|
|
11531
|
+
const references = /* @__PURE__ */ new Set();
|
|
11532
|
+
const skipKeys = /* @__PURE__ */ new Set([
|
|
11533
|
+
"type",
|
|
11534
|
+
"start",
|
|
11535
|
+
"end",
|
|
11536
|
+
"loc",
|
|
11537
|
+
"range",
|
|
11538
|
+
"raw",
|
|
11539
|
+
"typeAnnotation",
|
|
11540
|
+
"returnType",
|
|
11541
|
+
"typeParameters",
|
|
11542
|
+
"typeArguments",
|
|
11543
|
+
"implements"
|
|
11544
|
+
]);
|
|
11545
|
+
const visitPatternDefaults = (pattern) => {
|
|
11546
|
+
if (!pattern || typeof pattern !== "object") return;
|
|
11547
|
+
if (pattern.type === "AssignmentPattern") {
|
|
11548
|
+
visit(pattern.right);
|
|
11549
|
+
visitPatternDefaults(pattern.left);
|
|
11550
|
+
} else if (pattern.type === "ObjectPattern") {
|
|
11551
|
+
for (const property of pattern.properties || []) {
|
|
11552
|
+
visitPatternDefaults(property.value || property.argument);
|
|
11553
|
+
}
|
|
11554
|
+
} else if (pattern.type === "ArrayPattern") {
|
|
11555
|
+
for (const element of pattern.elements || []) {
|
|
11556
|
+
visitPatternDefaults(element);
|
|
11557
|
+
}
|
|
11558
|
+
} else if (pattern.type === "RestElement") {
|
|
11559
|
+
visitPatternDefaults(pattern.argument);
|
|
11560
|
+
} else if (pattern.type === "TSParameterProperty") {
|
|
11561
|
+
visitPatternDefaults(pattern.parameter);
|
|
11562
|
+
}
|
|
11563
|
+
};
|
|
11564
|
+
const visitFunction = (fn) => {
|
|
11565
|
+
for (const param of fn.params || []) {
|
|
11566
|
+
visitPatternDefaults(param);
|
|
11567
|
+
}
|
|
11568
|
+
visit(fn.body);
|
|
11569
|
+
};
|
|
11570
|
+
const visitJSXName = (jsxName) => {
|
|
11571
|
+
if (!jsxName || typeof jsxName !== "object") return;
|
|
11572
|
+
if (jsxName.type === "JSXIdentifier") {
|
|
11573
|
+
if (/^[A-Z]/.test(jsxName.name)) {
|
|
11574
|
+
references.add(jsxName.name);
|
|
11575
|
+
}
|
|
11576
|
+
} else if (jsxName.type === "JSXMemberExpression") {
|
|
11577
|
+
visitJSXName(jsxName.object);
|
|
11578
|
+
} else if (jsxName.type === "JSXNamespacedName") {
|
|
11579
|
+
visitJSXName(jsxName.namespace);
|
|
11580
|
+
}
|
|
11581
|
+
};
|
|
11582
|
+
const visit = (current2, parent = null, parentKey = null) => {
|
|
11583
|
+
var _a, _b, _c;
|
|
11584
|
+
if (!current2 || typeof current2 !== "object") return;
|
|
11585
|
+
if (Array.isArray(current2)) {
|
|
11586
|
+
for (const item of current2) visit(item, parent, parentKey);
|
|
11587
|
+
return;
|
|
11588
|
+
}
|
|
11589
|
+
if ((_a = current2.type) == null ? void 0 : _a.startsWith("TS")) {
|
|
11590
|
+
if (isTypeWrapperExpression(current2)) {
|
|
11591
|
+
visit(getTypeWrapperInnerExpression(current2), current2, "expression");
|
|
11592
|
+
} else if (current2.type === "TSEnumDeclaration") {
|
|
11593
|
+
for (const member of current2.members || []) {
|
|
11594
|
+
visit(member.initializer);
|
|
11595
|
+
}
|
|
11596
|
+
}
|
|
11597
|
+
return;
|
|
11598
|
+
}
|
|
11599
|
+
switch (current2.type) {
|
|
11600
|
+
case "Identifier":
|
|
11601
|
+
references.add(current2.name);
|
|
11602
|
+
return;
|
|
11603
|
+
case "ImportDeclaration":
|
|
11604
|
+
return;
|
|
11605
|
+
case "ExportNamedDeclaration":
|
|
11606
|
+
if (current2.declaration) {
|
|
11607
|
+
visit(current2.declaration, current2, "declaration");
|
|
11608
|
+
} else if (current2.exportKind !== "type") {
|
|
11609
|
+
for (const specifier of current2.specifiers || []) {
|
|
11610
|
+
if (specifier.exportKind !== "type" && ((_b = specifier.local) == null ? void 0 : _b.name)) {
|
|
11611
|
+
references.add(specifier.local.name);
|
|
11612
|
+
}
|
|
11613
|
+
}
|
|
11614
|
+
}
|
|
11615
|
+
return;
|
|
11616
|
+
case "ExportDefaultDeclaration":
|
|
11617
|
+
visit(current2.declaration, current2, "declaration");
|
|
11618
|
+
return;
|
|
11619
|
+
case "VariableDeclarator":
|
|
11620
|
+
visitPatternDefaults(current2.id);
|
|
11621
|
+
visit(current2.init, current2, "init");
|
|
11622
|
+
return;
|
|
11623
|
+
case "FunctionDeclaration":
|
|
11624
|
+
visitFunction(current2);
|
|
11625
|
+
return;
|
|
11626
|
+
case "FunctionExpression":
|
|
11627
|
+
case "ArrowFunctionExpression":
|
|
11628
|
+
visitFunction(current2);
|
|
11629
|
+
return;
|
|
11630
|
+
case "ClassDeclaration":
|
|
11631
|
+
case "ClassExpression":
|
|
11632
|
+
visit(current2.superClass, current2, "superClass");
|
|
11633
|
+
visit(current2.body, current2, "body");
|
|
11634
|
+
return;
|
|
11635
|
+
case "MemberExpression":
|
|
11636
|
+
case "OptionalMemberExpression":
|
|
11637
|
+
visit(current2.object, current2, "object");
|
|
11638
|
+
if (current2.computed) {
|
|
11639
|
+
visit(current2.property, current2, "property");
|
|
11640
|
+
}
|
|
11641
|
+
return;
|
|
11642
|
+
case "Property":
|
|
11643
|
+
if (current2.computed) {
|
|
11644
|
+
visit(current2.key, current2, "key");
|
|
11645
|
+
}
|
|
11646
|
+
visit(current2.value, current2, "value");
|
|
11647
|
+
return;
|
|
11648
|
+
case "MethodDefinition":
|
|
11649
|
+
case "PropertyDefinition":
|
|
11650
|
+
if (current2.computed) {
|
|
11651
|
+
visit(current2.key, current2, "key");
|
|
11652
|
+
}
|
|
11653
|
+
if (!current2.declare && !current2.abstract) {
|
|
11654
|
+
visit(current2.value, current2, "value");
|
|
11655
|
+
}
|
|
11656
|
+
return;
|
|
11657
|
+
case "AssignmentPattern":
|
|
11658
|
+
visit(current2.right, current2, "right");
|
|
11659
|
+
return;
|
|
11660
|
+
case "RestElement":
|
|
11661
|
+
return;
|
|
11662
|
+
case "ObjectPattern":
|
|
11663
|
+
case "ArrayPattern":
|
|
11664
|
+
visitPatternDefaults(current2);
|
|
11665
|
+
return;
|
|
11666
|
+
case "JSXElement":
|
|
11667
|
+
visitJSXName((_c = current2.openingElement) == null ? void 0 : _c.name);
|
|
11668
|
+
for (const child of current2.children || []) {
|
|
11669
|
+
visit(child);
|
|
11670
|
+
}
|
|
11671
|
+
return;
|
|
11672
|
+
case "JSXFragment":
|
|
11673
|
+
for (const child of current2.children || []) {
|
|
11674
|
+
visit(child);
|
|
11675
|
+
}
|
|
11676
|
+
return;
|
|
11677
|
+
case "JSXExpressionContainer":
|
|
11678
|
+
visit(current2.expression, current2, "expression");
|
|
11679
|
+
return;
|
|
11680
|
+
}
|
|
11681
|
+
for (const [key, value] of Object.entries(current2)) {
|
|
11682
|
+
if (skipKeys.has(key)) continue;
|
|
11683
|
+
visit(value, current2, key);
|
|
11684
|
+
}
|
|
11685
|
+
};
|
|
11686
|
+
visit(node);
|
|
11687
|
+
return references;
|
|
11688
|
+
}
|
|
11530
11689
|
var Interpreter = class _Interpreter {
|
|
11531
11690
|
constructor(globalEnv, options = {}) {
|
|
11532
11691
|
this.globalEnv = globalEnv;
|
|
@@ -11535,6 +11694,8 @@ var Interpreter = class _Interpreter {
|
|
|
11535
11694
|
this.moduleResolutionCache = options.moduleResolutionCache || /* @__PURE__ */ new Map();
|
|
11536
11695
|
this.moduleExports = {};
|
|
11537
11696
|
this.currentModulePath = options.currentModulePath;
|
|
11697
|
+
this.isTypeScriptModule = options.isTypeScriptModule || false;
|
|
11698
|
+
this.runtimeIdentifierReferences = null;
|
|
11538
11699
|
this.abortSignal = options.abortSignal;
|
|
11539
11700
|
this.executionController = options.executionController;
|
|
11540
11701
|
}
|
|
@@ -11700,14 +11861,20 @@ var Interpreter = class _Interpreter {
|
|
|
11700
11861
|
return void 0;
|
|
11701
11862
|
}
|
|
11702
11863
|
if (node.type === "Program") {
|
|
11864
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
11865
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
11703
11866
|
let result = void 0;
|
|
11704
|
-
|
|
11705
|
-
|
|
11706
|
-
|
|
11707
|
-
|
|
11867
|
+
try {
|
|
11868
|
+
for (const statement of node.body) {
|
|
11869
|
+
result = await this.evaluateAsync(statement, env);
|
|
11870
|
+
if (result instanceof ReturnValue || result instanceof ThrowSignal) {
|
|
11871
|
+
return result;
|
|
11872
|
+
}
|
|
11708
11873
|
}
|
|
11874
|
+
return result;
|
|
11875
|
+
} finally {
|
|
11876
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
11709
11877
|
}
|
|
11710
|
-
return result;
|
|
11711
11878
|
}
|
|
11712
11879
|
if (node.type === "ImportDeclaration") {
|
|
11713
11880
|
return await this.evaluateImportDeclaration(node, env);
|
|
@@ -12331,23 +12498,29 @@ var Interpreter = class _Interpreter {
|
|
|
12331
12498
|
}
|
|
12332
12499
|
}
|
|
12333
12500
|
evaluateProgram(node, env) {
|
|
12501
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
12502
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
12334
12503
|
let result = void 0;
|
|
12335
|
-
|
|
12336
|
-
|
|
12337
|
-
|
|
12338
|
-
|
|
12339
|
-
|
|
12340
|
-
|
|
12341
|
-
|
|
12504
|
+
try {
|
|
12505
|
+
for (let i = 0; i < node.body.length; i++) {
|
|
12506
|
+
const statement = node.body[i];
|
|
12507
|
+
const isLast = i === node.body.length - 1;
|
|
12508
|
+
if (isLast && statement.type === "BlockStatement") {
|
|
12509
|
+
const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
|
|
12510
|
+
if (objLiteral !== null) {
|
|
12511
|
+
return objLiteral;
|
|
12512
|
+
}
|
|
12342
12513
|
}
|
|
12514
|
+
const statementResult = this.evaluate(statement, env);
|
|
12515
|
+
if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
|
|
12516
|
+
return statementResult;
|
|
12517
|
+
}
|
|
12518
|
+
result = statementResult;
|
|
12343
12519
|
}
|
|
12344
|
-
|
|
12345
|
-
|
|
12346
|
-
|
|
12347
|
-
}
|
|
12348
|
-
result = statementResult;
|
|
12520
|
+
return result;
|
|
12521
|
+
} finally {
|
|
12522
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
12349
12523
|
}
|
|
12350
|
-
return result;
|
|
12351
12524
|
}
|
|
12352
12525
|
// Try to convert a BlockStatement to an object literal
|
|
12353
12526
|
// Returns null if the block doesn't look like an object literal
|
|
@@ -12995,6 +13168,9 @@ var Interpreter = class _Interpreter {
|
|
|
12995
13168
|
if (node.importKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.importKind === "type")) {
|
|
12996
13169
|
return void 0;
|
|
12997
13170
|
}
|
|
13171
|
+
if (this.isTypeScriptModule && node.specifiers.length > 0 && node.specifiers.every((specifier) => !this.isRuntimeImportSpecifier(specifier))) {
|
|
13172
|
+
return void 0;
|
|
13173
|
+
}
|
|
12998
13174
|
if (!this.moduleResolver) {
|
|
12999
13175
|
throw new Error("Module resolver not configured - cannot import modules");
|
|
13000
13176
|
}
|
|
@@ -13030,6 +13206,7 @@ var Interpreter = class _Interpreter {
|
|
|
13030
13206
|
moduleResolver: this.moduleResolver,
|
|
13031
13207
|
moduleResolutionCache: this.moduleResolutionCache,
|
|
13032
13208
|
currentModulePath: resolvedPath,
|
|
13209
|
+
isTypeScriptModule: isTypeScriptPath(resolvedPath),
|
|
13033
13210
|
abortSignal: this.abortSignal,
|
|
13034
13211
|
executionController: this.executionController
|
|
13035
13212
|
});
|
|
@@ -13044,7 +13221,7 @@ var Interpreter = class _Interpreter {
|
|
|
13044
13221
|
}
|
|
13045
13222
|
bindImportSpecifiers(node, env, modulePath, moduleExports) {
|
|
13046
13223
|
for (const specifier of node.specifiers) {
|
|
13047
|
-
if (specifier
|
|
13224
|
+
if (!this.isRuntimeImportSpecifier(specifier)) {
|
|
13048
13225
|
continue;
|
|
13049
13226
|
}
|
|
13050
13227
|
if (specifier.type === "ImportSpecifier") {
|
|
@@ -13067,6 +13244,20 @@ var Interpreter = class _Interpreter {
|
|
|
13067
13244
|
}
|
|
13068
13245
|
return void 0;
|
|
13069
13246
|
}
|
|
13247
|
+
isRuntimeImportSpecifier(specifier) {
|
|
13248
|
+
var _a;
|
|
13249
|
+
if (specifier.importKind === "type") {
|
|
13250
|
+
return false;
|
|
13251
|
+
}
|
|
13252
|
+
if (!this.isTypeScriptModule) {
|
|
13253
|
+
return true;
|
|
13254
|
+
}
|
|
13255
|
+
const localName = (_a = specifier.local) == null ? void 0 : _a.name;
|
|
13256
|
+
if (!localName || !this.runtimeIdentifierReferences) {
|
|
13257
|
+
return true;
|
|
13258
|
+
}
|
|
13259
|
+
return this.runtimeIdentifierReferences.has(localName);
|
|
13260
|
+
}
|
|
13070
13261
|
evaluateExportNamedDeclaration(node, env) {
|
|
13071
13262
|
if (node.exportKind === "type" || isTypeOnlyDeclaration(node.declaration)) {
|
|
13072
13263
|
return void 0;
|
|
@@ -14551,7 +14742,8 @@ async function execute(code, env = null, options = {}) {
|
|
|
14551
14742
|
moduleResolver: options.moduleResolver,
|
|
14552
14743
|
abortSignal: options.abortSignal,
|
|
14553
14744
|
executionController: controller,
|
|
14554
|
-
currentModulePath: options.sourcePath
|
|
14745
|
+
currentModulePath: options.sourcePath,
|
|
14746
|
+
isTypeScriptModule: options.typescript || options.tsx || isTypeScriptPath2(options.sourcePath)
|
|
14555
14747
|
});
|
|
14556
14748
|
const needsAsync = options.sourceType === "module" || containsModuleDeclarations(ast) || containsTopLevelAwait(ast) || controller != null;
|
|
14557
14749
|
if (needsAsync) {
|
package/dist/index.d.cts
CHANGED
|
@@ -12224,6 +12224,174 @@ function getPatternName(pattern) {
|
|
|
12224
12224
|
return pattern.name;
|
|
12225
12225
|
}
|
|
12226
12226
|
|
|
12227
|
+
function collectRuntimeIdentifierReferences(node) {
|
|
12228
|
+
const references = new Set();
|
|
12229
|
+
const skipKeys = new Set([
|
|
12230
|
+
'type',
|
|
12231
|
+
'start',
|
|
12232
|
+
'end',
|
|
12233
|
+
'loc',
|
|
12234
|
+
'range',
|
|
12235
|
+
'raw',
|
|
12236
|
+
'typeAnnotation',
|
|
12237
|
+
'returnType',
|
|
12238
|
+
'typeParameters',
|
|
12239
|
+
'typeArguments',
|
|
12240
|
+
'implements'
|
|
12241
|
+
]);
|
|
12242
|
+
|
|
12243
|
+
const visitPatternDefaults = (pattern) => {
|
|
12244
|
+
if (!pattern || typeof pattern !== 'object') return;
|
|
12245
|
+
if (pattern.type === 'AssignmentPattern') {
|
|
12246
|
+
visit(pattern.right);
|
|
12247
|
+
visitPatternDefaults(pattern.left);
|
|
12248
|
+
} else if (pattern.type === 'ObjectPattern') {
|
|
12249
|
+
for (const property of pattern.properties || []) {
|
|
12250
|
+
visitPatternDefaults(property.value || property.argument);
|
|
12251
|
+
}
|
|
12252
|
+
} else if (pattern.type === 'ArrayPattern') {
|
|
12253
|
+
for (const element of pattern.elements || []) {
|
|
12254
|
+
visitPatternDefaults(element);
|
|
12255
|
+
}
|
|
12256
|
+
} else if (pattern.type === 'RestElement') {
|
|
12257
|
+
visitPatternDefaults(pattern.argument);
|
|
12258
|
+
} else if (pattern.type === 'TSParameterProperty') {
|
|
12259
|
+
visitPatternDefaults(pattern.parameter);
|
|
12260
|
+
}
|
|
12261
|
+
};
|
|
12262
|
+
|
|
12263
|
+
const visitFunction = (fn) => {
|
|
12264
|
+
for (const param of fn.params || []) {
|
|
12265
|
+
visitPatternDefaults(param);
|
|
12266
|
+
}
|
|
12267
|
+
visit(fn.body);
|
|
12268
|
+
};
|
|
12269
|
+
|
|
12270
|
+
const visitJSXName = (jsxName) => {
|
|
12271
|
+
if (!jsxName || typeof jsxName !== 'object') return;
|
|
12272
|
+
if (jsxName.type === 'JSXIdentifier') {
|
|
12273
|
+
if (/^[A-Z]/.test(jsxName.name)) {
|
|
12274
|
+
references.add(jsxName.name);
|
|
12275
|
+
}
|
|
12276
|
+
} else if (jsxName.type === 'JSXMemberExpression') {
|
|
12277
|
+
visitJSXName(jsxName.object);
|
|
12278
|
+
} else if (jsxName.type === 'JSXNamespacedName') {
|
|
12279
|
+
visitJSXName(jsxName.namespace);
|
|
12280
|
+
}
|
|
12281
|
+
};
|
|
12282
|
+
|
|
12283
|
+
const visit = (current, parent = null, parentKey = null) => {
|
|
12284
|
+
if (!current || typeof current !== 'object') return;
|
|
12285
|
+
|
|
12286
|
+
if (Array.isArray(current)) {
|
|
12287
|
+
for (const item of current) visit(item, parent, parentKey);
|
|
12288
|
+
return;
|
|
12289
|
+
}
|
|
12290
|
+
|
|
12291
|
+
if (current.type?.startsWith('TS')) {
|
|
12292
|
+
if (isTypeWrapperExpression(current)) {
|
|
12293
|
+
visit(getTypeWrapperInnerExpression(current), current, 'expression');
|
|
12294
|
+
} else if (current.type === 'TSEnumDeclaration') {
|
|
12295
|
+
for (const member of current.members || []) {
|
|
12296
|
+
visit(member.initializer);
|
|
12297
|
+
}
|
|
12298
|
+
}
|
|
12299
|
+
return;
|
|
12300
|
+
}
|
|
12301
|
+
|
|
12302
|
+
switch (current.type) {
|
|
12303
|
+
case 'Identifier':
|
|
12304
|
+
references.add(current.name);
|
|
12305
|
+
return;
|
|
12306
|
+
case 'ImportDeclaration':
|
|
12307
|
+
return;
|
|
12308
|
+
case 'ExportNamedDeclaration':
|
|
12309
|
+
if (current.declaration) {
|
|
12310
|
+
visit(current.declaration, current, 'declaration');
|
|
12311
|
+
} else if (current.exportKind !== 'type') {
|
|
12312
|
+
for (const specifier of current.specifiers || []) {
|
|
12313
|
+
if (specifier.exportKind !== 'type' && specifier.local?.name) {
|
|
12314
|
+
references.add(specifier.local.name);
|
|
12315
|
+
}
|
|
12316
|
+
}
|
|
12317
|
+
}
|
|
12318
|
+
return;
|
|
12319
|
+
case 'ExportDefaultDeclaration':
|
|
12320
|
+
visit(current.declaration, current, 'declaration');
|
|
12321
|
+
return;
|
|
12322
|
+
case 'VariableDeclarator':
|
|
12323
|
+
visitPatternDefaults(current.id);
|
|
12324
|
+
visit(current.init, current, 'init');
|
|
12325
|
+
return;
|
|
12326
|
+
case 'FunctionDeclaration':
|
|
12327
|
+
visitFunction(current);
|
|
12328
|
+
return;
|
|
12329
|
+
case 'FunctionExpression':
|
|
12330
|
+
case 'ArrowFunctionExpression':
|
|
12331
|
+
visitFunction(current);
|
|
12332
|
+
return;
|
|
12333
|
+
case 'ClassDeclaration':
|
|
12334
|
+
case 'ClassExpression':
|
|
12335
|
+
visit(current.superClass, current, 'superClass');
|
|
12336
|
+
visit(current.body, current, 'body');
|
|
12337
|
+
return;
|
|
12338
|
+
case 'MemberExpression':
|
|
12339
|
+
case 'OptionalMemberExpression':
|
|
12340
|
+
visit(current.object, current, 'object');
|
|
12341
|
+
if (current.computed) {
|
|
12342
|
+
visit(current.property, current, 'property');
|
|
12343
|
+
}
|
|
12344
|
+
return;
|
|
12345
|
+
case 'Property':
|
|
12346
|
+
if (current.computed) {
|
|
12347
|
+
visit(current.key, current, 'key');
|
|
12348
|
+
}
|
|
12349
|
+
visit(current.value, current, 'value');
|
|
12350
|
+
return;
|
|
12351
|
+
case 'MethodDefinition':
|
|
12352
|
+
case 'PropertyDefinition':
|
|
12353
|
+
if (current.computed) {
|
|
12354
|
+
visit(current.key, current, 'key');
|
|
12355
|
+
}
|
|
12356
|
+
if (!current.declare && !current.abstract) {
|
|
12357
|
+
visit(current.value, current, 'value');
|
|
12358
|
+
}
|
|
12359
|
+
return;
|
|
12360
|
+
case 'AssignmentPattern':
|
|
12361
|
+
visit(current.right, current, 'right');
|
|
12362
|
+
return;
|
|
12363
|
+
case 'RestElement':
|
|
12364
|
+
return;
|
|
12365
|
+
case 'ObjectPattern':
|
|
12366
|
+
case 'ArrayPattern':
|
|
12367
|
+
visitPatternDefaults(current);
|
|
12368
|
+
return;
|
|
12369
|
+
case 'JSXElement':
|
|
12370
|
+
visitJSXName(current.openingElement?.name);
|
|
12371
|
+
for (const child of current.children || []) {
|
|
12372
|
+
visit(child);
|
|
12373
|
+
}
|
|
12374
|
+
return;
|
|
12375
|
+
case 'JSXFragment':
|
|
12376
|
+
for (const child of current.children || []) {
|
|
12377
|
+
visit(child);
|
|
12378
|
+
}
|
|
12379
|
+
return;
|
|
12380
|
+
case 'JSXExpressionContainer':
|
|
12381
|
+
visit(current.expression, current, 'expression');
|
|
12382
|
+
return;
|
|
12383
|
+
}
|
|
12384
|
+
|
|
12385
|
+
for (const [key, value] of Object.entries(current)) {
|
|
12386
|
+
if (skipKeys.has(key)) continue;
|
|
12387
|
+
visit(value, current, key);
|
|
12388
|
+
}
|
|
12389
|
+
};
|
|
12390
|
+
|
|
12391
|
+
visit(node);
|
|
12392
|
+
return references;
|
|
12393
|
+
}
|
|
12394
|
+
|
|
12227
12395
|
class Interpreter {
|
|
12228
12396
|
constructor(globalEnv, options = {}) {
|
|
12229
12397
|
this.globalEnv = globalEnv;
|
|
@@ -12232,6 +12400,8 @@ class Interpreter {
|
|
|
12232
12400
|
this.moduleResolutionCache = options.moduleResolutionCache || new Map();
|
|
12233
12401
|
this.moduleExports = {}; // Track exports in current module
|
|
12234
12402
|
this.currentModulePath = options.currentModulePath;
|
|
12403
|
+
this.isTypeScriptModule = options.isTypeScriptModule || false;
|
|
12404
|
+
this.runtimeIdentifierReferences = null;
|
|
12235
12405
|
this.abortSignal = options.abortSignal;
|
|
12236
12406
|
this.executionController = options.executionController;
|
|
12237
12407
|
}
|
|
@@ -12454,15 +12624,21 @@ class Interpreter {
|
|
|
12454
12624
|
|
|
12455
12625
|
// For Program nodes (evaluate all statements async)
|
|
12456
12626
|
if (node.type === 'Program') {
|
|
12627
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
12628
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
12457
12629
|
let result = undefined;
|
|
12458
|
-
|
|
12459
|
-
|
|
12460
|
-
|
|
12461
|
-
|
|
12462
|
-
|
|
12630
|
+
try {
|
|
12631
|
+
for (const statement of node.body) {
|
|
12632
|
+
result = await this.evaluateAsync(statement, env);
|
|
12633
|
+
// Handle top-level return and throw
|
|
12634
|
+
if (result instanceof ReturnValue || result instanceof ThrowSignal) {
|
|
12635
|
+
return result;
|
|
12636
|
+
}
|
|
12463
12637
|
}
|
|
12638
|
+
return result;
|
|
12639
|
+
} finally {
|
|
12640
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
12464
12641
|
}
|
|
12465
|
-
return result;
|
|
12466
12642
|
}
|
|
12467
12643
|
|
|
12468
12644
|
// For import declarations (always async)
|
|
@@ -13265,27 +13441,33 @@ class Interpreter {
|
|
|
13265
13441
|
}
|
|
13266
13442
|
|
|
13267
13443
|
evaluateProgram(node, env) {
|
|
13444
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
13445
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
13268
13446
|
let result = undefined;
|
|
13269
|
-
|
|
13270
|
-
|
|
13271
|
-
|
|
13272
|
-
|
|
13273
|
-
|
|
13274
|
-
|
|
13275
|
-
|
|
13276
|
-
|
|
13277
|
-
|
|
13278
|
-
|
|
13447
|
+
try {
|
|
13448
|
+
for (let i = 0; i < node.body.length; i++) {
|
|
13449
|
+
const statement = node.body[i];
|
|
13450
|
+
const isLast = i === node.body.length - 1;
|
|
13451
|
+
|
|
13452
|
+
// Special case: Last statement is a BlockStatement that looks like object literal
|
|
13453
|
+
// Handle both shorthand { x, y } and full syntax { key: value, key2: value2 }
|
|
13454
|
+
if (isLast && statement.type === 'BlockStatement') {
|
|
13455
|
+
const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
|
|
13456
|
+
if (objLiteral !== null) {
|
|
13457
|
+
return objLiteral;
|
|
13458
|
+
}
|
|
13279
13459
|
}
|
|
13280
|
-
}
|
|
13281
13460
|
|
|
13282
|
-
|
|
13283
|
-
|
|
13284
|
-
|
|
13461
|
+
const statementResult = this.evaluate(statement, env);
|
|
13462
|
+
if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
|
|
13463
|
+
return statementResult;
|
|
13464
|
+
}
|
|
13465
|
+
result = statementResult;
|
|
13285
13466
|
}
|
|
13286
|
-
result
|
|
13467
|
+
return result;
|
|
13468
|
+
} finally {
|
|
13469
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
13287
13470
|
}
|
|
13288
|
-
return result;
|
|
13289
13471
|
}
|
|
13290
13472
|
|
|
13291
13473
|
// Try to convert a BlockStatement to an object literal
|
|
@@ -14127,6 +14309,12 @@ class Interpreter {
|
|
|
14127
14309
|
return undefined;
|
|
14128
14310
|
}
|
|
14129
14311
|
|
|
14312
|
+
if (this.isTypeScriptModule &&
|
|
14313
|
+
node.specifiers.length > 0 &&
|
|
14314
|
+
node.specifiers.every(specifier => !this.isRuntimeImportSpecifier(specifier))) {
|
|
14315
|
+
return undefined;
|
|
14316
|
+
}
|
|
14317
|
+
|
|
14130
14318
|
// Check if module resolver is configured
|
|
14131
14319
|
if (!this.moduleResolver) {
|
|
14132
14320
|
throw new Error('Module resolver not configured - cannot import modules');
|
|
@@ -14179,6 +14367,7 @@ class Interpreter {
|
|
|
14179
14367
|
moduleResolver: this.moduleResolver,
|
|
14180
14368
|
moduleResolutionCache: this.moduleResolutionCache,
|
|
14181
14369
|
currentModulePath: resolvedPath,
|
|
14370
|
+
isTypeScriptModule: isTypeScriptPath$1(resolvedPath),
|
|
14182
14371
|
abortSignal: this.abortSignal,
|
|
14183
14372
|
executionController: this.executionController
|
|
14184
14373
|
});
|
|
@@ -14200,7 +14389,7 @@ class Interpreter {
|
|
|
14200
14389
|
bindImportSpecifiers(node, env, modulePath, moduleExports) {
|
|
14201
14390
|
// Import specified bindings into current environment
|
|
14202
14391
|
for (const specifier of node.specifiers) {
|
|
14203
|
-
if (specifier
|
|
14392
|
+
if (!this.isRuntimeImportSpecifier(specifier)) {
|
|
14204
14393
|
continue;
|
|
14205
14394
|
}
|
|
14206
14395
|
|
|
@@ -14233,6 +14422,23 @@ class Interpreter {
|
|
|
14233
14422
|
return undefined;
|
|
14234
14423
|
}
|
|
14235
14424
|
|
|
14425
|
+
isRuntimeImportSpecifier(specifier) {
|
|
14426
|
+
if (specifier.importKind === 'type') {
|
|
14427
|
+
return false;
|
|
14428
|
+
}
|
|
14429
|
+
|
|
14430
|
+
if (!this.isTypeScriptModule) {
|
|
14431
|
+
return true;
|
|
14432
|
+
}
|
|
14433
|
+
|
|
14434
|
+
const localName = specifier.local?.name;
|
|
14435
|
+
if (!localName || !this.runtimeIdentifierReferences) {
|
|
14436
|
+
return true;
|
|
14437
|
+
}
|
|
14438
|
+
|
|
14439
|
+
return this.runtimeIdentifierReferences.has(localName);
|
|
14440
|
+
}
|
|
14441
|
+
|
|
14236
14442
|
evaluateExportNamedDeclaration(node, env) {
|
|
14237
14443
|
if (node.exportKind === 'type' || isTypeOnlyDeclaration(node.declaration)) {
|
|
14238
14444
|
return undefined;
|
|
@@ -16175,7 +16381,8 @@ async function execute(code, env = null, options = {}) {
|
|
|
16175
16381
|
moduleResolver: options.moduleResolver,
|
|
16176
16382
|
abortSignal: options.abortSignal,
|
|
16177
16383
|
executionController: controller,
|
|
16178
|
-
currentModulePath: options.sourcePath
|
|
16384
|
+
currentModulePath: options.sourcePath,
|
|
16385
|
+
isTypeScriptModule: options.typescript || options.tsx || isTypeScriptPath(options.sourcePath)
|
|
16179
16386
|
});
|
|
16180
16387
|
|
|
16181
16388
|
// Use async evaluation if:
|