From 3d0b652de2d9c2f4ba0b0749ddf3da93211ee7f5 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 9 Dec 2022 14:21:26 +0300 Subject: [PATCH] fix(ecma/codegen): don't print trailing coma for rest argument --- crates/swc_ecma_codegen/src/lib.rs | 13 +----------- .../tests/fixture/issue-6589/js/input.js | 14 +++++++++++++ .../tests/fixture/issue-6589/js/output.js | 14 +++++++++++++ .../tests/fixture/issue-6589/js/output.min.js | 1 + .../tests/fixture/issue-6589/ts/input.ts | 21 +++++++++++++++++++ .../tests/fixture/issue-6589/ts/output.ts | 17 +++++++++++++++ 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 crates/swc_ecma_codegen/tests/fixture/issue-6589/js/input.js create mode 100644 crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.js create mode 100644 crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.min.js create mode 100644 crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/input.ts create mode 100644 crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/output.ts diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 21b76e4d2134..4afd25362e66 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -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 - }; - punct!("{"); self.emit_list( node.span(), Some(&node.props), - format | ListFormat::CanSkipTrailingComma, + ListFormat::ObjectBindingPatternElements | ListFormat::CanSkipTrailingComma, )?; punct!("}"); diff --git a/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/input.js b/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/input.js new file mode 100644 index 000000000000..880d8e98c64f --- /dev/null +++ b/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 +}; diff --git a/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.js b/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.js new file mode 100644 index 000000000000..851cf1bb0cae --- /dev/null +++ b/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 +}; diff --git a/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.min.js b/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.min.js new file mode 100644 index 000000000000..839f23f76c4c --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.min.js @@ -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}; diff --git a/crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/input.ts b/crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/input.ts new file mode 100644 index 000000000000..b62046906a7e --- /dev/null +++ b/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 +}; diff --git a/crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/output.ts b/crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/output.ts new file mode 100644 index 000000000000..2e52b3d0e0c9 --- /dev/null +++ b/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 +};