From dce369336d873a920a9c6bd56f03286a3487ae26 Mon Sep 17 00:00:00 2001 From: Austaras Date: Tue, 12 Dec 2023 13:53:32 +0800 Subject: [PATCH] fix(es/codegen): Emit extra paren emitting `AssignExpr` (#8413) **Related issue:** - Closes #8412 --- .../fixture/issues-8xxx/8412/input/.swcrc | 16 ++++ .../fixture/issues-8xxx/8412/input/index.js | 12 +++ .../fixture/issues-8xxx/8412/output/index.js | 7 ++ crates/swc_ecma_codegen/src/lib.rs | 89 ++++++++++--------- 4 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8412/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-8xxx/8412/input/index.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8412/output/index.js diff --git a/crates/swc/tests/fixture/issues-8xxx/8412/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8412/input/.swcrc new file mode 100644 index 00000000000..8a5d2ce2dee --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8412/input/.swcrc @@ -0,0 +1,16 @@ +{ + "jsc": { + "externalHelpers": true, + "parser": { + "tsx": true, + "syntax": "typescript" + }, + "preserveAllComments": true, + "target": "es5", + "minify": { + "compress": { + "defaults": true + } + } + } +} diff --git a/crates/swc/tests/fixture/issues-8xxx/8412/input/index.js b/crates/swc/tests/fixture/issues-8xxx/8412/input/index.js new file mode 100644 index 00000000000..aa891c4d390 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8412/input/index.js @@ -0,0 +1,12 @@ +export const fn = () => { + let varA; + if (condCheck) { + // a bad comment + varA = "a"; + } else { + varA = "b"; + } + return objCreator({ + varA, + }); +}; diff --git a/crates/swc/tests/fixture/issues-8xxx/8412/output/index.js b/crates/swc/tests/fixture/issues-8xxx/8412/output/index.js new file mode 100644 index 00000000000..94988db673a --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8412/output/index.js @@ -0,0 +1,7 @@ +export var fn = function() { + var varA; + return(// a bad comment + varA = condCheck ? "a" : "b", objCreator({ + varA: varA + })); +}; diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 082bc947025..1277d2d7e70 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -2885,9 +2885,7 @@ where } fn has_leading_comment(&self, arg: &Expr) -> bool { - if let Some(cmt) = self.comments { - let span = arg.span(); - + fn span_has_leading_comment(cmt: &dyn Comments, span: Span) -> bool { let lo = span.lo; // see #415 @@ -2903,36 +2901,32 @@ where return true; } } - } else { - return false; + + false } + let cmt = if let Some(cmt) = self.comments { + if span_has_leading_comment(cmt, arg.span()) { + return true; + } + + cmt + } else { + return false; + }; + match arg { - Expr::Call(c) => match &c.callee { - Callee::Super(callee) => { - if let Some(cmt) = self.comments { - let lo = callee.span.lo; + Expr::Call(c) => { + let has_leading = match &c.callee { + Callee::Super(callee) => span_has_leading_comment(cmt, callee.span), + Callee::Import(callee) => span_has_leading_comment(cmt, callee.span), + Callee::Expr(callee) => self.has_leading_comment(callee), + }; - if cmt.has_leading(lo) { - return true; - } - } + if has_leading { + return true; } - Callee::Import(callee) => { - if let Some(cmt) = self.comments { - let lo = callee.span.lo; - - if cmt.has_leading(lo) { - return true; - } - } - } - Callee::Expr(callee) => { - if self.has_leading_comment(callee) { - return true; - } - } - }, + } Expr::Member(m) => { if self.has_leading_comment(&m.obj) { @@ -2941,12 +2935,8 @@ where } Expr::SuperProp(m) => { - if let Some(cmt) = self.comments { - let lo = m.span.lo; - - if cmt.has_leading(lo) { - return true; - } + if span_has_leading_comment(cmt, m.span) { + return true; } } @@ -2971,18 +2961,29 @@ where } Expr::Assign(e) => { - if let Some(cmt) = self.comments { - let lo = e.span.lo; + let lo = e.span.lo; - if cmt.has_leading(lo) { - return true; - } + if cmt.has_leading(lo) { + return true; } - if let Some(e) = e.left.as_expr() { - if self.has_leading_comment(e) { - return true; - } + let has_leading = match &e.left { + PatOrExpr::Expr(e) => self.has_leading_comment(e), + + PatOrExpr::Pat(p) => match &**p { + Pat::Expr(e) => self.has_leading_comment(e), + Pat::Ident(i) => span_has_leading_comment(cmt, i.span), + Pat::Array(a) => span_has_leading_comment(cmt, a.span), + Pat::Object(o) => span_has_leading_comment(cmt, o.span), + // TODO: remove after #8333 + Pat::Rest(r) => span_has_leading_comment(cmt, r.span), + Pat::Assign(a) => span_has_leading_comment(cmt, a.span), + Pat::Invalid(_) => false, + }, + }; + + if has_leading { + return true; } } @@ -3015,7 +3016,7 @@ where keyword!("return"); - if let Some(ref arg) = n.arg { + if let Some(arg) = n.arg.as_deref() { let need_paren = n .arg .as_deref()