* 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)
| 2605 | * apply this to function bodies, only to top-level files and import lists). |
| 2606 | */ |
| 2607 | function stripJsComments(content: string): string { |
| 2608 | let out = ''; |
| 2609 | let i = 0; |
| 2610 | let str: '"' | "'" | '`' | null = null; |
| 2611 | while (i < content.length) { |
| 2612 | const ch = content[i]!; |
| 2613 | if (str !== null) { |
| 2614 | out += ch; |
| 2615 | if (ch === '\\' && i + 1 < content.length) { |
| 2616 | out += content[i + 1]!; |
| 2617 | i += 2; |
| 2618 | continue; |
| 2619 | } |
| 2620 | if (ch === str) str = null; |
| 2621 | i++; |
| 2622 | continue; |
| 2623 | } |
| 2624 | if (ch === '"' || ch === "'" || ch === '`') { |
| 2625 | str = ch; |
| 2626 | out += ch; |
| 2627 | i++; |
| 2628 | continue; |
| 2629 | } |
| 2630 | if (ch === '/' && content[i + 1] === '/') { |
| 2631 | while (i < content.length && content[i] !== '\n') i++; |
| 2632 | continue; |
| 2633 | } |
| 2634 | if (ch === '/' && content[i + 1] === '*') { |
| 2635 | i += 2; |
| 2636 | while (i < content.length && !(content[i] === '*' && content[i + 1] === '/')) i++; |
| 2637 | i += 2; |
| 2638 | // A space, not nothing: `A/**/as B` must stay two words. |
| 2639 | out += ' '; |
| 2640 | continue; |
| 2641 | } |
| 2642 | out += ch; |
| 2643 | i++; |
| 2644 | } |
| 2645 | return out; |
| 2646 | } |
| 2647 | |
| 2648 | /** |
| 2649 | * Extract JS/TS re-export declarations from `content`. |
no outgoing calls
no test coverage detected