diff --git a/ecmascript/transforms/src/compat/es2015/template_literal/tests.rs b/ecmascript/transforms/src/compat/es2015/template_literal/tests.rs index 1902cdd23d7..ca9b25ac2c0 100644 --- a/ecmascript/transforms/src/compat/es2015/template_literal/tests.rs +++ b/ecmascript/transforms/src/compat/es2015/template_literal/tests.rs @@ -1,5 +1,14 @@ use super::*; +test_exec!( + ::swc_ecma_parser::Syntax::default(), + |_| TemplateLiteral, + issue_231, + "const truthy = 'a=b'; +const foo = `http://example.com/foo/bar${truthy && '?'}${truthy}`; +console.log(foo);" +); + test!( ::swc_ecma_parser::Syntax::default(), |_| TemplateLiteral, diff --git a/ecmascript/transforms/src/fixer.rs b/ecmascript/transforms/src/fixer.rs index 2a64b6be954..77164f8ae0c 100644 --- a/ecmascript/transforms/src/fixer.rs +++ b/ecmascript/transforms/src/fixer.rs @@ -312,6 +312,13 @@ impl Fold for Fixer { e @ Expr::Assign(..) | e @ Expr::Seq(..) | e @ Expr::Yield(..) => { box e.wrap_with_paren() } + Expr::Bin(BinExpr { op: op_of_rhs, .. }) => { + if op_of_rhs.precedence() < expr.op.precedence() { + box expr.right.wrap_with_paren() + } else { + expr.right + } + } _ => expr.right, }; @@ -604,4 +611,6 @@ function a() { test_fixer!(fixer_12, "(((a, b), c), d) + e;", "d + e;"); test_fixer!(fixer_13, "delete (((1), a), (2));", "delete 2"); + + identical!(issue_231, "'' + (truthy && '?') + truthy;"); }