ReviewOS

also looking at this

stacks/dtsx

fix(bundle): leave template literal text alone when renaming identifiers

#3131
Open mkantautas wants to merge fix/bundle-template-literal-rename into main
3 files +108 -3
packages/dtsx/src/bundle-linker.tsmodified+42-3
Changes to packages/dtsx/src/bundle-linker.ts
@@ -332,9 +332,9 @@ function parseModule(file: string, dts: string): ModuleInfo {
332332}
333333
334334/**
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.
338338 */
339339export function renameIdentifiers(text: string, renames: Map<string, string>): string {
340340 if (renames.size === 0) return text
@@ -342,8 +342,47 @@ export function renameIdentifiers(text: string, renames: Map<string, string>): s
342342 let i = 0
343343 const n = text.length
344344 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[] = []
345348 while (i < n) {
346349 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 }
347386 if (c === '/' && text[i + 1] === '*') {
348387 const end = text.indexOf('*/', i + 2)
349388 const stop = end === -1 ? n : end + 2