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

Add rest/spread properties #658

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 0 additions & 2 deletions bin/run_test262.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const unsupportedFeatures = [
"class-fields",
"class-fields-private",
"class-fields-public",
"object-rest",
"object-spread",
"optional-catch-binding",
"regexp-lookbehind",
"regexp-named-groups",
Expand Down
28 changes: 28 additions & 0 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const pp = Parser.prototype
// strict mode, init properties are also not allowed to be repeated.

pp.checkPropClash = function(prop, propHash, refDestructuringErrors) {
if (this.options.ecmaVersion >= 9 && prop.type === "SpreadElement")
return
if (this.options.ecmaVersion >= 6 && (prop.computed || prop.method || prop.shorthand))
return
let {key} = prop, name
Expand Down Expand Up @@ -565,6 +567,32 @@ pp.parseObj = function(isPattern, refDestructuringErrors) {

pp.parseProperty = function(isPattern, refDestructuringErrors) {
let prop = this.startNode(), isGenerator, isAsync, startPos, startLoc
if (this.options.ecmaVersion >= 9 && this.eat(tt.ellipsis)) {
if (isPattern) {
prop.argument = this.parseIdent(false)
if (this.type === tt.comma) {
this.raise(this.start, "Comma is not permitted after the rest element")
}
return this.finishNode(prop, "RestElement")
}
// To disallow parenthesized identifier via `this.toAssignable()`.
if (this.type === tt.parenL && refDestructuringErrors) {
if (refDestructuringErrors.parenthesizedAssign < 0) {
refDestructuringErrors.parenthesizedAssign = this.start
}
if (refDestructuringErrors.parenthesizedBind < 0) {
refDestructuringErrors.parenthesizedBind = this.start
}
}
// Parse argument.
prop.argument = this.parseMaybeAssign(false, refDestructuringErrors)
// To disallow trailing comma via `this.toAssignable()`.
if (this.type === tt.comma && refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
refDestructuringErrors.trailingComma = this.start
}
// Finish
return this.finishNode(prop, "SpreadElement")
}
if (this.options.ecmaVersion >= 6) {
prop.method = false
prop.shorthand = false
Expand Down
13 changes: 10 additions & 3 deletions src/loose/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ lp.parseObj = function() {
if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
while (!this.closes(tt.braceR, indent, line)) {
let prop = this.startNode(), isGenerator, isAsync, start
if (this.options.ecmaVersion >= 9 && this.eat(tt.ellipsis)) {
prop.argument = this.parseMaybeAssign()
node.properties.push(this.finishNode(prop, "SpreadElement"))
this.eat(tt.comma)
continue
}
if (this.options.ecmaVersion >= 6) {
start = this.storeCurrentPos()
prop.method = false
Expand Down Expand Up @@ -476,12 +482,13 @@ lp.toAssignable = function(node, binding) {
return this.dummyIdent()
} else if (node.type == "ObjectExpression") {
node.type = "ObjectPattern"
let props = node.properties
for (let prop of props)
this.toAssignable(prop.value, binding)
for (let prop of node.properties)
this.toAssignable(prop, binding)
} else if (node.type == "ArrayExpression") {
node.type = "ArrayPattern"
this.toAssignableList(node.elements, binding)
} else if (node.type == "Property") {
this.toAssignable(node.value, binding)
} else if (node.type == "SpreadElement") {
node.type = "RestElement"
this.toAssignable(node.argument, binding)
Expand Down
14 changes: 13 additions & 1 deletion src/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ pp.toAssignable = function(node, isBinding, refDestructuringErrors) {
case "ObjectExpression":
node.type = "ObjectPattern"
if (refDestructuringErrors) this.checkPatternErrors(refDestructuringErrors, true)
for (let prop of node.properties)
for (let prop of node.properties) {
this.toAssignable(prop, isBinding)
// Early error:
// AssignmentRestProperty[Yield, Await] :
// `...` DestructuringAssignmentTarget[Yield, Await]
//
// It is a Syntax Error if |DestructuringAssignmentTarget| is an |ArrayLiteral| or an |ObjectLiteral|.
if (
prop.type === "RestElement" &&
(prop.argument.type === "ArrayPattern" || prop.argument.type === "ObjectPattern")
) {
this.raise(prop.argument.start, "Unexpected token")
}
}
break

case "Property":
Expand Down
6 changes: 5 additions & 1 deletion src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,17 @@ pp.checkPatternExport = function(exports, pat) {
this.checkExport(exports, pat.name, pat.start)
else if (type == "ObjectPattern")
for (let prop of pat.properties)
this.checkPatternExport(exports, prop.value)
this.checkPatternExport(exports, prop)
else if (type == "ArrayPattern")
for (let elt of pat.elements) {
if (elt) this.checkPatternExport(exports, elt)
}
else if (type == "Property")
this.checkPatternExport(exports, pat.value)
else if (type == "AssignmentPattern")
this.checkPatternExport(exports, pat.left)
else if (type == "RestElement")
this.checkPatternExport(exports, pat.argument)
else if (type == "ParenthesizedExpression")
this.checkPatternExport(exports, pat.expression)
}
Expand Down
1 change: 1 addition & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require("./tests-trailing-commas-in-func.js");
require("./tests-template-literal-revision.js");
require("./tests-directive.js");
require("./tests-rest-spread-properties.js");
acorn = require("../dist/acorn")
require("../dist/acorn_loose")
} else {
Expand Down
Loading