Skip to content

Commit f3770c9

Browse files
authored
Fix crash in abstract property checking (#62923)
1 parent 1f5f9f3 commit f3770c9

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34452,10 +34452,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3445234452
(flags & ModifierFlags.Abstract) && symbolHasNonMethodDeclaration(prop) &&
3445334453
(isThisProperty(location) || isThisInitializedObjectBindingExpression(location) || isObjectBindingPattern(location.parent) && isThisInitializedDeclaration(location.parent.parent))
3445434454
) {
34455-
const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!);
34456-
if (declaringClassDeclaration && isNodeUsedDuringClassInitialization(location)) {
34455+
const parentSymbol = getParentOfSymbol(prop);
34456+
if (parentSymbol && parentSymbol.flags & SymbolFlags.Class && isNodeUsedDuringClassInitialization(location)) {
3445734457
if (errorNode) {
34458-
error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), getTextOfIdentifierOrLiteral(declaringClassDeclaration.name!));
34458+
error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), symbolToString(parentSymbol));
3445934459
}
3446034460
return false;
3446134461
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
errorInUnnamedClassExpression.ts(5,14): error TS2715: Abstract property 'bar' in class 'Foo' cannot be accessed in the constructor.
2+
errorInUnnamedClassExpression.ts(7,5): error TS1253: Abstract properties can only appear within an abstract class.
3+
errorInUnnamedClassExpression.ts(7,14): error TS7008: Member 'bar' implicitly has an 'any' type.
4+
5+
6+
==== errorInUnnamedClassExpression.ts (3 errors) ====
7+
// https://github.com/microsoft/TypeScript/issues/62920
8+
9+
let Foo = class {
10+
constructor() {
11+
this.bar++;
12+
~~~
13+
!!! error TS2715: Abstract property 'bar' in class 'Foo' cannot be accessed in the constructor.
14+
}
15+
abstract bar;
16+
~~~~~~~~
17+
!!! error TS1253: Abstract properties can only appear within an abstract class.
18+
~~~
19+
!!! error TS7008: Member 'bar' implicitly has an 'any' type.
20+
};
21+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/errorInUnnamedClassExpression.ts] ////
2+
3+
=== errorInUnnamedClassExpression.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62920
5+
6+
let Foo = class {
7+
>Foo : Symbol(Foo, Decl(errorInUnnamedClassExpression.ts, 2, 3))
8+
9+
constructor() {
10+
this.bar++;
11+
>this.bar : Symbol(Foo.bar, Decl(errorInUnnamedClassExpression.ts, 5, 5))
12+
>this : Symbol(Foo, Decl(errorInUnnamedClassExpression.ts, 2, 9))
13+
>bar : Symbol(Foo.bar, Decl(errorInUnnamedClassExpression.ts, 5, 5))
14+
}
15+
abstract bar;
16+
>bar : Symbol(Foo.bar, Decl(errorInUnnamedClassExpression.ts, 5, 5))
17+
18+
};
19+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/errorInUnnamedClassExpression.ts] ////
2+
3+
=== errorInUnnamedClassExpression.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62920
5+
6+
let Foo = class {
7+
>Foo : typeof Foo
8+
> : ^^^^^^^^^^
9+
>class { constructor() { this.bar++; } abstract bar;} : typeof Foo
10+
> : ^^^^^^^^^^
11+
12+
constructor() {
13+
this.bar++;
14+
>this.bar++ : number
15+
> : ^^^^^^
16+
>this.bar : any
17+
> : ^^^
18+
>this : this
19+
> : ^^^^
20+
>bar : any
21+
> : ^^^
22+
}
23+
abstract bar;
24+
>bar : any
25+
> : ^^^
26+
27+
};
28+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/62920
5+
6+
let Foo = class {
7+
constructor() {
8+
this.bar++;
9+
}
10+
abstract bar;
11+
};

0 commit comments

Comments
 (0)