Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ecma/codegen): don't print trailing coma for rest argument
- Loading branch information
1 parent
66b5282
commit f9d831e
Showing
7 changed files
with
123 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use swc_common::SourceMapper; | ||
use swc_ecma_ast::SourceMapperExt; | ||
|
||
use crate::{text_writer::WriteJs, Emitter}; | ||
|
||
impl<'a, 'b, W, S: SourceMapper> Emitter<'a, W, S> | ||
where | ||
W: WriteJs, | ||
S: SourceMapperExt, | ||
{ | ||
/// Original context is restored when returned guard is dropped. | ||
#[inline] | ||
pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'b, W, S> { | ||
let orig_ctx = self.ctx; | ||
|
||
self.ctx = ctx; | ||
|
||
WithCtx { | ||
orig_ctx, | ||
inner: self, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Copy)] | ||
pub(crate) struct Ctx { | ||
pub in_function_params: bool, | ||
} | ||
|
||
pub(super) struct WithCtx<'w, I: 'w + WriteJs, S: 'w + SourceMapper + SourceMapperExt> { | ||
inner: &'w mut Emitter<'w, I, S>, | ||
orig_ctx: Ctx, | ||
} | ||
|
||
impl<'w, I: 'w + WriteJs, S: 'w + SourceMapper + SourceMapperExt> Deref for WithCtx<'w, I, S> { | ||
type Target = Emitter<'w, I, S>; | ||
|
||
fn deref(&self) -> &Emitter<'w, I, S> { | ||
self.inner | ||
} | ||
} | ||
|
||
impl<'w, I: 'w + WriteJs, S: 'w + SourceMapper + SourceMapperExt> DerefMut for WithCtx<'w, I, S> { | ||
fn deref_mut(&mut self) -> &mut Emitter<'w, I, S> { | ||
self.inner | ||
} | ||
} | ||
|
||
impl<'w, I: 'w + WriteJs, S: 'w + SourceMapper + SourceMapperExt> Drop for WithCtx<'w, I, S> { | ||
fn drop(&mut self) { | ||
self.inner.ctx = self.orig_ctx; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
crates/swc_ecma_codegen/tests/fixture/issue-6589/js/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
14
crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
1 change: 1 addition & 0 deletions
1
crates/swc_ecma_codegen/tests/fixture/issue-6589/js/output.min.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
21
crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
17
crates/swc_ecma_codegen/tests/fixture/issue-6589/ts/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |