-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid circular reference in this-property assignments (#37827)
* Avoid circular reference in this-property assignments To do this, don't check this-property assigments that have the this-property of the lhs appearing somewhere on the rhs: ```js class C { m() { this.x = 12 this.x = this.x + this.y } } ``` I tried suppressing the circularity error, but because we cache the first type discovered for a property, this still results in an implicit any for `x` in the previous example. It just doesn't have an error. Fixes #35099 * Add test case + rename function * Use isMatchingReference
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/thisPropertyAssignmentCircular.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//// [thisPropertyAssignmentCircular.js] | ||
export class Foo { | ||
constructor() { | ||
this.foo = "Hello"; | ||
} | ||
slicey() { | ||
this.foo = this.foo.slice(); | ||
} | ||
m() { | ||
this.foo | ||
} | ||
} | ||
|
||
/** @class */ | ||
function C() { | ||
this.x = 0; | ||
this.x = function() { this.x.toString(); } | ||
} | ||
|
||
|
||
|
||
|
||
//// [thisPropertyAssignmentCircular.d.ts] | ||
export class Foo { | ||
foo: string; | ||
slicey(): void; | ||
m(): void; | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/baselines/reference/thisPropertyAssignmentCircular.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
=== tests/cases/conformance/salsa/thisPropertyAssignmentCircular.js === | ||
export class Foo { | ||
>Foo : Symbol(Foo, Decl(thisPropertyAssignmentCircular.js, 0, 0)) | ||
|
||
constructor() { | ||
this.foo = "Hello"; | ||
>this.foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>this : Symbol(Foo, Decl(thisPropertyAssignmentCircular.js, 0, 0)) | ||
>foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
} | ||
slicey() { | ||
>slicey : Symbol(Foo.slicey, Decl(thisPropertyAssignmentCircular.js, 3, 5)) | ||
|
||
this.foo = this.foo.slice(); | ||
>this.foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>this : Symbol(Foo, Decl(thisPropertyAssignmentCircular.js, 0, 0)) | ||
>foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>this.foo.slice : Symbol(String.slice, Decl(lib.es5.d.ts, --, --)) | ||
>this.foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>this : Symbol(Foo, Decl(thisPropertyAssignmentCircular.js, 0, 0)) | ||
>foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>slice : Symbol(String.slice, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
m() { | ||
>m : Symbol(Foo.m, Decl(thisPropertyAssignmentCircular.js, 6, 5)) | ||
|
||
this.foo | ||
>this.foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
>this : Symbol(Foo, Decl(thisPropertyAssignmentCircular.js, 0, 0)) | ||
>foo : Symbol(Foo.foo, Decl(thisPropertyAssignmentCircular.js, 1, 19), Decl(thisPropertyAssignmentCircular.js, 4, 14)) | ||
} | ||
} | ||
|
||
/** @class */ | ||
function C() { | ||
>C : Symbol(C, Decl(thisPropertyAssignmentCircular.js, 10, 1)) | ||
|
||
this.x = 0; | ||
>this.x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
>this : Symbol(C, Decl(thisPropertyAssignmentCircular.js, 10, 1)) | ||
>x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
|
||
this.x = function() { this.x.toString(); } | ||
>this.x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
>this : Symbol(C, Decl(thisPropertyAssignmentCircular.js, 10, 1)) | ||
>x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
>this.x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
>this : Symbol(C, Decl(thisPropertyAssignmentCircular.js, 10, 1)) | ||
>x : Symbol(C.x, Decl(thisPropertyAssignmentCircular.js, 13, 14), Decl(thisPropertyAssignmentCircular.js, 14, 15)) | ||
} | ||
|
62 changes: 62 additions & 0 deletions
62
tests/baselines/reference/thisPropertyAssignmentCircular.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
=== tests/cases/conformance/salsa/thisPropertyAssignmentCircular.js === | ||
export class Foo { | ||
>Foo : Foo | ||
|
||
constructor() { | ||
this.foo = "Hello"; | ||
>this.foo = "Hello" : "Hello" | ||
>this.foo : string | ||
>this : this | ||
>foo : string | ||
>"Hello" : "Hello" | ||
} | ||
slicey() { | ||
>slicey : () => void | ||
|
||
this.foo = this.foo.slice(); | ||
>this.foo = this.foo.slice() : string | ||
>this.foo : string | ||
>this : this | ||
>foo : string | ||
>this.foo.slice() : string | ||
>this.foo.slice : (start?: number, end?: number) => string | ||
>this.foo : string | ||
>this : this | ||
>foo : string | ||
>slice : (start?: number, end?: number) => string | ||
} | ||
m() { | ||
>m : () => void | ||
|
||
this.foo | ||
>this.foo : string | ||
>this : this | ||
>foo : string | ||
} | ||
} | ||
|
||
/** @class */ | ||
function C() { | ||
>C : typeof C | ||
|
||
this.x = 0; | ||
>this.x = 0 : 0 | ||
>this.x : any | ||
>this : this | ||
>x : any | ||
>0 : 0 | ||
|
||
this.x = function() { this.x.toString(); } | ||
>this.x = function() { this.x.toString(); } : () => void | ||
>this.x : any | ||
>this : this | ||
>x : any | ||
>function() { this.x.toString(); } : () => void | ||
>this.x.toString() : any | ||
>this.x.toString : any | ||
>this.x : any | ||
>this : this | ||
>x : any | ||
>toString : any | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
tests/cases/conformance/salsa/thisPropertyAssignmentCircular.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// @allowJs: true | ||
// @checkJs: true | ||
// @declaration: true | ||
// @emitDeclarationOnly: true | ||
// @filename: thisPropertyAssignmentCircular.js | ||
export class Foo { | ||
constructor() { | ||
this.foo = "Hello"; | ||
} | ||
slicey() { | ||
this.foo = this.foo.slice(); | ||
} | ||
m() { | ||
this.foo | ||
} | ||
} | ||
|
||
/** @class */ | ||
function C() { | ||
this.x = 0; | ||
this.x = function() { this.x.toString(); } | ||
} |