Skip to content

Commit

Permalink
recognise this as a reference to the surrounding class in drop_unus…
Browse files Browse the repository at this point in the history
…ed. Closes #1472
  • Loading branch information
fabiosantoscode committed Feb 19, 2024
1 parent 56c33d5 commit c7c8f82
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/compress/drop-unused.js
Expand Up @@ -70,6 +70,7 @@ import {
AST_SymbolFunarg,
AST_SymbolRef,
AST_SymbolVar,
AST_This,
AST_Toplevel,
AST_Unary,
AST_Var,
Expand Down Expand Up @@ -126,6 +127,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
return node.expression;
}
};
var this_def = null;
var in_use_ids = new Map();
var fixed_ids = new Map();
if (self instanceof AST_Toplevel && compressor.top_retain) {
Expand All @@ -137,6 +139,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
}
var var_defs_by_id = new Map();
var initializations = new Map();

// pass 1: find out which symbols are directly used in
// this scope (not in nested scopes).
var scope = this;
Expand All @@ -149,11 +152,6 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
});
}
if (node === self) return;
if (node instanceof AST_Class) {
if (node.has_side_effects(compressor)) {
node.visit_nondeferred_class_parts(tw);
}
}
if (node instanceof AST_Defun || node instanceof AST_DefClass) {
var node_def = node.name.definition();
const in_export = tw.parent() instanceof AST_Export;
Expand All @@ -163,11 +161,22 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
}
}

if (node instanceof AST_DefClass && node.has_side_effects(compressor)) {
const save_this_def = this_def;
this_def = node_def;
node.visit_nondeferred_class_parts(tw);
this_def = save_this_def;
}

map_add(initializations, node_def.id, node);
return true; // don't go in nested scopes
}
// In the root scope, we drop things. In inner scopes, we just check for uses.
const in_root_scope = scope === self;
if (node instanceof AST_This && this_def && in_root_scope) {
in_use_ids.set(this_def.id, this_def);
return true;
}
if (node instanceof AST_SymbolFunarg && in_root_scope) {
map_add(var_defs_by_id, node.definition().id, node);
}
Expand Down
89 changes: 89 additions & 0 deletions test/compress/drop-unused.js
Expand Up @@ -3218,6 +3218,95 @@ issue_t1412_2: {
}
}

class_used_within_itself: {
options = {
toplevel: true,
unused: true,
side_effects: true,
pure_getters: true,
};
input: {
class C {
static [C.name] = 1;
}
}
expect: { }
}

class_used_within_itself_2: {
options = { toplevel: true, unused: true, side_effects: true };
input: {
globalThis.useThis = function(obj) {
obj.prototype.method()
}

const List = ['P', 'A', 'S', 'S'];
class Class {
method(t) {
List.forEach(letter => console.log(letter));
}
static prop = useThis(this);
}
}
expect: {
globalThis.useThis = function(obj) {
obj.prototype.method()
}

const List = ['P', 'A', 'S', 'S'];
class Class {
method(t) {
List.forEach(letter => console.log(letter));
}
static prop = useThis(this);
}
}
expect_stdout: [ 'P', 'A', 'S', 'S' ]
}

class_used_within_itself_3: {
options = { toplevel: true, unused: true, side_effects: true };
input: {
const importedfn = () => console.log("------------");

const cls = (() => {
var ErrorBoundary = importedfn;

let _Wrapper;
class Wrapper {
static _ = ((cls) => (_Wrapper = cls))(this);
render() {
ErrorBoundary("foobar");
}
}

return _Wrapper;
})();

new cls().render();
}
expect: {
const importedfn = () => console.log("------------");

const cls = (() => {
var ErrorBoundary = importedfn;

let _Wrapper;
class Wrapper {
static _ = ((cls) => (_Wrapper = cls))(this);
render() {
ErrorBoundary("foobar");
}
}

return _Wrapper;
})();

new cls().render();
}
}


issue_t1447_var: {
options = { unused: true, inline: true, reduce_vars: true }
input: {
Expand Down

0 comments on commit c7c8f82

Please sign in to comment.