From 02811ce35e8f513ed1c76a18f95f06bd1460a775 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 26 May 2017 03:12:52 +0800 Subject: [PATCH] fix issues related to `export` & `function` (#2002) - `unused` function names - confusion with function call syntax fixes #2001 --- lib/parse.js | 9 ++++-- lib/transform.js | 1 + test/compress/issue-2001.js | 63 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/compress/issue-2001.js diff --git a/lib/parse.js b/lib/parse.js index 88da98d8..469e14a6 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -2417,11 +2417,16 @@ function parse($TEXT, options) { } } - var is_definition = is("keyword", "var") || is("keyword", "let") || is("keyword", "const"); + var is_definition = is("keyword", "var") + || is("keyword", "let") + || is("keyword", "const") + || is("keyword", "function") && !is_default; if (is_definition) { exported_definition = statement(); + } else if (is("keyword", "function")) { + exported_value = expr_atom(false); } else { - exported_value = expression(); + exported_value = expression(false); semicolon(); } diff --git a/lib/transform.js b/lib/transform.js index 5a0e0f2e..3e9118e6 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -240,6 +240,7 @@ TreeTransformer.prototype = new TreeWalker; }); _(AST_Export, function(self, tw){ + if (self.exported_definition) self.exported_definition = self.exported_definition.transform(tw); if (self.exported_value) self.exported_value = self.exported_value.transform(tw); }); diff --git a/test/compress/issue-2001.js b/test/compress/issue-2001.js new file mode 100644 index 00000000..4efa8261 --- /dev/null +++ b/test/compress/issue-2001.js @@ -0,0 +1,63 @@ +export_func_1: { + options = { + unused: true, + } + input: { + export function f(){}; + } + expect_exact: "export function f(){};" +} + +export_func_2: { + options = { + side_effects: false, + unused: true, + } + input: { + export function f(){}(1); + } + expect_exact: "export function f(){};1;" +} + +export_func_3: { + options = { + side_effects: true, + unused: true, + } + input: { + export function f(){}(1); + } + expect_exact: "export function f(){};" +} + +export_default_func_1: { + options = { + unused: true, + } + input: { + export default function f(){}; + } + expect_exact: "export default function(){};" +} + +export_default_func_2: { + options = { + side_effects: false, + unused: true, + } + input: { + export default function f(){}(1); + } + expect_exact: "export default function(){};1;" +} + +export_default_func_3: { + options = { + side_effects: true, + unused: true, + } + input: { + export default function f(){}(1); + } + expect_exact: "export default function(){};" +}