Skip to content

Commit

Permalink
fix: remove trailing comma in single type param in default export
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 1, 2023
1 parent c9ca198 commit 42d1c4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6063,10 +6063,17 @@ fn gen_type_parameters<'a>(node: TypeParamNode<'a>, context: &mut Context<'a>) -
if type_params.start() == node.start() {
// Use trailing commas for function expressions in a JSX file
// if the absence of one would lead to a parsing ambiguity.
let is_jsx_fn_expr = context.is_jsx() && (node.parent().kind() == NodeKind::ArrowExpr || node.parent().parent().unwrap().kind() == NodeKind::FnExpr);
let parent = node.parent();
let is_ambiguous_jsx_fn_expr = context.is_jsx()
&& (parent.kind() == NodeKind::ArrowExpr || parent.parent().unwrap().kind() == NodeKind::FnExpr)
// not ambiguous in a default export
&& !matches!(
parent.parent().and_then(|p| p.parent()).map(|p| p.kind()),
Some(NodeKind::ExportDefaultExpr | NodeKind::ExportDefaultDecl)
);
// Prevent "This syntax is reserved in files with the .mts or .cts extension." diagnostic.
let is_cts_mts_arrow_fn = matches!(context.media_type, MediaType::Cts | MediaType::Mts) && node.parent().kind() == NodeKind::ArrowExpr;
if is_jsx_fn_expr || is_cts_mts_arrow_fn {
let is_cts_mts_arrow_fn = matches!(context.media_type, MediaType::Cts | MediaType::Mts) && parent.kind() == NodeKind::ArrowExpr;
if is_ambiguous_jsx_fn_expr || is_cts_mts_arrow_fn {
let children = type_params.children();
// It is not ambiguous if there are multiple type parameters.
if children.len() == 1 && children[0].kind() == NodeKind::TsTypeParam {
Expand Down
12 changes: 12 additions & 0 deletions tests/specs/general/Jsx_TrailingCommas.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const Test3 = <div></div>;
function test<T,>() {
}

// or export default
export default function test<T,>() {
}
export default function<T,>() {
}

// generic constraints are not ambiguous either
const Test4 = <P extends R<S, T>,>() => {};
const Test5 = <P extends R<S, T>>() => {};
Expand All @@ -28,6 +34,12 @@ const Test3 = <div></div>;
function test<T>() {
}

// or export default
export default function test<T>() {
}
export default function<T>() {
}

// generic constraints are not ambiguous either
const Test4 = <P extends R<S, T>>() => {};
const Test5 = <P extends R<S, T>>() => {};

0 comments on commit 42d1c4c

Please sign in to comment.