jslike 1.8.0 → 1.8.2
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/errors/enhanced-error.js +37 -11
- package/dist/esm/index.js +2 -1
- package/dist/esm/interpreter/interpreter.js +229 -23
- package/dist/index.cjs +242 -29
- package/dist/index.d.cts +268 -35
- package/dist/index.d.ts +268 -35
- package/dist/index.js +242 -29
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11391,19 +11391,40 @@ function findSimilar(target, candidates, maxDistance = 3) {
|
|
|
11391
11391
|
function getAvailableMethods(obj) {
|
|
11392
11392
|
if (obj === null || obj === void 0) return [];
|
|
11393
11393
|
const methods = /* @__PURE__ */ new Set();
|
|
11394
|
-
|
|
11395
|
-
|
|
11396
|
-
|
|
11394
|
+
const addSafeMethods = (target, includeConstructor = true) => {
|
|
11395
|
+
let names;
|
|
11396
|
+
try {
|
|
11397
|
+
names = Object.getOwnPropertyNames(target);
|
|
11398
|
+
} catch {
|
|
11399
|
+
return;
|
|
11397
11400
|
}
|
|
11398
|
-
|
|
11399
|
-
|
|
11400
|
-
|
|
11401
|
-
|
|
11402
|
-
|
|
11401
|
+
names.forEach((name) => {
|
|
11402
|
+
if (!includeConstructor && name === "constructor") return;
|
|
11403
|
+
let descriptor;
|
|
11404
|
+
try {
|
|
11405
|
+
descriptor = Object.getOwnPropertyDescriptor(target, name);
|
|
11406
|
+
} catch {
|
|
11407
|
+
return;
|
|
11408
|
+
}
|
|
11409
|
+
if (descriptor && "value" in descriptor && typeof descriptor.value === "function") {
|
|
11403
11410
|
methods.add(name);
|
|
11404
11411
|
}
|
|
11405
11412
|
});
|
|
11406
|
-
|
|
11413
|
+
};
|
|
11414
|
+
addSafeMethods(obj);
|
|
11415
|
+
let proto;
|
|
11416
|
+
try {
|
|
11417
|
+
proto = Object.getPrototypeOf(obj);
|
|
11418
|
+
} catch {
|
|
11419
|
+
proto = null;
|
|
11420
|
+
}
|
|
11421
|
+
while (proto && proto !== Object.prototype) {
|
|
11422
|
+
addSafeMethods(proto, false);
|
|
11423
|
+
try {
|
|
11424
|
+
proto = Object.getPrototypeOf(proto);
|
|
11425
|
+
} catch {
|
|
11426
|
+
proto = null;
|
|
11427
|
+
}
|
|
11407
11428
|
}
|
|
11408
11429
|
return Array.from(methods).sort();
|
|
11409
11430
|
}
|
|
@@ -11493,6 +11514,165 @@ function getPatternName(pattern) {
|
|
|
11493
11514
|
if (pattern.type === "TSParameterProperty") return getPatternName(pattern.parameter);
|
|
11494
11515
|
return pattern.name;
|
|
11495
11516
|
}
|
|
11517
|
+
function collectRuntimeIdentifierReferences(node) {
|
|
11518
|
+
const references = /* @__PURE__ */ new Set();
|
|
11519
|
+
const skipKeys = /* @__PURE__ */ new Set([
|
|
11520
|
+
"type",
|
|
11521
|
+
"start",
|
|
11522
|
+
"end",
|
|
11523
|
+
"loc",
|
|
11524
|
+
"range",
|
|
11525
|
+
"raw",
|
|
11526
|
+
"typeAnnotation",
|
|
11527
|
+
"returnType",
|
|
11528
|
+
"typeParameters",
|
|
11529
|
+
"typeArguments",
|
|
11530
|
+
"implements"
|
|
11531
|
+
]);
|
|
11532
|
+
const visitPatternDefaults = (pattern) => {
|
|
11533
|
+
if (!pattern || typeof pattern !== "object") return;
|
|
11534
|
+
if (pattern.type === "AssignmentPattern") {
|
|
11535
|
+
visit(pattern.right);
|
|
11536
|
+
visitPatternDefaults(pattern.left);
|
|
11537
|
+
} else if (pattern.type === "ObjectPattern") {
|
|
11538
|
+
for (const property of pattern.properties || []) {
|
|
11539
|
+
visitPatternDefaults(property.value || property.argument);
|
|
11540
|
+
}
|
|
11541
|
+
} else if (pattern.type === "ArrayPattern") {
|
|
11542
|
+
for (const element of pattern.elements || []) {
|
|
11543
|
+
visitPatternDefaults(element);
|
|
11544
|
+
}
|
|
11545
|
+
} else if (pattern.type === "RestElement") {
|
|
11546
|
+
visitPatternDefaults(pattern.argument);
|
|
11547
|
+
} else if (pattern.type === "TSParameterProperty") {
|
|
11548
|
+
visitPatternDefaults(pattern.parameter);
|
|
11549
|
+
}
|
|
11550
|
+
};
|
|
11551
|
+
const visitFunction = (fn) => {
|
|
11552
|
+
for (const param of fn.params || []) {
|
|
11553
|
+
visitPatternDefaults(param);
|
|
11554
|
+
}
|
|
11555
|
+
visit(fn.body);
|
|
11556
|
+
};
|
|
11557
|
+
const visitJSXName = (jsxName) => {
|
|
11558
|
+
if (!jsxName || typeof jsxName !== "object") return;
|
|
11559
|
+
if (jsxName.type === "JSXIdentifier") {
|
|
11560
|
+
if (/^[A-Z]/.test(jsxName.name)) {
|
|
11561
|
+
references.add(jsxName.name);
|
|
11562
|
+
}
|
|
11563
|
+
} else if (jsxName.type === "JSXMemberExpression") {
|
|
11564
|
+
visitJSXName(jsxName.object);
|
|
11565
|
+
} else if (jsxName.type === "JSXNamespacedName") {
|
|
11566
|
+
visitJSXName(jsxName.namespace);
|
|
11567
|
+
}
|
|
11568
|
+
};
|
|
11569
|
+
const visit = (current2, parent = null, parentKey = null) => {
|
|
11570
|
+
var _a, _b, _c;
|
|
11571
|
+
if (!current2 || typeof current2 !== "object") return;
|
|
11572
|
+
if (Array.isArray(current2)) {
|
|
11573
|
+
for (const item of current2) visit(item, parent, parentKey);
|
|
11574
|
+
return;
|
|
11575
|
+
}
|
|
11576
|
+
if ((_a = current2.type) == null ? void 0 : _a.startsWith("TS")) {
|
|
11577
|
+
if (isTypeWrapperExpression(current2)) {
|
|
11578
|
+
visit(getTypeWrapperInnerExpression(current2), current2, "expression");
|
|
11579
|
+
} else if (current2.type === "TSEnumDeclaration") {
|
|
11580
|
+
for (const member of current2.members || []) {
|
|
11581
|
+
visit(member.initializer);
|
|
11582
|
+
}
|
|
11583
|
+
}
|
|
11584
|
+
return;
|
|
11585
|
+
}
|
|
11586
|
+
switch (current2.type) {
|
|
11587
|
+
case "Identifier":
|
|
11588
|
+
references.add(current2.name);
|
|
11589
|
+
return;
|
|
11590
|
+
case "ImportDeclaration":
|
|
11591
|
+
return;
|
|
11592
|
+
case "ExportNamedDeclaration":
|
|
11593
|
+
if (current2.declaration) {
|
|
11594
|
+
visit(current2.declaration, current2, "declaration");
|
|
11595
|
+
} else if (current2.exportKind !== "type") {
|
|
11596
|
+
for (const specifier of current2.specifiers || []) {
|
|
11597
|
+
if (specifier.exportKind !== "type" && ((_b = specifier.local) == null ? void 0 : _b.name)) {
|
|
11598
|
+
references.add(specifier.local.name);
|
|
11599
|
+
}
|
|
11600
|
+
}
|
|
11601
|
+
}
|
|
11602
|
+
return;
|
|
11603
|
+
case "ExportDefaultDeclaration":
|
|
11604
|
+
visit(current2.declaration, current2, "declaration");
|
|
11605
|
+
return;
|
|
11606
|
+
case "VariableDeclarator":
|
|
11607
|
+
visitPatternDefaults(current2.id);
|
|
11608
|
+
visit(current2.init, current2, "init");
|
|
11609
|
+
return;
|
|
11610
|
+
case "FunctionDeclaration":
|
|
11611
|
+
visitFunction(current2);
|
|
11612
|
+
return;
|
|
11613
|
+
case "FunctionExpression":
|
|
11614
|
+
case "ArrowFunctionExpression":
|
|
11615
|
+
visitFunction(current2);
|
|
11616
|
+
return;
|
|
11617
|
+
case "ClassDeclaration":
|
|
11618
|
+
case "ClassExpression":
|
|
11619
|
+
visit(current2.superClass, current2, "superClass");
|
|
11620
|
+
visit(current2.body, current2, "body");
|
|
11621
|
+
return;
|
|
11622
|
+
case "MemberExpression":
|
|
11623
|
+
case "OptionalMemberExpression":
|
|
11624
|
+
visit(current2.object, current2, "object");
|
|
11625
|
+
if (current2.computed) {
|
|
11626
|
+
visit(current2.property, current2, "property");
|
|
11627
|
+
}
|
|
11628
|
+
return;
|
|
11629
|
+
case "Property":
|
|
11630
|
+
if (current2.computed) {
|
|
11631
|
+
visit(current2.key, current2, "key");
|
|
11632
|
+
}
|
|
11633
|
+
visit(current2.value, current2, "value");
|
|
11634
|
+
return;
|
|
11635
|
+
case "MethodDefinition":
|
|
11636
|
+
case "PropertyDefinition":
|
|
11637
|
+
if (current2.computed) {
|
|
11638
|
+
visit(current2.key, current2, "key");
|
|
11639
|
+
}
|
|
11640
|
+
if (!current2.declare && !current2.abstract) {
|
|
11641
|
+
visit(current2.value, current2, "value");
|
|
11642
|
+
}
|
|
11643
|
+
return;
|
|
11644
|
+
case "AssignmentPattern":
|
|
11645
|
+
visit(current2.right, current2, "right");
|
|
11646
|
+
return;
|
|
11647
|
+
case "RestElement":
|
|
11648
|
+
return;
|
|
11649
|
+
case "ObjectPattern":
|
|
11650
|
+
case "ArrayPattern":
|
|
11651
|
+
visitPatternDefaults(current2);
|
|
11652
|
+
return;
|
|
11653
|
+
case "JSXElement":
|
|
11654
|
+
visitJSXName((_c = current2.openingElement) == null ? void 0 : _c.name);
|
|
11655
|
+
for (const child of current2.children || []) {
|
|
11656
|
+
visit(child);
|
|
11657
|
+
}
|
|
11658
|
+
return;
|
|
11659
|
+
case "JSXFragment":
|
|
11660
|
+
for (const child of current2.children || []) {
|
|
11661
|
+
visit(child);
|
|
11662
|
+
}
|
|
11663
|
+
return;
|
|
11664
|
+
case "JSXExpressionContainer":
|
|
11665
|
+
visit(current2.expression, current2, "expression");
|
|
11666
|
+
return;
|
|
11667
|
+
}
|
|
11668
|
+
for (const [key, value] of Object.entries(current2)) {
|
|
11669
|
+
if (skipKeys.has(key)) continue;
|
|
11670
|
+
visit(value, current2, key);
|
|
11671
|
+
}
|
|
11672
|
+
};
|
|
11673
|
+
visit(node);
|
|
11674
|
+
return references;
|
|
11675
|
+
}
|
|
11496
11676
|
var Interpreter = class _Interpreter {
|
|
11497
11677
|
constructor(globalEnv, options = {}) {
|
|
11498
11678
|
this.globalEnv = globalEnv;
|
|
@@ -11501,6 +11681,8 @@ var Interpreter = class _Interpreter {
|
|
|
11501
11681
|
this.moduleResolutionCache = options.moduleResolutionCache || /* @__PURE__ */ new Map();
|
|
11502
11682
|
this.moduleExports = {};
|
|
11503
11683
|
this.currentModulePath = options.currentModulePath;
|
|
11684
|
+
this.isTypeScriptModule = options.isTypeScriptModule || false;
|
|
11685
|
+
this.runtimeIdentifierReferences = null;
|
|
11504
11686
|
this.abortSignal = options.abortSignal;
|
|
11505
11687
|
this.executionController = options.executionController;
|
|
11506
11688
|
}
|
|
@@ -11666,14 +11848,20 @@ var Interpreter = class _Interpreter {
|
|
|
11666
11848
|
return void 0;
|
|
11667
11849
|
}
|
|
11668
11850
|
if (node.type === "Program") {
|
|
11851
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
11852
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
11669
11853
|
let result = void 0;
|
|
11670
|
-
|
|
11671
|
-
|
|
11672
|
-
|
|
11673
|
-
|
|
11854
|
+
try {
|
|
11855
|
+
for (const statement of node.body) {
|
|
11856
|
+
result = await this.evaluateAsync(statement, env);
|
|
11857
|
+
if (result instanceof ReturnValue || result instanceof ThrowSignal) {
|
|
11858
|
+
return result;
|
|
11859
|
+
}
|
|
11674
11860
|
}
|
|
11861
|
+
return result;
|
|
11862
|
+
} finally {
|
|
11863
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
11675
11864
|
}
|
|
11676
|
-
return result;
|
|
11677
11865
|
}
|
|
11678
11866
|
if (node.type === "ImportDeclaration") {
|
|
11679
11867
|
return await this.evaluateImportDeclaration(node, env);
|
|
@@ -12297,23 +12485,29 @@ var Interpreter = class _Interpreter {
|
|
|
12297
12485
|
}
|
|
12298
12486
|
}
|
|
12299
12487
|
evaluateProgram(node, env) {
|
|
12488
|
+
const previousReferences = this.runtimeIdentifierReferences;
|
|
12489
|
+
this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
|
|
12300
12490
|
let result = void 0;
|
|
12301
|
-
|
|
12302
|
-
|
|
12303
|
-
|
|
12304
|
-
|
|
12305
|
-
|
|
12306
|
-
|
|
12307
|
-
|
|
12491
|
+
try {
|
|
12492
|
+
for (let i = 0; i < node.body.length; i++) {
|
|
12493
|
+
const statement = node.body[i];
|
|
12494
|
+
const isLast = i === node.body.length - 1;
|
|
12495
|
+
if (isLast && statement.type === "BlockStatement") {
|
|
12496
|
+
const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
|
|
12497
|
+
if (objLiteral !== null) {
|
|
12498
|
+
return objLiteral;
|
|
12499
|
+
}
|
|
12308
12500
|
}
|
|
12501
|
+
const statementResult = this.evaluate(statement, env);
|
|
12502
|
+
if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
|
|
12503
|
+
return statementResult;
|
|
12504
|
+
}
|
|
12505
|
+
result = statementResult;
|
|
12309
12506
|
}
|
|
12310
|
-
|
|
12311
|
-
|
|
12312
|
-
|
|
12313
|
-
}
|
|
12314
|
-
result = statementResult;
|
|
12507
|
+
return result;
|
|
12508
|
+
} finally {
|
|
12509
|
+
this.runtimeIdentifierReferences = previousReferences;
|
|
12315
12510
|
}
|
|
12316
|
-
return result;
|
|
12317
12511
|
}
|
|
12318
12512
|
// Try to convert a BlockStatement to an object literal
|
|
12319
12513
|
// Returns null if the block doesn't look like an object literal
|
|
@@ -12961,6 +13155,9 @@ var Interpreter = class _Interpreter {
|
|
|
12961
13155
|
if (node.importKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.importKind === "type")) {
|
|
12962
13156
|
return void 0;
|
|
12963
13157
|
}
|
|
13158
|
+
if (this.isTypeScriptModule && node.specifiers.length > 0 && node.specifiers.every((specifier) => !this.isRuntimeImportSpecifier(specifier))) {
|
|
13159
|
+
return void 0;
|
|
13160
|
+
}
|
|
12964
13161
|
if (!this.moduleResolver) {
|
|
12965
13162
|
throw new Error("Module resolver not configured - cannot import modules");
|
|
12966
13163
|
}
|
|
@@ -12996,6 +13193,7 @@ var Interpreter = class _Interpreter {
|
|
|
12996
13193
|
moduleResolver: this.moduleResolver,
|
|
12997
13194
|
moduleResolutionCache: this.moduleResolutionCache,
|
|
12998
13195
|
currentModulePath: resolvedPath,
|
|
13196
|
+
isTypeScriptModule: isTypeScriptPath(resolvedPath),
|
|
12999
13197
|
abortSignal: this.abortSignal,
|
|
13000
13198
|
executionController: this.executionController
|
|
13001
13199
|
});
|
|
@@ -13010,7 +13208,7 @@ var Interpreter = class _Interpreter {
|
|
|
13010
13208
|
}
|
|
13011
13209
|
bindImportSpecifiers(node, env, modulePath, moduleExports) {
|
|
13012
13210
|
for (const specifier of node.specifiers) {
|
|
13013
|
-
if (specifier
|
|
13211
|
+
if (!this.isRuntimeImportSpecifier(specifier)) {
|
|
13014
13212
|
continue;
|
|
13015
13213
|
}
|
|
13016
13214
|
if (specifier.type === "ImportSpecifier") {
|
|
@@ -13033,6 +13231,20 @@ var Interpreter = class _Interpreter {
|
|
|
13033
13231
|
}
|
|
13034
13232
|
return void 0;
|
|
13035
13233
|
}
|
|
13234
|
+
isRuntimeImportSpecifier(specifier) {
|
|
13235
|
+
var _a;
|
|
13236
|
+
if (specifier.importKind === "type") {
|
|
13237
|
+
return false;
|
|
13238
|
+
}
|
|
13239
|
+
if (!this.isTypeScriptModule) {
|
|
13240
|
+
return true;
|
|
13241
|
+
}
|
|
13242
|
+
const localName = (_a = specifier.local) == null ? void 0 : _a.name;
|
|
13243
|
+
if (!localName || !this.runtimeIdentifierReferences) {
|
|
13244
|
+
return true;
|
|
13245
|
+
}
|
|
13246
|
+
return this.runtimeIdentifierReferences.has(localName);
|
|
13247
|
+
}
|
|
13036
13248
|
evaluateExportNamedDeclaration(node, env) {
|
|
13037
13249
|
if (node.exportKind === "type" || isTypeOnlyDeclaration(node.declaration)) {
|
|
13038
13250
|
return void 0;
|
|
@@ -14517,7 +14729,8 @@ async function execute(code, env = null, options = {}) {
|
|
|
14517
14729
|
moduleResolver: options.moduleResolver,
|
|
14518
14730
|
abortSignal: options.abortSignal,
|
|
14519
14731
|
executionController: controller,
|
|
14520
|
-
currentModulePath: options.sourcePath
|
|
14732
|
+
currentModulePath: options.sourcePath,
|
|
14733
|
+
isTypeScriptModule: options.typescript || options.tsx || isTypeScriptPath2(options.sourcePath)
|
|
14521
14734
|
});
|
|
14522
14735
|
const needsAsync = options.sourceType === "module" || containsModuleDeclarations(ast) || containsTopLevelAwait(ast) || controller != null;
|
|
14523
14736
|
if (needsAsync) {
|