Skip to content

Bring back renamings in emitted d.ts files #55678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ import {
isFunctionDeclaration,
isFunctionLike,
isGlobalScopeAugmentation,
isIdentifier,
isIdentifierANonContextualKeyword,
isIdentifierText,
isImportDeclaration,
isImportEqualsDeclaration,
Expand Down Expand Up @@ -684,7 +682,7 @@ export function transformDeclarations(context: TransformationContext) {
return ret;
}

function filterBindingPatternInitializersAndRenamings(name: BindingName) {
function filterBindingPatternInitializers(name: BindingName) {
if (name.kind === SyntaxKind.Identifier) {
return name;
}
Expand All @@ -705,21 +703,11 @@ export function transformDeclarations(context: TransformationContext) {
if (elem.propertyName && isComputedPropertyName(elem.propertyName) && isEntityNameExpression(elem.propertyName.expression)) {
checkEntityNameVisibility(elem.propertyName.expression, enclosingDeclaration);
}
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name) && !elem.symbol.isReferenced && !isIdentifierANonContextualKeyword(elem.propertyName)) {
// Unnecessary property renaming is forbidden in types, so remove renaming
return factory.updateBindingElement(
elem,
elem.dotDotDotToken,
/*propertyName*/ undefined,
elem.propertyName,
shouldPrintWithInitializer(elem) ? elem.initializer : undefined,
);
}
return factory.updateBindingElement(
elem,
elem.dotDotDotToken,
elem.propertyName,
filterBindingPatternInitializersAndRenamings(elem.name),
filterBindingPatternInitializers(elem.name),
shouldPrintWithInitializer(elem) ? elem.initializer : undefined,
);
}
Expand All @@ -735,7 +723,7 @@ export function transformDeclarations(context: TransformationContext) {
p,
maskModifiers(factory, p, modifierMask),
p.dotDotDotToken,
filterBindingPatternInitializersAndRenamings(p.name),
filterBindingPatternInitializers(p.name),
resolver.isOptionalParameter(p) ? (p.questionToken || factory.createToken(SyntaxKind.QuestionToken)) : undefined,
ensureType(p, type || p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param
ensureNoInitializer(p),
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/declarationEmitBindingPatternsUnused.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/compiler/declarationEmitBindingPatternsUnused.ts] ////

//// [declarationEmitBindingPatternsUnused.ts]
function shouldNotKeep({ name: alias }: { name: string }) {

}

function shouldNotKeepButIsKept({ name: alias }: { name: string }) {
return alias;
}

function shouldKeep({ name: alias }: { name: string }): typeof alias {
return alias;
}


//// [declarationEmitBindingPatternsUnused.js]
function shouldNotKeep({ name: alias }) {
}
function shouldNotKeepButIsKept({ name: alias }) {
return alias;
}
function shouldKeep({ name: alias }) {
return alias;
}


//// [declarationEmitBindingPatternsUnused.d.ts]
declare function shouldNotKeep({ name: alias }: {
name: string;
}): void;
declare function shouldNotKeepButIsKept({ name: alias }: {
name: string;
}): string;
declare function shouldKeep({ name: alias }: {
name: string;
}): typeof alias;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitBindingPatternsUnused.ts] ////

=== declarationEmitBindingPatternsUnused.ts ===
function shouldNotKeep({ name: alias }: { name: string }) {
>shouldNotKeep : Symbol(shouldNotKeep, Decl(declarationEmitBindingPatternsUnused.ts, 0, 0))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 0, 41))
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 0, 24))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 0, 41))

}

function shouldNotKeepButIsKept({ name: alias }: { name: string }) {
>shouldNotKeepButIsKept : Symbol(shouldNotKeepButIsKept, Decl(declarationEmitBindingPatternsUnused.ts, 2, 1))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 4, 50))
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 4, 33))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 4, 50))

return alias;
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 4, 33))
}

function shouldKeep({ name: alias }: { name: string }): typeof alias {
>shouldKeep : Symbol(shouldKeep, Decl(declarationEmitBindingPatternsUnused.ts, 6, 1))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 8, 38))
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 8, 21))
>name : Symbol(name, Decl(declarationEmitBindingPatternsUnused.ts, 8, 38))
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 8, 21))

return alias;
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsUnused.ts, 8, 21))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitBindingPatternsUnused.ts] ////

=== declarationEmitBindingPatternsUnused.ts ===
function shouldNotKeep({ name: alias }: { name: string }) {
>shouldNotKeep : ({ name: alias }: { name: string;}) => void
>name : any
>alias : string
>name : string

}

function shouldNotKeepButIsKept({ name: alias }: { name: string }) {
>shouldNotKeepButIsKept : ({ name: alias }: { name: string;}) => string
>name : any
>alias : string
>name : string

return alias;
>alias : string
}

function shouldKeep({ name: alias }: { name: string }): typeof alias {
>shouldKeep : ({ name: alias }: { name: string;}) => typeof alias
>name : any
>alias : string
>name : string
>alias : string

return alias;
>alias : string
}

Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ declare function f2({ function: _function, ...rest }: P): {
await: boolean;
one: boolean;
};
declare function f3({ abstract, ...rest }: P): {
declare function f3({ abstract: _abstract, ...rest }: P): {
enum: boolean;
function: boolean;
async: boolean;
await: boolean;
one: boolean;
};
declare function f4({ async, ...rest }: P): {
declare function f4({ async: _async, ...rest }: P): {
enum: boolean;
function: boolean;
abstract: boolean;
await: boolean;
one: boolean;
};
declare function f5({ await, ...rest }: P): {
declare function f5({ await: _await, ...rest }: P): {
enum: boolean;
function: boolean;
abstract: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/destructuringInFunctionType.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type T3 = ([{
}, {
b: a;
}]);
type F3 = ([{ a }, { b }]: [{
type F3 = ([{ a: b }, { b: a }]: [{
a: any;
}, {
b: any;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/paramterDestrcuturingDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ interface C {

//// [paramterDestrcuturingDeclaration.d.ts]
interface C {
({ p }: {
({ p: name }: {
p: any;
}): any;
new ({ p }: {
new ({ p: boolean }: {
p: any;
}): any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ type O = {
c: number;
};
type F1 = (arg: number) => any;
type F2 = ({ a }: O) => any;
type F3 = ({ a, b, c }: O) => any;
type F4 = ({ a }: O) => any;
type F5 = ({ a, b, c }: O) => any;
type F2 = ({ a: string }: O) => any;
type F3 = ({ a: string, b, c }: O) => any;
type F4 = ({ a: string }: O) => any;
type F5 = ({ a: string, b, c }: O) => any;
type F6 = ({ a: string }: {
a: any;
}) => typeof string;
type F7 = ({ a, b: number }: {
type F7 = ({ a: string, b: number }: {
a: any;
b: any;
}) => typeof number;
Expand All @@ -118,14 +118,14 @@ type F8 = ({ a, b: number }: {
}) => typeof number;
type F9 = ([a, b, c]: [any, any, any]) => void;
type G1 = new (arg: number) => any;
type G2 = new ({ a }: O) => any;
type G3 = new ({ a, b, c }: O) => any;
type G4 = new ({ a }: O) => any;
type G5 = new ({ a, b, c }: O) => any;
type G2 = new ({ a: string }: O) => any;
type G3 = new ({ a: string, b, c }: O) => any;
type G4 = new ({ a: string }: O) => any;
type G5 = new ({ a: string, b, c }: O) => any;
type G6 = new ({ a: string }: {
a: any;
}) => typeof string;
type G7 = new ({ a, b: number }: {
type G7 = new ({ a: string, b: number }: {
a: any;
b: any;
}) => typeof number;
Expand Down Expand Up @@ -156,19 +156,19 @@ type G13 = new ({ [2]: string }: {
}) => void;
interface I {
method1(arg: number): any;
method2({ a }: {
method2({ a: string }: {
a: any;
}): any;
(arg: number): any;
({ a }: {
({ a: string }: {
a: any;
}): any;
new (arg: number): any;
new ({ a }: {
new ({ a: string }: {
a: any;
}): any;
}
declare function f1({ a }: O): void;
declare function f1({ a: string }: O): void;
declare const f2: ({ a: string }: O) => void;
declare const f3: ({ a: string, b, c }: O) => void;
declare const f4: ({ a: string }: O) => string;
Expand All @@ -179,7 +179,7 @@ declare const obj1: {
declare const obj2: {
method({ a: string }: O): string;
};
declare function f6({ a }: O): void;
declare function f6({ a: string }: O): void;
declare const f7: ({ a: string, b, c }: O) => void;
declare const f8: ({ "a": string }: O) => void;
declare function f9({ 2: string }: {
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/compiler/declarationEmitBindingPatternsUnused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @declaration: true
// @target: esnext
// @skipLibCheck: false

function shouldNotKeep({ name: alias }: { name: string }) {

}

function shouldNotKeepButIsKept({ name: alias }: { name: string }) {
return alias;
}

function shouldKeep({ name: alias }: { name: string }): typeof alias {
return alias;
}