| 1439 | // Collapse duplicate destructuring from the same temp namespace var (e.g., multiple const { x } = __ns_rt_ns1) |
| 1440 | try { |
| 1441 | const collapse = (code: string, prefix: string) => { |
| 1442 | const re = new RegExp(`(^|\\n)\\s*const\\s*\\{([^}]+)\\}\\s*=\\s*(${prefix}\\d+)\\s*;?\\s*`, 'gm'); |
| 1443 | const byVar: Record<string, Set<string>> = {}; |
| 1444 | code.replace(re, (_full, _pfx, specList, varName) => { |
| 1445 | const set = (byVar[varName] ||= new Set()); |
| 1446 | String(specList) |
| 1447 | .split(',') |
| 1448 | .map((s) => s.trim()) |
| 1449 | .filter(Boolean) |
| 1450 | .forEach((seg) => set.add(seg)); |
| 1451 | return ''; |
| 1452 | }); |
| 1453 | if (Object.keys(byVar).length) { |
| 1454 | // Remove all existing destructures first |
| 1455 | code = code.replace(re, (full, pfx) => pfx || ''); |
| 1456 | // Re-emit one per var |
| 1457 | const blocks = Object.entries(byVar).map(([varName, set]) => `const { ${Array.from(set).join(', ')} } = ${varName};`); |
| 1458 | code = blocks.join('\n') + '\n' + code; |
| 1459 | } |
| 1460 | return code; |
| 1461 | }; |
| 1462 | result = collapse(result, '__ns_rt_ns'); |
| 1463 | result = collapse(result, '__ns_core_ns'); |
| 1464 | } catch {} |