Skip to content

Commit b19a9da

Browse files
authored
Fix transform crash with destructured parameter property (#63043)
1 parent d9d9eea commit b19a9da

6 files changed

+212
-0
lines changed

src/compiler/transformers/ts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,10 @@ export function transformTypeScript(context: TransformationContext): Transformer
10551055

10561056
if (parametersWithPropertyAssignments) {
10571057
for (const parameter of parametersWithPropertyAssignments) {
1058+
// Ignore parameter properties with destructured names; we will have errored on them earlier.
1059+
if (!isIdentifier(parameter.name)) {
1060+
continue;
1061+
}
10581062
const parameterProperty = factory.createPropertyDeclaration(
10591063
/*modifiers*/ undefined,
10601064
parameter.name,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
declarationEmitDestructuringParameterProperties2.ts(2,17): error TS1187: A parameter property may not be declared using a binding pattern.
2+
declarationEmitDestructuringParameterProperties2.ts(8,17): error TS1187: A parameter property may not be declared using a binding pattern.
3+
declarationEmitDestructuringParameterProperties2.ts(14,17): error TS1187: A parameter property may not be declared using a binding pattern.
4+
5+
6+
==== declarationEmitDestructuringParameterProperties2.ts (3 errors) ====
7+
class C1 {
8+
constructor(public [x, y, z]: string[]) {
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS1187: A parameter property may not be declared using a binding pattern.
11+
}
12+
}
13+
14+
type TupleType1 =[string, number, boolean];
15+
class C2 {
16+
constructor(public [x, y, z]: TupleType1) {
17+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18+
!!! error TS1187: A parameter property may not be declared using a binding pattern.
19+
}
20+
}
21+
22+
type ObjType1 = { x: number; y: string; z: boolean }
23+
class C3 {
24+
constructor(public { x, y, z }: ObjType1) {
25+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26+
!!! error TS1187: A parameter property may not be declared using a binding pattern.
27+
}
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//// [tests/cases/compiler/declarationEmitDestructuringParameterProperties2.ts] ////
2+
3+
//// [declarationEmitDestructuringParameterProperties2.ts]
4+
class C1 {
5+
constructor(public [x, y, z]: string[]) {
6+
}
7+
}
8+
9+
type TupleType1 =[string, number, boolean];
10+
class C2 {
11+
constructor(public [x, y, z]: TupleType1) {
12+
}
13+
}
14+
15+
type ObjType1 = { x: number; y: string; z: boolean }
16+
class C3 {
17+
constructor(public { x, y, z }: ObjType1) {
18+
}
19+
}
20+
21+
//// [declarationEmitDestructuringParameterProperties2.js]
22+
class C1 {
23+
constructor([x, y, z]) {
24+
}
25+
}
26+
class C2 {
27+
constructor([x, y, z]) {
28+
}
29+
}
30+
class C3 {
31+
constructor({ x, y, z }) {
32+
}
33+
}
34+
35+
36+
//// [declarationEmitDestructuringParameterProperties2.d.ts]
37+
declare class C1 {
38+
x: string;
39+
y: string;
40+
z: string;
41+
constructor([x, y, z]: string[]);
42+
}
43+
type TupleType1 = [string, number, boolean];
44+
declare class C2 {
45+
x: string;
46+
y: number;
47+
z: boolean;
48+
constructor([x, y, z]: TupleType1);
49+
}
50+
type ObjType1 = {
51+
x: number;
52+
y: string;
53+
z: boolean;
54+
};
55+
declare class C3 {
56+
x: number;
57+
y: string;
58+
z: boolean;
59+
constructor({ x, y, z }: ObjType1);
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/declarationEmitDestructuringParameterProperties2.ts] ////
2+
3+
=== declarationEmitDestructuringParameterProperties2.ts ===
4+
class C1 {
5+
>C1 : Symbol(C1, Decl(declarationEmitDestructuringParameterProperties2.ts, 0, 0))
6+
7+
constructor(public [x, y, z]: string[]) {
8+
>x : Symbol(x, Decl(declarationEmitDestructuringParameterProperties2.ts, 1, 24))
9+
>y : Symbol(y, Decl(declarationEmitDestructuringParameterProperties2.ts, 1, 26))
10+
>z : Symbol(z, Decl(declarationEmitDestructuringParameterProperties2.ts, 1, 29))
11+
}
12+
}
13+
14+
type TupleType1 =[string, number, boolean];
15+
>TupleType1 : Symbol(TupleType1, Decl(declarationEmitDestructuringParameterProperties2.ts, 3, 1))
16+
17+
class C2 {
18+
>C2 : Symbol(C2, Decl(declarationEmitDestructuringParameterProperties2.ts, 5, 43))
19+
20+
constructor(public [x, y, z]: TupleType1) {
21+
>x : Symbol(x, Decl(declarationEmitDestructuringParameterProperties2.ts, 7, 24))
22+
>y : Symbol(y, Decl(declarationEmitDestructuringParameterProperties2.ts, 7, 26))
23+
>z : Symbol(z, Decl(declarationEmitDestructuringParameterProperties2.ts, 7, 29))
24+
>TupleType1 : Symbol(TupleType1, Decl(declarationEmitDestructuringParameterProperties2.ts, 3, 1))
25+
}
26+
}
27+
28+
type ObjType1 = { x: number; y: string; z: boolean }
29+
>ObjType1 : Symbol(ObjType1, Decl(declarationEmitDestructuringParameterProperties2.ts, 9, 1))
30+
>x : Symbol(x, Decl(declarationEmitDestructuringParameterProperties2.ts, 11, 17))
31+
>y : Symbol(y, Decl(declarationEmitDestructuringParameterProperties2.ts, 11, 28))
32+
>z : Symbol(z, Decl(declarationEmitDestructuringParameterProperties2.ts, 11, 39))
33+
34+
class C3 {
35+
>C3 : Symbol(C3, Decl(declarationEmitDestructuringParameterProperties2.ts, 11, 52))
36+
37+
constructor(public { x, y, z }: ObjType1) {
38+
>x : Symbol(x, Decl(declarationEmitDestructuringParameterProperties2.ts, 13, 24))
39+
>y : Symbol(y, Decl(declarationEmitDestructuringParameterProperties2.ts, 13, 27))
40+
>z : Symbol(z, Decl(declarationEmitDestructuringParameterProperties2.ts, 13, 30))
41+
>ObjType1 : Symbol(ObjType1, Decl(declarationEmitDestructuringParameterProperties2.ts, 9, 1))
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//// [tests/cases/compiler/declarationEmitDestructuringParameterProperties2.ts] ////
2+
3+
=== declarationEmitDestructuringParameterProperties2.ts ===
4+
class C1 {
5+
>C1 : C1
6+
> : ^^
7+
8+
constructor(public [x, y, z]: string[]) {
9+
>x : string
10+
> : ^^^^^^
11+
>y : string
12+
> : ^^^^^^
13+
>z : string
14+
> : ^^^^^^
15+
}
16+
}
17+
18+
type TupleType1 =[string, number, boolean];
19+
>TupleType1 : TupleType1
20+
> : ^^^^^^^^^^
21+
22+
class C2 {
23+
>C2 : C2
24+
> : ^^
25+
26+
constructor(public [x, y, z]: TupleType1) {
27+
>x : string
28+
> : ^^^^^^
29+
>y : number
30+
> : ^^^^^^
31+
>z : boolean
32+
> : ^^^^^^^
33+
}
34+
}
35+
36+
type ObjType1 = { x: number; y: string; z: boolean }
37+
>ObjType1 : ObjType1
38+
> : ^^^^^^^^
39+
>x : number
40+
> : ^^^^^^
41+
>y : string
42+
> : ^^^^^^
43+
>z : boolean
44+
> : ^^^^^^^
45+
46+
class C3 {
47+
>C3 : C3
48+
> : ^^
49+
50+
constructor(public { x, y, z }: ObjType1) {
51+
>x : number
52+
> : ^^^^^^
53+
>y : string
54+
> : ^^^^^^
55+
>z : boolean
56+
> : ^^^^^^^
57+
}
58+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @module: commonjs
2+
// @declaration: true
3+
// @target: es2024
4+
class C1 {
5+
constructor(public [x, y, z]: string[]) {
6+
}
7+
}
8+
9+
type TupleType1 =[string, number, boolean];
10+
class C2 {
11+
constructor(public [x, y, z]: TupleType1) {
12+
}
13+
}
14+
15+
type ObjType1 = { x: number; y: string; z: boolean }
16+
class C3 {
17+
constructor(public { x, y, z }: ObjType1) {
18+
}
19+
}

0 commit comments

Comments
 (0)