Fixed crash in classFields transform related to broken bodyless constructors (#59280)

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
This commit is contained in:
Mateusz Burzyński 2024-07-23 01:00:16 +02:00 committed by GitHub
parent 09e47d0638
commit ca4ef16c8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 68 additions and 2 deletions

View File

@ -2416,11 +2416,11 @@ export function transformClassFields(context: TransformationContext): (x: Source
factory.createBlock(
setTextRange(
factory.createNodeArray(statements),
/*location*/ constructor ? constructor.body!.statements : node.members,
/*location*/ constructor?.body?.statements ?? node.members,
),
multiLine,
),
/*location*/ constructor ? constructor.body : undefined,
/*location*/ constructor?.body,
);
}

View File

@ -0,0 +1,14 @@
classFieldsBrokenConstructorEmitNoCrash1.ts(3,3): error TS2390: Constructor implementation is missing.
classFieldsBrokenConstructorEmitNoCrash1.ts(4,1): error TS1005: '(' expected.
==== classFieldsBrokenConstructorEmitNoCrash1.ts (2 errors) ====
class Test {
prop = 42;
constructor
~~~~~~~~~~~
!!! error TS2390: Constructor implementation is missing.
}
~
!!! error TS1005: '(' expected.

View File

@ -0,0 +1,16 @@
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
//// [classFieldsBrokenConstructorEmitNoCrash1.ts]
class Test {
prop = 42;
constructor
}
//// [classFieldsBrokenConstructorEmitNoCrash1.js]
"use strict";
class Test {
constructor() {
this.prop = 42;
}
}

View File

@ -0,0 +1,12 @@
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
=== classFieldsBrokenConstructorEmitNoCrash1.ts ===
class Test {
>Test : Symbol(Test, Decl(classFieldsBrokenConstructorEmitNoCrash1.ts, 0, 0))
prop = 42;
>prop : Symbol(Test.prop, Decl(classFieldsBrokenConstructorEmitNoCrash1.ts, 0, 12))
constructor
}

View File

@ -0,0 +1,16 @@
//// [tests/cases/compiler/classFieldsBrokenConstructorEmitNoCrash1.ts] ////
=== classFieldsBrokenConstructorEmitNoCrash1.ts ===
class Test {
>Test : Test
> : ^^^^
prop = 42;
>prop : number
> : ^^^^^^
>42 : 42
> : ^^
constructor
}

View File

@ -0,0 +1,8 @@
// @strict: true
// @useDefineForClassFields: false
// @target: es2021
class Test {
prop = 42;
constructor
}