Skip to content

Commit

Permalink
enhance varify (#5737)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Nov 21, 2022
1 parent 1d400f1 commit 21aff99
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
46 changes: 41 additions & 5 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Compressor(options, false_by_default) {
switches : !false_by_default,
templates : !false_by_default,
top_retain : null,
toplevel : !!(options && (options["module"] || options["top_retain"])),
toplevel : !!(options && !options["expression"] && (options["module"] || options["top_retain"])),
typeofs : !false_by_default,
unsafe : false,
unsafe_comps : false,
Expand Down Expand Up @@ -9357,14 +9357,16 @@ Compressor.prototype.compress = function(node) {
OPT(AST_ForEnumeration, function(self, compressor) {
if (compressor.option("varify") && is_lexical_definition(self.init)) {
var name = self.init.definitions[0].name;
if ((name instanceof AST_Destructured || name instanceof AST_SymbolLet)
if ((compressor.option("module") || name instanceof AST_Destructured || name instanceof AST_SymbolLet)
&& !name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) {
var def = node.definition();
return !same_scope(def) || may_overlap(compressor, def);
}
}, true)) {
self.init = to_var(self.init, self.resolve());
} else if (can_letify(self.init, compressor, 1)) {
self.init = to_let(self.init);
}
}
return self;
Expand Down Expand Up @@ -10178,6 +10180,20 @@ Compressor.prototype.compress = function(node) {
}
}

function to_let(stat) {
return make_node(AST_Let, stat, {
definitions: stat.definitions.map(function(defn) {
return make_node(AST_VarDef, defn, {
name: defn.name.convert_symbol(AST_SymbolLet, function(name, node) {
var def = name.definition();
def.orig[def.orig.indexOf(node)] = name;
}),
value: defn.value,
});
}),
});
}

function to_var(stat, scope) {
return make_node(AST_Var, stat, {
definitions: stat.definitions.map(function(defn) {
Expand All @@ -10197,6 +10213,18 @@ Compressor.prototype.compress = function(node) {
});
}

function can_letify(stat, compressor, assigned) {
if (!(stat instanceof AST_Const)) return false;
if (!compressor.option("module") && all(stat.definitions, function(defn) {
return defn.name instanceof AST_SymbolConst;
})) return false;
return all(stat.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return node.definition().assignments != assigned;
}, true);
});
}

function can_varify(compressor, sym) {
var def = sym.definition();
return (def.fixed || def.fixed === 0)
Expand All @@ -10206,15 +10234,23 @@ Compressor.prototype.compress = function(node) {
}

function varify(self, compressor) {
return compressor.option("varify") && all(self.definitions, function(defn) {
return all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
}, true);
}) ? to_var(self, compressor.find_parent(AST_Scope)) : self;
}

OPT(AST_Const, varify);
OPT(AST_Let, varify);
OPT(AST_Const, function(self, compressor) {
if (!compressor.option("varify")) return self;
if (can_letify(self, compressor, 0)) return to_let(self);
return varify(self, compressor);
});

OPT(AST_Let, function(self, compressor) {
if (!compressor.option("varify")) return self;
return varify(self, compressor);
});

function trim_optional_chain(node, compressor) {
if (!compressor.option("optional_chains")) return;
Expand Down
2 changes: 1 addition & 1 deletion lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function minify(files, options) {
rename: undefined,
sourceMap: false,
timings: false,
toplevel: !!(options && options["module"]),
toplevel: !!(options && !options["expression"] && options["module"]),
v8: false,
validate: false,
warnings: false,
Expand Down
36 changes: 36 additions & 0 deletions test/compress/varify.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,42 @@ forin_const_2: {
node_version: ">=6"
}

forin_const_3: {
options = {
module: true,
reduce_vars: true,
toplevel: true,
varify: true,
}
input: {
"use strict";
const o = {
p: 42,
q: "PASS",
};
for (const k in o)
(function f() {
console.log(k, o[k]);
})();
}
expect: {
"use strict";
let o = {
p: 42,
q: "PASS",
};
for (let k in o)
(function f() {
console.log(k, o[k]);
})();
}
expect_stdout: [
"p 42",
"q PASS",
]
node_version: ">=4"
}

forin_let_1: {
options = {
join_vars: true,
Expand Down

0 comments on commit 21aff99

Please sign in to comment.