Skip to content

Commit

Permalink
enhance unused (#4858)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Apr 22, 2021
1 parent 3c161a6 commit bddb5a0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
29 changes: 22 additions & 7 deletions lib/compress.js
Expand Up @@ -5817,19 +5817,29 @@ merge(Compressor.prototype, {
} else if (node instanceof AST_Unary) {
if (node.write_only) sym = node.expression;
}
if (/strict/.test(compressor.option("pure_getters"))) {
while (sym instanceof AST_PropAccess && !sym.expression.may_throw_on_access(compressor)) {
if (sym instanceof AST_Sub) props.unshift(sym.property);
sym = sym.expression;
nested = true;
}
}
if (/strict/.test(compressor.option("pure_getters"))) sym = extract_reference(sym, props);
if (!(sym instanceof AST_SymbolRef)) return;
var def = sym.definition();
if (export_defaults[def.id]) return;
if (compressor.exposed(def)) return;
if (!can_drop_symbol(sym, compressor, nested)) return;
return sym;

function extract_reference(node, props) {
if (node instanceof AST_PropAccess) {
var expr = node.expression;
if (!expr.may_throw_on_access(compressor)) {
nested = true;
if (props && node instanceof AST_Sub) props.unshift(node.property);
return extract_reference(expr, props);
}
} else if (node instanceof AST_Assign && node.operator == "=") {
var ref = extract_reference(node.right);
if (props) props.assign = node;
return ref;
}
return node;
}
};
var assign_in_use = Object.create(null);
var export_defaults = Object.create(null);
Expand Down Expand Up @@ -6648,6 +6658,11 @@ merge(Compressor.prototype, {
}
}, true);
}))) {
if (props.assign) {
props.assign.write_only = true;
props.assign.walk(tw);
delete props.assign.write_only;
}
props.forEach(function(prop) {
prop.walk(tw);
});
Expand Down
20 changes: 20 additions & 0 deletions test/compress/classes.js
Expand Up @@ -1541,3 +1541,23 @@ issue_4848: {
expect_stdout: "PASS"
node_version: ">=4"
}

drop_unused_self_reference: {
options = {
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
"use strict";
class A {}
(A.p = A).q = console.log("PASS");
}
expect: {
"use strict";
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=4"
}
17 changes: 17 additions & 0 deletions test/compress/functions.js
Expand Up @@ -6021,3 +6021,20 @@ issue_4823: {
}
expect_stdout: "function"
}

drop_unused_self_reference: {
options = {
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f() {}
(f.p = f).q = console.log("PASS");
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
}
4 changes: 2 additions & 2 deletions test/ufuzz/index.js
Expand Up @@ -1796,7 +1796,7 @@ function createClassLiteral(recurmax, stmtDepth, canThrow, name) {
if (SUPPORT.class_field && rng(2)) {
s += internal || createObjectKey(recurmax, stmtDepth, canThrow);
if (rng(5)) {
async = bug_async_class_await && fixed;
async = bug_async_class_await && fixed && 0;
generator = false;
s += " = " + createExpression(recurmax, NO_COMMA, stmtDepth, fixed ? canThrow : CANNOT_THROW);
generator = save_generator;
Expand Down Expand Up @@ -2000,7 +2000,7 @@ function isBannedKeyword(name) {
case "arguments":
return in_class;
case "await":
return async;
return async !== false;
case "yield":
return generator || in_class;
}
Expand Down

0 comments on commit bddb5a0

Please sign in to comment.