@open-mercato/cli 0.5.1-develop.2699.f8b50c8046 → 0.5.1-develop.2709.b6bdd776ac

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.
@@ -238,6 +238,47 @@ function collectLocalDeclarations(parsed) {
238
238
  }
239
239
  return locals;
240
240
  }
241
+ function resolveImportedModuleFile(sourceFile, moduleSpecifier) {
242
+ if (!moduleSpecifier.startsWith(".")) return null;
243
+ return findExistingModuleFileByBaseNames(path.dirname(sourceFile), [
244
+ moduleSpecifier,
245
+ path.join(moduleSpecifier, "index")
246
+ ]);
247
+ }
248
+ function collectImportedBindings(parsed, sourceFile) {
249
+ const bindings = /* @__PURE__ */ new Map();
250
+ for (const statement of parsed.statements) {
251
+ if (!ts.isImportDeclaration(statement)) continue;
252
+ if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
253
+ const resolvedSourceFile = resolveImportedModuleFile(sourceFile, statement.moduleSpecifier.text);
254
+ if (!resolvedSourceFile) continue;
255
+ const importClause = statement.importClause;
256
+ if (!importClause) continue;
257
+ if (importClause.name) {
258
+ bindings.set(importClause.name.text, {
259
+ kind: "default",
260
+ sourceFile: resolvedSourceFile
261
+ });
262
+ }
263
+ const namedBindings = importClause.namedBindings;
264
+ if (!namedBindings) continue;
265
+ if (ts.isNamespaceImport(namedBindings)) {
266
+ bindings.set(namedBindings.name.text, {
267
+ kind: "namespace",
268
+ sourceFile: resolvedSourceFile
269
+ });
270
+ continue;
271
+ }
272
+ for (const element of namedBindings.elements) {
273
+ bindings.set(element.name.text, {
274
+ kind: "named",
275
+ sourceFile: resolvedSourceFile,
276
+ exportName: element.propertyName?.text ?? element.name.text
277
+ });
278
+ }
279
+ }
280
+ return bindings;
281
+ }
241
282
  function collectReexportedNames(parsed) {
242
283
  const exportedNames = /* @__PURE__ */ new Set();
243
284
  for (const statement of parsed.statements) {
@@ -284,7 +325,94 @@ function findExportedInitializer(parsed, exportName, exportedNames) {
284
325
  }
285
326
  return null;
286
327
  }
287
- function resolveExpressionValue(expression, locals, visited) {
328
+ function findDefaultExportInitializer(parsed) {
329
+ for (const statement of parsed.statements) {
330
+ if (ts.isExportAssignment(statement) && !statement.isExportEquals) {
331
+ return statement.expression;
332
+ }
333
+ if (!ts.isVariableStatement(statement)) continue;
334
+ const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : void 0;
335
+ const isDefaultExport = modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword) ?? false;
336
+ if (!isDefaultExport) continue;
337
+ for (const declaration of statement.declarationList.declarations) {
338
+ if (declaration.initializer) return declaration.initializer;
339
+ }
340
+ }
341
+ return null;
342
+ }
343
+ function loadParsedModuleResolutionContext(sourceFile, cache) {
344
+ if (cache.has(sourceFile)) {
345
+ return cache.get(sourceFile) ?? null;
346
+ }
347
+ let source = "";
348
+ try {
349
+ source = fs.readFileSync(sourceFile, "utf8");
350
+ } catch {
351
+ cache.set(sourceFile, null);
352
+ return null;
353
+ }
354
+ const parsed = ts.createSourceFile(
355
+ sourceFile,
356
+ source,
357
+ ts.ScriptTarget.Latest,
358
+ true,
359
+ inferScriptKind(sourceFile)
360
+ );
361
+ const context = {
362
+ sourceFile,
363
+ parsed,
364
+ locals: collectLocalDeclarations(parsed),
365
+ exportedNames: collectReexportedNames(parsed),
366
+ imports: collectImportedBindings(parsed, sourceFile)
367
+ };
368
+ cache.set(sourceFile, context);
369
+ return context;
370
+ }
371
+ function resolveImportedBindingValue(binding, pathSegments, visited, cache) {
372
+ const importedContext = loadParsedModuleResolutionContext(binding.sourceFile, cache);
373
+ if (!importedContext) return void 0;
374
+ if (binding.kind === "namespace") {
375
+ if (pathSegments.length === 0) return void 0;
376
+ const [exportName, ...restPath] = pathSegments;
377
+ const importKey2 = `${binding.sourceFile}::${exportName}`;
378
+ if (visited.has(importKey2)) return void 0;
379
+ let initializer2 = findExportedInitializer(importedContext.parsed, exportName, importedContext.exportedNames);
380
+ if (!initializer2 && importedContext.exportedNames.has(exportName)) {
381
+ initializer2 = importedContext.locals.get(exportName) ?? null;
382
+ }
383
+ if (!initializer2) return void 0;
384
+ const nextVisited2 = new Set(visited);
385
+ nextVisited2.add(importKey2);
386
+ let value2 = resolveExpressionValue(initializer2, importedContext, nextVisited2, cache);
387
+ for (const segment of restPath) {
388
+ if (value2 && typeof value2 === "object" && !Array.isArray(value2) && segment in value2) {
389
+ value2 = value2[segment];
390
+ } else {
391
+ return void 0;
392
+ }
393
+ }
394
+ return value2;
395
+ }
396
+ const importKey = binding.kind === "default" ? `${binding.sourceFile}::default` : `${binding.sourceFile}::${binding.exportName}`;
397
+ if (visited.has(importKey)) return void 0;
398
+ let initializer = binding.kind === "default" ? findDefaultExportInitializer(importedContext.parsed) : findExportedInitializer(importedContext.parsed, binding.exportName, importedContext.exportedNames);
399
+ if (!initializer && binding.kind === "named" && importedContext.exportedNames.has(binding.exportName)) {
400
+ initializer = importedContext.locals.get(binding.exportName) ?? null;
401
+ }
402
+ if (!initializer) return void 0;
403
+ const nextVisited = new Set(visited);
404
+ nextVisited.add(importKey);
405
+ let value = resolveExpressionValue(initializer, importedContext, nextVisited, cache);
406
+ for (const segment of pathSegments) {
407
+ if (value && typeof value === "object" && !Array.isArray(value) && segment in value) {
408
+ value = value[segment];
409
+ } else {
410
+ return void 0;
411
+ }
412
+ }
413
+ return value;
414
+ }
415
+ function resolveExpressionValue(expression, context, visited, cache) {
288
416
  const expr = unwrapExpression(expression);
289
417
  if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
290
418
  if (ts.isNumericLiteral(expr)) return Number(expr.text);
@@ -300,19 +428,19 @@ function resolveExpressionValue(expression, locals, visited) {
300
428
  if (ts.isPropertyAssignment(property)) {
301
429
  const key = ts.isIdentifier(property.name) ? property.name.text : ts.isStringLiteral(property.name) ? property.name.text : null;
302
430
  if (!key) continue;
303
- const value = resolveExpressionValue(property.initializer, locals, visited);
431
+ const value = resolveExpressionValue(property.initializer, context, visited, cache);
304
432
  if (value !== void 0) result[key] = value;
305
433
  } else if (ts.isShorthandPropertyAssignment(property)) {
306
434
  const key = property.name.text;
307
- const initializer = locals.get(key);
435
+ const initializer = context.locals.get(key);
308
436
  if (initializer && !visited.has(key)) {
309
437
  const nextVisited = new Set(visited);
310
438
  nextVisited.add(key);
311
- const value = resolveExpressionValue(initializer, locals, nextVisited);
439
+ const value = resolveExpressionValue(initializer, context, nextVisited, cache);
312
440
  if (value !== void 0) result[key] = value;
313
441
  }
314
442
  } else if (ts.isSpreadAssignment(property)) {
315
- const spread = resolveExpressionValue(property.expression, locals, visited);
443
+ const spread = resolveExpressionValue(property.expression, context, visited, cache);
316
444
  if (spread && typeof spread === "object" && !Array.isArray(spread)) {
317
445
  Object.assign(result, spread);
318
446
  }
@@ -324,22 +452,26 @@ function resolveExpressionValue(expression, locals, visited) {
324
452
  const result = [];
325
453
  for (const element of expr.elements) {
326
454
  if (ts.isSpreadElement(element)) {
327
- const spread = resolveExpressionValue(element.expression, locals, visited);
455
+ const spread = resolveExpressionValue(element.expression, context, visited, cache);
328
456
  if (Array.isArray(spread)) result.push(...spread);
329
457
  continue;
330
458
  }
331
- const value = resolveExpressionValue(element, locals, visited);
459
+ const value = resolveExpressionValue(element, context, visited, cache);
332
460
  if (value !== void 0) result.push(value);
333
461
  }
334
462
  return result;
335
463
  }
336
464
  if (ts.isIdentifier(expr)) {
337
465
  if (visited.has(expr.text)) return void 0;
338
- const initializer = locals.get(expr.text);
339
- if (!initializer) return void 0;
340
- const nextVisited = new Set(visited);
341
- nextVisited.add(expr.text);
342
- return resolveExpressionValue(initializer, locals, nextVisited);
466
+ const initializer = context.locals.get(expr.text);
467
+ if (initializer) {
468
+ const nextVisited = new Set(visited);
469
+ nextVisited.add(expr.text);
470
+ return resolveExpressionValue(initializer, context, nextVisited, cache);
471
+ }
472
+ const importBinding = context.imports.get(expr.text);
473
+ if (!importBinding) return void 0;
474
+ return resolveImportedBindingValue(importBinding, [], visited, cache);
343
475
  }
344
476
  if (ts.isPropertyAccessExpression(expr)) {
345
477
  const pathSegments = [];
@@ -351,39 +483,44 @@ function resolveExpressionValue(expression, locals, visited) {
351
483
  }
352
484
  if (!ts.isIdentifier(cursor)) return void 0;
353
485
  const rootName = cursor.text;
354
- if (visited.has(rootName)) return void 0;
355
- const rootInit = locals.get(rootName);
356
- if (!rootInit) return void 0;
357
- const unwrappedRoot = unwrapExpression(rootInit);
358
- const nextVisited = new Set(visited);
359
- nextVisited.add(rootName);
360
- if (ts.isCallExpression(unwrappedRoot) && unwrappedRoot.arguments.length > 0) {
361
- const firstArg = unwrapExpression(unwrappedRoot.arguments[0]);
362
- if (ts.isObjectLiteralExpression(firstArg)) {
363
- const argObject = resolveExpressionValue(firstArg, locals, nextVisited);
364
- if (argObject && typeof argObject === "object" && !Array.isArray(argObject)) {
365
- let current = argObject;
366
- for (const segment of pathSegments) {
367
- if (current && typeof current === "object" && !Array.isArray(current) && segment in current) {
368
- current = current[segment];
369
- } else {
370
- current = void 0;
371
- break;
486
+ if (!visited.has(rootName)) {
487
+ const rootInit = context.locals.get(rootName);
488
+ if (rootInit) {
489
+ const unwrappedRoot = unwrapExpression(rootInit);
490
+ const nextVisited = new Set(visited);
491
+ nextVisited.add(rootName);
492
+ if (ts.isCallExpression(unwrappedRoot) && unwrappedRoot.arguments.length > 0) {
493
+ const firstArg = unwrapExpression(unwrappedRoot.arguments[0]);
494
+ if (ts.isObjectLiteralExpression(firstArg)) {
495
+ const argObject = resolveExpressionValue(firstArg, context, nextVisited, cache);
496
+ if (argObject && typeof argObject === "object" && !Array.isArray(argObject)) {
497
+ let current = argObject;
498
+ for (const segment of pathSegments) {
499
+ if (current && typeof current === "object" && !Array.isArray(current) && segment in current) {
500
+ current = current[segment];
501
+ } else {
502
+ current = void 0;
503
+ break;
504
+ }
505
+ }
506
+ if (current !== void 0) return current;
372
507
  }
373
508
  }
374
- if (current !== void 0) return current;
375
509
  }
510
+ let value = resolveExpressionValue(rootInit, context, nextVisited, cache);
511
+ for (const segment of pathSegments) {
512
+ if (value && typeof value === "object" && !Array.isArray(value) && segment in value) {
513
+ value = value[segment];
514
+ } else {
515
+ return void 0;
516
+ }
517
+ }
518
+ return value;
376
519
  }
377
520
  }
378
- let value = resolveExpressionValue(rootInit, locals, nextVisited);
379
- for (const segment of pathSegments) {
380
- if (value && typeof value === "object" && !Array.isArray(value) && segment in value) {
381
- value = value[segment];
382
- } else {
383
- return void 0;
384
- }
385
- }
386
- return value;
521
+ const importBinding = context.imports.get(rootName);
522
+ if (!importBinding) return void 0;
523
+ return resolveImportedBindingValue(importBinding, pathSegments, visited, cache);
387
524
  }
388
525
  return void 0;
389
526
  }
@@ -427,27 +564,15 @@ function hasNamedExport(sourceFile, exportName) {
427
564
  return false;
428
565
  }
429
566
  function resolveNamedObjectExport(sourceFile, exportName) {
430
- let source = "";
431
- try {
432
- source = fs.readFileSync(sourceFile, "utf8");
433
- } catch {
434
- return null;
435
- }
436
- const parsed = ts.createSourceFile(
437
- sourceFile,
438
- source,
439
- ts.ScriptTarget.Latest,
440
- true,
441
- inferScriptKind(sourceFile)
442
- );
443
- const locals = collectLocalDeclarations(parsed);
444
- const exportedNames = collectReexportedNames(parsed);
445
- let initializer = findExportedInitializer(parsed, exportName, exportedNames);
446
- if (!initializer && exportedNames.has(exportName)) {
447
- initializer = locals.get(exportName) ?? null;
567
+ const cache = /* @__PURE__ */ new Map();
568
+ const context = loadParsedModuleResolutionContext(sourceFile, cache);
569
+ if (!context) return null;
570
+ let initializer = findExportedInitializer(context.parsed, exportName, context.exportedNames);
571
+ if (!initializer && context.exportedNames.has(exportName)) {
572
+ initializer = context.locals.get(exportName) ?? null;
448
573
  }
449
574
  if (!initializer) return null;
450
- const value = resolveExpressionValue(initializer, locals, /* @__PURE__ */ new Set());
575
+ const value = resolveExpressionValue(initializer, context, /* @__PURE__ */ new Set(), cache);
451
576
  if (!value || typeof value !== "object" || Array.isArray(value)) return null;
452
577
  const record = value;
453
578
  return Object.keys(record).length > 0 ? record : null;