You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allowed non-this, non-super code before super call in derived classes with property initializers (#29374)
* Allowed non-this, non-super code before super call in derived classes
Fixes#8277.
It feels wrong to put a new `forEachChild` loop in the checker, though in the vast majority of user files this will be a very quick one. Is there a better way to check for a reference to `super` or `this`?
* Used new isNewThisScope utility and dummy prop in baselines
* Accounted for parameters and get/set accessor bodies
* Accounted for class extensions
* (node|statement)RefersToSuperOrThis wasn't checking root level scope boundaries
```ts
function () {
return this;
}
```
It was immediately going to `ts.forEachChild` so the statement itself wasn't being counted as a new `this` scope.
* Better 'references' name and comments; accounted for more method edge case
* Limited super calls to root-level statements in constructor bodies
As per discussion in the issue, it would be ideal to consider any block that always ends up calling to super() the equivalent of a root-level super() statement. This would be valid:
```ts
foo = 1;
constructor() {
condition() ? super(1) : super(0);
this.foo;
}
```
...as it would compile to the equivalent of:
```ts
function () {
condition() ? super(1) : super(0);
this.foo = 1;
this.foo;
}
That change would a bit more intense and I'm very timid, so leaving it out of this PR. In the meantime the requirement is that the super() statement must itself be root-level.
* Fixed error number following 'master' merge
* Added decorator test cases
* Again allowed arrow functions
* Accepted new baselines
* Added allowance for (super())
* Reworked emitter transforms for ES this binding semantics
In trying to adjust to rbuckton's PR feedback, this orders derived class constructor bodies into five sections:
1. Pre-`super()` code
2. The `super()` call itself
3. Class properties with initializers
4. Parameter properties
5. The rest of the constructor
I've looked through the updated baselines and it looks like they're generally better than before. Within the existing test cases that result in semantic errors for `this` access before `super`, several previously resulted in a global `_this` being created; now, they correctly defer referring to `_this` until it's assigned to `_super.call(this) || this`.
* Used skipOuterExpressions when diving for super()s; fix prop ordering
* Allow direct var _this = super() when no pre-super() statements; lint fixes
* Always with the TSLint
* One last touchup: skipOuterExpressions in es2015 transformer
* Fixed new lint complaint in utilities.ts
* Again added a falls-through; it'd be swell if I could run linting locally
* This time I think I got it
* Well at least the error is a different one
* Undid irrelevant whitespace changes
* Mostly addressed private/field issues
* Accepted derivedClassSuperProperties baseline
* Lint fix, lovely
* Remove now-unnecesary comment
* First round of feedback
* Moved prologue statements to start of statements
* Added consideration for super statements in loops and the like
* Ordering and a _this_1 test
* Missed the one change I needed...
* First round of feedback corrections
* Feedback round two: statements
* Feedback: used more direct statements
* Fixed classFields emit to not duplicate temp variables
* Refactored es2015 helper to be less overloaded
* Accounted for parentheses
* Simpler feedback: -1, and emptyArray
* Next feedback: superStatementIndex
* Feedback: simplified to no longer create slice arrays
* Adjusted for default and rest parameters
* Added test case for commas
* Corrected comment ranges
* Handled comments after super, with tests
* Fixed Bad/Late super baselines
* Remove unused param and unnecessary baseline comments
Co-authored-by: Orta Therox <[email protected]>
Co-authored-by: Ron Buckton <[email protected]>
Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+5-1
Original file line number
Diff line number
Diff line change
@@ -1747,7 +1747,7 @@
1747
1747
"category": "Error",
1748
1748
"code": 2375
1749
1749
},
1750
-
"A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.": {
1750
+
"A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.": {
1751
1751
"category": "Error",
1752
1752
"code": 2376
1753
1753
},
@@ -1839,6 +1839,10 @@
1839
1839
"category": "Error",
1840
1840
"code": 2400
1841
1841
},
1842
+
"A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.": {
1843
+
"category": "Error",
1844
+
"code": 2401
1845
+
},
1842
1846
"Expression resolves to '_super' that compiler uses to capture base class reference.": {
0 commit comments