Skip to content

Commit

Permalink
fix deep cloning of labels (#1565)
Browse files Browse the repository at this point in the history
`AST_Label.references` get `.initialize()` to `[]` every time after `.clone()`

So walk down the tree to pick up the cloned `AST_LoopControl` pieces and put it back together.
  • Loading branch information
alexlamsl authored Mar 7, 2017
1 parent 8153b7b commit 8a8a94a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos
}, null);

var AST_Node = DEFNODE("Node", "start end", {
clone: function(deep) {
_clone: function(deep) {
if (deep) {
var self = this.clone();
return self.transform(new TreeTransformer(function(node) {
Expand All @@ -102,6 +102,9 @@ var AST_Node = DEFNODE("Node", "start end", {
}
return new this.CTOR(this);
},
clone: function(deep) {
return this._clone(deep);
},
$documentation: "Base class of all AST nodes",
$propdoc: {
start: "[AST_Token] The first token of this node",
Expand Down Expand Up @@ -207,6 +210,20 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
this.label._walk(visitor);
this.body._walk(visitor);
});
},
clone: function(deep) {
var node = this._clone(deep);
if (deep) {
var refs = node.label.references;
var label = this.label;
node.walk(new TreeWalker(function(node) {
if (node instanceof AST_LoopControl
&& node.label && node.label.thedef === label) {
refs.push(node);
}
}));
}
return node;
}
}, AST_StatementWithBody);

Expand Down
29 changes: 29 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,3 +1093,32 @@ func_modified: {
}
}
}

defun_label: {
options = {
passes: 2,
reduce_vars: true,
unused: true,
}
input: {
!function() {
function f(a) {
L: {
if (a) break L;
return 1;
}
}
console.log(f(2));
}();
}
expect: {
!function() {
console.log(function(a) {
L: {
if (a) break L;
return 1;
}
}(2));
}();
}
}

0 comments on commit 8a8a94a

Please sign in to comment.