From a9d0ddea9d6812ede67a1c318dc8a7f8d9a764a4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 23 Feb 2022 21:04:04 +0000 Subject: [PATCH] fix corner case in `directives` & `expression` (#5369) fixes #5368 --- lib/compress.js | 31 ++++++++++++++++--------------- test/compress/directives.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 3e5e9194b7..998626ab19 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -227,9 +227,7 @@ Compressor.prototype.exposed = function(def) { Compressor.prototype.compress = function(node) { node = node.resolve_defines(this); node.hoist_exports(this); - if (this.option("expression")) { - node.process_expression(true); - } + if (this.option("expression")) node.process_expression(true); var merge_vars = this.options.merge_vars; var passes = +this.options.passes || 1; var min_count = 1 / 0; @@ -261,9 +259,7 @@ Compressor.prototype.compress = function(node) { } } } - if (this.option("expression")) { - node.process_expression(false); - } + if (this.option("expression")) node.process_expression(false); return node; }; @@ -308,12 +304,19 @@ Compressor.prototype.compress = function(node) { AST_Scope.DEFMETHOD("process_expression", function(insert, transform) { var self = this; var tt = new TreeTransformer(function(node) { - if (insert && node instanceof AST_SimpleStatement) { - return transform ? transform(node) : make_node(AST_Return, node, { value: node.body }); - } - if (!insert && node instanceof AST_Return) { - return transform ? transform(node) : make_node(AST_SimpleStatement, node, { - body: node.value || make_node(AST_UnaryPrefix, node, { + if (insert) { + if (node instanceof AST_Directive) node = make_node(AST_SimpleStatement, node, { + body: make_node(AST_String, node, node), + }); + if (node instanceof AST_SimpleStatement) { + return transform ? transform(node) : make_node(AST_Return, node, { value: node.body }); + } + } else if (node instanceof AST_Return) { + if (transform) return transform(node); + var value = node.value; + if (value instanceof AST_String) return make_node(AST_Directive, node, value); + return make_node(AST_SimpleStatement, node, { + body: value || make_node(AST_UnaryPrefix, node, { operator: "void", expression: make_node(AST_Number, node, { value: 0 }), }), @@ -334,9 +337,7 @@ Compressor.prototype.compress = function(node) { } } else if (node instanceof AST_If) { node.body = node.body.transform(tt); - if (node.alternative) { - node.alternative = node.alternative.transform(tt); - } + if (node.alternative) node.alternative = node.alternative.transform(tt); } else if (node instanceof AST_With) { node.body = node.body.transform(tt); } diff --git a/test/compress/directives.js b/test/compress/directives.js index 6c85fded70..6e3fd13138 100644 --- a/test/compress/directives.js +++ b/test/compress/directives.js @@ -129,3 +129,32 @@ valid_after_invalid_2: { } expect_stdout: "undefined" } + +issue_5368_1: { + options = { + directives: true, + expression: true, + } + input: { + "foo"; + } + expect: { + "foo"; + } +} + +issue_5368_2: { + options = { + directives: true, + expression: true, + } + input: { + "foo"; + (function() { + "bar"; + })(); + } + expect: { + (function() {})(); + } +}