Skip to content

Commit

Permalink
Merge pull request #1921 from alexlamsl/v2.8.24
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 12, 2017
2 parents 13e5e33 + 9a98513 commit c736834
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -443,6 +443,11 @@ to set `true`; it's effectively a shortcut for `foo=true`).
- `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from
being compressed into `1/0`, which may cause performance issues on Chrome.

- `side_effects` -- default `false`. Pass `true` to potentially drop functions
marked as "pure". (A function is marked as "pure" via the comment annotation
`/* @__PURE__ */` or `/* #__PURE__ */`)


### The `unsafe` option

It enables some transformations that *might* break code logic in certain
Expand Down
28 changes: 22 additions & 6 deletions lib/compress.js
Expand Up @@ -326,10 +326,14 @@ merge(Compressor.prototype, {
// So existing transformation rules can work on them.
node.argnames.forEach(function(arg, i) {
var d = arg.definition();
d.fixed = function() {
return iife.args[i] || make_node(AST_Undefined, iife);
};
mark(d, true);
if (!node.uses_arguments && d.fixed === undefined) {
d.fixed = function() {
return iife.args[i] || make_node(AST_Undefined, iife);
};
mark(d, true);
} else {
d.fixed = false;
}
});
}
if (node instanceof AST_If || node instanceof AST_DWLoop) {
Expand Down Expand Up @@ -414,7 +418,9 @@ merge(Compressor.prototype, {

function reset_def(def) {
def.escaped = false;
if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
if (def.scope.uses_eval) {
def.fixed = false;
} else if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
def.fixed = undefined;
} else {
def.fixed = false;
Expand All @@ -440,6 +446,14 @@ merge(Compressor.prototype, {
return fixed();
});

function is_reference_const(ref) {
if (!(ref instanceof AST_SymbolRef)) return false;
var orig = ref.definition().orig;
for (var i = orig.length; --i >= 0;) {
if (orig[i] instanceof AST_SymbolConst) return true;
}
}

function find_variable(compressor, name) {
var scope, i = 0;
while (scope = compressor.parent(i++)) {
Expand Down Expand Up @@ -1909,6 +1923,7 @@ merge(Compressor.prototype, {
&& node instanceof AST_Assign
&& node.operator == "="
&& node.left instanceof AST_SymbolRef
&& !is_reference_const(node.left)
&& scope === self) {
node.right.walk(tw);
return true;
Expand Down Expand Up @@ -3066,7 +3081,8 @@ merge(Compressor.prototype, {
}
if (left
&& !(left instanceof AST_SymbolRef
&& left.definition().orig[0] instanceof AST_SymbolLambda)) {
&& (left.definition().orig[0] instanceof AST_SymbolLambda
|| is_reference_const(left)))) {
var parent, field;
var cdr = self.cdr;
while (true) {
Expand Down
46 changes: 46 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -1592,3 +1592,49 @@ var_side_effects_3: {
}
expect_stdout: true
}

reassign_const_1: {
options = {
collapse_vars: true,
}
input: {
function f() {
const a = 1;
a = 2;
return a;
}
console.log(f());
}
expect: {
function f() {
const a = 1;
a = 2;
return a;
}
console.log(f());
}
expect_stdout: true
}

reassign_const_2: {
options = {
collapse_vars: true,
}
input: {
function f() {
const a = 1;
++a;
return a;
}
console.log(f());
}
expect: {
function f() {
const a = 1;
++a;
return a;
}
console.log(f());
}
expect_stdout: true
}
25 changes: 25 additions & 0 deletions test/compress/drop-unused.js
Expand Up @@ -1064,3 +1064,28 @@ issue_1830_2: {
}
expect_stdout: "1"
}

reassign_const: {
options = {
cascade: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
function f() {
const a = 1;
a = 2;
return a;
}
console.log(f());
}
expect: {
function f() {
const a = 1;
return a = 2, a;
}
console.log(f());
}
expect_stdout: true
}
57 changes: 52 additions & 5 deletions test/compress/reduce_vars.js
Expand Up @@ -41,22 +41,22 @@ reduce_vars: {
var A = 1;
(function() {
console.log(-3);
console.log(-4);
console.log(A - 5);
})();
(function f1() {
var a = 2;
console.log(-3);
console.log(a - 5);
eval("console.log(a);");
})();
(function f2(eval) {
var a = 2;
console.log(-3);
console.log(a - 5);
eval("console.log(a);");
})(eval);
(function() {
return "yes";
})();
console.log(2);
console.log(A + 1);
}
expect_stdout: true
}
Expand Down Expand Up @@ -1732,7 +1732,10 @@ redefine_arguments_3: {
console.log(function() {
var arguments;
return typeof arguments;
}(), "number", "undefined");
}(), "number", function(x) {
var arguments = x;
return typeof arguments;
}());
}
expect_stdout: "object number undefined"
}
Expand Down Expand Up @@ -2122,3 +2125,47 @@ issue_1865: {
}
expect_stdout: true
}

issue_1922_1: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a) {
arguments[0] = 2;
return a;
}(1));
}
expect: {
console.log(function(a) {
arguments[0] = 2;
return a;
}(1));
}
expect_stdout: "2"
}

issue_1922_2: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function() {
var a;
eval("a = 1");
return a;
}(1));
}
expect: {
console.log(function() {
var a;
eval("a = 1");
return a;
}(1));
}
expect_stdout: "1"
}
24 changes: 24 additions & 0 deletions test/compress/sequences.js
Expand Up @@ -610,3 +610,27 @@ delete_seq_6: {
}
expect_stdout: true
}

reassign_const: {
options = {
cascade: true,
sequences: true,
side_effects: true,
}
input: {
function f() {
const a = 1;
a++;
return a;
}
console.log(f());
}
expect: {
function f() {
const a = 1;
return a++, a;
}
console.log(f());
}
expect_stdout: true
}

0 comments on commit c736834

Please sign in to comment.