Skip to content

Commit

Permalink
Do not rename a node if known global name refers to this a symbol of …
Browse files Browse the repository at this point in the history
…that node

Fixes #297
  • Loading branch information
timocov committed Jan 27, 2024
1 parent 43c2058 commit c0a7007
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/collisions-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getClosestModuleLikeNode,
getDeclarationNameSymbol,
getDeclarationsForSymbol,
hasGlobalName,
resolveGlobalName,
} from './helpers/typescript';
import { verboseLog } from './logger';

Expand Down Expand Up @@ -250,9 +250,12 @@ export class CollisionsResolver {

let nameIndex = collisionSymbols.size;
let newName = collisionSymbols.size === 0 ? symbolName : `${symbolName}$${nameIndex}`;
while (hasGlobalName(this.typeChecker, newName)) {

let resolvedGlobalSymbol = resolveGlobalName(this.typeChecker, newName);
while (resolvedGlobalSymbol !== undefined && resolvedGlobalSymbol !== identifierSymbol) {
nameIndex += 1;
newName = `${symbolName}$${nameIndex}`;
resolvedGlobalSymbol = resolveGlobalName(this.typeChecker, newName);
}

collisionSymbols.set(identifierSymbol, newName);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ export function getDeclarationsForExportedValues(exp: ts.ExportAssignment | ts.E
return getDeclarationsForSymbol(symbol);
}

export function hasGlobalName(typeChecker: ts.TypeChecker, name: string): boolean {
export function resolveGlobalName(typeChecker: ts.TypeChecker, name: string): ts.Symbol | undefined {
interface Ts54CompatTypeChecker extends ts.TypeChecker {
resolveName(name: string, location: ts.Node | undefined, meaning: ts.SymbolFlags, excludeGlobals: boolean): ts.Symbol | undefined;
}
Expand All @@ -664,5 +664,5 @@ export function hasGlobalName(typeChecker: ts.TypeChecker, name: string): boolea
const tsSymbolFlagsAll = /* ts.SymbolFlags.All */ -1 as ts.SymbolFlags;

// see https://github.com/microsoft/TypeScript/pull/56932
return (typeChecker as Ts54CompatTypeChecker).resolveName(name, undefined, tsSymbolFlagsAll, false) !== undefined;
return (typeChecker as Ts54CompatTypeChecker).resolveName(name, undefined, tsSymbolFlagsAll, false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ declare namespace MyModule {
field2: number;
}
}
interface LibInterface$1 {
interface LibInterface {
field: number;
}
export const myLib: LibInterface$1;
export const myLib: LibInterface;

export {
MyModule as newName,
Expand Down

0 comments on commit c0a7007

Please sign in to comment.