@@ -1828,6 +1828,29 @@ Compressor.prototype.compress = function(node) {
1828
1828
return wrap ? make_sequence(orig, [ make_node(AST_Number, orig, { value: 0 }), val ]) : val;
1829
1829
}
1830
1830
1831
+ function merge_expression(base, target, scope) {
1832
+ var fixed_by_id = new Dictionary();
1833
+ base.walk(new TreeWalker(function(node) {
1834
+ if (!(node instanceof AST_SymbolRef)) return;
1835
+ var def = node.definition();
1836
+ if (scope && def.scope.resolve() !== scope) return;
1837
+ var fixed = node.fixed;
1838
+ if (!fixed || !fixed_by_id.has(def.id)) {
1839
+ fixed_by_id.set(def.id, fixed);
1840
+ } else if (fixed_by_id.get(def.id) !== fixed) {
1841
+ fixed_by_id.set(def.id, false);
1842
+ }
1843
+ }));
1844
+ if (fixed_by_id.size() > 0) target.walk(new TreeWalker(function(node) {
1845
+ if (!(node instanceof AST_SymbolRef)) return;
1846
+ var def = node.definition();
1847
+ var fixed = node.fixed;
1848
+ if (!fixed || !fixed_by_id.has(def.id)) return;
1849
+ if (fixed_by_id.get(def.id) !== fixed) node.fixed = false;
1850
+ }));
1851
+ return target;
1852
+ }
1853
+
1831
1854
function merge_sequence(array, node) {
1832
1855
if (node instanceof AST_Sequence) {
1833
1856
[].push.apply(array, node.expressions);
@@ -3813,25 +3836,7 @@ Compressor.prototype.compress = function(node) {
3813
3836
case 2:
3814
3837
value = value.tail_node();
3815
3838
}
3816
- var fixed_by_id = new Dictionary();
3817
- value.walk(new TreeWalker(function(node) {
3818
- if (!(node instanceof AST_SymbolRef)) return;
3819
- var def = node.definition();
3820
- if (def.scope.resolve() !== scope) return;
3821
- var fixed = node.fixed;
3822
- if (!fixed || !fixed_by_id.has(def.id)) {
3823
- fixed_by_id.set(def.id, fixed);
3824
- } else if (fixed_by_id.get(def.id) !== fixed) {
3825
- fixed_by_id.set(def.id, false);
3826
- }
3827
- }));
3828
- if (fixed_by_id.size() > 0) jump.value.walk(new TreeWalker(function(node) {
3829
- if (!(node instanceof AST_SymbolRef)) return;
3830
- var def = node.definition();
3831
- var fixed = node.fixed;
3832
- if (!fixed || !fixed_by_id.has(def.id)) return;
3833
- if (fixed_by_id.get(def.id) !== fixed) node.fixed = false;
3834
- }));
3839
+ merge_expression(value, jump.value, scope);
3835
3840
}
3836
3841
3837
3842
function next_index(i) {
@@ -12933,7 +12938,7 @@ Compressor.prototype.compress = function(node) {
12933
12938
right: fuse(consequent, seq_tail, "condition"),
12934
12939
}),
12935
12940
consequent: seq_tail.consequent,
12936
- alternative: alternative,
12941
+ alternative: merge_expression(seq_tail. alternative, alternative) ,
12937
12942
});
12938
12943
}
12939
12944
// x ? (y ? a : b) : a ---> !x || y ? a : b
@@ -12945,7 +12950,7 @@ Compressor.prototype.compress = function(node) {
12945
12950
operator: "||",
12946
12951
right: fuse(consequent, seq_tail, "condition"),
12947
12952
}),
12948
- consequent: alternative,
12953
+ consequent: merge_expression(seq_tail.consequent, alternative) ,
12949
12954
alternative: seq_tail.alternative,
12950
12955
});
12951
12956
}
@@ -12958,7 +12963,7 @@ Compressor.prototype.compress = function(node) {
12958
12963
operator: "||",
12959
12964
right: fuse(alternative, alt_tail, "condition"),
12960
12965
}),
12961
- consequent: consequent,
12966
+ consequent: merge_expression( consequent, alt_tail.consequent) ,
12962
12967
alternative: alt_tail.alternative,
12963
12968
});
12964
12969
}
@@ -12972,7 +12977,7 @@ Compressor.prototype.compress = function(node) {
12972
12977
right: fuse(alternative, alt_tail, "condition"),
12973
12978
}),
12974
12979
consequent: alt_tail.consequent,
12975
- alternative: consequent,
12980
+ alternative: merge_expression( consequent, alt_tail.alternative) ,
12976
12981
});
12977
12982
}
12978
12983
// x ? y && a : a ---> (!x || y) && a
@@ -12986,7 +12991,7 @@ Compressor.prototype.compress = function(node) {
12986
12991
left: negated,
12987
12992
right: fuse(consequent, seq_tail, "left"),
12988
12993
}),
12989
- right: alternative,
12994
+ right: merge_expression(seq_tail.right, alternative) ,
12990
12995
}).optimize(compressor);
12991
12996
}
12992
12997
// x ? y || a : a ---> x && y || a
@@ -13000,7 +13005,7 @@ Compressor.prototype.compress = function(node) {
13000
13005
left: condition,
13001
13006
right: fuse(consequent, seq_tail, "left"),
13002
13007
}),
13003
- right: alternative,
13008
+ right: merge_expression(seq_tail.right, alternative) ,
13004
13009
}).optimize(compressor);
13005
13010
}
13006
13011
// x ? a : y && a ---> (x || y) && a
@@ -13014,7 +13019,7 @@ Compressor.prototype.compress = function(node) {
13014
13019
left: condition,
13015
13020
right: fuse(alternative, alt_tail, "left"),
13016
13021
}),
13017
- right: consequent,
13022
+ right: merge_expression( consequent, alt_tail.right) ,
13018
13023
}).optimize(compressor);
13019
13024
}
13020
13025
// x ? a : y || a ---> !x && y || a
@@ -13028,7 +13033,7 @@ Compressor.prototype.compress = function(node) {
13028
13033
left: negated,
13029
13034
right: fuse(alternative, alt_tail, "left"),
13030
13035
}),
13031
- right: consequent,
13036
+ right: merge_expression( consequent, alt_tail.right) ,
13032
13037
}).optimize(compressor);
13033
13038
}
13034
13039
var in_bool = compressor.option("booleans") && compressor.in_boolean_context();
0 commit comments