From bbf38dc9c00cedba6581c4ee92db984e2b24f670 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 24 Nov 2017 06:21:49 +0800 Subject: [PATCH] fix `reduce_vars` on arrow functions with `this` (#2504) fixes #2496 --- lib/compress.js | 8 ++++-- test/compress/reduce_vars.js | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8a6a5f25..ca74fed6 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 58086ae6..692fd837 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -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" +}