fix reduce_vars on arrow functions with this (#2504)

fixes #2496
This commit is contained in:
Alex Lam S.L
2017-11-24 06:21:49 +08:00
committed by GitHub
parent 3d8341a7ab
commit bbf38dc9c0
2 changed files with 57 additions and 2 deletions

View File

@@ -2390,6 +2390,10 @@ merge(Compressor.prototype, {
}
return true;
}
if (node instanceof AST_This && self instanceof AST_Arrow) {
result = false;
return true;
}
}));
return result;
});
@@ -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
@@ -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);

View File

@@ -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"
}