* Strip JS line + block comments from `content` while preserving * string literals (so `"//"` inside a string stays intact). A block * comment becomes one space, so the tokens either side stay apart. Used by * extractReExports over a whole file, so commented-out export-from * statements
(content: string)
| 2518 | * apply this to function bodies, only to top-level files and import lists). |
| 2519 | */ |
| 2520 | function stripJsComments(content: string): string { |
| 2521 | let out = ''; |
| 2522 | let i = 0; |
| 2523 | let str: '"' | "'" | '`' | null = null; |
| 2524 | while (i < content.length) { |
| 2525 | const ch = content[i]!; |
| 2526 | if (str !== null) { |
| 2527 | out += ch; |
| 2528 | if (ch === '\\' && i + 1 < content.length) { |
| 2529 | out += content[i + 1]!; |
| 2530 | i += 2; |
| 2531 | continue; |
| 2532 | } |
| 2533 | if (ch === str) str = null; |
| 2534 | i++; |
| 2535 | continue; |
| 2536 | } |
| 2537 | if (ch === '"' || ch === "'" || ch === '`') { |
| 2538 | str = ch; |
| 2539 | out += ch; |
| 2540 | i++; |
| 2541 | continue; |
| 2542 | } |
| 2543 | if (ch === '/' && content[i + 1] === '/') { |
| 2544 | while (i < content.length && content[i] !== '\n') i++; |
| 2545 | continue; |
| 2546 | } |
| 2547 | if (ch === '/' && content[i + 1] === '*') { |
| 2548 | i += 2; |
| 2549 | while (i < content.length && !(content[i] === '*' && content[i + 1] === '/')) i++; |
| 2550 | i += 2; |
| 2551 | // A space, not nothing: `A/**/as B` must stay two words. |
| 2552 | out += ' '; |
| 2553 | continue; |
| 2554 | } |
| 2555 | out += ch; |
| 2556 | i++; |
| 2557 | } |
| 2558 | return out; |
| 2559 | } |
| 2560 | |
| 2561 | /** |
| 2562 | * Extract JS/TS re-export declarations from `content`. |
no outgoing calls
no test coverage detected