Skip to content

Commit 2ff976e

Browse files
authored
Stop getFlowTypeOfProperty overwriting with base class property (#1432)
1 parent f616d7f commit 2ff976e

File tree

105 files changed

+331
-945
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+331
-945
lines changed

internal/checker/checker.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10908,7 +10908,9 @@ func (c *Checker) getControlFlowContainer(node *ast.Node) *ast.Node {
1090810908
func (c *Checker) getFlowTypeOfProperty(reference *ast.Node, prop *ast.Symbol) *Type {
1090910909
initialType := c.undefinedType
1091010910
if prop != nil && prop.ValueDeclaration != nil && (!c.isAutoTypedProperty(prop) || prop.ValueDeclaration.ModifierFlags()&ast.ModifierFlagsAmbient != 0) {
10911-
initialType = c.getTypeOfPropertyInBaseClass(prop)
10911+
if baseType := c.getTypeOfPropertyInBaseClass(prop); baseType != nil {
10912+
initialType = baseType
10913+
}
1091210914
}
1091310915
return c.getFlowTypeOfReferenceEx(reference, c.autoType, initialType, nil, nil)
1091410916
}
@@ -26217,7 +26219,13 @@ func (c *Checker) isAssignmentToReadonlyEntity(expr *ast.Node, symbol *ast.Symbo
2621726219
}
2621826220

2621926221
func (c *Checker) isThisPropertyAccessInConstructor(node *ast.Node, prop *ast.Symbol) bool {
26220-
return isThisProperty(node) && c.isAutoTypedProperty(prop) && ast.GetThisContainer(node, true /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/) == c.getDeclaringConstructor(prop)
26222+
var constructor *ast.Node
26223+
if kind, location := c.isConstructorDeclaredThisProperty(prop); kind == thisAssignmentDeclarationConstructor {
26224+
constructor = location
26225+
} else if isThisProperty(node) && c.isAutoTypedProperty(prop) {
26226+
constructor = c.getDeclaringConstructor(prop)
26227+
}
26228+
return ast.GetThisContainer(node, true /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/) == constructor
2622126229
}
2622226230

2622326231
func (c *Checker) isAutoTypedProperty(symbol *ast.Symbol) bool {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/conformance/salsa/controlFlowJSClassProperty.ts] ////
2+
3+
//// [controlFlowJSClassProperty.js]
4+
export class C {
5+
name = "CompileDiagnostic";
6+
7+
/**
8+
* @param {[number, number] | undefined} position
9+
*/
10+
constructor(position) {
11+
if (position) {
12+
this.position = position;
13+
}
14+
}
15+
}
16+
var c = new C([1, 2]);
17+
c.position;
18+
19+
20+
//// [controlFlowJSClassProperty.js]
21+
"use strict";
22+
Object.defineProperty(exports, "__esModule", { value: true });
23+
exports.C = void 0;
24+
class C {
25+
name = "CompileDiagnostic";
26+
/**
27+
* @param {[number, number] | undefined} position
28+
*/
29+
constructor(position) {
30+
if (position) {
31+
this.position = position;
32+
}
33+
}
34+
}
35+
exports.C = C;
36+
var c = new C([1, 2]);
37+
c.position;
38+
39+
40+
//// [controlFlowJSClassProperty.d.ts]
41+
export declare class C {
42+
name: string;
43+
/**
44+
* @param {[number, number] | undefined} position
45+
*/
46+
constructor(position: [number, number] | undefined);
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/conformance/salsa/controlFlowJSClassProperty.ts] ////
2+
3+
=== controlFlowJSClassProperty.js ===
4+
export class C {
5+
>C : Symbol(C, Decl(controlFlowJSClassProperty.js, 0, 0))
6+
7+
name = "CompileDiagnostic";
8+
>name : Symbol(name, Decl(controlFlowJSClassProperty.js, 0, 16))
9+
10+
/**
11+
* @param {[number, number] | undefined} position
12+
*/
13+
constructor(position) {
14+
>position : Symbol(position, Decl(controlFlowJSClassProperty.js, 6, 14))
15+
16+
if (position) {
17+
>position : Symbol(position, Decl(controlFlowJSClassProperty.js, 6, 14))
18+
19+
this.position = position;
20+
>this.position : Symbol(position, Decl(controlFlowJSClassProperty.js, 7, 19))
21+
>this : Symbol(C, Decl(controlFlowJSClassProperty.js, 0, 0))
22+
>position : Symbol(position, Decl(controlFlowJSClassProperty.js, 7, 19))
23+
>position : Symbol(position, Decl(controlFlowJSClassProperty.js, 6, 14))
24+
}
25+
}
26+
}
27+
var c = new C([1, 2]);
28+
>c : Symbol(c, Decl(controlFlowJSClassProperty.js, 12, 3))
29+
>C : Symbol(C, Decl(controlFlowJSClassProperty.js, 0, 0))
30+
31+
c.position;
32+
>c.position : Symbol(position, Decl(controlFlowJSClassProperty.js, 7, 19))
33+
>c : Symbol(c, Decl(controlFlowJSClassProperty.js, 12, 3))
34+
>position : Symbol(position, Decl(controlFlowJSClassProperty.js, 7, 19))
35+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/conformance/salsa/controlFlowJSClassProperty.ts] ////
2+
3+
=== controlFlowJSClassProperty.js ===
4+
export class C {
5+
>C : C
6+
7+
name = "CompileDiagnostic";
8+
>name : string
9+
>"CompileDiagnostic" : "CompileDiagnostic"
10+
11+
/**
12+
* @param {[number, number] | undefined} position
13+
*/
14+
constructor(position) {
15+
>position : [number, number] | undefined
16+
17+
if (position) {
18+
>position : [number, number] | undefined
19+
20+
this.position = position;
21+
>this.position = position : [number, number]
22+
>this.position : any
23+
>this : this
24+
>position : any
25+
>position : [number, number]
26+
}
27+
}
28+
}
29+
var c = new C([1, 2]);
30+
>c : C
31+
>new C([1, 2]) : C
32+
>C : typeof C
33+
>[1, 2] : [number, number]
34+
>1 : 1
35+
>2 : 2
36+
37+
c.position;
38+
>c.position : [number, number] | undefined
39+
>c : C
40+
>position : [number, number] | undefined
41+

testdata/baselines/reference/submodule/compiler/checkIndexConstraintOfJavascriptClassExpression.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ someFunction(function(BaseClass) {
2525

2626
this.foo = "bar";
2727
>this.foo = "bar" : "bar"
28-
>this.foo : string
28+
>this.foo : any
2929
>this : this
30-
>foo : string
30+
>foo : any
3131
>"bar" : "bar"
3232
}
3333
_render(error) {

testdata/baselines/reference/submodule/compiler/checkJsFiles_noErrorLocation.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class B extends A {
2727

2828
this.foo = () => 3;
2929
>this.foo = () => 3 : () => number
30-
>this.foo : () => number
30+
>this.foo : any
3131
>this : this
32-
>foo : () => number
32+
>foo : any
3333
>() => 3 : () => number
3434
>3 : 3
3535
}

testdata/baselines/reference/submodule/compiler/checkSuperCallBeforeThisAccessing9.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class Derived {
1414

1515
this.x = 10;
1616
>this.x = 10 : 10
17-
>this.x : number
17+
>this.x : any
1818
>this : this
19-
>x : number
19+
>x : any
2020
>10 : 10
2121

2222
var that = this;

testdata/baselines/reference/submodule/compiler/classExtendingAny.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class B extends Err {
6969

7070
this.wat = 12
7171
>this.wat = 12 : 12
72-
>this.wat : number
72+
>this.wat : any
7373
>this : this
74-
>wat : number
74+
>wat : any
7575
>12 : 12
7676
}
7777
f() {

testdata/baselines/reference/submodule/compiler/classFieldSuperNotAccessibleJs.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class YaddaBase {
99
constructor() {
1010
this.roots = "hi";
1111
>this.roots = "hi" : "hi"
12-
>this.roots : string
12+
>this.roots : any
1313
>this : this
14-
>roots : string
14+
>roots : any
1515
>"hi" : "hi"
1616

1717
/** @type number */

testdata/baselines/reference/submodule/compiler/jsFileClassPropertyType.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class C {
77
constructor () {
88
this.p = 0;
99
>this.p = 0 : 0
10-
>this.p : number
10+
>this.p : any
1111
>this : this
12-
>p : number
12+
>p : any
1313
>0 : 0
1414
}
1515
}

0 commit comments

Comments
 (0)