Skip to content

Commit

Permalink
Do not reset shorthandAssign when parsing maybeAssign
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored and marijnh committed Jan 20, 2020
1 parent 15a87cd commit 1a3a564
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 9 additions & 8 deletions acorn/src/expression.js
Expand Up @@ -113,12 +113,11 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
else this.exprAllowed = false
}

let ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldShorthandAssign = -1
let ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1
if (refDestructuringErrors) {
oldParenAssign = refDestructuringErrors.parenthesizedAssign
oldTrailingComma = refDestructuringErrors.trailingComma
oldShorthandAssign = refDestructuringErrors.shorthandAssign
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.shorthandAssign = -1
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1
} else {
refDestructuringErrors = new DestructuringErrors
ownDestructuringErrors = true
Expand All @@ -133,8 +132,11 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
let node = this.startNodeAt(startPos, startLoc)
node.operator = this.value
node.left = this.type === tt.eq ? this.toAssignable(left, false, refDestructuringErrors) : left
if (!ownDestructuringErrors) DestructuringErrors.call(refDestructuringErrors)
refDestructuringErrors.shorthandAssign = -1 // reset because shorthand default was used correctly
if (!ownDestructuringErrors) {
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.doubleProto = -1
}
if (refDestructuringErrors.shorthandAssign >= node.left.start)
refDestructuringErrors.shorthandAssign = -1 // reset because shorthand default was used correctly
this.checkLVal(left)
this.next()
node.right = this.parseMaybeAssign(noIn)
Expand All @@ -144,7 +146,6 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
}
if (oldParenAssign > -1) refDestructuringErrors.parenthesizedAssign = oldParenAssign
if (oldTrailingComma > -1) refDestructuringErrors.trailingComma = oldTrailingComma
if (oldShorthandAssign > -1) refDestructuringErrors.shorthandAssign = oldShorthandAssign
return left
}

Expand Down Expand Up @@ -249,8 +250,8 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
pp.parseExprSubscripts = function(refDestructuringErrors) {
let startPos = this.start, startLoc = this.startLoc
let expr = this.parseExprAtom(refDestructuringErrors)
let skipArrowSubscripts = expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")"
if (this.checkExpressionErrors(refDestructuringErrors) || skipArrowSubscripts) return expr
if (expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")")
return expr
let result = this.parseSubscripts(expr, startPos, startLoc)
if (refDestructuringErrors && result.type === "MemberExpression") {
if (refDestructuringErrors.parenthesizedAssign >= result.start) refDestructuringErrors.parenthesizedAssign = -1
Expand Down
2 changes: 2 additions & 0 deletions test/tests-harmony.js
Expand Up @@ -16513,4 +16513,6 @@ test("function *f2() { () => yield / 1 }", {}, {ecmaVersion: 6})

test("({ a = 42, b: c.d } = e)", {}, {ecmaVersion: 6})

testFail("({ a = 42, b: c = d })", "Shorthand property assignments are valid only in destructuring patterns (1:5)", {ecmaVersion: 6})

test("({ __proto__: x, __proto__: y, __proto__: z }) => {}", {}, {ecmaVersion: 6})

0 comments on commit 1a3a564

Please sign in to comment.