Skip to content

Commit 17d3623

Browse files
authored
fix: remove trailing comma for single type param in default export (#589)
1 parent c9ca198 commit 17d3623

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/generation/generate.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6063,10 +6063,17 @@ fn gen_type_parameters<'a>(node: TypeParamNode<'a>, context: &mut Context<'a>) -
60636063
if type_params.start() == node.start() {
60646064
// Use trailing commas for function expressions in a JSX file
60656065
// if the absence of one would lead to a parsing ambiguity.
6066-
let is_jsx_fn_expr = context.is_jsx() && (node.parent().kind() == NodeKind::ArrowExpr || node.parent().parent().unwrap().kind() == NodeKind::FnExpr);
6066+
let parent = node.parent();
6067+
let is_ambiguous_jsx_fn_expr = context.is_jsx()
6068+
&& (parent.kind() == NodeKind::ArrowExpr || parent.parent().unwrap().kind() == NodeKind::FnExpr)
6069+
// not ambiguous in a default export
6070+
&& !matches!(
6071+
parent.parent().and_then(|p| p.parent()).map(|p| p.kind()),
6072+
Some(NodeKind::ExportDefaultExpr | NodeKind::ExportDefaultDecl)
6073+
);
60676074
// Prevent "This syntax is reserved in files with the .mts or .cts extension." diagnostic.
6068-
let is_cts_mts_arrow_fn = matches!(context.media_type, MediaType::Cts | MediaType::Mts) && node.parent().kind() == NodeKind::ArrowExpr;
6069-
if is_jsx_fn_expr || is_cts_mts_arrow_fn {
6075+
let is_cts_mts_arrow_fn = matches!(context.media_type, MediaType::Cts | MediaType::Mts) && parent.kind() == NodeKind::ArrowExpr;
6076+
if is_ambiguous_jsx_fn_expr || is_cts_mts_arrow_fn {
60706077
let children = type_params.children();
60716078
// It is not ambiguous if there are multiple type parameters.
60726079
if children.len() == 1 && children[0].kind() == NodeKind::TsTypeParam {

tests/specs/general/Jsx_TrailingCommas.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const Test3 = <div></div>;
1515
function test<T,>() {
1616
}
1717

18+
// or export default
19+
export default function test<T,>() {
20+
}
21+
export default function<T,>() {
22+
}
23+
1824
// generic constraints are not ambiguous either
1925
const Test4 = <P extends R<S, T>,>() => {};
2026
const Test5 = <P extends R<S, T>>() => {};
@@ -28,6 +34,12 @@ const Test3 = <div></div>;
2834
function test<T>() {
2935
}
3036

37+
// or export default
38+
export default function test<T>() {
39+
}
40+
export default function<T>() {
41+
}
42+
3143
// generic constraints are not ambiguous either
3244
const Test4 = <P extends R<S, T>>() => {};
3345
const Test5 = <P extends R<S, T>>() => {};

0 commit comments

Comments
 (0)