Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions crates/oxc_formatter/src/print/type_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_span::FileExtension;

use crate::{
ast_nodes::{AstNode, AstNodes},
Expand Down Expand Up @@ -71,10 +72,17 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSTypeParameter<'a>>> {
// to avoid any ambiguity with JSX elements.
// Thus, we have to add a trailing comma when there is a single type parameter.
// The comma can be omitted in the case where the single parameter has a constraint,
// i.i. an `extends` clause.
// i.e. an `extends` clause.
//
// This applies to:
// - JSX files (`.jsx`, `.tsx`) where `<T>` could be parsed as a JSX element
// - `.mts` and `.cts` files which disallow JSX-like syntax without disambiguation
let trailing_separator = if self.len() == 1
// This only concern sources that allow JSX or a restricted standard variant.
&& f.context().source_type().is_jsx()
&& (f.context().source_type().is_jsx()
|| matches!(
f.context().source_type().extension(),
Some(FileExtension::Mts | FileExtension::Cts)
))
&& matches!(self.grand_parent(), AstNodes::ArrowFunctionExpression(_))
// Ignore Type parameter with an `extends` clause or a default type.
&& !self.first().is_some_and(|t| t.constraint().is_some() || t.default().is_some())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Single type parameter in arrow function requires trailing comma in .cts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// Single type parameter in arrow function requires trailing comma in .cts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// Single type parameter in arrow function requires trailing comma in .cts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

-------------------
{ printWidth: 100 }
-------------------
// Single type parameter in arrow function requires trailing comma in .cts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

===================== End =====================
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Single type parameter in arrow function requires trailing comma in .mts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// Single type parameter in arrow function requires trailing comma in .mts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// Single type parameter in arrow function requires trailing comma in .mts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

-------------------
{ printWidth: 100 }
-------------------
// Single type parameter in arrow function requires trailing comma in .mts files
const fn = <T,>() => {};

// With constraint - no trailing comma needed
const fn2 = <T extends string>() => {};

// Multiple type parameters - no special handling needed
const fn3 = <T, U>() => {};

===================== End =====================