Skip to content

Commit

Permalink
fix corner case in directives & expression (#5369)
Browse files Browse the repository at this point in the history
fixes #5368
  • Loading branch information
alexlamsl committed Feb 23, 2022
1 parent 313e497 commit a9d0dde
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/compress.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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 }),
}),
Expand All @@ -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);
}
Expand Down
29 changes: 29 additions & 0 deletions test/compress/directives.js
Expand Up @@ -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() {})();
}
}

0 comments on commit a9d0dde

Please sign in to comment.