Skip to content

Commit

Permalink
fix corner cases with template literals (#4903)
Browse files Browse the repository at this point in the history
fixes #4902
  • Loading branch information
alexlamsl committed May 3, 2021
1 parent 45b6d23 commit 5d9224d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/output.js
Expand Up @@ -709,6 +709,8 @@ function OutputStream(options) {
// (x++)[y]
// (typeof x).y
if (p instanceof AST_PropAccess) return p.expression === this;
// (~x)`foo`
if (p instanceof AST_Template) return p.tag === this;
}
PARENS(AST_Await, needs_parens_unary);
PARENS(AST_Unary, needs_parens_unary);
Expand Down Expand Up @@ -782,6 +784,8 @@ function OutputStream(options) {
if (p instanceof AST_Class) return true;
// (foo && bar)["prop"], (foo && bar).prop
if (p instanceof AST_PropAccess) return p.expression === this;
// (foo && bar)``
if (p instanceof AST_Template) return p.tag === this;
// typeof (foo && bar)
if (p instanceof AST_Unary) return true;
});
Expand Down
32 changes: 32 additions & 0 deletions test/compress/templates.js
Expand Up @@ -62,6 +62,23 @@ tag_parentheses_arrow: {
node_version: ">=4"
}

tag_parentheses_binary: {
options = {
collapse_vars: true,
toplevel: true,
unused: true,
}
input: {
var f = function() {
console.log("PASS");
} || console
f``;
}
expect_exact: '(function(){console.log("PASS")}||console)``;'
expect_stdout: "PASS"
node_version: ">=4"
}

tag_parentheses_new: {
input: {
(new function() {
Expand All @@ -87,6 +104,21 @@ tag_parentheses_sequence: {
node_version: ">=4"
}

tag_parentheses_unary: {
input: {
var a;
try {
(~a)``;
(a++)``;
} catch (e) {
console.log("PASS");
}
}
expect_exact: 'var a;try{(~a)``;(a++)``}catch(e){console.log("PASS")}'
expect_stdout: "PASS"
node_version: ">=4"
}

malformed_escape: {
input: {
(function(s) {
Expand Down

0 comments on commit 5d9224d

Please sign in to comment.