* 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
Previously these were incorrectly treated just like normal properties:
```ts
class Parent {
a: any;
constructor(arg: any) {
this.a = arg;
}
}
class Child extends Parent {
declare a: number;
constructor(arg: number) {
super(arg);
console.log(this.a); // Property 'a' is used before being assigned. (2565)
}
}
```
Fixes#35327
* Emit defineProperty calls before param prop assignments
Note that I restricted this to --useDefineForClassFields is true.
Nothing changes when it's off. I think this is the correct fix for a
patch release.
However, in principal there's nothing wrong with moving parameter
property initialisation after property declaration initialisation. It
would be Extremely Bad and Wrong to rely on this working:
```ts
class C {
p = this.q // what is q?
constructor(public q: number) { }
}
```
But today it does, and probably somebody relies on it without knowing.
* Put parameter property initialiser into defineProperty's value
* Combine ES5/ESNext into one test
* useDefineForClassFields skips emit of ambient properties
Previously:
```ts
class C {
declare p
}
```
would incorrectly emit
```js
class C {
constructor() {
Object.defineProperty(this, "p", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
}
```
when useDefineForClassFields was turned on (for targets <ESNext).
* Fix bug for ESNext as well
This moves the check earlier in the pipeline.
* update baselines
* Disallow property/accessor overrides
Unless the base property or accessor is abstract
* Disallow uninitialised property overrides
This causes quite a few test breaks. We'll probably want to revert many
of them by switching to the upcoming `declare x: number` syntax.
* Updates from design review + fix ancient bug
1. Don't error when overriding properties from interfaces.
2. Fix error when overriding methods with other things. This had no
tests so I assume that the code was always dead and never worked.
* Need to add a couple of errors and squash one
Will update after checking out other branch for a minute
* Everything works so far
Need to test properties initialised in constructor
* Check for constructor initialisation
* change error wording
* Improve error wording
* Add codefix to add missing 'declare'
* Always emit accessors in .d.ts files
* Allow 'declare' on any uninitialised property decl
* Undo code moves
* Let sleeping dogs lie
* Correctly set NodeFlags.Ambient
And simplify redundant parts of check.
* Remove more unneeded code
* Update baselines
* Update baselines
* Update baselines
* Ignore this-property assignments
* Fix base-in-interface check
* Do not error when base parent is interface
* Fix base interface check
* Add missed baselines
* Fix check
* Fix new errors in services
* Fix new errors in services
* Fix errors in testRunner
* Add flag and turn off errors when on
* Structure of new emit is correct, fake content
It is 'hi'.
* Basically right emit
* Fix one last unitialised property declaration
* Haha no I missed another one
* Fix whitespace back to CRLF
* Minor fix and code cleanup
* New test case
* Fix bug in isInitializedProperty
* Updates from design meeting.
1. Change flag name to useDefineForClassFields (and flip polarity).
2. Forbid ES3 + useDefineForClassFields (since there is no
defineProperty).
3. Forbid overriding an abstract property-with-initializer with an
accessor.
* Update baselines
* Object.defineProperty for methods too
Using code from Ron from his upcoming refactor of the factory functions.
* Update slow baselines
* Improve error message
* Update src/compiler/transformers/utilities.ts
Co-Authored-By: Andrew Branch <andrewbranch@users.noreply.github.com>
* Add test of computed properties
* Remove done TODO