make illegal LHS pattern error messages more user friendly
This commit is contained in:
@@ -103,8 +103,16 @@ pp.parseMaybeAssign = function(noIn, refShorthandDefaultPos, afterLeftParse) {
|
||||
node.left = this.type === tt.eq ? this.toAssignable(left) : left
|
||||
refShorthandDefaultPos.start = 0 // reset because shorthand default was used correctly
|
||||
this.checkLVal(left)
|
||||
if (left.parenthesizedExpression && (left.type === "ObjectPattern" || left.type === "ArrayPattern")) {
|
||||
this.raise(left.start, "You're trying to assign to a parenthesized expression, instead of `({ foo }) = {}` use `({ foo } = {})`");
|
||||
if (left.parenthesizedExpression) {
|
||||
let errorMsg
|
||||
if (left.type === "ObjectPattern") {
|
||||
errorMsg = "`({a}) = 0` use `({a} = 0)`"
|
||||
} else if (left.type === "ArrayPattern") {
|
||||
errorMsg = "`([a]) = 0` use `([a] = 0)`"
|
||||
}
|
||||
if (errorMsg) {
|
||||
this.raise(left.start, `You're trying to assign to a parenthesized expression, eg. instead of ${errorMsg}`)
|
||||
}
|
||||
}
|
||||
this.next()
|
||||
node.right = this.parseMaybeAssign(noIn)
|
||||
|
||||
@@ -2,11 +2,11 @@ var test = require("./driver.js").test;
|
||||
var testFail = require("./driver.js").testFail;
|
||||
var testAssert = require("./driver.js").testAssert;
|
||||
|
||||
testFail("({a}) = 2;", "You're trying to assign to a parenthesized expression, instead of `({ foo }) = {}` use `({ foo } = {})` (1:1)", {
|
||||
testFail("({a}) = 2;", "You're trying to assign to a parenthesized expression, eg. instead of `({a}) = 0` use `({a} = {})` (1:1)", {
|
||||
ecmaVersion: 6
|
||||
});
|
||||
|
||||
testFail("([a]) = 2;", "You're trying to assign to a parenthesized expression, instead of `({ foo }) = {}` use `({ foo } = {})` (1:1)", {
|
||||
testFail("([a]) = 2;", "You're trying to assign to a parenthesized expression, eg. instead of `([a]) = 0` use `([a] = {})` (1:1)", {
|
||||
ecmaVersion: 6
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user