Skip to content

Commit

Permalink
Add raiseRecoverable hook
Browse files Browse the repository at this point in the history
This allows consumers to override raiseRecoverable() errors if they want.
To start, I only added this in places where value validation failed.

Fixes acornjs#370
  • Loading branch information
nzakas committed Jan 19, 2016
1 parent 0aab0be commit cf61e5f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pp.checkPropClash = function(prop, propHash) {
let {kind} = prop
if (this.options.ecmaVersion >= 6) {
if (name === "__proto__" && kind === "init") {
if (propHash.proto) this.raise(key.start, "Redefinition of __proto__ property")
if (propHash.proto) this.raiseRecoverable(key.start, "Redefinition of __proto__ property")
propHash.proto = true
}
return
Expand All @@ -48,7 +48,7 @@ pp.checkPropClash = function(prop, propHash) {
if (other) {
let isGetSet = kind !== "init"
if ((this.strict || isGetSet) && other[kind] || !(isGetSet ^ other.init))
this.raise(key.start, "Redefinition of property")
this.raiseRecoverable(key.start, "Redefinition of property")
} else {
other = propHash[name] = {
init: false,
Expand Down Expand Up @@ -181,7 +181,7 @@ pp.parseMaybeUnary = function(refDestructuringErrors) {
if (update) this.checkLVal(node.argument)
else if (this.strict && node.operator === "delete" &&
node.argument.type === "Identifier")
this.raise(node.start, "Deleting local variable in strict mode")
this.raiseRecoverable(node.start, "Deleting local variable in strict mode")
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
}
let startPos = this.start, startLoc = this.startLoc
Expand Down Expand Up @@ -404,9 +404,9 @@ pp.parseNew = function() {
node.meta = meta
node.property = this.parseIdent(true)
if (node.property.name !== "target")
this.raise(node.property.start, "The only valid meta property for new is new.target")
this.raiseRecoverable(node.property.start, "The only valid meta property for new is new.target")
if (!this.inFunction)
this.raise(node.start, "new.target can only be used in functions")
this.raiseRecoverable(node.start, "new.target can only be used in functions")
return this.finishNode(node, "MetaProperty")
}
let startPos = this.start, startLoc = this.startLoc
Expand Down Expand Up @@ -496,19 +496,19 @@ pp.parsePropertyValue = function(prop, isPattern, isGenerator, startPos, startLo
if (prop.value.params.length !== paramCount) {
let start = prop.value.start
if (prop.kind === "get")
this.raise(start, "getter should have no params")
this.raiseRecoverable(start, "getter should have no params")
else
this.raise(start, "setter should have exactly one param")
this.raiseRecoverable(start, "setter should have exactly one param")
}
if (prop.kind === "set" && prop.value.params[0].type === "RestElement")
this.raise(prop.value.params[0].start, "Setter cannot use rest params")
this.raiseRecoverable(prop.value.params[0].start, "Setter cannot use rest params")
} else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
prop.kind = "init"
if (isPattern) {
if (this.keywords.test(prop.key.name) ||
(this.strict ? this.reservedWordsStrictBind : this.reservedWords).test(prop.key.name) ||
(this.inGenerator && prop.key.name == "yield"))
this.raise(prop.key.start, "Binding " + prop.key.name)
this.raiseRecoverable(prop.key.start, "Binding " + prop.key.name)
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key)
} else if (this.type === tt.eq && refDestructuringErrors) {
if (!refDestructuringErrors.shorthandAssign)
Expand Down Expand Up @@ -654,9 +654,9 @@ pp.parseIdent = function(liberal) {
if (!liberal && (this.strict ? this.reservedWordsStrict : this.reservedWords).test(this.value) &&
(this.options.ecmaVersion >= 6 ||
this.input.slice(this.start, this.end).indexOf("\\") == -1))
this.raise(this.start, "The keyword '" + this.value + "' is reserved")
this.raiseRecoverable(this.start, "The keyword '" + this.value + "' is reserved")
if (!liberal && this.inGenerator && this.value === "yield")
this.raise(this.start, "Can not use 'yield' as identifier inside a generator")
this.raiseRecoverable(this.start, "Can not use 'yield' as identifier inside a generator")
node.name = this.value
} else if (liberal && this.type.keyword) {
node.name = this.type.keyword
Expand Down
2 changes: 2 additions & 0 deletions src/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pp.raise = function(pos, message) {
throw err
}

pp.raiseRecoverable = pp.raise;

pp.curPosition = function() {
if (this.options.locations) {
return new Position(this.curLine, this.pos - this.lineStart)
Expand Down
6 changes: 3 additions & 3 deletions src/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ pp.checkLVal = function(expr, isBinding, checkClashes) {
switch (expr.type) {
case "Identifier":
if (this.strict && this.reservedWordsStrictBind.test(expr.name))
this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode")
this.raiseRecoverable(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode")
if (checkClashes) {
if (has(checkClashes, expr.name))
this.raise(expr.start, "Argument name clash")
this.raiseRecoverable(expr.start, "Argument name clash")
checkClashes[expr.name] = true
}
break

case "MemberExpression":
if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression")
if (isBinding) this.raiseRecoverable(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression")
break

case "ObjectPattern":
Expand Down
6 changes: 3 additions & 3 deletions src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pp.parseSwitchStatement = function(node) {
if (isCase) {
cur.test = this.parseExpression()
} else {
if (sawDefault) this.raise(this.lastTokStart, "Multiple default clauses")
if (sawDefault) this.raiseRecoverable(this.lastTokStart, "Multiple default clauses")
sawDefault = true
cur.test = null
}
Expand Down Expand Up @@ -490,9 +490,9 @@ pp.parseClass = function(node, isStatement) {
if (method.value.params.length !== paramCount) {
let start = method.value.start
if (method.kind === "get")
this.raise(start, "getter should have no params")
this.raiseRecoverable(start, "getter should have no params")
else
this.raise(start, "setter should have exactly one param")
this.raiseRecoverable(start, "setter should have exactly one param")
}
if (method.kind === "set" && method.value.params[0].type === "RestElement")
this.raise(method.value.params[0].start, "Setter cannot use rest params")
Expand Down

0 comments on commit cf61e5f

Please sign in to comment.