Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/codegen): Emit extra paren when return assign and assign targe… #8413

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8412/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"jsc": {
"externalHelpers": true,
"parser": {
"tsx": true,
"syntax": "typescript"
},
"preserveAllComments": true,
"target": "es5",
"minify": {
"compress": {
"defaults": true
}
}
}
}
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8412/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const fn = () => {
let varA;
if (condCheck) {
// a bad comment
varA = "a";
} else {
varA = "b";
}
return objCreator({
varA,
});
};
7 changes: 7 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8412/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export var fn = function() {
var varA;
return(// a bad comment
varA = condCheck ? "a" : "b", objCreator({
varA: varA
}));
};
87 changes: 44 additions & 43 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2903,36 +2901,32 @@ where
return true;
}
}

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;

if cmt.has_leading(lo) {
return true;
}
}
}
Callee::Import(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;
}
}
}
Callee::Expr(callee) => {
if self.has_leading_comment(callee) {
return true;
}
if has_leading {
return true;
}
},
}

Expr::Member(m) => {
if self.has_leading_comment(&m.obj) {
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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()
Expand Down