---
# System prepended metadata

title: Untitled

---

## Bug Report

**Current Behavior**
When the RestElement `...x` is inside a destructuring assignment of for-in statement and it is _not_ the last element, it is still parsed with error.

**Input Code**
- [REPL](https://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=GYewTgBAFA2gdAgHgGgLoQJYDsIxq1ASgG4g&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env&prettier=false&targets=&version=7.6.0&externalPlugins=)

```js
for ([...x,] in [[]]) ;
```

**Expected behavior/code**
It should throw `Rest element must be last element`.

**Possible Solution**

<!-- ALERT!!!!!!!
    Before submitting this comment, please:
      1. Write where to start searching the bug
      2. Write where to add a new test
    See https://github.com/babel/babel/issues/9563#issuecomment-466799807 for an example
-->

** Background **

We parse for-in statement in `parseForStatement`
https://github.com/babel/babel/blob/9c1ad0a9f7f8c57e02a20e9f151fce8076d80725/packages/babel-parser/src/parser/statement.js#L536-L541. The `parseExpression` will parse `[...x,]` into `init`, which is an `ArrayExpression` node with `SpreadElement` inside and later we change the node type to `ArrayPattern` with `RestElement` in `toAssignable`. However, it becomes too late to check for any comma after rest element since `this.state.commaAfterSpreadAt` has been reset before we exit `parseExpression`.

1. Note that `parseExpression` is passed with `noIn=true` in `parseForStatement`, we could pass `noIn` all the ways into `parseExprAtom`, so that we can apply `toAssignable` right after `ArrayExpression` is created if `noIn` is true.

1. You should add tests for this bug in https://github.com/babel/babel/tree/master/packages/babel-parser/test/fixtures/es2015/array-rest-spread

1. Run `make test-test262-update-whitelist` to update the whitelist, the following lines are expected to be removed.
https://github.com/babel/babel/blob/9c1ad0a9f7f8c57e02a20e9f151fce8076d80725/scripts/tests/test262/test262_whitelist.txt#L287-L294

If you don't know how to clone Babel, follow these steps: (you need to have `make` and `yarn` available on your machine).
1. Write a comment there to let other possible contributors know that you are working on this bug.
1. Fork the repo
1. Run `git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel`
1. Run `yarn && make bootstrap`
1. Wait :hourglass_flowing_sand: 
1. Run `make watch` (or `make build` whenever you change a file)
1. Add a test (only `input.js`; `output.js` will be automatically generated)
1. Update the code!
1. `yarn jest parser` to run the tests
    - If some test outputs don't match but the new results are correct, you can delete the bad `output.js` files and run the tests again
1. If it is working, run `make test` to run all the tests
1. Run `git push` and open a PR!