From 755e2a62c6abcd844f9428ec64f54fc2262613b5 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 28 Nov 2017 22:54:44 +0800 Subject: [PATCH] extend `hoist_props` to `AST_Arrow` & `AST_Class` (#2527) fixes #2503 --- lib/compress.js | 2 ++ test/compress/hoist_props.js | 40 +++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 07626336..65cce94c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -635,6 +635,8 @@ merge(Compressor.prototype, { || !immutable && parent instanceof AST_Call && parent.expression === node + && !(value instanceof AST_Arrow) + && !(value instanceof AST_Class) && (!(value instanceof AST_Function) || !(parent instanceof AST_New) && value.contains_this())) { return true; diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js index 94c8d986..025e3f5c 100644 --- a/test/compress/hoist_props.js +++ b/test/compress/hoist_props.js @@ -454,17 +454,12 @@ hoist_class_with_new: { console.log(o.p.name, o.p === o.p, new o.p(o.x).value, new o.p(o.y).value); } expect: { - // FIXME: class `o.p` not hoisted due to `new` - var o = { - p: class Foo { - constructor(value) { - this.value = 10 * value; - } - }, - x: 1, - y: 2 + var o_p = class Foo { + constructor(value) { + this.value = 10 * value; + } }; - console.log(o.p.name, o.p == o.p, new o.p(o.x).value, new o.p(o.y).value); + console.log(o_p.name, true, new o_p(1).value, new o_p(2).value); } node_version: ">=6" expect_stdout: "Foo true 10 20" @@ -772,3 +767,28 @@ issue_2508_5: { } expect_stdout: true } + +issue_2508_6: { + options = { + collapse_vars: true, + hoist_props: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var o = { + f: x => { + console.log(x); + } + }; + o.f(o.f); + } + expect: { + var o_f = x => { + console.log(x); + }; + o_f(o_f); + } + expect_stdout: true +}