also looking at this
fix(bundle): leave template literal text alone when renaming identifiers
#3131
3 files
+108
-3
| @@ -332,9 +332,9 @@ function parseModule(file: string, dts: string): ModuleInfo { | ||
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | /** |
| 335 | * Rename identifier references in declaration text. Strings, comments, | |
| 336 | * qualified-name members (`a.b`) and property or parameter names (`{ b: T }`, | |
| 337 | * `(b: T)`) are left alone. | |
| 335 | * Rename identifier references in declaration text. Strings, template literal | |
| 336 | * text (not its `${}` placeholders), comments, qualified-name members (`a.b`) | |
| 337 | * and property or parameter names (`{ b: T }`, `(b: T)`) are left alone. | |
| 338 | 338 | */ |
| 339 | 339 | export function renameIdentifiers(text: string, renames: Map<string, string>): string { |
| 340 | 340 | if (renames.size === 0) return text |
| @@ -342,8 +342,47 @@ export function renameIdentifiers(text: string, renames: Map<string, string>): s | ||
| 342 | 342 | let i = 0 |
| 343 | 343 | const n = text.length |
| 344 | 344 | let lastSignificant = '\n' |
| 345 | // One entry per template literal or placeholder the scan is in: -1 for | |
| 346 | // template text, otherwise the braces open inside that placeholder. | |
| 347 | const templates: number[] = [] | |
| 345 | 348 | while (i < n) { |
| 346 | 349 | const c = text[i] |
| 350 | if (templates.length > 0 && templates[templates.length - 1] === -1) { | |
| 351 | if (c === '\\') { | |
| 352 | out += text.slice(i, i + 2) | |
| 353 | i += 2 | |
| 354 | } | |
| 355 | else if (c === '`') { | |
| 356 | templates.pop() | |
| 357 | out += c | |
| 358 | lastSignificant = c | |
| 359 | i++ | |
| 360 | } | |
| 361 | else if (c === '$' && text[i + 1] === '{') { | |
| 362 | templates.push(0) | |
| 363 | out += '${' | |
| 364 | lastSignificant = '{' | |
| 365 | i += 2 | |
| 366 | } | |
| 367 | else { | |
| 368 | out += c | |
| 369 | i++ | |
| 370 | } | |
| 371 | continue | |
| 372 | } | |
| 373 | if (c === '`') { | |
| 374 | templates.push(-1) | |
| 375 | out += c | |
| 376 | i++ | |
| 377 | continue | |
| 378 | } | |
| 379 | if (templates.length > 0 && c === '{') { | |
| 380 | templates[templates.length - 1]++ | |
| 381 | } | |
| 382 | else if (templates.length > 0 && c === '}') { | |
| 383 | if (templates[templates.length - 1] === 0) templates.pop() | |
| 384 | else templates[templates.length - 1]-- | |
| 385 | } | |
| 347 | 386 | if (c === '/' && text[i + 1] === '*') { |
| 348 | 387 | const end = text.indexOf('*/', i + 2) |
| 349 | 388 | const stop = end === -1 ? n : end + 2 |