Skip to content

Commit bdabd74

Browse files
Do not remove binding alias in function declarations
1 parent 7f3e34b commit bdabd74

16 files changed

+525
-472
lines changed

src/compiler/checker.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -6225,16 +6225,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
62256225

62266226
function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMakeVisible: boolean): SymbolVisibilityResult | undefined {
62276227
let aliasesToMakeVisible: LateVisibilityPaintedStatement[] | undefined;
6228-
let bindingElementToMakeVisible: BindingElement | undefined;
62296228
if (!every(filter(symbol.declarations, d => d.kind !== SyntaxKind.Identifier), getIsDeclarationVisible)) {
62306229
return undefined;
62316230
}
6232-
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible, bindingElementToMakeVisible };
6231+
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible };
62336232

62346233
function getIsDeclarationVisible(declaration: Declaration) {
6235-
if (isBindingElement(declaration) && findAncestor(declaration, isParameter)) {
6236-
bindingElementToMakeVisible = declaration;
6237-
}
62386234
if (!isDeclarationVisible(declaration)) {
62396235
// Mark the unexported alias as visible if its parent is visible
62406236
// because these kind of aliases can be used to name types in declaration file

src/compiler/transformers/declarations.ts

+57-283
Large diffs are not rendered by default.

src/compiler/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5592,7 +5592,6 @@ export type LateVisibilityPaintedStatement =
55925592
/** @internal */
55935593
export interface SymbolVisibilityResult {
55945594
accessibility: SymbolAccessibility;
5595-
bindingElementToMakeVisible?: BindingElement;
55965595
aliasesToMakeVisible?: LateVisibilityPaintedStatement[]; // aliases that need to have this symbol visible
55975596
errorSymbolName?: string; // Optional symbol name that results in error
55985597
errorNode?: Node; // optional node that results in error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatternsFunctionExpr.ts] ////
2+
3+
//// [declarationEmitBindingPatternsFunctionExpr.ts]
4+
type Named = { name: string }
5+
// Tempting to remove alias if unused
6+
let notReferenced = ({ name: alias }: Named) => { }
7+
8+
// Resons we can't remove aliases that are not used in the function signature:
9+
10+
// 1.Causes duplicate identifier if we remove alias
11+
const duplicateIndetifiers = ({ name: alias, name: alias2 }: Named) => { }
12+
const duplicateIndetifiers2 = (name: string, { name: alias }: Named) => { }
13+
const duplicateIndetifiers3 = ({ name: alias }: Named, { name: alias2 }: Named) => { }
14+
15+
let value = "";
16+
// 2.Can change in meaning for typeof value if we remove alias
17+
const shadowedVariable = ({ value: alias }: { value: string }): typeof value => value;
18+
19+
//// [declarationEmitBindingPatternsFunctionExpr.js]
20+
// Tempting to remove alias if unused
21+
let notReferenced = ({ name: alias }) => { };
22+
// Resons we can't remove aliases that are not used in the function signature:
23+
// 1.Causes duplicate identifier if we remove alias
24+
const duplicateIndetifiers = ({ name: alias, name: alias2 }) => { };
25+
const duplicateIndetifiers2 = (name, { name: alias }) => { };
26+
const duplicateIndetifiers3 = ({ name: alias }, { name: alias2 }) => { };
27+
let value = "";
28+
// 2.Can change in meaning for typeof value if we remove alias
29+
const shadowedVariable = ({ value: alias }) => value;
30+
31+
32+
//// [declarationEmitBindingPatternsFunctionExpr.d.ts]
33+
type Named = {
34+
name: string;
35+
};
36+
declare let notReferenced: ({ name: alias }: Named) => void;
37+
declare const duplicateIndetifiers: ({ name: alias, name: alias2 }: Named) => void;
38+
declare const duplicateIndetifiers2: (name: string, { name: alias }: Named) => void;
39+
declare const duplicateIndetifiers3: ({ name: alias }: Named, { name: alias2 }: Named) => void;
40+
declare let value: string;
41+
declare const shadowedVariable: ({ value: alias }: {
42+
value: string;
43+
}) => typeof value;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatternsFunctionExpr.ts] ////
2+
3+
=== declarationEmitBindingPatternsFunctionExpr.ts ===
4+
type Named = { name: string }
5+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
6+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
7+
8+
// Tempting to remove alias if unused
9+
let notReferenced = ({ name: alias }: Named) => { }
10+
>notReferenced : Symbol(notReferenced, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 2, 3))
11+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
12+
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 2, 22))
13+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
14+
15+
// Resons we can't remove aliases that are not used in the function signature:
16+
17+
// 1.Causes duplicate identifier if we remove alias
18+
const duplicateIndetifiers = ({ name: alias, name: alias2 }: Named) => { }
19+
>duplicateIndetifiers : Symbol(duplicateIndetifiers, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 7, 5))
20+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
21+
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 7, 31))
22+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
23+
>alias2 : Symbol(alias2, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 7, 44))
24+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
25+
26+
const duplicateIndetifiers2 = (name: string, { name: alias }: Named) => { }
27+
>duplicateIndetifiers2 : Symbol(duplicateIndetifiers2, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 8, 5))
28+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 8, 31))
29+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
30+
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 8, 46))
31+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
32+
33+
const duplicateIndetifiers3 = ({ name: alias }: Named, { name: alias2 }: Named) => { }
34+
>duplicateIndetifiers3 : Symbol(duplicateIndetifiers3, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 9, 5))
35+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
36+
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 9, 32))
37+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
38+
>name : Symbol(name, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 14))
39+
>alias2 : Symbol(alias2, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 9, 56))
40+
>Named : Symbol(Named, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 0, 0))
41+
42+
let value = "";
43+
>value : Symbol(value, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 11, 3))
44+
45+
// 2.Can change in meaning for typeof value if we remove alias
46+
const shadowedVariable = ({ value: alias }: { value: string }): typeof value => value;
47+
>shadowedVariable : Symbol(shadowedVariable, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 13, 5))
48+
>value : Symbol(value, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 13, 45))
49+
>alias : Symbol(alias, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 13, 27))
50+
>value : Symbol(value, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 13, 45))
51+
>value : Symbol(value, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 11, 3))
52+
>value : Symbol(value, Decl(declarationEmitBindingPatternsFunctionExpr.ts, 11, 3))
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatternsFunctionExpr.ts] ////
2+
3+
=== declarationEmitBindingPatternsFunctionExpr.ts ===
4+
type Named = { name: string }
5+
>Named : { name: string; }
6+
>name : string
7+
8+
// Tempting to remove alias if unused
9+
let notReferenced = ({ name: alias }: Named) => { }
10+
>notReferenced : ({ name: alias }: Named) => void
11+
>({ name: alias }: Named) => { } : ({ name: alias }: Named) => void
12+
>name : any
13+
>alias : string
14+
15+
// Resons we can't remove aliases that are not used in the function signature:
16+
17+
// 1.Causes duplicate identifier if we remove alias
18+
const duplicateIndetifiers = ({ name: alias, name: alias2 }: Named) => { }
19+
>duplicateIndetifiers : ({ name: alias, name: alias2 }: Named) => void
20+
>({ name: alias, name: alias2 }: Named) => { } : ({ name: alias, name: alias2 }: Named) => void
21+
>name : any
22+
>alias : string
23+
>name : any
24+
>alias2 : string
25+
26+
const duplicateIndetifiers2 = (name: string, { name: alias }: Named) => { }
27+
>duplicateIndetifiers2 : (name: string, { name: alias }: Named) => void
28+
>(name: string, { name: alias }: Named) => { } : (name: string, { name: alias }: Named) => void
29+
>name : string
30+
>name : any
31+
>alias : string
32+
33+
const duplicateIndetifiers3 = ({ name: alias }: Named, { name: alias2 }: Named) => { }
34+
>duplicateIndetifiers3 : ({ name: alias }: Named, { name: alias2 }: Named) => void
35+
>({ name: alias }: Named, { name: alias2 }: Named) => { } : ({ name: alias }: Named, { name: alias2 }: Named) => void
36+
>name : any
37+
>alias : string
38+
>name : any
39+
>alias2 : string
40+
41+
let value = "";
42+
>value : string
43+
>"" : ""
44+
45+
// 2.Can change in meaning for typeof value if we remove alias
46+
const shadowedVariable = ({ value: alias }: { value: string }): typeof value => value;
47+
>shadowedVariable : ({ value: alias }: { value: string;}) => typeof value
48+
>({ value: alias }: { value: string }): typeof value => value : ({ value: alias }: { value: string;}) => typeof value
49+
>value : any
50+
>alias : string
51+
>value : string
52+
>value : string
53+
>value : string
54+

tests/baselines/reference/declarationEmitBindingPatternsUnused.errors.txt

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
declarationEmitBindingPatternsUnused.ts(85,35): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
2-
declarationEmitBindingPatternsUnused.ts(89,41): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
3-
declarationEmitBindingPatternsUnused.ts(95,11): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
4-
declarationEmitBindingPatternsUnused.ts(96,15): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
5-
declarationEmitBindingPatternsUnused.ts(97,16): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
6-
declarationEmitBindingPatternsUnused.ts(98,12): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
1+
declarationEmitBindingPatternsUnused.ts(95,35): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
2+
declarationEmitBindingPatternsUnused.ts(99,41): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
3+
declarationEmitBindingPatternsUnused.ts(105,11): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
4+
declarationEmitBindingPatternsUnused.ts(106,15): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
5+
declarationEmitBindingPatternsUnused.ts(107,16): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
6+
declarationEmitBindingPatternsUnused.ts(108,12): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
77

88

99
==== declarationEmitBindingPatternsUnused.ts (6 errors) ====
1010
type Named = { name: string }
11+
12+
// Resons we can't remove aliases that are not used in the function signature:
13+
14+
// 1.Causes duplicate identifier if we remove alias
15+
function duplicateIndetifiers({ name: alias, name: alias2 }: Named) { }
16+
function duplicateIndetifiers2(name: string, { name: alias }: Named) { }
17+
function duplicateIndetifiers3({ name: alias }: Named, { name: alias2 }: Named) { }
18+
19+
let value = "";
20+
// 2.Can change in meaning for typeof value if we remove alias
21+
function shadowedVariable({ value: alias }: { value: string }): typeof value { return value }
22+
1123
function notReferenced({ name: alias }: Named) {
1224

1325
}
@@ -16,8 +28,6 @@ declarationEmitBindingPatternsUnused.ts(98,12): error TS2842: 'alias' is an unus
1628
function notReferencedArrayAlias([a, b, { name: alias }]: Named[]) {
1729
}
1830

19-
20-
2131
function referencedInCode({ name: alias }: Named) {
2232
return alias;
2333
}

tests/baselines/reference/declarationEmitBindingPatternsUnused.js

+43-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
//// [declarationEmitBindingPatternsUnused.ts]
44
type Named = { name: string }
5+
6+
// Resons we can't remove aliases that are not used in the function signature:
7+
8+
// 1.Causes duplicate identifier if we remove alias
9+
function duplicateIndetifiers({ name: alias, name: alias2 }: Named) { }
10+
function duplicateIndetifiers2(name: string, { name: alias }: Named) { }
11+
function duplicateIndetifiers3({ name: alias }: Named, { name: alias2 }: Named) { }
12+
13+
let value = "";
14+
// 2.Can change in meaning for typeof value if we remove alias
15+
function shadowedVariable({ value: alias }: { value: string }): typeof value { return value }
16+
517
function notReferenced({ name: alias }: Named) {
618

719
}
@@ -10,8 +22,6 @@ function notReferencedNestedAlias({ p: { name: alias } }: { p: Named }) {
1022
function notReferencedArrayAlias([a, b, { name: alias }]: Named[]) {
1123
}
1224

13-
14-
1525
function referencedInCode({ name: alias }: Named) {
1626
return alias;
1727
}
@@ -114,6 +124,14 @@ interface ReferencedInSignartureInterface {
114124
}
115125

116126
//// [declarationEmitBindingPatternsUnused.js]
127+
// Resons we can't remove aliases that are not used in the function signature:
128+
// 1.Causes duplicate identifier if we remove alias
129+
function duplicateIndetifiers({ name: alias, name: alias2 }) { }
130+
function duplicateIndetifiers2(name, { name: alias }) { }
131+
function duplicateIndetifiers3({ name: alias }, { name: alias2 }) { }
132+
let value = "";
133+
// 2.Can change in meaning for typeof value if we remove alias
134+
function shadowedVariable({ value: alias }) { return value; }
117135
function notReferenced({ name: alias }) {
118136
}
119137
function notReferencedNestedAlias({ p: { name: alias } }) {
@@ -191,12 +209,19 @@ let referencedInSignartureParamTypeCtorType;
191209
type Named = {
192210
name: string;
193211
};
194-
declare function notReferenced({ name }: Named): void;
195-
declare function notReferencedNestedAlias({ p: { name } }: {
212+
declare function duplicateIndetifiers({ name: alias, name: alias2 }: Named): void;
213+
declare function duplicateIndetifiers2(name: string, { name: alias }: Named): void;
214+
declare function duplicateIndetifiers3({ name: alias }: Named, { name: alias2 }: Named): void;
215+
declare let value: string;
216+
declare function shadowedVariable({ value: alias }: {
217+
value: string;
218+
}): typeof value;
219+
declare function notReferenced({ name: alias }: Named): void;
220+
declare function notReferencedNestedAlias({ p: { name: alias } }: {
196221
p: Named;
197222
}): void;
198-
declare function notReferencedArrayAlias([a, b, { name }]: Named[]): void;
199-
declare function referencedInCode({ name }: Named): string;
223+
declare function notReferencedArrayAlias([a, b, { name: alias }]: Named[]): void;
224+
declare function referencedInCode({ name: alias }: Named): string;
200225
declare function referencedInSignarture({ name: alias }: Named): typeof alias;
201226
declare function referencedInSignartureKeyword({ function: alias }: {
202227
function: string;
@@ -210,14 +235,14 @@ declare function referencedNestedAlias({ p: { name: alias } }: {
210235
}): typeof alias;
211236
declare function referencedArrayAlias([a, b, { name: alias }]: Named[]): typeof alias;
212237
declare class NotReferencedClass {
213-
constructor({ name }: Named);
214-
set x({ name }: Named);
215-
m({ name }: Named): void;
238+
constructor({ name: alias }: Named);
239+
set x({ name: alias }: Named);
240+
m({ name: alias }: Named): void;
216241
}
217242
declare class ReferencedInCodeClas {
218-
constructor({ name }: Named);
219-
set x({ name }: Named);
220-
m({ name }: Named): void;
243+
constructor({ name: alias }: Named);
244+
set x({ name: alias }: Named);
245+
m({ name: alias }: Named): void;
221246
}
222247
declare class ReferencedInSignartureClass {
223248
constructor({ name: alias }: Named, p: typeof alias);
@@ -228,17 +253,17 @@ declare class ReferencedInSignartureClass {
228253
mRerturnTypeNested({ name: alias }: Named): NonNullable<typeof alias>;
229254
mParameter({ name: alias }: Named, p: typeof alias): any;
230255
}
231-
declare let notReferencedFnType: ({ name }: Named) => void;
256+
declare let notReferencedFnType: ({ name: alias }: Named) => void;
232257
declare let referencedInSignartureReturnTypeFnType: ({ name: alias }: Named) => typeof alias;
233258
declare let referencedInSignartureParamTypeFnType: ({ name: alias }: Named, p: typeof alias) => void;
234-
declare let notReferencedCtorType: new ({ name }: Named) => void;
259+
declare let notReferencedCtorType: new ({ name: alias }: Named) => void;
235260
declare let referencedInSignartureReturnTypeCtorType: new ({ name: alias }: Named) => typeof alias;
236261
declare let referencedInSignartureParamTypeCtorType: new ({ name: alias }: Named, p: typeof alias) => void;
237262
interface NotReferencedInterface {
238-
({ name }: Named): void;
239-
new ({ name }: Named): void;
240-
set x({ name }: Named);
241-
m({ name }: Named): any;
263+
({ name: alias }: Named): void;
264+
new ({ name: alias }: Named): void;
265+
set x({ name: alias }: Named);
266+
m({ name: alias }: Named): any;
242267
}
243268
interface ReferencedInSignartureInterface {
244269
({ name: alias }: Named, p: typeof alias): void;

0 commit comments

Comments
 (0)