From 402bbda1d17eef06907bbe270675d1a01d29f021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Wed, 7 Mar 2018 20:50:01 +0000 Subject: [PATCH 1/4] parenthesise exported iifes correctly --- lib/output.js | 14 ++++++++++++++ test/compress/export.js | 2 +- test/compress/issue-2977.js | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/compress/issue-2977.js diff --git a/lib/output.js b/lib/output.js index e4987179..c6d6355b 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1106,6 +1106,20 @@ function OutputStream(options) { self._do_print(output); }); + DEFPRINT(AST_Function, function(self, output) { + var parent = output.parent(); + var grandparent = output.parent(1); + var is_exported_iife = parent instanceof AST_Call && parent.expression == self && + grandparent instanceof AST_Export && grandparent.is_default; + if (is_exported_iife) { + output.with_parens(function() { + self._do_print(output); + }); + } else { + self._do_print(output); + } + }); + DEFPRINT(AST_PrefixedTemplateString, function(self, output) { self.prefix.print(output); self.template_string.print(output); diff --git a/test/compress/export.js b/test/compress/export.js index 783fa80a..34cf2606 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -410,7 +410,7 @@ export_default_anonymous_function_not_call: { export default function(){}(foo); } // FIXME: should be `export default function(){};foo;` - expect_exact: "export default function(){}(foo);" + expect_exact: "export default(function(){})(foo);" } export_default_anonymous_generator_not_call: { diff --git a/test/compress/issue-2977.js b/test/compress/issue-2977.js new file mode 100644 index 00000000..853e0a43 --- /dev/null +++ b/test/compress/issue-2977.js @@ -0,0 +1,7 @@ + +issue_2977: { + input: { + export default (function () {})(); + } + expect_exact: "export default(function(){})();" +} From e5a0fcdfc6be26cf863ff3ed54cd19953e3f5e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Fri, 9 Mar 2018 18:14:25 +0000 Subject: [PATCH 2/4] move test to export.js --- test/compress/export.js | 7 +++++++ test/compress/issue-2977.js | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 test/compress/issue-2977.js diff --git a/test/compress/export.js b/test/compress/export.js index 34cf2606..b4ad46c5 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -444,3 +444,10 @@ export_default_anonymous_async_function_not_call: { // agrees with `acorn` and `babylon 7` expect_exact: "export default async function(){};foo;" } + +issue_2977: { + input: { + export default (function () {})(); + } + expect_exact: "export default(function(){})();" +} diff --git a/test/compress/issue-2977.js b/test/compress/issue-2977.js deleted file mode 100644 index 853e0a43..00000000 --- a/test/compress/issue-2977.js +++ /dev/null @@ -1,7 +0,0 @@ - -issue_2977: { - input: { - export default (function () {})(); - } - expect_exact: "export default(function(){})();" -} From 113a5236bca47e8db0e7b4432b5379d3a738a6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Fri, 9 Mar 2018 18:52:53 +0000 Subject: [PATCH 3/4] implement suggested changeset from @kzc --- lib/output.js | 18 +++--------------- test/compress/export.js | 4 ++-- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/output.js b/lib/output.js index c6d6355b..a3807bf7 100644 --- a/lib/output.js +++ b/lib/output.js @@ -773,6 +773,7 @@ function OutputStream(options) { || p instanceof AST_Expansion // [...(a, b)] || p instanceof AST_ForOf && this === p.object // for (e of (foo, bar)) {} || p instanceof AST_Yield // yield (foo, bar) + || p instanceof AST_Export // export default (foo, bar) ; }); @@ -844,7 +845,8 @@ function OutputStream(options) { PARENS(AST_Call, function(output){ var p = output.parent(), p1; - if (p instanceof AST_New && p.expression === this) + if (p instanceof AST_New && p.expression === this + || p instanceof AST_Export && p.is_default && this.expression instanceof AST_Function) return true; // workaround for Safari bug. @@ -1106,20 +1108,6 @@ function OutputStream(options) { self._do_print(output); }); - DEFPRINT(AST_Function, function(self, output) { - var parent = output.parent(); - var grandparent = output.parent(1); - var is_exported_iife = parent instanceof AST_Call && parent.expression == self && - grandparent instanceof AST_Export && grandparent.is_default; - if (is_exported_iife) { - output.with_parens(function() { - self._do_print(output); - }); - } else { - self._do_print(output); - } - }); - DEFPRINT(AST_PrefixedTemplateString, function(self, output) { self.prefix.print(output); self.template_string.print(output); diff --git a/test/compress/export.js b/test/compress/export.js index b4ad46c5..94cb0228 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -410,7 +410,7 @@ export_default_anonymous_function_not_call: { export default function(){}(foo); } // FIXME: should be `export default function(){};foo;` - expect_exact: "export default(function(){})(foo);" + expect_exact: "export default(function(){}(foo));" } export_default_anonymous_generator_not_call: { @@ -449,5 +449,5 @@ issue_2977: { input: { export default (function () {})(); } - expect_exact: "export default(function(){})();" + expect_exact: "export default(function(){}());" } From 2aed51a95abd3717ba394d8845a06a070eb2b326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Fri, 9 Mar 2018 21:30:04 +0000 Subject: [PATCH 4/4] add test for export of sequence --- test/compress/export.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/compress/export.js b/test/compress/export.js index 94cb0228..00425c9d 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -280,6 +280,13 @@ export_default_anonymous_function: { expect_exact: "export default function(){foo()};" } +export_default_seq: { + input: { + export default (1, 2) + } + expect_exact: "export default(1,2);" +} + export_default_arrow: { options = { reduce_vars: true,