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.d.ts CHANGED
@@ -12100,22 +12100,48 @@ function getAvailableMethods(obj) {
12100
12100
 
12101
12101
  const methods = new Set();
12102
12102
 
12103
- // Get own properties
12104
- Object.getOwnPropertyNames(obj).forEach(name => {
12105
- if (typeof obj[name] === 'function') {
12106
- methods.add(name);
12103
+ const addSafeMethods = (target, includeConstructor = true) => {
12104
+ let names;
12105
+ try {
12106
+ names = Object.getOwnPropertyNames(target);
12107
+ } catch {
12108
+ return;
12107
12109
  }
12108
- });
12109
12110
 
12110
- // Get prototype methods
12111
- let proto = Object.getPrototypeOf(obj);
12112
- while (proto && proto !== Object.prototype) {
12113
- Object.getOwnPropertyNames(proto).forEach(name => {
12114
- if (typeof proto[name] === 'function' && name !== 'constructor') {
12111
+ names.forEach(name => {
12112
+ if (!includeConstructor && name === 'constructor') return;
12113
+
12114
+ let descriptor;
12115
+ try {
12116
+ descriptor = Object.getOwnPropertyDescriptor(target, name);
12117
+ } catch {
12118
+ return;
12119
+ }
12120
+
12121
+ if (descriptor && 'value' in descriptor && typeof descriptor.value === 'function') {
12115
12122
  methods.add(name);
12116
12123
  }
12117
12124
  });
12118
- proto = Object.getPrototypeOf(proto);
12125
+ };
12126
+
12127
+ // Get own properties
12128
+ addSafeMethods(obj);
12129
+
12130
+ // Get prototype methods
12131
+ let proto;
12132
+ try {
12133
+ proto = Object.getPrototypeOf(obj);
12134
+ } catch {
12135
+ proto = null;
12136
+ }
12137
+
12138
+ while (proto && proto !== Object.prototype) {
12139
+ addSafeMethods(proto, false);
12140
+ try {
12141
+ proto = Object.getPrototypeOf(proto);
12142
+ } catch {
12143
+ proto = null;
12144
+ }
12119
12145
  }
12120
12146
 
12121
12147
  return Array.from(methods).sort();
@@ -12224,6 +12250,174 @@ function getPatternName(pattern) {
12224
12250
  return pattern.name;
12225
12251
  }
12226
12252
 
12253
+ function collectRuntimeIdentifierReferences(node) {
12254
+ const references = new Set();
12255
+ const skipKeys = new Set([
12256
+ 'type',
12257
+ 'start',
12258
+ 'end',
12259
+ 'loc',
12260
+ 'range',
12261
+ 'raw',
12262
+ 'typeAnnotation',
12263
+ 'returnType',
12264
+ 'typeParameters',
12265
+ 'typeArguments',
12266
+ 'implements'
12267
+ ]);
12268
+
12269
+ const visitPatternDefaults = (pattern) => {
12270
+ if (!pattern || typeof pattern !== 'object') return;
12271
+ if (pattern.type === 'AssignmentPattern') {
12272
+ visit(pattern.right);
12273
+ visitPatternDefaults(pattern.left);
12274
+ } else if (pattern.type === 'ObjectPattern') {
12275
+ for (const property of pattern.properties || []) {
12276
+ visitPatternDefaults(property.value || property.argument);
12277
+ }
12278
+ } else if (pattern.type === 'ArrayPattern') {
12279
+ for (const element of pattern.elements || []) {
12280
+ visitPatternDefaults(element);
12281
+ }
12282
+ } else if (pattern.type === 'RestElement') {
12283
+ visitPatternDefaults(pattern.argument);
12284
+ } else if (pattern.type === 'TSParameterProperty') {
12285
+ visitPatternDefaults(pattern.parameter);
12286
+ }
12287
+ };
12288
+
12289
+ const visitFunction = (fn) => {
12290
+ for (const param of fn.params || []) {
12291
+ visitPatternDefaults(param);
12292
+ }
12293
+ visit(fn.body);
12294
+ };
12295
+
12296
+ const visitJSXName = (jsxName) => {
12297
+ if (!jsxName || typeof jsxName !== 'object') return;
12298
+ if (jsxName.type === 'JSXIdentifier') {
12299
+ if (/^[A-Z]/.test(jsxName.name)) {
12300
+ references.add(jsxName.name);
12301
+ }
12302
+ } else if (jsxName.type === 'JSXMemberExpression') {
12303
+ visitJSXName(jsxName.object);
12304
+ } else if (jsxName.type === 'JSXNamespacedName') {
12305
+ visitJSXName(jsxName.namespace);
12306
+ }
12307
+ };
12308
+
12309
+ const visit = (current, parent = null, parentKey = null) => {
12310
+ if (!current || typeof current !== 'object') return;
12311
+
12312
+ if (Array.isArray(current)) {
12313
+ for (const item of current) visit(item, parent, parentKey);
12314
+ return;
12315
+ }
12316
+
12317
+ if (current.type?.startsWith('TS')) {
12318
+ if (isTypeWrapperExpression(current)) {
12319
+ visit(getTypeWrapperInnerExpression(current), current, 'expression');
12320
+ } else if (current.type === 'TSEnumDeclaration') {
12321
+ for (const member of current.members || []) {
12322
+ visit(member.initializer);
12323
+ }
12324
+ }
12325
+ return;
12326
+ }
12327
+
12328
+ switch (current.type) {
12329
+ case 'Identifier':
12330
+ references.add(current.name);
12331
+ return;
12332
+ case 'ImportDeclaration':
12333
+ return;
12334
+ case 'ExportNamedDeclaration':
12335
+ if (current.declaration) {
12336
+ visit(current.declaration, current, 'declaration');
12337
+ } else if (current.exportKind !== 'type') {
12338
+ for (const specifier of current.specifiers || []) {
12339
+ if (specifier.exportKind !== 'type' && specifier.local?.name) {
12340
+ references.add(specifier.local.name);
12341
+ }
12342
+ }
12343
+ }
12344
+ return;
12345
+ case 'ExportDefaultDeclaration':
12346
+ visit(current.declaration, current, 'declaration');
12347
+ return;
12348
+ case 'VariableDeclarator':
12349
+ visitPatternDefaults(current.id);
12350
+ visit(current.init, current, 'init');
12351
+ return;
12352
+ case 'FunctionDeclaration':
12353
+ visitFunction(current);
12354
+ return;
12355
+ case 'FunctionExpression':
12356
+ case 'ArrowFunctionExpression':
12357
+ visitFunction(current);
12358
+ return;
12359
+ case 'ClassDeclaration':
12360
+ case 'ClassExpression':
12361
+ visit(current.superClass, current, 'superClass');
12362
+ visit(current.body, current, 'body');
12363
+ return;
12364
+ case 'MemberExpression':
12365
+ case 'OptionalMemberExpression':
12366
+ visit(current.object, current, 'object');
12367
+ if (current.computed) {
12368
+ visit(current.property, current, 'property');
12369
+ }
12370
+ return;
12371
+ case 'Property':
12372
+ if (current.computed) {
12373
+ visit(current.key, current, 'key');
12374
+ }
12375
+ visit(current.value, current, 'value');
12376
+ return;
12377
+ case 'MethodDefinition':
12378
+ case 'PropertyDefinition':
12379
+ if (current.computed) {
12380
+ visit(current.key, current, 'key');
12381
+ }
12382
+ if (!current.declare && !current.abstract) {
12383
+ visit(current.value, current, 'value');
12384
+ }
12385
+ return;
12386
+ case 'AssignmentPattern':
12387
+ visit(current.right, current, 'right');
12388
+ return;
12389
+ case 'RestElement':
12390
+ return;
12391
+ case 'ObjectPattern':
12392
+ case 'ArrayPattern':
12393
+ visitPatternDefaults(current);
12394
+ return;
12395
+ case 'JSXElement':
12396
+ visitJSXName(current.openingElement?.name);
12397
+ for (const child of current.children || []) {
12398
+ visit(child);
12399
+ }
12400
+ return;
12401
+ case 'JSXFragment':
12402
+ for (const child of current.children || []) {
12403
+ visit(child);
12404
+ }
12405
+ return;
12406
+ case 'JSXExpressionContainer':
12407
+ visit(current.expression, current, 'expression');
12408
+ return;
12409
+ }
12410
+
12411
+ for (const [key, value] of Object.entries(current)) {
12412
+ if (skipKeys.has(key)) continue;
12413
+ visit(value, current, key);
12414
+ }
12415
+ };
12416
+
12417
+ visit(node);
12418
+ return references;
12419
+ }
12420
+
12227
12421
  class Interpreter {
12228
12422
  constructor(globalEnv, options = {}) {
12229
12423
  this.globalEnv = globalEnv;
@@ -12232,6 +12426,8 @@ class Interpreter {
12232
12426
  this.moduleResolutionCache = options.moduleResolutionCache || new Map();
12233
12427
  this.moduleExports = {}; // Track exports in current module
12234
12428
  this.currentModulePath = options.currentModulePath;
12429
+ this.isTypeScriptModule = options.isTypeScriptModule || false;
12430
+ this.runtimeIdentifierReferences = null;
12235
12431
  this.abortSignal = options.abortSignal;
12236
12432
  this.executionController = options.executionController;
12237
12433
  }
@@ -12454,15 +12650,21 @@ class Interpreter {
12454
12650
 
12455
12651
  // For Program nodes (evaluate all statements async)
12456
12652
  if (node.type === 'Program') {
12653
+ const previousReferences = this.runtimeIdentifierReferences;
12654
+ this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
12457
12655
  let result = undefined;
12458
- for (const statement of node.body) {
12459
- result = await this.evaluateAsync(statement, env);
12460
- // Handle top-level return and throw
12461
- if (result instanceof ReturnValue || result instanceof ThrowSignal) {
12462
- return result;
12656
+ try {
12657
+ for (const statement of node.body) {
12658
+ result = await this.evaluateAsync(statement, env);
12659
+ // Handle top-level return and throw
12660
+ if (result instanceof ReturnValue || result instanceof ThrowSignal) {
12661
+ return result;
12662
+ }
12463
12663
  }
12664
+ return result;
12665
+ } finally {
12666
+ this.runtimeIdentifierReferences = previousReferences;
12464
12667
  }
12465
- return result;
12466
12668
  }
12467
12669
 
12468
12670
  // For import declarations (always async)
@@ -13265,27 +13467,33 @@ class Interpreter {
13265
13467
  }
13266
13468
 
13267
13469
  evaluateProgram(node, env) {
13470
+ const previousReferences = this.runtimeIdentifierReferences;
13471
+ this.runtimeIdentifierReferences = collectRuntimeIdentifierReferences(node);
13268
13472
  let result = undefined;
13269
- for (let i = 0; i < node.body.length; i++) {
13270
- const statement = node.body[i];
13271
- const isLast = i === node.body.length - 1;
13272
-
13273
- // Special case: Last statement is a BlockStatement that looks like object literal
13274
- // Handle both shorthand { x, y } and full syntax { key: value, key2: value2 }
13275
- if (isLast && statement.type === 'BlockStatement') {
13276
- const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
13277
- if (objLiteral !== null) {
13278
- return objLiteral;
13473
+ try {
13474
+ for (let i = 0; i < node.body.length; i++) {
13475
+ const statement = node.body[i];
13476
+ const isLast = i === node.body.length - 1;
13477
+
13478
+ // Special case: Last statement is a BlockStatement that looks like object literal
13479
+ // Handle both shorthand { x, y } and full syntax { key: value, key2: value2 }
13480
+ if (isLast && statement.type === 'BlockStatement') {
13481
+ const objLiteral = this.tryConvertBlockToObjectLiteral(statement, env);
13482
+ if (objLiteral !== null) {
13483
+ return objLiteral;
13484
+ }
13279
13485
  }
13280
- }
13281
13486
 
13282
- const statementResult = this.evaluate(statement, env);
13283
- if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
13284
- return statementResult;
13487
+ const statementResult = this.evaluate(statement, env);
13488
+ if (statementResult instanceof ReturnValue || statementResult instanceof ThrowSignal) {
13489
+ return statementResult;
13490
+ }
13491
+ result = statementResult;
13285
13492
  }
13286
- result = statementResult;
13493
+ return result;
13494
+ } finally {
13495
+ this.runtimeIdentifierReferences = previousReferences;
13287
13496
  }
13288
- return result;
13289
13497
  }
13290
13498
 
13291
13499
  // Try to convert a BlockStatement to an object literal
@@ -14127,6 +14335,12 @@ class Interpreter {
14127
14335
  return undefined;
14128
14336
  }
14129
14337
 
14338
+ if (this.isTypeScriptModule &&
14339
+ node.specifiers.length > 0 &&
14340
+ node.specifiers.every(specifier => !this.isRuntimeImportSpecifier(specifier))) {
14341
+ return undefined;
14342
+ }
14343
+
14130
14344
  // Check if module resolver is configured
14131
14345
  if (!this.moduleResolver) {
14132
14346
  throw new Error('Module resolver not configured - cannot import modules');
@@ -14179,6 +14393,7 @@ class Interpreter {
14179
14393
  moduleResolver: this.moduleResolver,
14180
14394
  moduleResolutionCache: this.moduleResolutionCache,
14181
14395
  currentModulePath: resolvedPath,
14396
+ isTypeScriptModule: isTypeScriptPath$1(resolvedPath),
14182
14397
  abortSignal: this.abortSignal,
14183
14398
  executionController: this.executionController
14184
14399
  });
@@ -14200,7 +14415,7 @@ class Interpreter {
14200
14415
  bindImportSpecifiers(node, env, modulePath, moduleExports) {
14201
14416
  // Import specified bindings into current environment
14202
14417
  for (const specifier of node.specifiers) {
14203
- if (specifier.importKind === 'type') {
14418
+ if (!this.isRuntimeImportSpecifier(specifier)) {
14204
14419
  continue;
14205
14420
  }
14206
14421
 
@@ -14233,6 +14448,23 @@ class Interpreter {
14233
14448
  return undefined;
14234
14449
  }
14235
14450
 
14451
+ isRuntimeImportSpecifier(specifier) {
14452
+ if (specifier.importKind === 'type') {
14453
+ return false;
14454
+ }
14455
+
14456
+ if (!this.isTypeScriptModule) {
14457
+ return true;
14458
+ }
14459
+
14460
+ const localName = specifier.local?.name;
14461
+ if (!localName || !this.runtimeIdentifierReferences) {
14462
+ return true;
14463
+ }
14464
+
14465
+ return this.runtimeIdentifierReferences.has(localName);
14466
+ }
14467
+
14236
14468
  evaluateExportNamedDeclaration(node, env) {
14237
14469
  if (node.exportKind === 'type' || isTypeOnlyDeclaration(node.declaration)) {
14238
14470
  return undefined;
@@ -16175,7 +16407,8 @@ async function execute(code, env = null, options = {}) {
16175
16407
  moduleResolver: options.moduleResolver,
16176
16408
  abortSignal: options.abortSignal,
16177
16409
  executionController: controller,
16178
- currentModulePath: options.sourcePath
16410
+ currentModulePath: options.sourcePath,
16411
+ isTypeScriptModule: options.typescript || options.tsx || isTypeScriptPath(options.sourcePath)
16179
16412
  });
16180
16413
 
16181
16414
  // Use async evaluation if: