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): Preserve more parens #6268

Merged
merged 6 commits into from Oct 27, 2022
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
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6255/1/input/input.js
@@ -0,0 +1,6 @@
function test(node) {
return (
// comment from the source code
(_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter((m) => !ts.isDecorator(m))
);
}
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6255/1/output/input.js
@@ -0,0 +1,6 @@
function test(node) {
return(// comment from the source code
(_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter(function(m) {
return !ts.isDecorator(m);
}));
}
22 changes: 22 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Expand Up @@ -2736,6 +2736,12 @@ where
}
}

Expr::Cond(e) => {
if self.has_leading_comment(&e.test) {
return true;
}
}

Expr::Seq(e) => {
if let Some(e) = e.exprs.first() {
if self.has_leading_comment(e) {
Expand All @@ -2744,6 +2750,22 @@ where
}
}

Expr::Assign(e) => {
if let Some(cmt) = self.comments {
let lo = e.span.lo;

if cmt.has_leading(lo) {
return true;
}
}

if let Some(e) = e.left.as_expr() {
if self.has_leading_comment(e) {
return true;
}
}
}

_ => {}
}

Expand Down