ESNext+[[Define]]: reference to param props illegal (#36425)

* ESNext+[[Define]]: reference to param props illegal

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]].

* improve test
This commit is contained in:
Nathan Shively-Sanders
2020-01-24 14:53:28 -08:00
committed by GitHub
parent 43fc19c958
commit 368db997ed
7 changed files with 264 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
// @useDefineForClassFields: true
// @target: esnext
class C {
qux = this.bar // should error
bar = this.foo // should error
quiz = this.bar // ok
m1() {
this.foo // ok
}
constructor(private foo: string) {}
quim = this.baz // should error
baz = this.foo; // should error
quid = this.baz // ok
m2() {
this.foo // ok
}
}