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(ecma/codegen): don't print trailing coma for rest argument #6610

Merged
merged 2 commits into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 1 addition & 12 deletions crates/swc_ecma_codegen/src/lib.rs
Expand Up @@ -2481,23 +2481,12 @@ where
self.emit_leading_comments_of_span(node.span(), false)?;

srcmap!(node, true);

let is_last_rest = match node.props.last() {
Some(ObjectPatProp::Rest(..)) => true,
_ => false,
};
let format = if is_last_rest {
ListFormat::ObjectBindingPatternElements ^ ListFormat::AllowTrailingComma
} else {
ListFormat::ObjectBindingPatternElements
};
kdy1 marked this conversation as resolved.
Show resolved Hide resolved

punct!("{");

self.emit_list(
node.span(),
Some(&node.props),
format | ListFormat::CanSkipTrailingComma,
ListFormat::ObjectBindingPatternElements | ListFormat::CanSkipTrailingComma,
)?;

punct!("}");
Expand Down
14 changes: 14 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issue-6589/js/input.js
@@ -0,0 +1,14 @@
export function func({a, b, ...rest}) {
console.log(a,b, rest)
}

const other = { unknow: "foo" }

let foo = {
...other,
bar: "baz"
};
let bar = {
bar: "baz",
...other
};
14 changes: 14 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.js
@@ -0,0 +1,14 @@
export function func({ a , b , ...rest }) {
console.log(a, b, rest);
}
const other = {
unknow: "foo"
};
let foo = {
...other,
bar: "baz"
};
let bar = {
bar: "baz",
...other
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES2022 allows trailing comma here, but we don't have an option to force enable/disable it (or we can print it when it supported, but currently we don't use this logic), so I think we should solve it in the separated PR

};
@@ -0,0 +1 @@
export function func({a,b,...rest}){console.log(a,b,rest)}const other={unknow:"foo"};let foo={...other,bar:"baz"};let bar={bar:"baz",...other};
21 changes: 21 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/input.ts
@@ -0,0 +1,21 @@
export function func({
a,
b,
...rest
}: {
a: string,
b: string,
}) {
console.log(a,b, rest)
}

const other = { unknow: "foo" }

let foo = {
...other,
bar: "baz"
};
let bar = {
bar: "baz",
...other
};
17 changes: 17 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/output.ts
@@ -0,0 +1,17 @@
export function func({ a , b , ...rest }: {
a: string;
b: string;
}) {
console.log(a, b, rest);
}
const other = {
unknow: "foo"
};
let foo = {
...other,
bar: "baz"
};
let bar = {
bar: "baz",
...other
};