fix drop_unused() accounting of symbols within export function (#2132)

fixes #2131
This commit is contained in:
Alex Lam S.L
2017-06-21 12:32:58 +08:00
committed by GitHub
parent 11923e3ae8
commit db877e8729
2 changed files with 32 additions and 3 deletions

View File

@@ -2090,7 +2090,8 @@ merge(Compressor.prototype, {
var tw = new TreeWalker(function(node, descend){ var tw = new TreeWalker(function(node, descend){
if (node !== self) { if (node !== self) {
if (node instanceof AST_Defun || node instanceof AST_DefClass) { if (node instanceof AST_Defun || node instanceof AST_DefClass) {
if (!drop_funcs && scope === self) { var in_export = tw.parent() instanceof AST_Export;
if (in_export || !drop_funcs && scope === self) {
var node_def = node.name.definition(); var node_def = node.name.definition();
if (node_def.global && !(node_def.id in in_use_ids)) { if (node_def.global && !(node_def.id in in_use_ids)) {
in_use_ids[node_def.id] = true; in_use_ids[node_def.id] = true;
@@ -2227,7 +2228,6 @@ merge(Compressor.prototype, {
// pass 3: we should drop declarations not in_use // pass 3: we should drop declarations not in_use
var tt = new TreeTransformer( var tt = new TreeTransformer(
function before(node, descend, in_list) { function before(node, descend, in_list) {
var parent = tt.parent();
if (!compressor.option("keep_fnames") if (!compressor.option("keep_fnames")
&& node.name && (node instanceof AST_Function || node instanceof AST_ClassExpression)) { && node.name && (node instanceof AST_Function || node instanceof AST_ClassExpression)) {
var def = node.name.definition(); var def = node.name.definition();
@@ -2264,7 +2264,7 @@ merge(Compressor.prototype, {
} }
} }
} }
if ((node instanceof AST_Defun || node instanceof AST_DefClass) && !(parent instanceof AST_Export) && node !== self) { if ((node instanceof AST_Defun || node instanceof AST_DefClass) && node !== self) {
var keep = (node.name.definition().id in in_use_ids) || !drop_funcs && node.name.definition().global; var keep = (node.name.definition().id in in_use_ids) || !drop_funcs && node.name.definition().global;
if (!keep) { if (!keep) {
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name)); compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
@@ -2272,6 +2272,7 @@ merge(Compressor.prototype, {
} }
return node; return node;
} }
var parent = tt.parent();
if (node instanceof AST_Definitions if (node instanceof AST_Definitions
&& !(parent instanceof AST_ForIn && parent.init === node) && !(parent instanceof AST_ForIn && parent.init === node)
&& (drop_vars || !(parent instanceof AST_Toplevel) && !(node instanceof AST_Var))) { && (drop_vars || !(parent instanceof AST_Toplevel) && !(node instanceof AST_Var))) {

View File

@@ -67,3 +67,31 @@ beautify: {
} }
expect_exact: "export { A as B, C as D };" expect_exact: "export { A as B, C as D };"
} }
issue_2131: {
options = {
toplevel: true,
unused: true,
}
input: {
function no() {
console.log(42);
}
function go() {
console.log(42);
}
var X = 1, Y = 2;
export function main() {
go(X);
};
}
expect: {
function go() {
console.log(42);
}
var X = 1;
export function main() {
go(X);
};
}
}