(code: string)
| 57 | * import { A, B } from "/ns/core?p=index.js"; |
| 58 | */ |
| 59 | export function fixDanglingCoreFrom(code: string): string { |
| 60 | if (!code || typeof code !== 'string') return code; |
| 61 | try { |
| 62 | // Also normalize any concatenated imports before processing |
| 63 | code = code.replace(/;\s*import\s+/g, ';\nimport '); |
| 64 | |
| 65 | // Multi-line form: import { ...\n ...\n} from\nimport "<core-url>";[tail] |
| 66 | // Merge into: import { ... } from "<core-url>";\n[tail] |
| 67 | code = code.replace(/(^|\n|;)\s*import\s*\{([\s\S]*?)\}\s*from\s*\n\s*import\s+["']((?:https?:\/\/[^"']+)?\/ns\/core(?:\/\d+)?(?:\?p=[^"']+)?)["'];?(.*)/g, (_m, pre: string, named: string, url: string, tail: string) => { |
| 68 | const prefix = pre ? (String(pre).endsWith(';') ? String(pre) + '\n' : String(pre)) : ''; |
| 69 | const tailNorm = (tail || '').replace(/^\s*;?\s*/, '').trim(); |
| 70 | return prefix + `import {${named}} from "${url}";` + (tailNorm ? `\n${tailNorm}` : ''); |
| 71 | }); |
| 72 | const lines = code.split('\n'); |
| 73 | const out: string[] = []; |
| 74 | for (let i = 0; i < lines.length; i++) { |
| 75 | const ln = lines[i]; |
| 76 | const m = /^\s*import\s*\{[^}]+\}\s*from\s*$/.exec(ln); |
| 77 | if (m && i + 1 < lines.length) { |
| 78 | const next = lines[i + 1]; |
| 79 | // Support when the next line starts with the core import followed by another ';import ...' |
| 80 | // Capture the core import URL and any trailing content after it (another import concatenated) |
| 81 | const m2 = /^\s*import\s+["']((?:https?:\/\/[^"']+)?\/ns\/core(?:\/\d+)?(?:\?p=[^"']+)?)["'];?(.*)$/.exec(next); |
| 82 | if (m2) { |
| 83 | const url = m2[1]; |
| 84 | const tail = m2[2] || ''; |
| 85 | out.push(ln + ' ' + `"${url}";`); |
| 86 | // If there is a tail like ';import { ... } from "..."', normalize it onto a new line |
| 87 | const tailNorm = tail.replace(/^\s*;?\s*/, ''); |
| 88 | if (tailNorm.trim().length > 0) { |
| 89 | out.push(tailNorm); |
| 90 | } |
| 91 | i++; // consume the next line |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | out.push(ln); |
| 96 | } |
| 97 | return out.join('\n'); |
| 98 | } catch { |
| 99 | return code; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Fallback: normalize ANY import spec that references '@nativescript/core' (including |
no test coverage detected