Skip to content

Commit

Permalink
fix reduce_vars on arrow functions with this (#2504)
Browse files Browse the repository at this point in the history
fixes #2496
  • Loading branch information
alexlamsl committed Nov 23, 2017
1 parent 3d8341a commit bbf38dc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,10 @@ merge(Compressor.prototype, {
}
return true;
}
if (node instanceof AST_This && self instanceof AST_Arrow) {
result = false;
return true;
}
}));
return result;
});
Expand Down Expand Up @@ -4582,7 +4586,7 @@ merge(Compressor.prototype, {
if (fixed instanceof AST_Defun) {
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
}
if (d.single_use && fixed instanceof AST_Function) {
if (d.single_use && is_func_expr(fixed)) {
if (d.scope !== self.scope
&& (!compressor.option("reduce_funcs")
|| d.escaped
Expand All @@ -4595,7 +4599,7 @@ merge(Compressor.prototype, {
if (d.single_use == "f") {
var scope = self.scope;
do {
if (scope instanceof AST_Defun || scope instanceof AST_Function) {
if (scope instanceof AST_Defun || is_func_expr(scope)) {
scope.inlined = true;
}
} while (scope = scope.parent_scope);
Expand Down
51 changes: 51 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -4819,3 +4819,54 @@ issue_2485: {
}
expect_stdout: "6"
}

issue_2496: {
options = {
passes: 2,
reduce_funcs: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function execute(callback) {
callback();
}
class Foo {
constructor(message) {
this.message = message;
}
go() {
this.message = "PASS";
console.log(this.message);
}
run() {
execute(() => {
this.go();
});
}
}
new Foo("FAIL").run();
}
expect: {
class Foo {
constructor(message) {
this.message = message;
}
go() {
this.message = "PASS";
console.log(this.message);
}
run() {
(function(callback) {
callback();
})(() => {
this.go();
});
}
}
new Foo("FAIL").run();
}
expect_stdout: "PASS"
node_version: ">=6"
}

0 comments on commit bbf38dc

Please sign in to comment.