-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Added error for class properties used within their own declaration #29395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sandersn
merged 8 commits into
microsoft:master
from
JoshuaKGoldberg:property-use-before-declare
Apr 5, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd88f6a
Added error for class properties used within their own declaration
38e1856
Accepted 'witness' baselines; removed unnecessary !==
3c8ce9b
Addressed quick feedback items
ee4e90c
Merge branch 'master' into property-use-before-declare
6661f90
Accepted odd new baseline
236fed9
Fixed post-merge introduced lint errors
e767ee0
Merge branch 'master' into property-use-before-declare
0c832f2
Updated baselines again
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
66 changes: 66 additions & 0 deletions
66
tests/baselines/reference/classUsedBeforeInitializedVariables.errors.txt
This file contains hidden or 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,66 @@ | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(4,15): error TS2729: Property 'p4' is used before its initialization. | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(7,34): error TS2729: Property 'directlyAssigned' is used before its initialization. | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(16,15): error TS2729: Property 'withinObjectLiteral' is used before its initialization. | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(20,19): error TS2729: Property 'withinObjectLiteralGetterName' is used before its initialization. | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(26,19): error TS2729: Property 'withinObjectLiteralSetterName' is used before its initialization. | ||
tests/cases/compiler/classUsedBeforeInitializedVariables.ts(29,64): error TS2729: Property 'withinClassDeclarationExtension' is used before its initialization. | ||
|
||
|
||
==== tests/cases/compiler/classUsedBeforeInitializedVariables.ts (6 errors) ==== | ||
class Test { | ||
p1 = 0; | ||
p2 = this.p1; | ||
p3 = this.p4; | ||
~~ | ||
!!! error TS2729: Property 'p4' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:5:5: 'p4' is declared here. | ||
p4 = 0; | ||
|
||
directlyAssigned: any = this.directlyAssigned; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2729: Property 'directlyAssigned' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:7:5: 'directlyAssigned' is declared here. | ||
|
||
withinArrowFunction: any = () => this.withinArrowFunction; | ||
|
||
withinFunction: any = function () { | ||
return this.withinFunction; | ||
}; | ||
|
||
withinObjectLiteral: any = { | ||
[this.withinObjectLiteral]: true, | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2729: Property 'withinObjectLiteral' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:15:5: 'withinObjectLiteral' is declared here. | ||
}; | ||
|
||
withinObjectLiteralGetterName: any = { | ||
get [this.withinObjectLiteralGetterName]() { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2729: Property 'withinObjectLiteralGetterName' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:19:5: 'withinObjectLiteralGetterName' is declared here. | ||
return true; | ||
} | ||
}; | ||
|
||
withinObjectLiteralSetterName: any = { | ||
set [this.withinObjectLiteralSetterName](_: any) {} | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2729: Property 'withinObjectLiteralSetterName' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:25:5: 'withinObjectLiteralSetterName' is declared here. | ||
}; | ||
|
||
withinClassDeclarationExtension: any = (class extends this.withinClassDeclarationExtension { }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2729: Property 'withinClassDeclarationExtension' is used before its initialization. | ||
!!! related TS2728 tests/cases/compiler/classUsedBeforeInitializedVariables.ts:29:5: 'withinClassDeclarationExtension' is declared here. | ||
|
||
// These error cases are ignored (not checked by control flow analysis) | ||
|
||
assignedByArrowFunction: any = (() => this.assignedByFunction)(); | ||
|
||
assignedByFunction: any = (function () { | ||
return this.assignedByFunction; | ||
})(); | ||
} | ||
|
102 changes: 102 additions & 0 deletions
102
tests/baselines/reference/classUsedBeforeInitializedVariables.js
This file contains hidden or 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,102 @@ | ||
//// [classUsedBeforeInitializedVariables.ts] | ||
class Test { | ||
p1 = 0; | ||
p2 = this.p1; | ||
p3 = this.p4; | ||
p4 = 0; | ||
|
||
directlyAssigned: any = this.directlyAssigned; | ||
|
||
withinArrowFunction: any = () => this.withinArrowFunction; | ||
|
||
withinFunction: any = function () { | ||
return this.withinFunction; | ||
}; | ||
|
||
withinObjectLiteral: any = { | ||
[this.withinObjectLiteral]: true, | ||
}; | ||
|
||
withinObjectLiteralGetterName: any = { | ||
get [this.withinObjectLiteralGetterName]() { | ||
return true; | ||
} | ||
}; | ||
|
||
withinObjectLiteralSetterName: any = { | ||
set [this.withinObjectLiteralSetterName](_: any) {} | ||
}; | ||
|
||
withinClassDeclarationExtension: any = (class extends this.withinClassDeclarationExtension { }); | ||
|
||
// These error cases are ignored (not checked by control flow analysis) | ||
|
||
assignedByArrowFunction: any = (() => this.assignedByFunction)(); | ||
|
||
assignedByFunction: any = (function () { | ||
return this.assignedByFunction; | ||
})(); | ||
} | ||
|
||
|
||
//// [classUsedBeforeInitializedVariables.js] | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var Test = /** @class */ (function () { | ||
function Test() { | ||
var _a, _b, _c; | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var _this = this; | ||
this.p1 = 0; | ||
this.p2 = this.p1; | ||
this.p3 = this.p4; | ||
this.p4 = 0; | ||
this.directlyAssigned = this.directlyAssigned; | ||
this.withinArrowFunction = function () { return _this.withinArrowFunction; }; | ||
this.withinFunction = function () { | ||
return this.withinFunction; | ||
}; | ||
this.withinObjectLiteral = (_a = {}, | ||
_a[this.withinObjectLiteral] = true, | ||
_a); | ||
this.withinObjectLiteralGetterName = (_b = {}, | ||
Object.defineProperty(_b, this.withinObjectLiteralGetterName, { | ||
get: function () { | ||
return true; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
_b); | ||
this.withinObjectLiteralSetterName = (_c = {}, | ||
Object.defineProperty(_c, this.withinObjectLiteralSetterName, { | ||
set: function (_) { }, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
_c); | ||
this.withinClassDeclarationExtension = (/** @class */ (function (_super) { | ||
__extends(class_1, _super); | ||
function class_1() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return class_1; | ||
}(this.withinClassDeclarationExtension))); | ||
// These error cases are ignored (not checked by control flow analysis) | ||
this.assignedByArrowFunction = (function () { return _this.assignedByFunction; })(); | ||
this.assignedByFunction = (function () { | ||
return this.assignedByFunction; | ||
})(); | ||
} | ||
return Test; | ||
}()); |
97 changes: 97 additions & 0 deletions
97
tests/baselines/reference/classUsedBeforeInitializedVariables.symbols
This file contains hidden or 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,97 @@ | ||
=== tests/cases/compiler/classUsedBeforeInitializedVariables.ts === | ||
class Test { | ||
>Test : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
|
||
p1 = 0; | ||
>p1 : Symbol(Test.p1, Decl(classUsedBeforeInitializedVariables.ts, 0, 12)) | ||
|
||
p2 = this.p1; | ||
>p2 : Symbol(Test.p2, Decl(classUsedBeforeInitializedVariables.ts, 1, 11)) | ||
>this.p1 : Symbol(Test.p1, Decl(classUsedBeforeInitializedVariables.ts, 0, 12)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>p1 : Symbol(Test.p1, Decl(classUsedBeforeInitializedVariables.ts, 0, 12)) | ||
|
||
p3 = this.p4; | ||
>p3 : Symbol(Test.p3, Decl(classUsedBeforeInitializedVariables.ts, 2, 17)) | ||
>this.p4 : Symbol(Test.p4, Decl(classUsedBeforeInitializedVariables.ts, 3, 17)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>p4 : Symbol(Test.p4, Decl(classUsedBeforeInitializedVariables.ts, 3, 17)) | ||
|
||
p4 = 0; | ||
>p4 : Symbol(Test.p4, Decl(classUsedBeforeInitializedVariables.ts, 3, 17)) | ||
|
||
directlyAssigned: any = this.directlyAssigned; | ||
>directlyAssigned : Symbol(Test.directlyAssigned, Decl(classUsedBeforeInitializedVariables.ts, 4, 11)) | ||
>this.directlyAssigned : Symbol(Test.directlyAssigned, Decl(classUsedBeforeInitializedVariables.ts, 4, 11)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>directlyAssigned : Symbol(Test.directlyAssigned, Decl(classUsedBeforeInitializedVariables.ts, 4, 11)) | ||
|
||
withinArrowFunction: any = () => this.withinArrowFunction; | ||
>withinArrowFunction : Symbol(Test.withinArrowFunction, Decl(classUsedBeforeInitializedVariables.ts, 6, 50)) | ||
>this.withinArrowFunction : Symbol(Test.withinArrowFunction, Decl(classUsedBeforeInitializedVariables.ts, 6, 50)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>withinArrowFunction : Symbol(Test.withinArrowFunction, Decl(classUsedBeforeInitializedVariables.ts, 6, 50)) | ||
|
||
withinFunction: any = function () { | ||
>withinFunction : Symbol(Test.withinFunction, Decl(classUsedBeforeInitializedVariables.ts, 8, 62)) | ||
|
||
return this.withinFunction; | ||
}; | ||
|
||
withinObjectLiteral: any = { | ||
>withinObjectLiteral : Symbol(Test.withinObjectLiteral, Decl(classUsedBeforeInitializedVariables.ts, 12, 6)) | ||
|
||
[this.withinObjectLiteral]: true, | ||
>[this.withinObjectLiteral] : Symbol([this.withinObjectLiteral], Decl(classUsedBeforeInitializedVariables.ts, 14, 32)) | ||
>this.withinObjectLiteral : Symbol(Test.withinObjectLiteral, Decl(classUsedBeforeInitializedVariables.ts, 12, 6)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>withinObjectLiteral : Symbol(Test.withinObjectLiteral, Decl(classUsedBeforeInitializedVariables.ts, 12, 6)) | ||
|
||
}; | ||
|
||
withinObjectLiteralGetterName: any = { | ||
>withinObjectLiteralGetterName : Symbol(Test.withinObjectLiteralGetterName, Decl(classUsedBeforeInitializedVariables.ts, 16, 6)) | ||
|
||
get [this.withinObjectLiteralGetterName]() { | ||
>[this.withinObjectLiteralGetterName] : Symbol([this.withinObjectLiteralGetterName], Decl(classUsedBeforeInitializedVariables.ts, 18, 42)) | ||
>this.withinObjectLiteralGetterName : Symbol(Test.withinObjectLiteralGetterName, Decl(classUsedBeforeInitializedVariables.ts, 16, 6)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>withinObjectLiteralGetterName : Symbol(Test.withinObjectLiteralGetterName, Decl(classUsedBeforeInitializedVariables.ts, 16, 6)) | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
withinObjectLiteralSetterName: any = { | ||
>withinObjectLiteralSetterName : Symbol(Test.withinObjectLiteralSetterName, Decl(classUsedBeforeInitializedVariables.ts, 22, 6)) | ||
|
||
set [this.withinObjectLiteralSetterName](_: any) {} | ||
>[this.withinObjectLiteralSetterName] : Symbol([this.withinObjectLiteralSetterName], Decl(classUsedBeforeInitializedVariables.ts, 24, 42)) | ||
>this.withinObjectLiteralSetterName : Symbol(Test.withinObjectLiteralSetterName, Decl(classUsedBeforeInitializedVariables.ts, 22, 6)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>withinObjectLiteralSetterName : Symbol(Test.withinObjectLiteralSetterName, Decl(classUsedBeforeInitializedVariables.ts, 22, 6)) | ||
>_ : Symbol(_, Decl(classUsedBeforeInitializedVariables.ts, 25, 49)) | ||
|
||
}; | ||
|
||
withinClassDeclarationExtension: any = (class extends this.withinClassDeclarationExtension { }); | ||
>withinClassDeclarationExtension : Symbol(Test.withinClassDeclarationExtension, Decl(classUsedBeforeInitializedVariables.ts, 26, 6)) | ||
>this.withinClassDeclarationExtension : Symbol(Test.withinClassDeclarationExtension, Decl(classUsedBeforeInitializedVariables.ts, 26, 6)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>withinClassDeclarationExtension : Symbol(Test.withinClassDeclarationExtension, Decl(classUsedBeforeInitializedVariables.ts, 26, 6)) | ||
|
||
// These error cases are ignored (not checked by control flow analysis) | ||
|
||
assignedByArrowFunction: any = (() => this.assignedByFunction)(); | ||
>assignedByArrowFunction : Symbol(Test.assignedByArrowFunction, Decl(classUsedBeforeInitializedVariables.ts, 28, 100)) | ||
>this.assignedByFunction : Symbol(Test.assignedByFunction, Decl(classUsedBeforeInitializedVariables.ts, 32, 69)) | ||
>this : Symbol(Test, Decl(classUsedBeforeInitializedVariables.ts, 0, 0)) | ||
>assignedByFunction : Symbol(Test.assignedByFunction, Decl(classUsedBeforeInitializedVariables.ts, 32, 69)) | ||
|
||
assignedByFunction: any = (function () { | ||
>assignedByFunction : Symbol(Test.assignedByFunction, Decl(classUsedBeforeInitializedVariables.ts, 32, 69)) | ||
|
||
return this.assignedByFunction; | ||
})(); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.