also looking at this
chore(deps): update all non-major dependencies
#2997
29 files
+1047
-444
| @@ -95,6 +95,47 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | /** Strip trailing comments from a line, preserving leading whitespace */ | |
| 99 | function stripTrailingComment(line: string): string { | |
| 100 | const lineLen = line.length | |
| 101 | let inString = 0 // 0=none, quote char code when in string | |
| 102 | let end = lineLen | |
| 103 | for (let i = 0; i < lineLen; i++) { | |
| 104 | const c = line.charCodeAt(i) | |
| 105 | if (inString) { | |
| 106 | if (c === CH_BACKSLASH) { i++; continue } | |
| 107 | if (c === inString) inString = 0 | |
| 108 | continue | |
| 109 | } | |
| 110 | if (c === CH_SQUOTE || c === CH_DQUOTE || c === CH_BACKTICK) { | |
| 111 | inString = c | |
| 112 | continue | |
| 113 | } | |
| 114 | if (c === CH_SLASH && i + 1 < lineLen) { | |
| 115 | const next = line.charCodeAt(i + 1) | |
| 116 | if (next === CH_SLASH) { | |
| 117 | // Trim trailing whitespace before // | |
| 118 | end = i | |
| 119 | while (end > 0 && (line.charCodeAt(end - 1) === CH_SPACE || line.charCodeAt(end - 1) === CH_TAB)) end-- | |
| 120 | return line.slice(0, end) | |
| 121 | } | |
| 122 | if (next === CH_STAR) { | |
| 123 | // Inline /* ... */ — find closing and replace with space | |
| 124 | const closeIdx = line.indexOf('*/', i + 2) | |
| 125 | if (closeIdx !== -1) { | |
| 126 | line = line.slice(0, i) + ' ' + line.slice(closeIdx + 2) | |
| 127 | i-- // re-scan | |
| 128 | continue | |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | } | |
| 133 | // Trim trailing whitespace | |
| 134 | end = lineLen | |
| 135 | while (end > 0 && (line.charCodeAt(end - 1) === CH_SPACE || line.charCodeAt(end - 1) === CH_TAB || line.charCodeAt(end - 1) === CH_CR)) end-- | |
| 136 | return end < lineLen ? line.slice(0, end) : line | |
| 137 | } | |
| 138 | ||
| 98 | 139 | function skipString(quote: number): void { |
| 99 | 140 | pos++ // skip opening quote |
| 100 | 141 | while (pos < len) { |
| @@ -165,10 +206,72 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 165 | 206 | pos += 2 |
| 166 | 207 | return true |
| 167 | 208 | } |
| 209 | // Check for regex literal: / not followed by / or *, preceded by non-expression char | |
| 210 | if (isRegexStart()) { | |
| 211 | skipRegex() | |
| 212 | return true | |
| 213 | } | |
| 168 | 214 | } |
| 169 | 215 | return false |
| 170 | 216 | } |
| 171 | 217 | |
| 218 | /** Check if `/` at current pos is the start of a regex literal (not division) */ | |
| 219 | function isRegexStart(): boolean { | |
| 220 | // Look backward for the previous non-whitespace character | |
| 221 | let p = pos - 1 | |
| 222 | while (p >= 0 && (source.charCodeAt(p) === CH_SPACE || source.charCodeAt(p) === CH_TAB || source.charCodeAt(p) === CH_LF || source.charCodeAt(p) === CH_CR)) p-- | |
| 223 | if (p < 0) | |
| 224 | return true // start of file | |
| 225 | const prev = source.charCodeAt(p) | |
| 226 | // After these chars, `/` starts a regex (not division) | |
| 227 | // = ( [ ! & | ? : , ; { } ^ ~ + - * % < > \n | |
| 228 | if (prev === CH_EQUAL || prev === CH_LPAREN || prev === CH_LBRACKET | |
| 229 | || prev === 33 /* ! */ || prev === 38 /* & */ || prev === 124 /* | */ | |
| 230 | || prev === CH_QUESTION || prev === CH_COLON || prev === CH_COMMA | |
| 231 | || prev === CH_SEMI || prev === CH_LBRACE || prev === CH_RBRACE | |
| 232 | || prev === 94 /* ^ */ || prev === 126 /* ~ */ | |
| 233 | || prev === 43 /* + */ || prev === 45 /* - */ || prev === CH_STAR | |
| 234 | || prev === 37 /* % */ || prev === CH_LANGLE || prev === CH_RANGLE) { | |
| 235 | return true | |
| 236 | } | |
| 237 | // After keywords like return, typeof, void, delete, throw, new, in, of, case | |
| 238 | if (isIdentChar(prev)) { | |
| 239 | let wp = p | |
| 240 | while (wp >= 0 && isIdentChar(source.charCodeAt(wp))) wp-- | |
| 241 | const word = source.slice(wp + 1, p + 1) | |
| 242 | if (word === 'return' || word === 'typeof' || word === 'void' | |
| 243 | || word === 'delete' || word === 'throw' || word === 'new' | |
| 244 | || word === 'in' || word === 'of' || word === 'case' | |
| 245 | || word === 'instanceof' || word === 'yield' || word === 'await') { | |
| 246 | return true | |
| 247 | } | |
| 248 | } | |
| 249 | return false | |
| 250 | } | |
| 251 | ||
| 252 | /** Skip a regex literal /.../ including flags */ | |
| 253 | function skipRegex(): void { | |
| 254 | pos++ // skip opening / | |
| 255 | let inCharClass = false | |
| 256 | while (pos < len) { | |
| 257 | const ch = source.charCodeAt(pos) | |
| 258 | if (ch === CH_BACKSLASH) { pos += 2; continue } // skip escaped char | |
| 259 | if (inCharClass) { | |
| 260 | if (ch === CH_RBRACKET) | |
| 261 | inCharClass = false | |
| 262 | pos++ | |
| 263 | continue | |
| 264 | } | |
| 265 | if (ch === CH_LBRACKET) { inCharClass = true; pos++; continue } | |
| 266 | if (ch === CH_SLASH) { pos++; break } // closing / | |
| 267 | if (ch === CH_LF || ch === CH_CR) | |
| 268 | break // unterminated regex | |
| 269 | pos++ | |
| 270 | } | |
| 271 | // Skip flags (g, i, m, s, u, y, d, v) | |
| 272 | while (pos < len && isIdentChar(source.charCodeAt(pos))) pos++ | |
| 273 | } | |
| 274 | ||
| 172 | 275 | /** Read an identifier at current position */ |
| 173 | 276 | function readIdent(): string { |
| 174 | 277 | const start = pos |
| @@ -192,12 +295,22 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 192 | 295 | |
| 193 | 296 | /** Check if current position is at a top-level statement-starting keyword */ |
| 194 | 297 | function isTopLevelKeyword(): boolean { |
| 195 | return matchWord('export') || matchWord('import') || matchWord('function') | |
| 196 | || matchWord('class') || matchWord('interface') || matchWord('type') | |
| 197 | || matchWord('enum') || matchWord('const') || matchWord('let') | |
| 198 | || matchWord('var') || matchWord('declare') || matchWord('module') | |
| 199 | || matchWord('namespace') || matchWord('abstract') || matchWord('async') | |
| 200 | || matchWord('default') | |
| 298 | if (pos >= len) return false | |
| 299 | const ch = source.charCodeAt(pos) | |
| 300 | switch (ch) { | |
| 301 | case 101: /* e */ return matchWord('export') || matchWord('enum') | |
| 302 | case 105: /* i */ return matchWord('import') || matchWord('interface') | |
| 303 | case 102: /* f */ return matchWord('function') | |
| 304 | case 99: /* c */ return matchWord('class') || matchWord('const') | |
| 305 | case 116: /* t */ return matchWord('type') | |
| 306 | case 108: /* l */ return matchWord('let') | |
| 307 | case 118: /* v */ return matchWord('var') | |
| 308 | case 100: /* d */ return matchWord('declare') || matchWord('default') | |
| 309 | case 109: /* m */ return matchWord('module') | |
| 310 | case 110: /* n */ return matchWord('namespace') | |
| 311 | case 97: /* a */ return matchWord('abstract') || matchWord('async') | |
| 312 | default: return false | |
| 313 | } | |
| 201 | 314 | } |
| 202 | 315 | |
| 203 | 316 | /** |
| @@ -215,12 +328,24 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 215 | 328 | while (pos < len) { |
| 216 | 329 | const c = source.charCodeAt(pos) |
| 217 | 330 | if (c === CH_SPACE || c === CH_TAB || c === CH_CR || c === CH_LF) { pos++; continue } |
| 218 | if (c === CH_SLASH && pos + 1 < len && source.charCodeAt(pos + 1) === CH_SLASH) { | |
| 219 | pos += 2 | |
| 220 | while (pos < len && source.charCodeAt(pos) !== CH_LF) pos++ | |
| 221 | if (pos < len) | |
| 222 | pos++ | |
| 223 | continue | |
| 331 | if (c === CH_SLASH && pos + 1 < len) { | |
| 332 | const next = source.charCodeAt(pos + 1) | |
| 333 | if (next === CH_SLASH) { | |
| 334 | // Skip line comment | |
| 335 | pos += 2 | |
| 336 | while (pos < len && source.charCodeAt(pos) !== CH_LF) pos++ | |
| 337 | if (pos < len) | |
| 338 | pos++ | |
| 339 | continue | |
| 340 | } | |
| 341 | if (next === CH_STAR) { | |
| 342 | // Skip block/JSDoc comment | |
| 343 | pos += 2 | |
| 344 | while (pos < len - 1 && !(source.charCodeAt(pos) === CH_STAR && source.charCodeAt(pos + 1) === CH_SLASH)) pos++ | |
| 345 | if (pos < len - 1) | |
| 346 | pos += 2 | |
| 347 | continue | |
| 348 | } | |
| 224 | 349 | } |
| 225 | 350 | break |
| 226 | 351 | } |
| @@ -331,10 +456,11 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 331 | 456 | */ |
| 332 | 457 | function skipExportBraces(): void { |
| 333 | 458 | findMatchingClose(CH_LBRACE, CH_RBRACE) |
| 334 | skipWhitespaceAndComments() | |
| 459 | // Only skip whitespace (not comments) to find 'from' — comments belong to next declaration | |
| 460 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 335 | 461 | if (matchWord('from')) { |
| 336 | 462 | pos += 4 |
| 337 | skipWhitespaceAndComments() | |
| 463 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 338 | 464 | if (pos < len) { |
| 339 | 465 | const ch = source.charCodeAt(pos) |
| 340 | 466 | if (ch === CH_SQUOTE || ch === CH_DQUOTE) |
| @@ -352,16 +478,16 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 352 | 478 | */ |
| 353 | 479 | function skipExportStar(): void { |
| 354 | 480 | pos++ // skip * |
| 355 | skipWhitespaceAndComments() | |
| 481 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 356 | 482 | if (matchWord('as')) { |
| 357 | 483 | pos += 2 |
| 358 | skipWhitespaceAndComments() | |
| 484 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 359 | 485 | readIdent() |
| 360 | skipWhitespaceAndComments() | |
| 486 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 361 | 487 | } |
| 362 | 488 | if (matchWord('from')) { |
| 363 | 489 | pos += 4 |
| 364 | skipWhitespaceAndComments() | |
| 490 | while (pos < len && isWhitespace(source.charCodeAt(pos))) pos++ | |
| 365 | 491 | if (pos < len) { |
| 366 | 492 | const ch = source.charCodeAt(pos) |
| 367 | 493 | if (ch === CH_SQUOTE || ch === CH_DQUOTE) |
| @@ -387,8 +513,10 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 387 | 513 | return undefined |
| 388 | 514 | |
| 389 | 515 | const comments: string[] = [] |
| 516 | let hasBlockComment = false | |
| 390 | 517 | |
| 391 | 518 | // Scan backwards for consecutive comment blocks |
| 519 | // Use push() + reverse at end (O(1) per append vs O(n) for unshift) | |
| 392 | 520 | while (p >= 0) { |
| 393 | 521 | // Check for block comment ending with */ |
| 394 | 522 | if (p >= 1 && source.charCodeAt(p) === CH_SLASH && source.charCodeAt(p - 1) === CH_STAR) { |
| @@ -396,7 +524,8 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 396 | 524 | let start = p - 2 |
| 397 | 525 | while (start >= 1 && !(source.charCodeAt(start) === CH_SLASH && source.charCodeAt(start + 1) === CH_STAR)) start-- |
| 398 | 526 | if (start >= 0 && source.charCodeAt(start) === CH_SLASH && source.charCodeAt(start + 1) === CH_STAR) { |
| 399 | comments.unshift(source.slice(start, p + 1)) | |
| 527 | comments.push(source.slice(start, p + 1)) | |
| 528 | hasBlockComment = true | |
| 400 | 529 | p = start - 1 |
| 401 | 530 | while (p >= 0 && isWhitespace(source.charCodeAt(p))) p-- |
| 402 | 531 | continue |
| @@ -411,7 +540,11 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 411 | 540 | const lineText = source.slice(lineStart, p + 1).trim() |
| 412 | 541 | |
| 413 | 542 | if (lineText.startsWith('//')) { |
| 414 | // Collect consecutive single-line comments | |
| 543 | // Don't include // comments above block comments (they're section headers, not doc comments) | |
| 544 | if (hasBlockComment) | |
| 545 | break | |
| 546 | ||
| 547 | // Collect consecutive single-line comments (push + reverse) | |
| 415 | 548 | const singleLines: string[] = [lineText] |
| 416 | 549 | p = lineStart - 1 |
| 417 | 550 | while (p >= 0 && (source.charCodeAt(p) === CH_LF || source.charCodeAt(p) === CH_CR)) p-- |
| @@ -421,7 +554,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 421 | 554 | while (ls > 0 && source.charCodeAt(ls - 1) !== CH_LF) ls-- |
| 422 | 555 | const lt = source.slice(ls, p + 1).trim() |
| 423 | 556 | if (lt.startsWith('//')) { |
| 424 | singleLines.unshift(lt) | |
| 557 | singleLines.push(lt) | |
| 425 | 558 | p = ls - 1 |
| 426 | 559 | while (p >= 0 && (source.charCodeAt(p) === CH_LF || source.charCodeAt(p) === CH_CR)) p-- |
| 427 | 560 | } |
| @@ -433,7 +566,8 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 433 | 566 | break |
| 434 | 567 | } |
| 435 | 568 | } |
| 436 | comments.unshift(singleLines.join('\n')) | |
| 569 | singleLines.reverse() | |
| 570 | comments.push(singleLines.join('\n')) | |
| 437 | 571 | continue |
| 438 | 572 | } |
| 439 | 573 | |
| @@ -441,7 +575,9 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 441 | 575 | break |
| 442 | 576 | } |
| 443 | 577 | |
| 444 | return comments.length > 0 ? comments : undefined | |
| 578 | if (comments.length === 0) return undefined | |
| 579 | comments.reverse() | |
| 580 | return comments | |
| 445 | 581 | } |
| 446 | 582 | |
| 447 | 583 | // --- High-level extraction --- |
| @@ -471,15 +607,50 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 471 | 607 | } |
| 472 | 608 | |
| 473 | 609 | const text = source.slice(stmtStart, pos).trim() |
| 474 | const isTypeOnly = text.startsWith('import type ') || text.startsWith('import type{') | |
| 475 | ||
| 476 | // Detect side-effect imports | |
| 477 | const isSideEffect = !text.includes('{') && !text.includes(' as ') && !/import\s+\w/.test(text.replace(/import\s+type\s/, 'import ').replace(/import\s+['"]/, 'import_str ')) | |
| 478 | const isSideEffectImport = /^import\s+['"]/.test(text) || /^import\s+type\s+['"]/.test(text.replace('type ', '')) | |
| 479 | ||
| 480 | // Extract source module | |
| 481 | const sourceMatch = text.match(/from\s+['"]([^'"]+)['"]/) || text.match(/import\s+['"]([^'"]+)['"]/) | |
| 482 | const moduleSrc = sourceMatch ? sourceMatch[1] : '' | |
| 610 | const isTypeOnly = text.charCodeAt(7) === 116 /* t */ && (text.startsWith('import type ') || text.startsWith('import type{')) | |
| 611 | ||
| 612 | // Detect side-effect imports: import 'foo' or import "foo" (no names imported) | |
| 613 | // After 'import' + optional 'type', next non-ws must be a quote | |
| 614 | let isSideEffectImport = false | |
| 615 | { | |
| 616 | let si = 6 // skip 'import' | |
| 617 | while (si < text.length && (text.charCodeAt(si) === CH_SPACE || text.charCodeAt(si) === CH_TAB)) si++ | |
| 618 | if (si < text.length && text.charCodeAt(si) === 116 /* t */ && text.startsWith('type', si)) { | |
| 619 | si += 4 | |
| 620 | while (si < text.length && (text.charCodeAt(si) === CH_SPACE || text.charCodeAt(si) === CH_TAB)) si++ | |
| 621 | } | |
| 622 | if (si < text.length) { | |
| 623 | const qc = text.charCodeAt(si) | |
| 624 | isSideEffectImport = qc === CH_SQUOTE || qc === CH_DQUOTE | |
| 625 | } | |
| 626 | } | |
| 627 | ||
| 628 | // Extract source module: find 'from' or direct quote after import | |
| 629 | let moduleSrc = '' | |
| 630 | { | |
| 631 | const fromIdx = text.indexOf('from ') | |
| 632 | if (fromIdx !== -1) { | |
| 633 | let mi = fromIdx + 5 | |
| 634 | while (mi < text.length && (text.charCodeAt(mi) === CH_SPACE || text.charCodeAt(mi) === CH_TAB)) mi++ | |
| 635 | if (mi < text.length) { | |
| 636 | const q = text.charCodeAt(mi) | |
| 637 | if (q === CH_SQUOTE || q === CH_DQUOTE) { | |
| 638 | const end = text.indexOf(String.fromCharCode(q), mi + 1) | |
| 639 | if (end !== -1) moduleSrc = text.slice(mi + 1, end) | |
| 640 | } | |
| 641 | } | |
| 642 | } | |
| 643 | else if (isSideEffectImport) { | |
| 644 | // import 'module' — find the quote | |
| 645 | let mi = 6 | |
| 646 | while (mi < text.length && text.charCodeAt(mi) !== CH_SQUOTE && text.charCodeAt(mi) !== CH_DQUOTE) mi++ | |
| 647 | if (mi < text.length) { | |
| 648 | const q = text.charCodeAt(mi) | |
| 649 | const end = text.indexOf(String.fromCharCode(q), mi + 1) | |
| 650 | if (end !== -1) moduleSrc = text.slice(mi + 1, end) | |
| 651 | } | |
| 652 | } | |
| 653 | } | |
| 483 | 654 | |
| 484 | 655 | const comments = extractLeadingComments(stmtStart) |
| 485 | 656 | |
| @@ -509,34 +680,87 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 509 | 680 | |
| 510 | 681 | /** Strip inline comments from a brace block and normalize indentation */ |
| 511 | 682 | function cleanBraceBlock(raw: string): string { |
| 512 | // Remove inline // and /** */ comments, normalize indentation | |
| 683 | // Strip standalone comment lines and inline trailing comments, preserve relative indentation | |
| 684 | // Pass 1: filter + compute min indent simultaneously | |
| 513 | 685 | const lines = raw.split('\n') |
| 514 | const cleaned: string[] = [] | |
| 515 | for (const line of lines) { | |
| 516 | // Strip full-line comments | |
| 517 | const trimmed = line.trim() | |
| 518 | if (trimmed.startsWith('//') || trimmed.startsWith('/**') || trimmed.startsWith('/*') || trimmed.startsWith('*')) | |
| 519 | continue | |
| 520 | if (trimmed === '' || trimmed === '{' || trimmed === '}') { | |
| 521 | cleaned.push(trimmed) | |
| 686 | const filtered: string[] = [] | |
| 687 | const trimCache: string[] = [] // cache trimmed versions | |
| 688 | const indentCache: number[] = [] // cache indent levels | |
| 689 | let inBlockComment = false | |
| 690 | let minIndent = Infinity | |
| 691 | ||
| 692 | for (let i = 0; i < lines.length; i++) { | |
| 693 | const line = lines[i] | |
| 694 | if (inBlockComment) { | |
| 695 | if (line.includes('*/')) | |
| 696 | inBlockComment = false | |
| 522 | 697 | continue |
| 523 | 698 | } |
| 524 | // Remove trailing inline comments | |
| 525 | const result = trimmed.replace(/\s*\/\/.*$/, '').replace(/\s*\/\*.*?\*\/\s*/g, ' ').trim() | |
| 526 | if (result) | |
| 527 | cleaned.push(result) | |
| 699 | ||
| 700 | const trimmed = line.trim() | |
| 701 | ||
| 702 | // Skip standalone comment lines | |
| 703 | if (trimmed.length === 0) continue | |
| 704 | const ch0 = trimmed.charCodeAt(0) | |
| 705 | if (ch0 === CH_SLASH) { | |
| 706 | const ch1 = trimmed.charCodeAt(1) | |
| 707 | if (ch1 === CH_SLASH) continue // // | |
| 708 | if (ch1 === CH_STAR) { // /* or /** | |
| 709 | if (!trimmed.includes('*/')) | |
| 710 | inBlockComment = true | |
| 711 | continue | |
| 712 | } | |
| 713 | } | |
| 714 | if (ch0 === CH_STAR) continue | |
| 715 | ||
| 716 | // Remove trailing inline comments without regex | |
| 717 | let cleaned = stripTrailingComment(line) | |
| 718 | // Strip trailing semicolons from member lines (DTS convention for interfaces) | |
| 719 | if (cleaned.charCodeAt(cleaned.length - 1) === CH_SEMI) | |
| 720 | cleaned = cleaned.slice(0, -1) | |
| 721 | const ct = cleaned.trim() | |
| 722 | if (!ct) continue | |
| 723 | ||
| 724 | filtered.push(cleaned) | |
| 725 | trimCache.push(ct) | |
| 726 | ||
| 727 | // Compute indent simultaneously (pass 1 + 2 merged) | |
| 728 | if (ct !== '{' && ct !== '}') { | |
| 729 | const indent = cleaned.length - cleaned.trimStart().length | |
| 730 | if (indent < minIndent) minIndent = indent | |
| 731 | } | |
| 732 | indentCache.push(cleaned.length - cleaned.trimStart().length) | |
| 528 | 733 | } |
| 529 | // Rebuild with consistent 2-space indent | |
| 530 | const members: string[] = [] | |
| 531 | for (const line of cleaned) { | |
| 532 | if (line === '{' || line === '}') | |
| 734 | ||
| 735 | if (filtered.length === 0) | |
| 736 | return '{}' | |
| 737 | ||
| 738 | if (minIndent === Infinity || minIndent <= 2) | |
| 739 | return filtered.join('\n') // Already at correct indent | |
| 740 | ||
| 741 | // Pass 2: re-indent using cached values | |
| 742 | const offset = minIndent - 2 | |
| 743 | const rebased: string[] = new Array(filtered.length) | |
| 744 | for (let i = 0; i < filtered.length; i++) { | |
| 745 | const t = trimCache[i] | |
| 746 | if (t === '{') { | |
| 747 | rebased[i] = t | |
| 533 | 748 | continue |
| 534 | if (line) | |
| 535 | members.push(` ${line}`) | |
| 749 | } | |
| 750 | const currentIndent = indentCache[i] | |
| 751 | if (currentIndent > minIndent) { | |
| 752 | rebased[i] = filtered[i] | |
| 753 | } | |
| 754 | else if (currentIndent === minIndent && (t.charCodeAt(0) === CH_RBRACE || t.charCodeAt(0) === CH_RBRACKET || t.charCodeAt(0) === CH_RPAREN)) { | |
| 755 | rebased[i] = filtered[i] | |
| 756 | } | |
| 757 | else { | |
| 758 | const newIndent = Math.max(0, currentIndent - offset) | |
| 759 | rebased[i] = ' '.repeat(newIndent) + t | |
| 760 | } | |
| 536 | 761 | } |
| 537 | if (members.length === 0) | |
| 538 | return '{}' | |
| 539 | return `{\n${members.join('\n')}\n}` | |
| 762 | ||
| 763 | return rebased.join('\n') | |
| 540 | 764 | } |
| 541 | 765 | |
| 542 | 766 | /** Extract type parameters <...> (normalized to single line) */ |
| @@ -546,9 +770,25 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 546 | 770 | const start = pos |
| 547 | 771 | findMatchingClose(CH_LANGLE, CH_RANGLE) |
| 548 | 772 | const raw = source.slice(start, pos) |
| 549 | // Normalize multi-line generics to single line | |
| 773 | // Normalize multi-line generics to single line (avoid regex) | |
| 550 | 774 | if (raw.includes('\n')) { |
| 551 | return raw.replace(/\s*\n\s*/g, ' ').replace(/\s+/g, ' ').replace(/^<\s+/, '<').replace(/\s+>$/, '>') | |
| 775 | // Collapse whitespace/newlines to single spaces | |
| 776 | let result = '' | |
| 777 | let prevSpace = false | |
| 778 | for (let i = 0; i < raw.length; i++) { | |
| 779 | const c = raw.charCodeAt(i) | |
| 780 | if (c === 32 || c === 9 || c === 10 || c === 13) { | |
| 781 | if (!prevSpace && result.length > 0) { result += ' '; prevSpace = true } | |
| 782 | } | |
| 783 | else { | |
| 784 | result += raw[i] | |
| 785 | prevSpace = false | |
| 786 | } | |
| 787 | } | |
| 788 | // Trim spaces around < and > | |
| 789 | if (result.charCodeAt(1) === 32) result = '<' + result.slice(2) | |
| 790 | if (result.charCodeAt(result.length - 2) === 32) result = result.slice(0, -2) + '>' | |
| 791 | return result | |
| 552 | 792 | } |
| 553 | 793 | return raw |
| 554 | 794 | } |
| @@ -618,6 +858,86 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 618 | 858 | return `(${dtsParams.join(', ')})` |
| 619 | 859 | } |
| 620 | 860 | |
| 861 | /** Clean a destructured parameter name: strip defaults and rest operators */ | |
| 862 | function cleanDestructuredName(name: string): string { | |
| 863 | if (!name.startsWith('{') && !name.startsWith('[')) | |
| 864 | return name | |
| 865 | ||
| 866 | let result = '' | |
| 867 | let i = 0 | |
| 868 | let depth = 0 | |
| 869 | let inStr = false | |
| 870 | let strCh = 0 | |
| 871 | ||
| 872 | while (i < name.length) { | |
| 873 | const ch = name.charCodeAt(i) | |
| 874 | ||
| 875 | if (inStr) { | |
| 876 | if (ch === CH_BACKSLASH && i + 1 < name.length) { result += name[i] + name[i + 1]; i += 2; continue } | |
| 877 | if (ch === strCh) | |
| 878 | inStr = false | |
| 879 | result += name[i]; i++; continue | |
| 880 | } | |
| 881 | if (ch === CH_SQUOTE || ch === CH_DQUOTE || ch === CH_BACKTICK) { | |
| 882 | inStr = true; strCh = ch | |
| 883 | result += name[i]; i++; continue | |
| 884 | } | |
| 885 | ||
| 886 | if (ch === CH_LBRACE || ch === CH_LBRACKET || ch === CH_LPAREN) | |
| 887 | depth++ | |
| 888 | else if (ch === CH_RBRACE || ch === CH_RBRACKET || ch === CH_RPAREN) | |
| 889 | depth-- | |
| 890 | ||
| 891 | // At depth 1+, strip `= <default>` by skipping until , or closing bracket | |
| 892 | if (depth >= 1 && ch === CH_EQUAL | |
| 893 | && (i + 1 >= name.length || (name.charCodeAt(i + 1) !== CH_EQUAL && name.charCodeAt(i + 1) !== CH_RANGLE))) { | |
| 894 | // Also strip preceding whitespace | |
| 895 | while (result.length > 0 && (result[result.length - 1] === ' ' || result[result.length - 1] === '\t' || result[result.length - 1] === '\n')) | |
| 896 | result = result.slice(0, -1) | |
| 897 | // Skip the default value | |
| 898 | i++ | |
| 899 | let skipDepth = 0 | |
| 900 | while (i < name.length) { | |
| 901 | const sc = name.charCodeAt(i) | |
| 902 | if (sc === CH_SQUOTE || sc === CH_DQUOTE || sc === CH_BACKTICK) { | |
| 903 | const q = sc; i++ | |
| 904 | while (i < name.length) { | |
| 905 | if (name.charCodeAt(i) === CH_BACKSLASH) { i += 2; continue } | |
| 906 | if (name.charCodeAt(i) === q) { i++; break } | |
| 907 | i++ | |
| 908 | } | |
| 909 | continue | |
| 910 | } | |
| 911 | if (sc === CH_LBRACE || sc === CH_LBRACKET || sc === CH_LPAREN) { | |
| 912 | skipDepth++ | |
| 913 | } | |
| 914 | else if (sc === CH_RBRACE || sc === CH_RBRACKET || sc === CH_RPAREN) { | |
| 915 | if (skipDepth === 0) | |
| 916 | break | |
| 917 | skipDepth-- | |
| 918 | } | |
| 919 | else if (sc === CH_COMMA && skipDepth === 0) { | |
| 920 | break | |
| 921 | } | |
| 922 | i++ | |
| 923 | } | |
| 924 | continue | |
| 925 | } | |
| 926 | ||
| 927 | // At depth 1, strip `...` rest operator | |
| 928 | if (depth === 1 && ch === CH_DOT && i + 2 < name.length | |
| 929 | && name.charCodeAt(i + 1) === CH_DOT && name.charCodeAt(i + 2) === CH_DOT) { | |
| 930 | i += 3 | |
| 931 | continue | |
| 932 | } | |
| 933 | ||
| 934 | result += name[i] | |
| 935 | i++ | |
| 936 | } | |
| 937 | ||
| 938 | return result | |
| 939 | } | |
| 940 | ||
| 621 | 941 | /** Build a single DTS parameter from raw source text */ |
| 622 | 942 | function buildSingleDtsParam(raw: string): string { |
| 623 | 943 | let p = raw.trim() |
| @@ -720,6 +1040,28 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 720 | 1040 | type = 'unknown' |
| 721 | 1041 | } |
| 722 | 1042 | |
| 1043 | // Clean destructured parameter names (strip defaults, rest operators) | |
| 1044 | name = cleanDestructuredName(name) | |
| 1045 | if ((name.startsWith('{') || name.startsWith('[')) && name.includes('\n')) { | |
| 1046 | // Collapse to single line only if short enough, otherwise normalize indent | |
| 1047 | const collapsed = name.replace(/\s*\n\s*/g, ' ').replace(/\s+/g, ' ') | |
| 1048 | if (collapsed.length <= 40) { | |
| 1049 | name = collapsed | |
| 1050 | } | |
| 1051 | else { | |
| 1052 | // Normalize to 2-space indent per line, keep braces unindented | |
| 1053 | const lines = name.split('\n') | |
| 1054 | name = lines.map((l) => { | |
| 1055 | const t = l.trim() | |
| 1056 | if (!t) | |
| 1057 | return '' | |
| 1058 | if (t === '{' || t === '}' || t === '[' || t === ']' || t.startsWith('}') || t.startsWith(']')) | |
| 1059 | return t | |
| 1060 | return ` ${t}` | |
| 1061 | }).join('\n') | |
| 1062 | } | |
| 1063 | } | |
| 1064 | ||
| 723 | 1065 | // Handle optional marker |
| 724 | 1066 | const isOptional = name.endsWith('?') || hasDefault |
| 725 | 1067 | if (name.endsWith('?')) |
| @@ -732,12 +1074,28 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 732 | 1074 | return `${name}${optionalMarker}: ${type}` |
| 733 | 1075 | } |
| 734 | 1076 | |
| 1077 | /** Check if string is a numeric literal (integer or decimal, optionally negative) */ | |
| 1078 | function isNumericLiteral(v: string): boolean { | |
| 1079 | let i = 0 | |
| 1080 | if (i < v.length && v.charCodeAt(i) === 45 /* - */) i++ | |
| 1081 | if (i >= v.length) return false | |
| 1082 | const c = v.charCodeAt(i) | |
| 1083 | if (c < 48 || c > 57) return false // not a digit | |
| 1084 | while (i < v.length && v.charCodeAt(i) >= 48 && v.charCodeAt(i) <= 57) i++ | |
| 1085 | if (i < v.length && v.charCodeAt(i) === 46 /* . */) { | |
| 1086 | i++ | |
| 1087 | if (i >= v.length || v.charCodeAt(i) < 48 || v.charCodeAt(i) > 57) return false | |
| 1088 | while (i < v.length && v.charCodeAt(i) >= 48 && v.charCodeAt(i) <= 57) i++ | |
| 1089 | } | |
| 1090 | return i === v.length | |
| 1091 | } | |
| 1092 | ||
| 735 | 1093 | /** Infer type from a default value expression (simple cases) */ |
| 736 | 1094 | function inferTypeFromDefault(value: string): string { |
| 737 | 1095 | const v = value.trim() |
| 738 | 1096 | if (v === 'true' || v === 'false') |
| 739 | 1097 | return 'boolean' |
| 740 | if (/^-?\d+(\.\d+)?$/.test(v)) | |
| 1098 | if (isNumericLiteral(v)) | |
| 741 | 1099 | return 'number' |
| 742 | 1100 | if ((v.startsWith('\'') && v.endsWith('\'')) || (v.startsWith('"') && v.endsWith('"'))) |
| 743 | 1101 | return 'string' |
| @@ -753,7 +1111,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 753 | 1111 | const v = value.trim() |
| 754 | 1112 | if (v === 'true' || v === 'false') |
| 755 | 1113 | return v |
| 756 | if (/^-?\d+(\.\d+)?$/.test(v)) | |
| 1114 | if (isNumericLiteral(v)) | |
| 757 | 1115 | return v |
| 758 | 1116 | if ((v.startsWith('\'') && v.endsWith('\'')) || (v.startsWith('"') && v.endsWith('"'))) |
| 759 | 1117 | return v |
| @@ -764,8 +1122,11 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 764 | 1122 | function extractAssertion(initText: string): string | null { |
| 765 | 1123 | if (initText.endsWith('as const')) |
| 766 | 1124 | return null |
| 767 | const m = initText.match(/\bas\s+(.+)$/) | |
| 768 | return m ? m[1].trim() : null | |
| 1125 | // Find last ' as ' — the spaces already serve as word boundaries | |
| 1126 | const asIdx = initText.lastIndexOf(' as ') | |
| 1127 | if (asIdx === -1) return null | |
| 1128 | const afterAs = initText.slice(asIdx + 4).trim() | |
| 1129 | return afterAs || null | |
| 769 | 1130 | } |
| 770 | 1131 | |
| 771 | 1132 | /** Extract return type annotation `: ReturnType` after params, before `{` or `;` */ |
| @@ -877,16 +1238,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 877 | 1238 | |
| 878 | 1239 | // Build DTS text |
| 879 | 1240 | const dtsParams = buildDtsParams(rawParams) |
| 880 | const parts: string[] = [] | |
| 881 | if (isExported) | |
| 882 | parts.push('export ') | |
| 883 | parts.push('declare function ') | |
| 884 | parts.push(name || 'default') | |
| 885 | parts.push(generics) | |
| 886 | parts.push(dtsParams) | |
| 887 | parts.push(': ', returnType, ';') | |
| 888 | ||
| 889 | const text = parts.join('') | |
| 1241 | const text = (isExported ? 'export ' : '') + 'declare function ' + (name || 'default') + generics + dtsParams + ': ' + returnType + ';' | |
| 890 | 1242 | const comments = extractLeadingComments(declStart) |
| 891 | 1243 | |
| 892 | 1244 | // Record index if this function had a body (implementation signature for overloads) |
| @@ -1077,16 +1429,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 1077 | 1429 | const body = cleanBraceBlock(rawBody) |
| 1078 | 1430 | |
| 1079 | 1431 | // Build DTS text |
| 1080 | const parts: string[] = [] | |
| 1081 | if (isExported) | |
| 1082 | parts.push('export ') | |
| 1083 | parts.push('declare interface ', name) | |
| 1084 | parts.push(generics) | |
| 1085 | if (extendsClause) | |
| 1086 | parts.push(' extends ', extendsClause) | |
| 1087 | parts.push(' ', body) | |
| 1088 | ||
| 1089 | const text = parts.join('') | |
| 1432 | const text = (isExported ? 'export ' : '') + 'declare interface ' + name + generics + (extendsClause ? ' extends ' + extendsClause : '') + ' ' + body | |
| 1090 | 1433 | const comments = extractLeadingComments(declStart) |
| 1091 | 1434 | |
| 1092 | 1435 | return { |
| @@ -1143,12 +1486,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 1143 | 1486 | pos++ |
| 1144 | 1487 | |
| 1145 | 1488 | // Build DTS text |
| 1146 | const parts: string[] = [] | |
| 1147 | if (isExported) | |
| 1148 | parts.push('export ') | |
| 1149 | parts.push('type ', name, generics, ' = ', typeBody) | |
| 1150 | ||
| 1151 | const text = parts.join('') | |
| 1489 | const text = (isExported ? 'export ' : '') + 'type ' + name + generics + ' = ' + typeBody | |
| 1152 | 1490 | const comments = extractLeadingComments(declStart) |
| 1153 | 1491 | |
| 1154 | 1492 | return { |
| @@ -1262,20 +1600,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 1262 | 1600 | const classBody = buildClassBodyDts() |
| 1263 | 1601 | |
| 1264 | 1602 | // Build DTS text |
| 1265 | const parts: string[] = [] | |
| 1266 | if (isExported) | |
| 1267 | parts.push('export ') | |
| 1268 | parts.push('declare ') | |
| 1269 | if (isAbstract) | |
| 1270 | parts.push('abstract ') | |
| 1271 | parts.push('class ', name, generics) | |
| 1272 | if (extendsClause) | |
| 1273 | parts.push(' extends ', extendsClause) | |
| 1274 | if (implementsList && implementsList.length > 0) | |
| 1275 | parts.push(' implements ', implementsList.join(', ')) | |
| 1276 | parts.push(' ', classBody) | |
| 1277 | ||
| 1278 | const text = parts.join('') | |
| 1603 | const text = (isExported ? 'export ' : '') + 'declare ' + (isAbstract ? 'abstract ' : '') + 'class ' + name + generics + (extendsClause ? ' extends ' + extendsClause : '') + (implementsList && implementsList.length > 0 ? ' implements ' + implementsList.join(', ') : '') + ' ' + classBody | |
| 1279 | 1604 | const comments = extractLeadingComments(declStart) |
| 1280 | 1605 | |
| 1281 | 1606 | return { |
| @@ -1958,7 +2283,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 1958 | 2283 | skipWhitespaceAndComments() |
| 1959 | 2284 | const nname = readIdent() |
| 1960 | 2285 | skipWhitespaceAndComments() |
| 1961 | const body = buildNamespaceBodyDts(`${indent} `) | |
| 2286 | const body = buildNamespaceBodyDts(indent) | |
| 1962 | 2287 | lines.push(`${indent}${prefix}${kw} ${nname} ${body}`) |
| 1963 | 2288 | } |
| 1964 | 2289 | else if (matchWord('abstract')) { |
| @@ -1995,7 +2320,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 1995 | 2320 | skipToStatementEnd() |
| 1996 | 2321 | const defText = source.slice(defStart, pos).trim().replace(/;$/, '') |
| 1997 | 2322 | if (defText) { |
| 1998 | lines.push(`${indent}export default ${defText}`) | |
| 2323 | lines.push(`${indent}export default ${defText};`) | |
| 1999 | 2324 | } |
| 2000 | 2325 | } |
| 2001 | 2326 | else if (!hasExport && (source.charCodeAt(pos) === CH_SQUOTE || source.charCodeAt(pos) === CH_DQUOTE || source.charCodeAt(pos) === CH_BACKTICK)) { |
| @@ -2055,13 +2380,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2055 | 2380 | const body = pos < len && source.charCodeAt(pos) === CH_LBRACE ? buildNamespaceBodyDts() : '{}' |
| 2056 | 2381 | |
| 2057 | 2382 | // Build DTS text |
| 2058 | const parts: string[] = [] | |
| 2059 | if (isExported) | |
| 2060 | parts.push('export ') | |
| 2061 | parts.push('declare ') | |
| 2062 | parts.push(keyword, ' ', name, ' ', body) | |
| 2063 | ||
| 2064 | const text = parts.join('') | |
| 2383 | const text = (isExported ? 'export ' : '') + 'declare ' + keyword + ' ' + name + ' ' + body | |
| 2065 | 2384 | const comments = extractLeadingComments(declStart) |
| 2066 | 2385 | const isAmbient = name.startsWith('\'') || name.startsWith('"') |
| 2067 | 2386 | |
| @@ -2077,7 +2396,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2077 | 2396 | } |
| 2078 | 2397 | } |
| 2079 | 2398 | |
| 2080 | // --- Main scan loop --- | |
| 2399 | // --- Main scan loop (first-char dispatch for speed) --- | |
| 2081 | 2400 | |
| 2082 | 2401 | while (pos < len) { |
| 2083 | 2402 | skipWhitespaceAndComments() |
| @@ -2085,25 +2404,28 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2085 | 2404 | break |
| 2086 | 2405 | |
| 2087 | 2406 | const stmtStart = pos |
| 2407 | const ch0 = source.charCodeAt(pos) | |
| 2088 | 2408 | |
| 2089 | // Detect keywords at top level | |
| 2090 | if (matchWord('import')) { | |
| 2409 | // Fast dispatch on first character of keyword | |
| 2410 | if (ch0 === 105 /* i */ && matchWord('import')) { | |
| 2091 | 2411 | declarations.push(extractImport(stmtStart)) |
| 2092 | 2412 | } |
| 2093 | else if (matchWord('export')) { | |
| 2413 | else if (ch0 === 101 /* e */ && matchWord('export')) { | |
| 2094 | 2414 | pos += 6 // skip 'export' |
| 2095 | 2415 | skipWhitespaceAndComments() |
| 2416 | const ech = source.charCodeAt(pos) | |
| 2096 | 2417 | |
| 2097 | if (matchWord('default')) { | |
| 2418 | if (ech === 100 /* d */ && matchWord('default')) { | |
| 2098 | 2419 | pos += 7 |
| 2099 | 2420 | skipWhitespaceAndComments() |
| 2421 | const dch = source.charCodeAt(pos) | |
| 2100 | 2422 | |
| 2101 | if (matchWord('function')) { | |
| 2423 | if (dch === 102 /* f */ && matchWord('function')) { | |
| 2102 | 2424 | const decl = extractFunction(stmtStart, true, false, true) |
| 2103 | 2425 | if (decl) |
| 2104 | 2426 | declarations.push(decl) |
| 2105 | 2427 | } |
| 2106 | else if (matchWord('async') && peekAfterWord('async') !== CH_SEMI) { | |
| 2428 | else if (dch === 97 /* a */ && matchWord('async') && peekAfterWord('async') !== CH_SEMI) { | |
| 2107 | 2429 | pos += 5 |
| 2108 | 2430 | skipWhitespaceAndComments() |
| 2109 | 2431 | if (matchWord('function')) { |
| @@ -2113,7 +2435,6 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2113 | 2435 | } |
| 2114 | 2436 | else { |
| 2115 | 2437 | // export default async expression |
| 2116 | const text = source.slice(stmtStart, pos).trim() | |
| 2117 | 2438 | skipToStatementEnd() |
| 2118 | 2439 | const fullText = source.slice(stmtStart, pos).trim() |
| 2119 | 2440 | declarations.push({ |
| @@ -2127,11 +2448,11 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2127 | 2448 | }) |
| 2128 | 2449 | } |
| 2129 | 2450 | } |
| 2130 | else if (matchWord('class')) { | |
| 2451 | else if (dch === 99 /* c */ && matchWord('class')) { | |
| 2131 | 2452 | const decl = extractClass(stmtStart, true, false) |
| 2132 | 2453 | declarations.push(decl) |
| 2133 | 2454 | } |
| 2134 | else if (matchWord('abstract')) { | |
| 2455 | else if (dch === 97 /* a */ && matchWord('abstract')) { | |
| 2135 | 2456 | pos += 8 |
| 2136 | 2457 | skipWhitespaceAndComments() |
| 2137 | 2458 | if (matchWord('class')) { |
| @@ -2156,7 +2477,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2156 | 2477 | }) |
| 2157 | 2478 | } |
| 2158 | 2479 | } |
| 2159 | else if (matchWord('type')) { | |
| 2480 | else if (ech === 116 /* t */ && matchWord('type')) { | |
| 2160 | 2481 | // Could be `export type Name = ...` or `export type { ... }` |
| 2161 | 2482 | const savedPos = pos |
| 2162 | 2483 | pos += 4 |
| @@ -2199,16 +2520,16 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2199 | 2520 | declarations.push(decl) |
| 2200 | 2521 | } |
| 2201 | 2522 | } |
| 2202 | else if (matchWord('interface')) { | |
| 2523 | else if (ech === 105 /* i */ && matchWord('interface')) { | |
| 2203 | 2524 | const decl = extractInterface(stmtStart, true) |
| 2204 | 2525 | declarations.push(decl) |
| 2205 | 2526 | } |
| 2206 | else if (matchWord('function')) { | |
| 2527 | else if (ech === 102 /* f */ && matchWord('function')) { | |
| 2207 | 2528 | const decl = extractFunction(stmtStart, true, false, false) |
| 2208 | 2529 | if (decl) |
| 2209 | 2530 | declarations.push(decl) |
| 2210 | 2531 | } |
| 2211 | else if (matchWord('async')) { | |
| 2532 | else if (ech === 97 /* a */ && matchWord('async')) { | |
| 2212 | 2533 | pos += 5 |
| 2213 | 2534 | skipWhitespaceAndComments() |
| 2214 | 2535 | if (matchWord('function')) { |
| @@ -2220,62 +2541,67 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2220 | 2541 | skipToStatementEnd() |
| 2221 | 2542 | } |
| 2222 | 2543 | } |
| 2223 | else if (matchWord('class')) { | |
| 2224 | const decl = extractClass(stmtStart, true, false) | |
| 2225 | declarations.push(decl) | |
| 2226 | } | |
| 2227 | else if (matchWord('abstract')) { | |
| 2228 | pos += 8 | |
| 2229 | skipWhitespaceAndComments() | |
| 2544 | else if (ech === 99 /* c */) { | |
| 2230 | 2545 | if (matchWord('class')) { |
| 2231 | const decl = extractClass(stmtStart, true, true) | |
| 2546 | const decl = extractClass(stmtStart, true, false) | |
| 2232 | 2547 | declarations.push(decl) |
| 2233 | 2548 | } |
| 2234 | } | |
| 2235 | else if (matchWord('const')) { | |
| 2236 | // Could be 'export const enum' or 'export const ...' | |
| 2237 | const savedPos = pos | |
| 2238 | pos += 5 | |
| 2239 | skipWhitespaceAndComments() | |
| 2240 | if (matchWord('enum')) { | |
| 2241 | pos = savedPos + 5 | |
| 2549 | else if (matchWord('const')) { | |
| 2550 | // Could be 'export const enum' or 'export const ...' | |
| 2551 | const savedPos = pos | |
| 2552 | pos += 5 | |
| 2242 | 2553 | skipWhitespaceAndComments() |
| 2243 | const decl = extractEnum(stmtStart, true, true) | |
| 2244 | declarations.push(decl) | |
| 2554 | if (matchWord('enum')) { | |
| 2555 | pos = savedPos + 5 | |
| 2556 | skipWhitespaceAndComments() | |
| 2557 | const decl = extractEnum(stmtStart, true, true) | |
| 2558 | declarations.push(decl) | |
| 2559 | } | |
| 2560 | else { | |
| 2561 | pos = savedPos | |
| 2562 | const decls = extractVariable(stmtStart, 'const', true) | |
| 2563 | declarations.push(...decls) | |
| 2564 | } | |
| 2245 | 2565 | } |
| 2246 | 2566 | else { |
| 2247 | pos = savedPos | |
| 2248 | const decls = extractVariable(stmtStart, 'const', true) | |
| 2249 | declarations.push(...decls) | |
| 2567 | skipToStatementEnd() | |
| 2568 | } | |
| 2569 | } | |
| 2570 | else if (ech === 97 /* a */ && matchWord('abstract')) { | |
| 2571 | pos += 8 | |
| 2572 | skipWhitespaceAndComments() | |
| 2573 | if (matchWord('class')) { | |
| 2574 | const decl = extractClass(stmtStart, true, true) | |
| 2575 | declarations.push(decl) | |
| 2250 | 2576 | } |
| 2251 | 2577 | } |
| 2252 | else if (matchWord('let')) { | |
| 2578 | else if (ech === 108 /* l */ && matchWord('let')) { | |
| 2253 | 2579 | const decls = extractVariable(stmtStart, 'let', true) |
| 2254 | 2580 | declarations.push(...decls) |
| 2255 | 2581 | } |
| 2256 | else if (matchWord('var')) { | |
| 2582 | else if (ech === 118 /* v */ && matchWord('var')) { | |
| 2257 | 2583 | const decls = extractVariable(stmtStart, 'var', true) |
| 2258 | 2584 | declarations.push(...decls) |
| 2259 | 2585 | } |
| 2260 | else if (matchWord('enum')) { | |
| 2586 | else if (ech === 101 /* e */ && matchWord('enum')) { | |
| 2261 | 2587 | const decl = extractEnum(stmtStart, true, false) |
| 2262 | 2588 | declarations.push(decl) |
| 2263 | 2589 | } |
| 2264 | else if (matchWord('declare')) { | |
| 2590 | else if (ech === 100 /* d */ && matchWord('declare')) { | |
| 2265 | 2591 | pos += 7 |
| 2266 | 2592 | skipWhitespaceAndComments() |
| 2267 | 2593 | // export declare ... |
| 2268 | 2594 | handleDeclare(stmtStart, true) |
| 2269 | 2595 | } |
| 2270 | else if (matchWord('namespace')) { | |
| 2596 | else if (ech === 110 /* n */ && matchWord('namespace')) { | |
| 2271 | 2597 | const decl = extractModule(stmtStart, true, 'namespace') |
| 2272 | 2598 | declarations.push(decl) |
| 2273 | 2599 | } |
| 2274 | else if (matchWord('module')) { | |
| 2600 | else if (ech === 109 /* m */ && matchWord('module')) { | |
| 2275 | 2601 | const decl = extractModule(stmtStart, true, 'module') |
| 2276 | 2602 | declarations.push(decl) |
| 2277 | 2603 | } |
| 2278 | else if (source.charCodeAt(pos) === CH_LBRACE) { | |
| 2604 | else if (ech === CH_LBRACE) { | |
| 2279 | 2605 | // export { ... } or export { ... } from '...' |
| 2280 | 2606 | skipExportBraces() |
| 2281 | 2607 | const text = source.slice(stmtStart, pos).trim() |
| @@ -2292,7 +2618,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2292 | 2618 | end: pos, |
| 2293 | 2619 | }) |
| 2294 | 2620 | } |
| 2295 | else if (source.charCodeAt(pos) === CH_STAR) { | |
| 2621 | else if (ech === CH_STAR) { | |
| 2296 | 2622 | // export * from '...' |
| 2297 | 2623 | skipExportStar() |
| 2298 | 2624 | const text = source.slice(stmtStart, pos).trim() |
| @@ -2326,56 +2652,96 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2326 | 2652 | } |
| 2327 | 2653 | } |
| 2328 | 2654 | } |
| 2329 | else if (matchWord('declare')) { | |
| 2655 | else if (ch0 === 100 /* d */ && matchWord('declare')) { | |
| 2330 | 2656 | pos += 7 |
| 2331 | 2657 | skipWhitespaceAndComments() |
| 2332 | 2658 | handleDeclare(stmtStart, false) |
| 2333 | 2659 | } |
| 2334 | else if (matchWord('interface')) { | |
| 2660 | else if (ch0 === 105 /* i */ && matchWord('interface')) { | |
| 2335 | 2661 | // Non-exported interface |
| 2336 | 2662 | const decl = extractInterface(stmtStart, false) |
| 2337 | 2663 | nonExportedTypes.set(decl.name, decl) |
| 2338 | 2664 | } |
| 2339 | else if (matchWord('type')) { | |
| 2665 | else if (ch0 === 116 /* t */ && matchWord('type')) { | |
| 2340 | 2666 | // Non-exported type alias |
| 2341 | 2667 | const decl = extractTypeAlias(stmtStart, false) |
| 2342 | 2668 | nonExportedTypes.set(decl.name, decl) |
| 2343 | 2669 | declarations.push(decl) |
| 2344 | 2670 | } |
| 2345 | else if (matchWord('function') || matchWord('async')) { | |
| 2671 | else if (ch0 === 102 /* f */ && matchWord('function')) { | |
| 2346 | 2672 | // Non-exported function — skip for DTS |
| 2347 | 2673 | skipToStatementEnd() |
| 2348 | 2674 | } |
| 2349 | else if (matchWord('class') || matchWord('abstract')) { | |
| 2350 | // Non-exported class | |
| 2351 | if (matchWord('abstract')) { | |
| 2675 | else if (ch0 === 97 /* a */) { | |
| 2676 | if (matchWord('async')) { | |
| 2677 | // Non-exported async function — skip for DTS | |
| 2678 | skipToStatementEnd() | |
| 2679 | } | |
| 2680 | else if (matchWord('abstract')) { | |
| 2352 | 2681 | pos += 8 |
| 2353 | 2682 | skipWhitespaceAndComments() |
| 2683 | if (matchWord('class')) { | |
| 2684 | const decl = extractClass(stmtStart, false, false) | |
| 2685 | nonExportedTypes.set(decl.name, decl) | |
| 2686 | declarations.push(decl) | |
| 2687 | } | |
| 2688 | else { | |
| 2689 | skipToStatementEnd() | |
| 2690 | } | |
| 2691 | } | |
| 2692 | else { | |
| 2693 | pos++ | |
| 2694 | skipToStatementEnd() | |
| 2354 | 2695 | } |
| 2696 | } | |
| 2697 | else if (ch0 === 99 /* c */) { | |
| 2355 | 2698 | if (matchWord('class')) { |
| 2356 | 2699 | const decl = extractClass(stmtStart, false, false) |
| 2357 | 2700 | nonExportedTypes.set(decl.name, decl) |
| 2358 | 2701 | declarations.push(decl) |
| 2359 | 2702 | } |
| 2703 | else if (matchWord('const')) { | |
| 2704 | // Check for 'const enum' before skipping as variable | |
| 2705 | const savedPos = pos | |
| 2706 | pos += 5 | |
| 2707 | skipWhitespaceAndComments() | |
| 2708 | if (matchWord('enum')) { | |
| 2709 | pos = savedPos + 5 | |
| 2710 | skipWhitespaceAndComments() | |
| 2711 | const decl = extractEnum(stmtStart, false, true) | |
| 2712 | nonExportedTypes.set(decl.name, decl) | |
| 2713 | declarations.push(decl) | |
| 2714 | } | |
| 2715 | else { | |
| 2716 | // Non-exported const variable — skip for DTS | |
| 2717 | pos = savedPos | |
| 2718 | skipToStatementEnd() | |
| 2719 | } | |
| 2720 | } | |
| 2360 | 2721 | else { |
| 2722 | pos++ | |
| 2361 | 2723 | skipToStatementEnd() |
| 2362 | 2724 | } |
| 2363 | 2725 | } |
| 2364 | else if (matchWord('enum')) { | |
| 2726 | else if (ch0 === 101 /* e */ && matchWord('enum')) { | |
| 2365 | 2727 | // Non-exported enum |
| 2366 | 2728 | const decl = extractEnum(stmtStart, false, false) |
| 2367 | 2729 | nonExportedTypes.set(decl.name, decl) |
| 2368 | 2730 | declarations.push(decl) |
| 2369 | 2731 | } |
| 2370 | else if (matchWord('const') || matchWord('let') || matchWord('var')) { | |
| 2732 | else if (ch0 === 108 /* l */ && matchWord('let')) { | |
| 2371 | 2733 | // Non-exported variable — skip for DTS |
| 2372 | 2734 | skipToStatementEnd() |
| 2373 | 2735 | } |
| 2374 | else if (matchWord('module')) { | |
| 2736 | else if (ch0 === 118 /* v */ && matchWord('var')) { | |
| 2737 | // Non-exported variable — skip for DTS | |
| 2738 | skipToStatementEnd() | |
| 2739 | } | |
| 2740 | else if (ch0 === 109 /* m */ && matchWord('module')) { | |
| 2375 | 2741 | const decl = extractModule(stmtStart, false, 'module') |
| 2376 | 2742 | declarations.push(decl) |
| 2377 | 2743 | } |
| 2378 | else if (matchWord('namespace')) { | |
| 2744 | else if (ch0 === 110 /* n */ && matchWord('namespace')) { | |
| 2379 | 2745 | const decl = extractModule(stmtStart, false, 'namespace') |
| 2380 | 2746 | declarations.push(decl) |
| 2381 | 2747 | } |
| @@ -2539,7 +2905,7 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2539 | 2905 | // declare global { ... } |
| 2540 | 2906 | pos += 6 |
| 2541 | 2907 | skipWhitespaceAndComments() |
| 2542 | const body = pos < len && source.charCodeAt(pos) === CH_LBRACE ? extractBraceBlock() : '{}' | |
| 2908 | const body = pos < len && source.charCodeAt(pos) === CH_LBRACE ? buildNamespaceBodyDts() : '{}' | |
| 2543 | 2909 | const text = `declare global ${body}` |
| 2544 | 2910 | const comments = extractLeadingComments(stmtStart) |
| 2545 | 2911 | declarations.push({ |
| @@ -2558,27 +2924,69 @@ export function scanDeclarations(source: string, filename: string, keepComments: | ||
| 2558 | 2924 | } |
| 2559 | 2925 | } |
| 2560 | 2926 | |
| 2927 | /** Check if name appears as a whole word in text (fast includes + boundary check) */ | |
| 2928 | function isWordInText(name: string, text: string): boolean { | |
| 2929 | let searchFrom = 0 | |
| 2930 | const nameLen = name.length | |
| 2931 | while (searchFrom < text.length) { | |
| 2932 | const idx = text.indexOf(name, searchFrom) | |
| 2933 | if (idx === -1) return false | |
| 2934 | // Check word boundaries | |
| 2935 | const before = idx > 0 ? text.charCodeAt(idx - 1) : 32 | |
| 2936 | const after = idx + nameLen < text.length ? text.charCodeAt(idx + nameLen) : 32 | |
| 2937 | const beforeOk = !((before >= 65 && before <= 90) || (before >= 97 && before <= 122) || (before >= 48 && before <= 57) || before === 95 || before === 36) | |
| 2938 | const afterOk = !((after >= 65 && after <= 90) || (after >= 97 && after <= 122) || (after >= 48 && after <= 57) || after === 95 || after === 36) | |
| 2939 | if (beforeOk && afterOk) return true | |
| 2940 | searchFrom = idx + 1 | |
| 2941 | } | |
| 2942 | return false | |
| 2943 | } | |
| 2944 | ||
| 2561 | 2945 | /** Resolve non-exported types that are referenced by exported declarations */ |
| 2562 | 2946 | function resolveReferencedTypes(declarations: Declaration[], nonExportedTypes: Map<string, Declaration>): void { |
| 2563 | // Build combined text of all exported declarations | |
| 2564 | const exportedTexts: string[] = [] | |
| 2565 | for (const decl of declarations) { | |
| 2566 | if (decl.isExported && decl.kind !== 'import') { | |
| 2567 | exportedTexts.push(decl.text) | |
| 2947 | // Iteratively resolve referenced non-exported types (transitive closure) | |
| 2948 | const resolved = new Set<string>() | |
| 2949 | const declNames = new Set<string>() | |
| 2950 | for (const d of declarations) declNames.add(d.name) | |
| 2951 | ||
| 2952 | for (;;) { | |
| 2953 | // Build combined text of all current declarations (excluding imports) | |
| 2954 | let combinedText = '' | |
| 2955 | for (let i = 0; i < declarations.length; i++) { | |
| 2956 | if (declarations[i].kind !== 'import') { | |
| 2957 | combinedText += declarations[i].text | |
| 2958 | combinedText += '\n' | |
| 2959 | } | |
| 2568 | 2960 | } |
| 2569 | } | |
| 2570 | const combinedText = exportedTexts.join('\n') | |
| 2571 | ||
| 2572 | // Check each non-exported type | |
| 2573 | for (const [name, decl] of nonExportedTypes) { | |
| 2574 | // Simple word-boundary check | |
| 2575 | const re = new RegExp(`\\b${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`) | |
| 2576 | if (re.test(combinedText)) { | |
| 2577 | // Check it's not already in declarations | |
| 2578 | const existing = declarations.find(d => d.name === name) | |
| 2579 | if (!existing) { | |
| 2580 | declarations.push(decl) | |
| 2961 | ||
| 2962 | // Collect referenced non-exported types not yet resolved | |
| 2963 | const toInsert: Declaration[] = [] | |
| 2964 | for (const [name, decl] of nonExportedTypes) { | |
| 2965 | if (resolved.has(name)) | |
| 2966 | continue | |
| 2967 | // Fast path: includes() check first (avoids RegExp entirely for non-matches) | |
| 2968 | if (combinedText.includes(name) && isWordInText(name, combinedText)) { | |
| 2969 | if (!declNames.has(name)) { | |
| 2970 | toInsert.push(decl) | |
| 2971 | declNames.add(name) | |
| 2972 | } | |
| 2973 | resolved.add(name) | |
| 2974 | } | |
| 2975 | } | |
| 2976 | ||
| 2977 | if (toInsert.length === 0) | |
| 2978 | break | |
| 2979 | ||
| 2980 | // Insert at correct source positions to preserve declaration order | |
| 2981 | for (const decl of toInsert) { | |
| 2982 | let insertIdx = declarations.length | |
| 2983 | for (let i = 0; i < declarations.length; i++) { | |
| 2984 | if (declarations[i].start > decl.start) { | |
| 2985 | insertIdx = i | |
| 2986 | break | |
| 2987 | } | |
| 2581 | 2988 | } |
| 2989 | declarations.splice(insertIdx, 0, decl) | |
| 2582 | 2990 | } |
| 2583 | 2991 | } |
| 2584 | 2992 | } |