@stacksjs/stx 0.2.145 → 0.2.146

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.
@@ -350,6 +350,18 @@ function detectModelUsage(code) {
350
350
  models.add(match[1]);
351
351
  return Array.from(models);
352
352
  }
353
+ function collectLocalBindings(code) {
354
+ const bindings = new Set;
355
+ for (const match of code.matchAll(/\b(?:const|let|var|function|class)\s+([A-Za-z_$][\w$]*)/g))
356
+ bindings.add(match[1]);
357
+ for (const match of code.matchAll(/\b(?:const|let|var)\s*\{([^}]*)\}/g))
358
+ for (const binding of match[1].split(",")) {
359
+ const localName = binding.trim().split(/[:=]/).pop()?.trim();
360
+ if (localName && /^[A-Za-z_$][\w$]*$/.test(localName))
361
+ bindings.add(localName);
362
+ }
363
+ return bindings;
364
+ }
353
365
  function transformAutoImports(code) {
354
366
  const usedStxImports = new Set, usedBrowserImports = new Set;
355
367
  let transformedCode = code;
@@ -360,30 +372,43 @@ function transformAutoImports(code) {
360
372
  const imports = match[1], source = match[2];
361
373
  if (match[0].includes("import type"))
362
374
  continue;
363
- imports.split(",").map((s) => s.trim().split(/\s+as\s+/)[0]).filter((s) => s.length > 0).forEach((name) => {
364
- existingImports.add(name);
375
+ const aliases = [];
376
+ for (const rawSpecifier of imports.split(",")) {
377
+ const specifier = rawSpecifier.trim();
378
+ if (!specifier || specifier.startsWith("type "))
379
+ continue;
380
+ const aliasMatch = specifier.match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
381
+ if (!aliasMatch)
382
+ continue;
383
+ const importedName = aliasMatch[1], localName = aliasMatch[2] || importedName;
384
+ existingImports.add(importedName);
385
+ existingImports.add(localName);
365
386
  if (source === "stx" || source === "@stacksjs/stx")
366
- usedStxImports.add(name);
387
+ usedStxImports.add(importedName);
367
388
  else
368
- usedBrowserImports.add(name);
369
- });
370
- transformedCode = transformedCode.replace(match[0], "// [auto-import processed]");
389
+ usedBrowserImports.add(importedName);
390
+ if (localName !== importedName)
391
+ aliases.push(`${localName} = ${importedName}`);
392
+ }
393
+ const replacement = aliases.length > 0 ? `var ${aliases.join(", ")}; /* [auto-import processed] */` : "// [auto-import processed]";
394
+ transformedCode = transformedCode.replace(match[0], replacement);
371
395
  }
396
+ const locallyDeclared = collectLocalBindings(transformedCode);
372
397
  for (const symbol of STX_AUTO_IMPORTS) {
373
- if (existingImports.has(symbol))
398
+ if (existingImports.has(symbol) || locallyDeclared.has(symbol))
374
399
  continue;
375
400
  if (new RegExp(`\\b${symbol}\\s*\\(`, "g").test(transformedCode))
376
401
  usedStxImports.add(symbol);
377
402
  }
378
403
  for (const symbol of BROWSER_CORE_IMPORTS) {
379
- if (existingImports.has(symbol))
404
+ if (existingImports.has(symbol) || locallyDeclared.has(symbol))
380
405
  continue;
381
406
  if (new RegExp(`\\b${symbol}\\b`, "g").test(transformedCode))
382
407
  usedBrowserImports.add(symbol);
383
408
  }
384
409
  const detectedModels = detectModelUsage(transformedCode);
385
410
  for (const model of detectedModels)
386
- if (!existingImports.has(model))
411
+ if (!existingImports.has(model) && !locallyDeclared.has(model))
387
412
  usedBrowserImports.add(model);
388
413
  return {
389
414
  code: transformedCode,