}
function join_object_assignments(defn, body) {
- if (!(defn instanceof AST_Definitions)) return;
- var def = defn.definitions[defn.definitions.length - 1];
- if (!(def.value instanceof AST_Object)) return;
var exprs;
if (body instanceof AST_Assign) {
exprs = [ body ];
exprs = body.expressions.slice();
}
if (!exprs) return;
+ if (defn instanceof AST_Definitions) {
+ var def = defn.definitions[defn.definitions.length - 1];
+ if (trim_object_assignments(def.name, def.value, exprs)) return exprs;
+ }
+ for (var i = exprs.length - 1; --i >= 0;) {
+ var expr = exprs[i];
+ if (!(expr instanceof AST_Assign)) continue;
+ if (expr.operator != "=") continue;
+ if (!(expr.left instanceof AST_SymbolRef)) continue;
+ var tail = exprs.slice(i + 1);
+ if (!trim_object_assignments(expr.left, expr.right, tail)) continue;
+ return exprs.slice(0, i + 1).concat(tail);
+ }
+ }
+
+ function trim_object_assignments(name, value, exprs) {
+ if (!(value instanceof AST_Object)) return;
var trimmed = false;
do {
var node = exprs[0];
if (!(node.left instanceof AST_PropAccess)) break;
var sym = node.left.expression;
if (!(sym instanceof AST_SymbolRef)) break;
- if (def.name.name != sym.name) break;
+ if (name.name != sym.name) break;
if (!node.right.is_constant_expression(scope)) break;
var prop = node.left.property;
if (prop instanceof AST_Node) {
} : function(node) {
return node.key.name != prop;
};
- if (!all(def.value.properties, diff)) break;
- def.value.properties.push(make_node(AST_ObjectKeyVal, node, {
+ if (!all(value.properties, diff)) break;
+ value.properties.push(make_node(AST_ObjectKeyVal, node, {
key: prop,
value: node.right
}));
exprs.shift();
trimmed = true;
} while (exprs.length);
- return trimmed && exprs;
+ return trimmed;
}
function join_consecutive_vars(statements) {