Skip to content

Commit 3880fce

Browse files
authored
fix(52455): destructuring function props with "enum" as property name breaks .d.ts (#52544)
1 parent 0715fe5 commit 3880fce

File tree

5 files changed

+294
-1
lines changed

5 files changed

+294
-1
lines changed

src/compiler/transformers/declarations.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import {
113113
isFunctionLike,
114114
isGlobalScopeAugmentation,
115115
isIdentifier,
116+
isIdentifierANonContextualKeyword,
116117
isImportDeclaration,
117118
isImportEqualsDeclaration,
118119
isIndexSignatureDeclaration,
@@ -695,7 +696,7 @@ export function transformDeclarations(context: TransformationContext) {
695696
if (elem.kind === SyntaxKind.OmittedExpression) {
696697
return elem;
697698
}
698-
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name) && !elem.symbol.isReferenced) {
699+
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name) && !elem.symbol.isReferenced && !isIdentifierANonContextualKeyword(elem.propertyName)) {
699700
// Unnecessary property renaming is forbidden in types, so remove renaming
700701
return factory.updateBindingElement(
701702
elem,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//// [declarationEmitKeywordDestructuring.ts]
2+
type P = {
3+
enum: boolean;
4+
function: boolean;
5+
abstract: boolean;
6+
async: boolean;
7+
await: boolean;
8+
one: boolean;
9+
};
10+
11+
function f1({ enum: _enum, ...rest }: P) {
12+
return rest;
13+
}
14+
15+
function f2({ function: _function, ...rest }: P) {
16+
return rest;
17+
}
18+
19+
function f3({ abstract: _abstract, ...rest }: P) {
20+
return rest;
21+
}
22+
23+
function f4({ async: _async, ...rest }: P) {
24+
return rest;
25+
}
26+
27+
function f5({ await: _await, ...rest }: P) {
28+
return rest;
29+
}
30+
31+
32+
//// [declarationEmitKeywordDestructuring.js]
33+
var __rest = (this && this.__rest) || function (s, e) {
34+
var t = {};
35+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
36+
t[p] = s[p];
37+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
38+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
39+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
40+
t[p[i]] = s[p[i]];
41+
}
42+
return t;
43+
};
44+
function f1(_a) {
45+
var _enum = _a.enum, rest = __rest(_a, ["enum"]);
46+
return rest;
47+
}
48+
function f2(_a) {
49+
var _function = _a.function, rest = __rest(_a, ["function"]);
50+
return rest;
51+
}
52+
function f3(_a) {
53+
var _abstract = _a.abstract, rest = __rest(_a, ["abstract"]);
54+
return rest;
55+
}
56+
function f4(_a) {
57+
var _async = _a.async, rest = __rest(_a, ["async"]);
58+
return rest;
59+
}
60+
function f5(_a) {
61+
var _await = _a.await, rest = __rest(_a, ["await"]);
62+
return rest;
63+
}
64+
65+
66+
//// [declarationEmitKeywordDestructuring.d.ts]
67+
type P = {
68+
enum: boolean;
69+
function: boolean;
70+
abstract: boolean;
71+
async: boolean;
72+
await: boolean;
73+
one: boolean;
74+
};
75+
declare function f1({ enum: _enum, ...rest }: P): {
76+
function: boolean;
77+
abstract: boolean;
78+
async: boolean;
79+
await: boolean;
80+
one: boolean;
81+
};
82+
declare function f2({ function: _function, ...rest }: P): {
83+
enum: boolean;
84+
abstract: boolean;
85+
async: boolean;
86+
await: boolean;
87+
one: boolean;
88+
};
89+
declare function f3({ abstract, ...rest }: P): {
90+
enum: boolean;
91+
function: boolean;
92+
async: boolean;
93+
await: boolean;
94+
one: boolean;
95+
};
96+
declare function f4({ async, ...rest }: P): {
97+
enum: boolean;
98+
function: boolean;
99+
abstract: boolean;
100+
await: boolean;
101+
one: boolean;
102+
};
103+
declare function f5({ await, ...rest }: P): {
104+
enum: boolean;
105+
function: boolean;
106+
abstract: boolean;
107+
async: boolean;
108+
one: boolean;
109+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
=== tests/cases/compiler/declarationEmitKeywordDestructuring.ts ===
2+
type P = {
3+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
4+
5+
enum: boolean;
6+
>enum : Symbol(enum, Decl(declarationEmitKeywordDestructuring.ts, 0, 10))
7+
8+
function: boolean;
9+
>function : Symbol(function, Decl(declarationEmitKeywordDestructuring.ts, 1, 18))
10+
11+
abstract: boolean;
12+
>abstract : Symbol(abstract, Decl(declarationEmitKeywordDestructuring.ts, 2, 22))
13+
14+
async: boolean;
15+
>async : Symbol(async, Decl(declarationEmitKeywordDestructuring.ts, 3, 22))
16+
17+
await: boolean;
18+
>await : Symbol(await, Decl(declarationEmitKeywordDestructuring.ts, 4, 19))
19+
20+
one: boolean;
21+
>one : Symbol(one, Decl(declarationEmitKeywordDestructuring.ts, 5, 19))
22+
23+
};
24+
25+
function f1({ enum: _enum, ...rest }: P) {
26+
>f1 : Symbol(f1, Decl(declarationEmitKeywordDestructuring.ts, 7, 2))
27+
>enum : Symbol(enum, Decl(declarationEmitKeywordDestructuring.ts, 0, 10))
28+
>_enum : Symbol(_enum, Decl(declarationEmitKeywordDestructuring.ts, 9, 13))
29+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 9, 26))
30+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
31+
32+
return rest;
33+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 9, 26))
34+
}
35+
36+
function f2({ function: _function, ...rest }: P) {
37+
>f2 : Symbol(f2, Decl(declarationEmitKeywordDestructuring.ts, 11, 1))
38+
>function : Symbol(function, Decl(declarationEmitKeywordDestructuring.ts, 1, 18))
39+
>_function : Symbol(_function, Decl(declarationEmitKeywordDestructuring.ts, 13, 13))
40+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 13, 34))
41+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
42+
43+
return rest;
44+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 13, 34))
45+
}
46+
47+
function f3({ abstract: _abstract, ...rest }: P) {
48+
>f3 : Symbol(f3, Decl(declarationEmitKeywordDestructuring.ts, 15, 1))
49+
>abstract : Symbol(abstract, Decl(declarationEmitKeywordDestructuring.ts, 2, 22))
50+
>_abstract : Symbol(_abstract, Decl(declarationEmitKeywordDestructuring.ts, 17, 13))
51+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 17, 34))
52+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
53+
54+
return rest;
55+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 17, 34))
56+
}
57+
58+
function f4({ async: _async, ...rest }: P) {
59+
>f4 : Symbol(f4, Decl(declarationEmitKeywordDestructuring.ts, 19, 1))
60+
>async : Symbol(async, Decl(declarationEmitKeywordDestructuring.ts, 3, 22))
61+
>_async : Symbol(_async, Decl(declarationEmitKeywordDestructuring.ts, 21, 13))
62+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 21, 28))
63+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
64+
65+
return rest;
66+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 21, 28))
67+
}
68+
69+
function f5({ await: _await, ...rest }: P) {
70+
>f5 : Symbol(f5, Decl(declarationEmitKeywordDestructuring.ts, 23, 1))
71+
>await : Symbol(await, Decl(declarationEmitKeywordDestructuring.ts, 4, 19))
72+
>_await : Symbol(_await, Decl(declarationEmitKeywordDestructuring.ts, 25, 13))
73+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 25, 28))
74+
>P : Symbol(P, Decl(declarationEmitKeywordDestructuring.ts, 0, 0))
75+
76+
return rest;
77+
>rest : Symbol(rest, Decl(declarationEmitKeywordDestructuring.ts, 25, 28))
78+
}
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
=== tests/cases/compiler/declarationEmitKeywordDestructuring.ts ===
2+
type P = {
3+
>P : { enum: boolean; function: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
4+
5+
enum: boolean;
6+
>enum : boolean
7+
8+
function: boolean;
9+
>function : boolean
10+
11+
abstract: boolean;
12+
>abstract : boolean
13+
14+
async: boolean;
15+
>async : boolean
16+
17+
await: boolean;
18+
>await : boolean
19+
20+
one: boolean;
21+
>one : boolean
22+
23+
};
24+
25+
function f1({ enum: _enum, ...rest }: P) {
26+
>f1 : ({ enum: _enum, ...rest }: P) => { function: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
27+
>enum : any
28+
>_enum : boolean
29+
>rest : { function: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
30+
31+
return rest;
32+
>rest : { function: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
33+
}
34+
35+
function f2({ function: _function, ...rest }: P) {
36+
>f2 : ({ function: _function, ...rest }: P) => { enum: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
37+
>function : any
38+
>_function : boolean
39+
>rest : { enum: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
40+
41+
return rest;
42+
>rest : { enum: boolean; abstract: boolean; async: boolean; await: boolean; one: boolean; }
43+
}
44+
45+
function f3({ abstract: _abstract, ...rest }: P) {
46+
>f3 : ({ abstract: _abstract, ...rest }: P) => { enum: boolean; function: boolean; async: boolean; await: boolean; one: boolean; }
47+
>abstract : any
48+
>_abstract : boolean
49+
>rest : { enum: boolean; function: boolean; async: boolean; await: boolean; one: boolean; }
50+
51+
return rest;
52+
>rest : { enum: boolean; function: boolean; async: boolean; await: boolean; one: boolean; }
53+
}
54+
55+
function f4({ async: _async, ...rest }: P) {
56+
>f4 : ({ async: _async, ...rest }: P) => { enum: boolean; function: boolean; abstract: boolean; await: boolean; one: boolean; }
57+
>async : any
58+
>_async : boolean
59+
>rest : { enum: boolean; function: boolean; abstract: boolean; await: boolean; one: boolean; }
60+
61+
return rest;
62+
>rest : { enum: boolean; function: boolean; abstract: boolean; await: boolean; one: boolean; }
63+
}
64+
65+
function f5({ await: _await, ...rest }: P) {
66+
>f5 : ({ await: _await, ...rest }: P) => { enum: boolean; function: boolean; abstract: boolean; async: boolean; one: boolean; }
67+
>await : any
68+
>_await : boolean
69+
>rest : { enum: boolean; function: boolean; abstract: boolean; async: boolean; one: boolean; }
70+
71+
return rest;
72+
>rest : { enum: boolean; function: boolean; abstract: boolean; async: boolean; one: boolean; }
73+
}
74+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @declaration: true
2+
3+
type P = {
4+
enum: boolean;
5+
function: boolean;
6+
abstract: boolean;
7+
async: boolean;
8+
await: boolean;
9+
one: boolean;
10+
};
11+
12+
function f1({ enum: _enum, ...rest }: P) {
13+
return rest;
14+
}
15+
16+
function f2({ function: _function, ...rest }: P) {
17+
return rest;
18+
}
19+
20+
function f3({ abstract: _abstract, ...rest }: P) {
21+
return rest;
22+
}
23+
24+
function f4({ async: _async, ...rest }: P) {
25+
return rest;
26+
}
27+
28+
function f5({ await: _await, ...rest }: P) {
29+
return rest;
30+
}

0 commit comments

Comments
 (0)