ESNext+[[Define]]: reference to param props illegal#36425
Merged
Conversation
When target: "esnext" and useDefineForClassFields: true, property
declaration initialisers should not be able to reference parameter
properties; class fields are initialised before the constructor runs,
but parameter properties are not:
```ts
class C {
foo = this.bar
constructor(public bar: string) { }
}
```
emits code that looks like this:
```js
class C {
bar
foo = this.bar
constructor(bar) {
this.bar = bar
}
}
new C('x').foo.length // crashes; foo is undefined
```
This PR adds an error on foo's declaration with ESNext+[[Define]].
sandersn
commented
Jan 24, 2020
| class C { | ||
| qux = this.bar // should error | ||
| bar = this.foo // should error | ||
| quiz = this.bar // ok |
Member
Author
There was a problem hiding this comment.
Given the code change, I don't understand why the lines with // ok don't error. It is nice that they don't though.
andrewbranch
approved these changes
Jan 24, 2020
ajafff
reviewed
Jan 26, 2020
Contributor
ajafff
left a comment
There was a problem hiding this comment.
Something in this PR broke checking of deferred access to (regular) properties. Will open an issue after investigating.
class C {
bar = () => this.foo; // was no error in -dev.20200124, is an error in -dev.20200125
foo = 1;
}
This was referenced Jan 26, 2020
1 task
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
When target: "esnext" and useDefineForClassFields: true, property
declaration initialisers should not be able to reference parameter
properties; class fields are initialised before the constructor runs,
but parameter properties are not:
emits code that looks like this:
This PR adds an error on foo's declaration with ESNext+[[Define]].
Fixes #36410