From 5d9224deb893c383e9a2c2b11c5926db96bd7a1f Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 3 May 2021 15:26:20 +0100 Subject: [PATCH] fix corner cases with template literals (#4903) fixes #4902 --- lib/output.js | 4 ++++ test/compress/templates.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/output.js b/lib/output.js index 6272264fa4..308fcf54f1 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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); @@ -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; }); diff --git a/test/compress/templates.js b/test/compress/templates.js index e46f3c0616..bac1f06198 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -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() { @@ -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) {