Destructuring assignments.
This commit is contained in:
parent
5ab6837208
commit
9b697af803
44
acorn.js
44
acorn.js
@ -1155,7 +1155,8 @@
|
|||||||
// to.
|
// to.
|
||||||
|
|
||||||
function checkLVal(expr) {
|
function checkLVal(expr) {
|
||||||
if (expr.type !== "Identifier" && expr.type !== "MemberExpression")
|
if (expr.type !== "Identifier" && expr.type !== "MemberExpression" &&
|
||||||
|
expr.type !== "ObjectPattern" && expr.type !== "ArrayPattern")
|
||||||
raise(expr.start, "Assigning to rvalue");
|
raise(expr.start, "Assigning to rvalue");
|
||||||
if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
|
if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
|
||||||
raise(expr.start, "Assigning to " + expr.name + " in strict mode");
|
raise(expr.start, "Assigning to " + expr.name + " in strict mode");
|
||||||
@ -1508,8 +1509,8 @@
|
|||||||
node.kind = kind;
|
node.kind = kind;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
var decl = startNode();
|
var decl = startNode();
|
||||||
decl.id = parseIdent();
|
decl.id = options.ecmaVersion >= 6 ? toAssignable(parseExprAtom()) : parseIdent();
|
||||||
if (strict && isStrictBadIdWord(decl.id.name))
|
if (strict && decl.id.type === "Identifier" && isStrictBadIdWord(decl.id.name))
|
||||||
raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
|
raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
|
||||||
decl.init = eat(_eq) ? parseExpression(true, noIn) : (kind === _const.keyword ? unexpected() : null);
|
decl.init = eat(_eq) ? parseExpression(true, noIn) : (kind === _const.keyword ? unexpected() : null);
|
||||||
node.declarations.push(finishNode(decl, "VariableDeclarator"));
|
node.declarations.push(finishNode(decl, "VariableDeclarator"));
|
||||||
@ -1910,22 +1911,31 @@
|
|||||||
// Parse function parameters.
|
// Parse function parameters.
|
||||||
|
|
||||||
function parseFunctionParams(node) {
|
function parseFunctionParams(node) {
|
||||||
|
var defaults = [], hasDefaults = false;
|
||||||
|
|
||||||
expect(_parenL);
|
expect(_parenL);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (eat(_parenR)) {
|
if (eat(_parenR)) {
|
||||||
break;
|
break;
|
||||||
} else if (options.ecmaVersion >= 6 && eat(_ellipsis)) {
|
} else if (options.ecmaVersion >= 6 && eat(_ellipsis)) {
|
||||||
node.rest = parseIdent();
|
node.rest = toAssignable(parseExprAtom());
|
||||||
expect(_parenR);
|
expect(_parenR);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
node.params.push(parseIdent());
|
node.params.push(options.ecmaVersion >= 6 ? toAssignable(parseExprAtom()) : parseIdent());
|
||||||
|
if (options.ecmaVersion >= 6 && tokVal === '=') {
|
||||||
|
next();
|
||||||
|
hasDefaults = true;
|
||||||
|
defaults.push(parseExpression(true));
|
||||||
|
}
|
||||||
if (!eat(_comma)) {
|
if (!eat(_comma)) {
|
||||||
expect(_parenR);
|
expect(_parenR);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasDefaults) node.defaults = defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse function body and check parameters.
|
// Parse function body and check parameters.
|
||||||
@ -2043,4 +2053,28 @@
|
|||||||
return finishNode(node, "Identifier");
|
return finishNode(node, "Identifier");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert existing expression atom to assignable pattern
|
||||||
|
// if possible.
|
||||||
|
|
||||||
|
function toAssignable(node) {
|
||||||
|
if (options.ecmaVersion >= 6) {
|
||||||
|
switch (node.type) {
|
||||||
|
case "Identifier":
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "ObjectExpression":
|
||||||
|
node.type = "ObjectPattern";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "ArrayExpression":
|
||||||
|
node.type = "ArrayPattern";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
unexpected(node.start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user