Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make await x ** y fail to parse #1029

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pp.buildBinary = function(startPos, startLoc, left, right, op, logical) {

// Parse unary operators, both prefix and postfix.

pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec) {
let startPos = this.start, startLoc = this.startLoc, expr
if (this.isContextual("await") && (this.inAsync || (!this.inFunction && this.options.allowAwaitOutsideFunction))) {
expr = this.parseAwait()
Expand All @@ -231,7 +231,7 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
node.operator = this.value
node.prefix = true
this.next()
node.argument = this.parseMaybeUnary(null, true)
node.argument = this.parseMaybeUnary(null, true, update)
this.checkExpressionErrors(refDestructuringErrors, true)
if (update) this.checkLValSimple(node.argument)
else if (this.strict && node.operator === "delete" &&
Expand All @@ -255,10 +255,14 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
}
}

if (!sawUnary && this.eat(tt.starstar))
return this.buildBinary(startPos, startLoc, expr, this.parseMaybeUnary(null, false), "**", false)
else
if (!incDec && this.eat(tt.starstar)) {
if (sawUnary)
this.unexpected(this.lastTokStart)
else
return this.buildBinary(startPos, startLoc, expr, this.parseMaybeUnary(null, false), "**", false)
} else {
return expr
}
}

function isPrivateFieldAccess(node) {
Expand Down
12 changes: 12 additions & 0 deletions test/tests-asyncawait.js
Original file line number Diff line number Diff line change
Expand Up @@ -3526,4 +3526,16 @@ testFail("abc: async function a() {}", "Unexpected token (1:5)", {ecmaVersion: 8

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

test("(async() => { await (4 ** 2) })()", {}, {ecmaVersion: 8})

testFail("async() => (await 1 ** 3)", "Unexpected token (1:20)", {ecmaVersion: 8})

test("async() => (await (1 ** 3))", {}, {ecmaVersion: 8})

testFail("async() => await 5 ** 6", "Unexpected token (1:19)", {ecmaVersion: 8})

test("async() => await (5 ** 6)", {}, {ecmaVersion: 8})

testFail("async() => await (5) ** 6", "Unexpected token (1:21)", {ecmaVersion: 8})

testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: false})
2 changes: 2 additions & 0 deletions test/tests-es7.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ testFail("~3 ** 2;", "Unexpected token (1:3)", { ecmaVersion: 7 });
testFail("!1 ** 2;", "Unexpected token (1:3)", { ecmaVersion: 7 });
testFail("-2** 2;", "Unexpected token (1:2)", { ecmaVersion: 7 });
testFail("+2** 2;", "Unexpected token (1:2)", { ecmaVersion: 7 });
testFail("-(i--) ** 2", "Unexpected token (1:7)", {ecmaVersion: 7});
testFail("+(i--) ** 2", "Unexpected token (1:7)", {ecmaVersion: 7});

// make sure base operand check doesn't affect other operators
test("-a * 5", {
Expand Down