-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Avoid replacing last statement of derived constructors if 'this' is referenced. #12893
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7da4b85
Don't replace the last statement if 'this' is used/captured within th…
DanielRosenwasser 0de57d9
Accepted baselines.
DanielRosenwasser dd0a380
Added a tests for super property accesses within super calls of deriv…
DanielRosenwasser a01c195
Accepted baselines.
DanielRosenwasser 784eac5
Indented and-ed expressions.
DanielRosenwasser 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
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
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/captureSuperPropertyAccessInSuperCall01.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,17 @@ | ||
| tests/cases/compiler/captureSuperPropertyAccessInSuperCall01.ts(9,24): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/captureSuperPropertyAccessInSuperCall01.ts (1 errors) ==== | ||
| class A { | ||
| constructor(f: () => string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(() => { return super.blah(); }) | ||
| ~~~~~ | ||
| !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/captureSuperPropertyAccessInSuperCall01.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,33 @@ | ||
| //// [captureSuperPropertyAccessInSuperCall01.ts] | ||
| class A { | ||
| constructor(f: () => string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(() => { return super.blah(); }) | ||
| } | ||
| } | ||
|
|
||
| //// [captureSuperPropertyAccessInSuperCall01.js] | ||
| var __extends = (this && this.__extends) || function (d, b) { | ||
| for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
| function __() { this.constructor = d; } | ||
| d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
| }; | ||
| var A = (function () { | ||
| function A(f) { | ||
| } | ||
| A.prototype.blah = function () { return ""; }; | ||
| return A; | ||
| }()); | ||
| var B = (function (_super) { | ||
| __extends(B, _super); | ||
| function B() { | ||
| var _this = _super.call(this, function () { return _super.blah.call(_this); }) || this; | ||
| return _this; | ||
| } | ||
| return B; | ||
| }(A)); |
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
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
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
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
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
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
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/superPropertyAccessInSuperCall01.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,17 @@ | ||
| tests/cases/compiler/superPropertyAccessInSuperCall01.ts(9,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/superPropertyAccessInSuperCall01.ts (1 errors) ==== | ||
| class A { | ||
| constructor(f: string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(super.blah()) | ||
| ~~~~~ | ||
| !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/superPropertyAccessInSuperCall01.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,33 @@ | ||
| //// [superPropertyAccessInSuperCall01.ts] | ||
| class A { | ||
| constructor(f: string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(super.blah()) | ||
| } | ||
| } | ||
|
|
||
| //// [superPropertyAccessInSuperCall01.js] | ||
| var __extends = (this && this.__extends) || function (d, b) { | ||
| for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
| function __() { this.constructor = d; } | ||
| d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
| }; | ||
| var A = (function () { | ||
| function A(f) { | ||
| } | ||
| A.prototype.blah = function () { return ""; }; | ||
| return A; | ||
| }()); | ||
| var B = (function (_super) { | ||
| __extends(B, _super); | ||
| function B() { | ||
| var _this = _super.call(this, _super.blah.call(_this)) || this; | ||
| return _this; | ||
| } | ||
| return B; | ||
| }(A)); |
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
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
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
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
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
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
11 changes: 11 additions & 0 deletions
11
tests/cases/compiler/captureSuperPropertyAccessInSuperCall01.ts
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,11 @@ | ||
| class A { | ||
| constructor(f: () => string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(() => { return super.blah(); }) | ||
| } | ||
| } |
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,11 @@ | ||
| class A { | ||
| constructor(f: string) { | ||
| } | ||
| public blah(): string { return ""; } | ||
| } | ||
|
|
||
| class B extends A { | ||
| constructor() { | ||
| super(super.blah()) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These baselines don't quite seem correct. Surely, _this should be declared and set to
thisfirst before thesupercall. That way, if the super call invokes the function, it'll still function as expected if the super does not return a new value forthis.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional - in ES2015 it is an error for
thisto be used before a call tosuper(...)completes.This means that it shouldn't be an error to reference
_this, but it should be an error to evaluate_thisin a callback before thesuper(...)call returns.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ES2015 doesn't have a bunch of existing TypeScript code that evaluates this in a callback before the super() call returns.