Skip to content

Commit

Permalink
fix(es/codegen): Emit extra paren emitting AssignExpr (#8413)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8412
  • Loading branch information
Austaras authored Dec 12, 2023
1 parent f202a28 commit dce3693
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 43 deletions.
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

0 comments on commit dce3693

Please sign in to comment.