Skip to content

Commit

Permalink
Add explicit exception for 'async of' not starting arrow in for init …
Browse files Browse the repository at this point in the history
…context

FIX: Fix issue where the library couldn't parse 'for (async of ...)'.

Closes #1031
  • Loading branch information
marijnh committed May 4, 2021
1 parent 556a7ab commit 7cbe0f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
}

let startPos = this.start, startLoc = this.startLoc
if (this.type === tt.parenL || this.type === tt.name)
if (this.type === tt.parenL || this.type === tt.name) {
this.potentialArrowAt = this.start
this.forbidAsyncOfArrow = !!noIn
}
let left = this.parseMaybeConditional(noIn, refDestructuringErrors)
if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc)
if (this.type.isAssign) {
Expand Down Expand Up @@ -414,7 +416,8 @@ pp.parseExprAtom = function(refDestructuringErrors) {
if (canBeArrow && !this.canInsertSemicolon()) {
if (this.eat(tt.arrow))
return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false)
if (this.options.ecmaVersion >= 8 && id.name === "async" && this.type === tt.name && !containsEsc) {
if (this.options.ecmaVersion >= 8 && id.name === "async" && this.type === tt.name && !containsEsc &&
(!this.forbidAsyncOfArrow || this.value !== "of" || this.containsEsc)) {
id = this.parseIdent(false)
if (this.canInsertSemicolon() || !this.eat(tt.arrow))
this.unexpected()
Expand Down
1 change: 1 addition & 0 deletions acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Parser {

// Used to signify the start of a potential arrow function
this.potentialArrowAt = -1
this.forbidAsyncOfArrow = false

// Positions to delayed-check that yield/await does not exist in default parameters.
this.yieldPos = this.awaitPos = this.awaitIdentPos = 0
Expand Down
2 changes: 2 additions & 0 deletions test/tests-asyncawait.js
Original file line number Diff line number Diff line change
Expand Up @@ -3522,6 +3522,8 @@ test(

test("({ async delete() {} })", {}, {ecmaVersion: 8})

test("for (async of []) {}", {}, {ecmaVersion: 8})

testFail("abc: async function a() {}", "Unexpected token (1:5)", {ecmaVersion: 8})

testFail("(async() => { await 4 ** 2 })()", "Unexpected token (1:22)", {ecmaVersion: 8})
Expand Down

0 comments on commit 7cbe0f9

Please sign in to comment.