Skip to content

Commit

Permalink
Rename type parameters when they are shadowed. (#61342)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirtitian authored Mar 4, 2025
1 parent 47442d9 commit c85e626
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,15 +1122,16 @@ export function createSyntacticTypeNodeBuilder(
);
}
function reuseTypeParameters(typeParameters: NodeArray<TypeParameterDeclaration> | undefined, context: SyntacticTypeNodeBuilderContext) {
return typeParameters?.map(tp =>
factory.updateTypeParameterDeclaration(
return typeParameters?.map(tp => {
const { node: tpName } = resolver.trackExistingEntityName(context, tp.name);
return factory.updateTypeParameterDeclaration(
tp,
tp.modifiers?.map(m => reuseNode(context, m)),
reuseNode(context, tp.name),
tpName,
serializeExistingTypeNodeWithFallback(tp.constraint, context),
serializeExistingTypeNodeWithFallback(tp.default, context),
)
);
);
});
}

function typeFromObjectLiteralMethod(method: MethodDeclaration, name: PropertyName, context: SyntacticTypeNodeBuilderContext, isConstContext: boolean) {
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/declarationEmitShadowing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////

//// [declarationEmitShadowing.ts]
export class A<T = any> {
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
return null as any
}
}

export function needsRenameForShadowing<T>() {
type A = T
return function O<T>(t: A, t2: T) {
}
}


//// [declarationEmitShadowing.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.A = void 0;
exports.needsRenameForShadowing = needsRenameForShadowing;
var A = /** @class */ (function () {
function A() {
this.ShadowedButDoesNotRequireRenaming = function () {
return null;
};
}
return A;
}());
exports.A = A;
function needsRenameForShadowing() {
return function O(t, t2) {
};
}


//// [declarationEmitShadowing.d.ts]
export declare class A<T = any> {
readonly ShadowedButDoesNotRequireRenaming: <T_1>() => T_1;
}
export declare function needsRenameForShadowing<T>(): <T_1>(t: T, t2: T_1) => void;
34 changes: 34 additions & 0 deletions tests/baselines/reference/declarationEmitShadowing.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////

=== declarationEmitShadowing.ts ===
export class A<T = any> {
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 0, 0))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 0, 15))

public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
>ShadowedButDoesNotRequireRenaming : Symbol(A.ShadowedButDoesNotRequireRenaming, Decl(declarationEmitShadowing.ts, 0, 25))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 1, 55))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 1, 55))

return null as any
}
}

export function needsRenameForShadowing<T>() {
>needsRenameForShadowing : Symbol(needsRenameForShadowing, Decl(declarationEmitShadowing.ts, 4, 1))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 6, 40))

type A = T
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 6, 46))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 6, 40))

return function O<T>(t: A, t2: T) {
>O : Symbol(O, Decl(declarationEmitShadowing.ts, 8, 8))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 8, 20))
>t : Symbol(t, Decl(declarationEmitShadowing.ts, 8, 23))
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 6, 46))
>t2 : Symbol(t2, Decl(declarationEmitShadowing.ts, 8, 28))
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 8, 20))
}
}

38 changes: 38 additions & 0 deletions tests/baselines/reference/declarationEmitShadowing.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////

=== declarationEmitShadowing.ts ===
export class A<T = any> {
>A : A<T>
> : ^^^^

public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
>ShadowedButDoesNotRequireRenaming : <T_1>() => T_1
> : ^^^^^^^^^^^
><T>(): T => { return null as any } : <T_1>() => T_1
> : ^^^^^^^^^^^

return null as any
>null as any : any
}
}

export function needsRenameForShadowing<T>() {
>needsRenameForShadowing : <T>() => <T_1>(t: T, t2: T_1) => void
> : ^ ^^^^^^^^ ^^ ^^^ ^^^^^^^^^

type A = T
>A : T
> : ^

return function O<T>(t: A, t2: T) {
>function O<T>(t: A, t2: T) { } : <T_1>(t: T, t2: T_1) => void
> : ^^^^^^ ^^^^^ ^^ ^^^^^^^^^
>O : <T>(t: T_1, t2: T) => void
> : ^ ^^ ^^^^^^^ ^^ ^^^^^^^^^
>t : T_1
> : ^^^
>t2 : T
> : ^
}
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/declarationEmitShadowing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @declaration: true

export class A<T = any> {
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
return null as any
}
}

export function needsRenameForShadowing<T>() {
type A = T
return function O<T>(t: A, t2: T) {
}
}

0 comments on commit c85e626

Please sign in to comment.