Skip to content

Commit

Permalink
fix corner case in inline & unused (#5725)
Browse files Browse the repository at this point in the history
fixes #5724
  • Loading branch information
alexlamsl authored Oct 29, 2022
1 parent 7270671 commit f40dbd6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Compressor.prototype.compress = function(node) {
expression: make_node(AST_Function, self, {
argnames: [],
body: self.body,
}).init_vars(self),
}).init_vars(self, self),
args: [],
});
}
Expand Down Expand Up @@ -8963,7 +8963,7 @@ Compressor.prototype.compress = function(node) {
argnames: [],
body: [],
value: make_value(),
}).init_vars(self.parent_scope),
}).init_vars(self.parent_scope, self),
args: [],
}));
return make_sequence(self, exprs);
Expand Down
48 changes: 28 additions & 20 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,35 +439,43 @@ AST_Toplevel.DEFMETHOD("def_global", function(node) {
}
});

function init_block_vars(scope, parent) {
scope.enclosed = []; // variables from this or outer scope(s) that are referenced from this or inner scopes
scope.parent_scope = parent; // the parent scope (null if this is the top level)
scope.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
scope.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
if (parent) scope.make_def = parent.make_def; // top-level tracking of SymbolDef instances
function init_block_vars(scope, parent, orig) {
// variables from this or outer scope(s) that are referenced from this or inner scopes
scope.enclosed = orig ? orig.enclosed.slice() : [];
// map name to AST_SymbolDefun (functions defined in this scope)
scope.functions = orig ? orig.functions.clone() : new Dictionary();
// map name to AST_SymbolVar (variables defined in this scope; includes functions)
scope.variables = orig ? orig.variables.clone() : new Dictionary();
if (!parent) return;
// top-level tracking of SymbolDef instances
scope.make_def = parent.make_def;
// the parent scope (null if this is the top level)
scope.parent_scope = parent;
}

function init_scope_vars(scope, parent) {
init_block_vars(scope, parent);
scope.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
scope.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
function init_scope_vars(scope, parent, orig) {
init_block_vars(scope, parent, orig);
// will be set to true if this or nested scope uses the global `eval`
scope.uses_eval = false;
// will be set to true if this or some nested scope uses the `with` statement
scope.uses_with = false;
}

AST_BlockScope.DEFMETHOD("init_vars", function(parent_scope) {
init_block_vars(this, parent_scope);
AST_BlockScope.DEFMETHOD("init_vars", function(parent, orig) {
init_block_vars(this, parent, orig);
});
AST_Scope.DEFMETHOD("init_vars", function(parent_scope) {
init_scope_vars(this, parent_scope);
AST_Scope.DEFMETHOD("init_vars", function(parent, orig) {
init_scope_vars(this, parent, orig);
});
AST_Arrow.DEFMETHOD("init_vars", function(parent_scope) {
init_scope_vars(this, parent_scope);
AST_Arrow.DEFMETHOD("init_vars", function(parent, orig) {
init_scope_vars(this, parent, orig);
return this;
});
AST_AsyncArrow.DEFMETHOD("init_vars", function(parent_scope) {
init_scope_vars(this, parent_scope);
AST_AsyncArrow.DEFMETHOD("init_vars", function(parent, orig) {
init_scope_vars(this, parent, orig);
});
AST_Lambda.DEFMETHOD("init_vars", function(parent_scope) {
init_scope_vars(this, parent_scope);
AST_Lambda.DEFMETHOD("init_vars", function(parent, orig) {
init_scope_vars(this, parent, orig);
this.uses_arguments = false;
this.def_variable(new AST_SymbolFunarg({
name: "arguments",
Expand Down
26 changes: 26 additions & 0 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3901,3 +3901,29 @@ issue_5682_class_key_computed: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_5724: {
options = {
arrows: true,
inline: true,
keep_fargs: true,
toplevel: true,
unused: true,
}
input: {
"use strict";
class A {
static P = function(a) {
console.log(a, a);
}(a);
}
}
expect: {
"use strict";
(function(a) {
console.log(a, a);
})(a);
}
expect_stdout: ReferenceError("a is not defined")
node_version: ">=12"
}

0 comments on commit f40dbd6

Please sign in to comment.