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/dist/index.cjs CHANGED
@@ -11425,19 +11425,40 @@ function findSimilar(target, candidates, maxDistance = 3) {
11425
11425
  function getAvailableMethods(obj) {
11426
11426
  if (obj === null || obj === void 0) return [];
11427
11427
  const methods = /* @__PURE__ */ new Set();
11428
- Object.getOwnPropertyNames(obj).forEach((name) => {
11429
- if (typeof obj[name] === "function") {
11430
- methods.add(name);
11428
+ const addSafeMethods = (target, includeConstructor = true) => {
11429
+ let names;
11430
+ try {
11431
+ names = Object.getOwnPropertyNames(target);
11432
+ } catch {
11433
+ return;
11431
11434
  }
11432
- });
11433
- let proto = Object.getPrototypeOf(obj);
11434
- while (proto && proto !== Object.prototype) {
11435
- Object.getOwnPropertyNames(proto).forEach((name) => {
11436
- if (typeof proto[name] === "function" && name !== "constructor") {
11435
+ names.forEach((name) => {
11436
+ if (!includeConstructor && name === "constructor") return;
11437
+ let descriptor;
11438
+ try {
11439
+ descriptor = Object.getOwnPropertyDescriptor(target, name);
11440
+ } catch {
11441
+ return;
11442
+ }
11443
+ if (descriptor && "value" in descriptor && typeof descriptor.value === "function") {
11437
11444
  methods.add(name);
11438
11445
  }
11439
11446
  });
11440
- proto = Object.getPrototypeOf(proto);
11447
+ };
11448
+ addSafeMethods(obj);
11449
+ let proto;
11450
+ try {
11451
+ proto = Object.getPrototypeOf(obj);
11452
+ } catch {
11453
+ proto = null;
11454
+ }
11455
+ while (proto && proto !== Object.prototype) {
11456
+ addSafeMethods(proto, false);
11457
+ try {
11458
+ proto = Object.getPrototypeOf(proto);
11459
+ } catch {
11460
+ proto = null;
11461
+ }
11441
11462
  }
11442
11463
  return Array.from(methods).sort();
11443
11464
  }
@@ -11527,6 +11548,165 @@ function getPatternName(pattern) {
11527
11548
  if (pattern.type === "TSParameterProperty") return getPatternName(pattern.parameter);
11528
11549
  return pattern.name;
11529
11550
  }
11551
+ function collectRuntimeIdentifierReferences(node) {
11552
+ const references = /* @__PURE__ */ new Set();
11553
+ const skipKeys = /* @__PURE__ */ new Set([
11554
+ "type",
11555
+ "start",
11556
+ "end",
11557
+ "loc",
11558
+ "range",
11559
+ "raw",
11560
+ "typeAnnotation",
11561
+ "returnType",
11562
+ "typeParameters",
11563
+ "typeArguments",
11564
+ "implements"
11565
+ ]);
11566
+ const visitPatternDefaults = (pattern) => {
11567
+ if (!pattern || typeof pattern !== "object") return;
11568
+ if (pattern.type === "AssignmentPattern") {
11569
+ visit(pattern.right);
11570
+ visitPatternDefaults(pattern.left);
11571
+ } else if (pattern.type === "ObjectPattern") {
11572
+ for (const property of pattern.properties || []) {
11573
+ visitPatternDefaults(property.value || property.argument);
11574
+ }
11575
+ } else if (pattern.type === "ArrayPattern") {
11576
+ for (const element of pattern.elements || []) {
11577
+ visitPatternDefaults(element);
11578
+ }
11579
+ } else if (pattern.type === "RestElement") {
11580
+ visitPatternDefaults(pattern.argument);
11581
+ } else if (pattern.type === "TSParameterProperty") {
11582
+ visitPatternDefaults(pattern.parameter);
11583
+ }
11584
+ };
11585
+ const visitFunction = (fn) => {
11586
+ for (const param of fn.params || []) {
11587
+ visitPatternDefaults(param);
11588
+ }
11589
+ visit(fn.body);
11590
+ };
11591
+ const visitJSXName = (jsxName) => {
11592
+ if (!jsxName || typeof jsxName !== "object") return;
11593
+ if (jsxName.type === "JSXIdentifier") {
11594
+ if (/^[A-Z]/.test(jsxName.name)) {
11595
+ references.add(jsxName.name);
11596
+ }
11597
+ } else if (jsxName.type === "JSXMemberExpression") {
11598
+ visitJSXName(jsxName.object);
11599
+ } else if (jsxName.type === "JSXNamespacedName") {
11600
+ visitJSXName(jsxName.namespace);
11601
+ }
11602
+ };
11603
+ const visit = (current2, parent = null, parentKey = null) => {
11604
+ var _a, _b, _c;
11605
+ if (!current2 || typeof current2 !== "object") return;
11606
+ if (Array.isArray(current2)) {
11607
+ for (const item of current2) visit(item, parent, parentKey);
11608
+ return;
11609
+ }
11610
+ if ((_a = current2.type) == null ? void 0 : _a.startsWith("TS")) {
11611
+ if (isTypeWrapperExpression(current2)) {
11612
+ visit(getTypeWrapperInnerExpression(current2), current2, "expression");
11613
+ } else if (current2.type === "TSEnumDeclaration") {
11614
+ for (const member of current2.members || []) {
11615
+ visit(member.initializer);
11616
+ }
11617
+ }
11618
+ return;
11619
+ }
11620
+ switch (current2.type) {
11621
+ case "Identifier":
11622
+ references.add(current2.name);
11623
+ return;
11624
+ case "ImportDeclaration":
11625
+ return;
11626
+ case "ExportNamedDeclaration":
11627
+ if (current2.declaration) {
11628
+ visit(current2.declaration, current2, "declaration");
11629
+ } else if (current2.exportKind !== "type") {
11630
+ for (const specifier of current2.specifiers || []) {
11631
+ if (specifier.exportKind !== "type" && ((_b = specifier.local) == null ? void 0 : _b.name)) {
11632
+ references.add(specifier.local.name);
11633
+ }
11634
+ }
11635
+ }
11636
+ return;
11637
+ case "ExportDefaultDeclaration":
11638
+ visit(current2.declaration, current2, "declaration");
11639
+ return;
11640
+ case "VariableDeclarator":
11641
+ visitPatternDefaults(current2.id);
11642
+ visit(current2.init, current2, "init");
11643
+ return;
11644
+ case "FunctionDeclaration":
11645
+ visitFunction(current2);
11646
+ return;
11647
+ case "FunctionExpression":
11648
+ case "ArrowFunctionExpression":
11649
+ visitFunction(current2);
11650
+ return;
11651
+ case "ClassDeclaration":
11652
+ case "ClassExpression":
11653
+ visit(current2.superClass, current2, "superClass");
11654
+ visit(current2.body, current2, "body");
11655
+ return;
11656
+ case "MemberExpression":
11657
+ case "OptionalMemberExpression":
11658
+ visit(current2.object, current2, "object");
11659
+ if (current2.computed) {
11660
+ visit(current2.property, current2, "property");
11661
+ }
11662
+ return;
11663
+ case "Property":
11664
+ if (current2.computed) {
11665
+ visit(current2.key, current2, "key");
11666
+ }
11667
+ visit(current2.value, current2, "value");
11668
+ return;
11669
+ case "MethodDefinition":
11670
+ case "PropertyDefinition":
11671
+ if (current2.computed) {
11672
+ visit(current2.key, current2, "key");
11673
+ }
11674
+ if (!current2.declare && !current2.abstract) {
11675
+ visit(current2.value, current2, "value");
11676
+ }
11677
+ return;
11678
+ case "AssignmentPattern":
11679
+ visit(current2.right, current2, "right");
11680
+ return;
11681
+ case "RestElement":
11682
+ return;
11683
+ case "ObjectPattern":
11684
+ case "ArrayPattern":
11685
+ visitPatternDefaults(current2);
11686
+ return;
11687
+ case "JSXElement":
11688
+ visitJSXName((_c = current2.openingElement) == null ? void 0 : _c.name);
11689
+ for (const child of current2.children || []) {
11690
+ visit(child);
11691
+ }
11692
+ return;
11693
+ case "JSXFragment":
11694
+ for (const child of current2.children || []) {
11695
+ visit(child);
11696
+ }
11697
+ return;
11698
+ case "JSXExpressionContainer":
11699
+ visit(current2.expression, current2, "expression");
11700
+ return;
11701
+ }
11702
+ for (const [key, value] of Object.entries(current2)) {
11703
+ if (skipKeys.has(key)) continue;
11704
+ visit(value, current2, key);
11705
+ }
11706
+ };
11707
+ visit(node);
11708
+ return references;
11709
+ }
11530
11710
  var Interpreter = class _Interpreter {
11531
11711
  constructor(globalEnv, options = {}) {
11532
11712
  this.globalEnv = globalEnv;
@@ -11535,6 +11715,8 @@ var Interpreter = class _Interpreter {
11535
11715
  this.moduleResolutionCache = options.moduleResolutionCache || /* @__PURE__ */ new Map();
11536
11716
  this.moduleExports = {};
11537
11717
  this.currentModulePath = options.currentModulePath;
11718
+ this.isTypeScriptModule = options.isTypeScriptModule || false;
11719
+ this.runtimeIdentifierReferences = null;
11538
11720
  this.abortSignal = options.abortSignal;
11539
11721
  this.executionController = options.executionController;
11540
11722
  }
@@ -11700,14 +11882,20 @@ var Interpreter = class _Interpreter {
11700
11882
  return void 0;
11701
11883
  }
11702
11884
  if (node.type === "Program") {
11885
+ const previousReferences = this.runtimeIdentifierReferences;
11886
+ this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
11703
11887
  let result = void 0;
11704
- for (const statement of node.body) {
11705
- result = await this.evaluateAsync(statement, env);
11706
- if (result instanceof ReturnValue || result instanceof ThrowSignal) {
11707
- return result;
11888
+ try {
11889
+ for (const statement of node.body) {
11890
+ result = await this.evaluateAsync(statement, env);
11891
+ if (result instanceof ReturnValue || result instanceof ThrowSignal) {
11892
+ return result;
11893
+ }
11708
11894
  }
11895
+ return result;
11896
+ } finally {
11897
+ this.runtimeIdentifierReferences = previousReferences;
11709
11898
  }
11710
- return result;
11711
11899
  }
11712
11900
  if (node.type === "ImportDeclaration") {
11713
11901
  return await this.evaluateImportDeclaration(node, env);
@@ -12331,23 +12519,29 @@ var Interpreter = class _Interpreter {
12331
12519
  }
12332
12520
  }
12333
12521
  evaluateProgram(node, env) {
12522
+ const previousReferences = this.runtimeIdentifierReferences;
12523
+ this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
12334
12524
  let result = void 0;
12335
- for (let i = 0; i < node.body.length; i++) {
12336
- const statement = node.body[i];
12337
- const isLast = i === node.body.length - 1;
12338
- if (isLast && statement.type === "BlockStatement") {
12339
- const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
12340
- if (objLiteral !== null) {
12341
- return objLiteral;
12525
+ try {
12526
+ for (let i = 0; i < node.body.length; i++) {
12527
+ const statement = node.body[i];
12528
+ const isLast = i === node.body.length - 1;
12529
+ if (isLast && statement.type === "BlockStatement") {
12530
+ const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
12531
+ if (objLiteral !== null) {
12532
+ return objLiteral;
12533
+ }
12342
12534
  }
12535
+ const statementResult = this.evaluate(statement, env);
12536
+ if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
12537
+ return statementResult;
12538
+ }
12539
+ result = statementResult;
12343
12540
  }
12344
- const statementResult = this.evaluate(statement, env);
12345
- if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
12346
- return statementResult;
12347
- }
12348
- result = statementResult;
12541
+ return result;
12542
+ } finally {
12543
+ this.runtimeIdentifierReferences = previousReferences;
12349
12544
  }
12350
- return result;
12351
12545
  }
12352
12546
  // Try to convert a BlockStatement to an object literal
12353
12547
  // Returns null if the block doesn't look like an object literal
@@ -12995,6 +13189,9 @@ var Interpreter = class _Interpreter {
12995
13189
  if (node.importKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.importKind === "type")) {
12996
13190
  return void 0;
12997
13191
  }
13192
+ if (this.isTypeScriptModule && node.specifiers.length > 0 && node.specifiers.every((specifier) => !this.isRuntimeImportSpecifier(specifier))) {
13193
+ return void 0;
13194
+ }
12998
13195
  if (!this.moduleResolver) {
12999
13196
  throw new Error("Module resolver not configured - cannot import modules");
13000
13197
  }
@@ -13030,6 +13227,7 @@ var Interpreter = class _Interpreter {
13030
13227
  moduleResolver: this.moduleResolver,
13031
13228
  moduleResolutionCache: this.moduleResolutionCache,
13032
13229
  currentModulePath: resolvedPath,
13230
+ isTypeScriptModule: isTypeScriptPath(resolvedPath),
13033
13231
  abortSignal: this.abortSignal,
13034
13232
  executionController: this.executionController
13035
13233
  });
@@ -13044,7 +13242,7 @@ var Interpreter = class _Interpreter {
13044
13242
  }
13045
13243
  bindImportSpecifiers(node, env, modulePath, moduleExports) {
13046
13244
  for (const specifier of node.specifiers) {
13047
- if (specifier.importKind === "type") {
13245
+ if (!this.isRuntimeImportSpecifier(specifier)) {
13048
13246
  continue;
13049
13247
  }
13050
13248
  if (specifier.type === "ImportSpecifier") {
@@ -13067,6 +13265,20 @@ var Interpreter = class _Interpreter {
13067
13265
  }
13068
13266
  return void 0;
13069
13267
  }
13268
+ isRuntimeImportSpecifier(specifier) {
13269
+ var _a;
13270
+ if (specifier.importKind === "type") {
13271
+ return false;
13272
+ }
13273
+ if (!this.isTypeScriptModule) {
13274
+ return true;
13275
+ }
13276
+ const localName = (_a = specifier.local) == null ? void 0 : _a.name;
13277
+ if (!localName || !this.runtimeIdentifierReferences) {
13278
+ return true;
13279
+ }
13280
+ return this.runtimeIdentifierReferences.has(localName);
13281
+ }
13070
13282
  evaluateExportNamedDeclaration(node, env) {
13071
13283
  if (node.exportKind === "type" || isTypeOnlyDeclaration(node.declaration)) {
13072
13284
  return void 0;
@@ -14551,7 +14763,8 @@ async function execute(code, env = null, options = {}) {
14551
14763
  moduleResolver: options.moduleResolver,
14552
14764
  abortSignal: options.abortSignal,
14553
14765
  executionController: controller,
14554
- currentModulePath: options.sourcePath
14766
+ currentModulePath: options.sourcePath,
14767
+ isTypeScriptModule: options.typescript || options.tsx || isTypeScriptPath2(options.sourcePath)
14555
14768
  });
14556
14769
  const needsAsync = options.sourceType === "module" || containsModuleDeclarations(ast) || containsTopLevelAwait(ast) || controller != null;
14557
14770
  if (needsAsync) {