From ba4a771bbccb0b6026588a56b0c31f5bac466775 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 26 Feb 2021 20:56:34 +0000 Subject: support limited `ufuzz` testing for `export` (#4693) fixes #4692 --- lib/ast.js | 10 +--------- lib/compress.js | 12 +++++++++--- lib/output.js | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/ast.js b/lib/ast.js index 3c6672c2..5fe6efc1 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -1176,15 +1176,7 @@ var AST_ExportDefault = DEFNODE("ExportDefault", "body", { }); }, _validate: function() { - if (this.body instanceof AST_Class && this.body.name) { - if (!(this.body instanceof AST_DefClass)) { - throw new Error("body must be AST_DefClass when named"); - } - } else if (this.body instanceof AST_Lambda && this.body.name) { - if (!(this.body instanceof AST_LambdaDefinition)) { - throw new Error("body must be AST_LambdaDefinition when named"); - } - } else { + if (!(this.body instanceof AST_DefClass || this.body instanceof AST_LambdaDefinition)) { must_be_expression(this, "body"); } }, diff --git a/lib/compress.js b/lib/compress.js index 0e698804..101fdb4e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -940,6 +940,8 @@ merge(Compressor.prototype, { }); if (!node.name) return; var d = node.name.definition(); + var parent = tw.parent(); + if (parent instanceof AST_ExportDeclaration || parent instanceof AST_ExportDefault) d.single_use = false; if (safe_to_assign(tw, d, true)) { mark(tw, d); tw.loop_ids[d.id] = tw.in_loop; @@ -6709,6 +6711,7 @@ merge(Compressor.prototype, { var var_decl = 0; self.walk(new TreeWalker(function(node) { if (var_decl > 1) return true; + if (node instanceof AST_ExportDeclaration) return true; if (node instanceof AST_Scope && node !== self) return true; if (node instanceof AST_Var) { var_decl++; @@ -6728,12 +6731,15 @@ merge(Compressor.prototype, { dirs.push(node); return make_node(AST_EmptyStatement, node); } - if (hoist_funs && node instanceof AST_Defun - && (tt.parent() === self || !compressor.has_directive("use strict"))) { + if (node instanceof AST_Defun) { + if (!hoist_funs) return node; + if (tt.parent() !== self && compressor.has_directive("use strict")) return node; hoisted.push(node); return make_node(AST_EmptyStatement, node); } - if (hoist_vars && node instanceof AST_Var) { + if (node instanceof AST_Var) { + if (!hoist_vars) return node; + if (tt.parent() instanceof AST_ExportDeclaration) return node; if (!all(node.definitions, function(defn) { var sym = defn.name; return sym instanceof AST_SymbolVar diff --git a/lib/output.js b/lib/output.js index 49164359..bebcd7fd 100644 --- a/lib/output.js +++ b/lib/output.js @@ -664,7 +664,9 @@ function OutputStream(options) { function needs_parens_function(output) { if (!output.has_parens() && first_in_statement(output)) return true; var p = output.parent(); - // export default (function() {})() + // export default (function foo() {}); + if (this.name && p instanceof AST_ExportDefault) return true; + // export default (function() {})(foo); if (p && p.TYPE == "Call" && output.parent(1) instanceof AST_ExportDefault) return true; if (output.option("webkit") && p instanceof AST_PropAccess && p.expression === this) return true; if (output.option("wrap_iife") && p instanceof AST_Call && p.expression === this) return true; @@ -725,6 +727,8 @@ function OutputStream(options) { // { [(1, 2)]: foo } = bar // { 1: (2, foo) } = bar || p instanceof AST_DestructuredKeyVal + // export default (foo, bar) + || p instanceof AST_ExportDefault // for (foo of (bar, baz)); || p instanceof AST_ForOf // { [(1, 2)]: 3 }[2] ---> 3 @@ -1032,9 +1036,16 @@ function OutputStream(options) { output.space(); output.print("default"); output.space(); - this.body.print(output); - if (this.body instanceof AST_Class) return; - if (this.body instanceof AST_Lambda && !is_arrow(this.body)) return; + var body = this.body; + body.print(output); + if (body instanceof AST_ClassExpression) { + if (!body.name) return; + } + if (body instanceof AST_DefClass) return; + if (body instanceof AST_LambdaDefinition) return; + if (body instanceof AST_LambdaExpression) { + if (!body.name && !is_arrow(body)) return; + } output.semicolon(); }); DEFPRINT(AST_ExportForeign, function(output) { -- cgit v1.2.3