Skip to content

Commit ca4ef16

Browse files
Andaristjakebailey
andauthored
Fixed crash in classFields transform related to broken bodyless constructors (#59280)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 09e47d0 commit ca4ef16

6 files changed

+68
-2
lines changed

src/compiler/transformers/classFields.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,11 +2416,11 @@ export function transformClassFields(context: TransformationContext): (x: Source
24162416
factory.createBlock(
24172417
setTextRange(
24182418
factory.createNodeArray(statements),
2419-
/*location*/ constructor ? constructor.body!.statements : node.members,
2419+
/*location*/ constructor?.body?.statements ?? node.members,
24202420
),
24212421
multiLine,
24222422
),
2423-
/*location*/ constructor ? constructor.body : undefined,
2423+
/*location*/ constructor?.body,
24242424
);
24252425
}
24262426

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
classFieldsBrokenConstructorEmitNoCrash1.ts(3,3): error TS2390: Constructor implementation is missing.
2+
classFieldsBrokenConstructorEmitNoCrash1.ts(4,1): error TS1005: '(' expected.
3+
4+
5+
==== classFieldsBrokenConstructorEmitNoCrash1.ts (2 errors) ====
6+
class Test {
7+
prop = 42;
8+
constructor
9+
~~~~~~~~~~~
10+
!!! error TS2390: Constructor implementation is missing.
11+
}
12+
~
13+
!!! error TS1005: '(' expected.
14+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
2+
3+
//// [classFieldsBrokenConstructorEmitNoCrash1.ts]
4+
class Test {
5+
prop = 42;
6+
constructor
7+
}
8+
9+
10+
//// [classFieldsBrokenConstructorEmitNoCrash1.js]
11+
"use strict";
12+
class Test {
13+
constructor() {
14+
this.prop = 42;
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
2+
3+
=== classFieldsBrokenConstructorEmitNoCrash1.ts ===
4+
class Test {
5+
>Test : Symbol(Test, Decl(classFieldsBrokenConstructorEmitNoCrash1.ts, 0, 0))
6+
7+
prop = 42;
8+
>prop : Symbol(Test.prop, Decl(classFieldsBrokenConstructorEmitNoCrash1.ts, 0, 12))
9+
10+
constructor
11+
}
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
2+
3+
=== classFieldsBrokenConstructorEmitNoCrash1.ts ===
4+
class Test {
5+
>Test : Test
6+
> : ^^^^
7+
8+
prop = 42;
9+
>prop : number
10+
> : ^^^^^^
11+
>42 : 42
12+
> : ^^
13+
14+
constructor
15+
}
16+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @strict: true
2+
// @useDefineForClassFields: false
3+
// @target: es2021
4+
5+
class Test {
6+
prop = 42;
7+
constructor
8+
}

0 commit comments

Comments
 (0)