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 1 commit
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
21 changes: 21 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,25 @@ 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)) {
// To disallow parenthesized identifier.
if (this.type === tt.parenL && refDestructuringErrors) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆 just to clarify the check above

To disallow parenthesized identifier via this.toAssignable()

Is that the default param value guard? Or something else (does it have a test).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the guard for the parenthesized identifier in RestElement. E.g. ({...(obj)} = foo). Yes, there are tests.

if (refDestructuringErrors.parenthesizedAssign < 0) {
refDestructuringErrors.parenthesizedAssign = this.start
}
if (refDestructuringErrors.parenthesizedBind < 0) {
refDestructuringErrors.parenthesizedBind = this.start
}
}
// Parse argument.
prop.argument = isPattern ? this.parseIdent(false) : this.parseMaybeAssign(false, refDestructuringErrors)
// To disallow trailing comma.
if (this.type === tt.comma && refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
refDestructuringErrors.trailingComma = this.start
}
// Finish
return this.finishNode(prop, isPattern ? "RestElement" : "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
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