Chaining multiple methods that use the spread operator breaks `this` value
- Dominant language
- JavaScript
- Stars
- 874
- Forks
- 62
- PR merge metrics
- No merged PRs in 30d
Description
Here's a minimal reproduction: https://buble.surge.sh/#class%20Test%20%7B%0A%09constructor()%7B%0A%20%20%20%20%20%20this.list%20%3D%20%5B%5D%3B%0A%20%20%20%20%7D%0A%20%20%09add(...items)%7B%0A%20%20%20%20%20%20this.list.push(...items)%3B%0A%20%20%20%20%20%20return%20this%3B%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20numbers%20%3D%20%5B1%2C2%2C3%5D%3B%0Alet%20letters%20%3D%20%5B%22A%22%2C%22B%22%2C%22C%22%5D%3B%0A%0A%2F%2F%20OK%2C%20uses%20correct%20ref%0Anew%20Test()%0A%20%20%20%20.add(...letters)%3B%0A%0A%2F%2F%20NOT%20OK%2C%20first%20.add()%20will%20have%20undefined%20%60this%60%20value%0Anew%20Test()%0A%09.add(...numbers)%0A%20%20%09.add(...letters)%3B
When chaining:
```js
new Test()
.add(...numbers)
.add(...letters);
```
The transpiled output:
```js
(ref$2 = (ref$1 = new Test())
.add.apply(ref$2, numbers)) // This will throw
.add.apply(ref$1, letters);
```
Uses `ref$2` as the `this` value for the first chained call before it has a value.
I'm guessing the desired output would be something like?
```js
(ref$3 = (ref$2 = (ref$1 = new Test()))
.add.apply(ref$2, numbers))
.add.apply(ref$3, letters);
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.