Remove unused state variable in_parameters, and also remove unreachable code (try_an_object always returned an object!)

This commit is contained in:
Fábio Santos
2015-09-05 23:01:25 +01:00
parent af22b9c657
commit 2fac2bbfe4
2 changed files with 58 additions and 87 deletions

View File

@@ -672,7 +672,6 @@ function parse($TEXT, options) {
prev : null, prev : null,
peeked : null, peeked : null,
in_function : 0, in_function : 0,
in_parameters : false,
in_directives : true, in_directives : true,
in_loop : 0, in_loop : 0,
labels : [] labels : []
@@ -1044,7 +1043,6 @@ function parse($TEXT, options) {
expect("("); expect("(");
var first = true; var first = true;
var a = []; var a = [];
S.in_parameters = true;
while (!is("punc", ")")) { while (!is("punc", ")")) {
if (first) first = false; else expect(","); if (first) first = false; else expect(",");
if (is("expand", "...")) { if (is("expand", "...")) {
@@ -1058,7 +1056,6 @@ function parse($TEXT, options) {
a.push(expression(false)); a.push(expression(false));
} }
} }
S.in_parameters = false;
var end = S.token var end = S.token
next(); next();
return new AST_ArrowParametersOrSeq({ return new AST_ArrowParametersOrSeq({
@@ -1375,97 +1372,60 @@ function parse($TEXT, options) {
}); });
var object_or_object_destructuring_ = embed_tokens(function() { var object_or_object_destructuring_ = embed_tokens(function() {
var start = S.token; var start = S.token, first = true, a = [];
expect("{"); expect("{");
function try_an_object() { while (!is("punc", "}")) {
var first = true, a = []; if (first) first = false; else expect(",");
while (!is("punc", "}")) { if (!options.strict && is("punc", "}"))
if (first) first = false; else expect(","); // allow trailing comma
if (!options.strict && is("punc", "}")) break;
// allow trailing comma var start = S.token;
break; var type = start.type;
var start = S.token; var name = as_property_name();
var type = start.type; if (type == "name" && !is("punc", ":")) {
var name = as_property_name(); if (name == "get") {
if (type == "name" && !is("punc", ":")) { a.push(new AST_ObjectGetter({
if (name == "get") {
a.push(new AST_ObjectGetter({
start : start,
key : as_atom_node(),
value : function_(AST_Accessor),
end : prev()
}));
continue;
}
if (name == "set") {
a.push(new AST_ObjectSetter({
start : start,
key : as_atom_node(),
value : function_(AST_Accessor),
end : prev()
}));
continue;
}
}
if (!is("punc", ":")) {
// It's one of those object destructurings, the value is its own name
a.push(new AST_ObjectSymbol({
start: start,
end: start,
symbol: new AST_SymbolRef({
start: start,
end: start,
name: name
})
}));
} else {
if (S.in_parameters) {
croak("Cannot destructure", S.token.line, S.token.col);
}
expect(":");
a.push(new AST_ObjectKeyVal({
start : start, start : start,
key : name, key : as_atom_node(),
value : expression(false), value : function_(AST_Accessor),
end : prev() end : prev()
})); }));
continue;
}
if (name == "set") {
a.push(new AST_ObjectSetter({
start : start,
key : as_atom_node(),
value : function_(AST_Accessor),
end : prev()
}));
continue;
} }
} }
next();
return new AST_Object({ properties: a }) if (!is("punc", ":")) {
// It's one of those object destructurings, the value is its own name
a.push(new AST_ObjectSymbol({
start: start,
end: start,
symbol: new AST_SymbolRef({
start: start,
end: start,
name: name
})
}));
} else {
expect(":");
a.push(new AST_ObjectKeyVal({
start : start,
key : name,
value : expression(false),
end : prev()
}));
}
} }
next();
var obj = try_an_object(); return new AST_Object({ properties: a })
if (obj instanceof AST_Object) { return obj; }
if (!S.in_parameters) {
croak("Cannot destructure", S.token.line, S.token.col);
}
var firstName = obj;
var namesInDestructuring = [];
namesInDestructuring.push( new AST_SymbolRef({
start : prev(),
end : prev(),
name : firstName
}));
while (!is("punc", "}")) {
expect(",");
namesInDestructuring.push(as_symbol(AST_SymbolRef))
}
expect('}');
return new AST_Destructuring({
start : start,
end : S.token,
names : namesInDestructuring,
is_array : false
})
}); });
function as_property_name() { function as_property_name() {

View File

@@ -122,3 +122,14 @@ number_literals: {
9; 9;
} }
} }
// Fabio: My patches accidentally caused a crash whenever
// there's an extraneous set of parens around an object.
regression_cannot_destructure: {
input: {
var x = ({ x : 3 });
x(({ x: 3 }));
}
expect_exact: "var x={x:3};x({x:3});";
}