Skip to content

Commit 27636da

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/typescript-go into fix/2426
2 parents c950912 + 1611cc9 commit 27636da

File tree

9 files changed

+251
-3
lines changed

9 files changed

+251
-3
lines changed

internal/checker/checker.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11452,10 +11452,10 @@ func (c *Checker) checkPropertyAccessibilityAtLocation(location *ast.Node, isSup
1145211452
if flags&ast.ModifierFlagsAbstract != 0 && c.symbolHasNonMethodDeclaration(prop) && (isThisProperty(location) ||
1145311453
isThisInitializedObjectBindingExpression(location) ||
1145411454
ast.IsObjectBindingPattern(location.Parent) && isThisInitializedDeclaration(location.Parent.Parent)) {
11455-
declaringClassDeclaration := ast.GetClassLikeDeclarationOfSymbol(c.getParentOfSymbol(prop))
11456-
if declaringClassDeclaration != nil && c.isNodeUsedDuringClassInitialization(location) {
11455+
parentSymbol := c.getParentOfSymbol(prop)
11456+
if parentSymbol != nil && parentSymbol.Flags&ast.SymbolFlagsClass != 0 && c.isNodeUsedDuringClassInitialization(location) {
1145711457
if errorNode != nil {
11458-
c.error(errorNode, diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, c.symbolToString(prop), declaringClassDeclaration.Name().Text())
11458+
c.error(errorNode, diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, c.symbolToString(prop), c.symbolToString(parentSymbol))
1145911459
}
1146011460
return false
1146111461
}
@@ -18556,6 +18556,9 @@ func findIndexInfo(indexInfos []*IndexInfo, keyType *Type) *IndexInfo {
1855618556
}
1855718557

1855818558
func (c *Checker) getBaseTypes(t *Type) []*Type {
18559+
if t.objectFlags&(ObjectFlagsClassOrInterface|ObjectFlagsReference) == 0 {
18560+
return nil
18561+
}
1855918562
data := t.AsInterfaceType()
1856018563
if !data.baseTypesResolved {
1856118564
if !c.pushTypeResolution(t, TypeSystemPropertyNameResolvedBaseTypes) {
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
>class { constructor() { this.bar++; } abstract bar;} : typeof Foo
9+
10+
constructor() {
11+
this.bar++;
12+
>this.bar++ : number
13+
>this.bar : any
14+
>this : this
15+
>bar : any
16+
}
17+
abstract bar;
18+
>bar : any
19+
20+
};
21+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
noCrashOnMixin2.ts(11,33): error TS2370: A rest parameter must be of an array type.
2+
noCrashOnMixin2.ts(11,40): error TS1047: A rest parameter cannot be optional.
3+
noCrashOnMixin2.ts(14,12): error TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'.
4+
noCrashOnMixin2.ts(23,9): error TS2674: Constructor of class 'Abstract' is protected and only accessible within the class declaration.
5+
6+
7+
==== noCrashOnMixin2.ts (4 errors) ====
8+
// https://github.com/microsoft/TypeScript/issues/62921
9+
10+
class Abstract {
11+
protected constructor() {
12+
}
13+
}
14+
15+
class Concrete extends Abstract {
16+
}
17+
18+
type Constructor<T = {}> = new (...args?: any[]) => T;
19+
~~~~~~~~~~~~~~~
20+
!!! error TS2370: A rest parameter must be of an array type.
21+
~
22+
!!! error TS1047: A rest parameter cannot be optional.
23+
24+
function Mixin<TBase extends Constructor>(Base: TBase) {
25+
return class extends Base {
26+
~~~~~
27+
!!! error TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'.
28+
};
29+
}
30+
31+
class Empty {
32+
}
33+
34+
class CrashTrigger extends Mixin(Empty) {
35+
public trigger() {
36+
new Concrete();
37+
~~~~~~~~~~~~~~
38+
!!! error TS2674: Constructor of class 'Abstract' is protected and only accessible within the class declaration.
39+
}
40+
}
41+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/compiler/noCrashOnMixin2.ts] ////
2+
3+
=== noCrashOnMixin2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62921
5+
6+
class Abstract {
7+
>Abstract : Symbol(Abstract, Decl(noCrashOnMixin2.ts, 0, 0))
8+
9+
protected constructor() {
10+
}
11+
}
12+
13+
class Concrete extends Abstract {
14+
>Concrete : Symbol(Concrete, Decl(noCrashOnMixin2.ts, 5, 1))
15+
>Abstract : Symbol(Abstract, Decl(noCrashOnMixin2.ts, 0, 0))
16+
}
17+
18+
type Constructor<T = {}> = new (...args?: any[]) => T;
19+
>Constructor : Symbol(Constructor, Decl(noCrashOnMixin2.ts, 8, 1))
20+
>T : Symbol(T, Decl(noCrashOnMixin2.ts, 10, 17))
21+
>args : Symbol(args, Decl(noCrashOnMixin2.ts, 10, 32))
22+
>T : Symbol(T, Decl(noCrashOnMixin2.ts, 10, 17))
23+
24+
function Mixin<TBase extends Constructor>(Base: TBase) {
25+
>Mixin : Symbol(Mixin, Decl(noCrashOnMixin2.ts, 10, 54))
26+
>TBase : Symbol(TBase, Decl(noCrashOnMixin2.ts, 12, 15))
27+
>Constructor : Symbol(Constructor, Decl(noCrashOnMixin2.ts, 8, 1))
28+
>Base : Symbol(Base, Decl(noCrashOnMixin2.ts, 12, 42))
29+
>TBase : Symbol(TBase, Decl(noCrashOnMixin2.ts, 12, 15))
30+
31+
return class extends Base {
32+
>Base : Symbol(Base, Decl(noCrashOnMixin2.ts, 12, 42))
33+
34+
};
35+
}
36+
37+
class Empty {
38+
>Empty : Symbol(Empty, Decl(noCrashOnMixin2.ts, 15, 1))
39+
}
40+
41+
class CrashTrigger extends Mixin(Empty) {
42+
>CrashTrigger : Symbol(CrashTrigger, Decl(noCrashOnMixin2.ts, 18, 1))
43+
>Mixin : Symbol(Mixin, Decl(noCrashOnMixin2.ts, 10, 54))
44+
>Empty : Symbol(Empty, Decl(noCrashOnMixin2.ts, 15, 1))
45+
46+
public trigger() {
47+
>trigger : Symbol(CrashTrigger.trigger, Decl(noCrashOnMixin2.ts, 20, 41))
48+
49+
new Concrete();
50+
>Concrete : Symbol(Concrete, Decl(noCrashOnMixin2.ts, 5, 1))
51+
}
52+
}
53+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/compiler/noCrashOnMixin2.ts] ////
2+
3+
=== noCrashOnMixin2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62921
5+
6+
class Abstract {
7+
>Abstract : Abstract
8+
9+
protected constructor() {
10+
}
11+
}
12+
13+
class Concrete extends Abstract {
14+
>Concrete : Concrete
15+
>Abstract : Abstract
16+
}
17+
18+
type Constructor<T = {}> = new (...args?: any[]) => T;
19+
>Constructor : Constructor<T>
20+
>args : any[] | undefined
21+
22+
function Mixin<TBase extends Constructor>(Base: TBase) {
23+
>Mixin : <TBase extends Constructor<{}>>(Base: TBase) => { new (...args?: any[] | undefined): (Anonymous class); prototype: Mixin.(Anonymous class); } & TBase
24+
>Base : TBase
25+
26+
return class extends Base {
27+
>class extends Base { } : { new (...args?: any[] | undefined): (Anonymous class); prototype: Mixin.(Anonymous class); } & TBase
28+
>Base : {}
29+
30+
};
31+
}
32+
33+
class Empty {
34+
>Empty : Empty
35+
}
36+
37+
class CrashTrigger extends Mixin(Empty) {
38+
>CrashTrigger : CrashTrigger
39+
>Mixin(Empty) : Mixin.(Anonymous class)
40+
>Mixin : <TBase extends Constructor<{}>>(Base: TBase) => { new (...args?: any[] | undefined): (Anonymous class); prototype: Mixin.(Anonymous class); } & TBase
41+
>Empty : typeof Empty
42+
43+
public trigger() {
44+
>trigger : () => void
45+
46+
new Concrete();
47+
>new Concrete() : Concrete
48+
>Concrete : typeof Concrete
49+
}
50+
}
51+
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+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/62921
5+
6+
class Abstract {
7+
protected constructor() {
8+
}
9+
}
10+
11+
class Concrete extends Abstract {
12+
}
13+
14+
type Constructor<T = {}> = new (...args?: any[]) => T;
15+
16+
function Mixin<TBase extends Constructor>(Base: TBase) {
17+
return class extends Base {
18+
};
19+
}
20+
21+
class Empty {
22+
}
23+
24+
class CrashTrigger extends Mixin(Empty) {
25+
public trigger() {
26+
new Concrete();
27+
}
28+
}

0 commit comments

Comments
 (0)