From 9c0718b162e537ee6ad1dd5c7af6fbf5fcb6ae1d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 18 Dec 2020 06:55:20 +0000 Subject: [PATCH] enhance `arrows` (#4403) --- lib/compress.js | 25 +++++++++++-------------- test/compress/arrows.js | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index ab5ad06d..a3842bed 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4670,22 +4670,19 @@ merge(Compressor.prototype, { OPT(AST_Arrow, function(self, compressor) { if (!compressor.option("arrows")) return self; - if (self.value) { - var value = self.value; - if (is_undefined(value, compressor)) { - self.value = null; - } else if (value instanceof AST_UnaryPrefix && value.operator == "void") { - self.body.push(make_node(AST_SimpleStatement, value, { - body: value.expression - })); - self.value = null; - } - } else if (self.body.length == 1) { - var stat = self.body[0]; - if (stat instanceof AST_Return && stat.value) { - self.body.pop(); + var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor); + switch (body.length) { + case 1: + var stat = body[0]; + if (stat instanceof AST_Return) { + self.body.length = 0; self.value = stat.value; + break; } + default: + self.body = body; + self.value = null; + break; } return self; }); diff --git a/test/compress/arrows.js b/test/compress/arrows.js index c91cd275..0088392c 100644 --- a/test/compress/arrows.js +++ b/test/compress/arrows.js @@ -320,6 +320,8 @@ inline_this: { trim_body: { options = { arrows: true, + if_return: true, + side_effects: true, } input: { var f = a => { @@ -337,6 +339,25 @@ trim_body: { node_version: ">=4" } +collapse_value: { + options = { + arrows: true, + collapse_vars: true, + keep_fargs: "strict", + unused: true, + } + input: { + var a = 42; + console.log((b => Math.floor(b))(a)); + } + expect: { + var a = 42; + console.log((() => Math.floor(a))()); + } + expect_stdout: "42" + node_version: ">=4" +} + reduce_iife_1: { options = { evaluate: true, -- 2.34.1