fix(formatter): break index signature brackets (#25296)

Fixes #25286
This commit is contained in:
leaysgur committed 2026-08-05 04:25:23 +00:00
1 parent 495b02483d
commit 4e6f3f1356
3 files changed
+124 -1

No files matched your search

+1 -1
View File
@@ -204,7 +204,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSIndexSignature<'a>> {
f,
[
"[",
self.parameter(),
group(&soft_block_indent(self.parameter())),
"]",
self.type_annotation(),
is_class.then_some(OptionalSemicolon)
@@ -0,0 +1,27 @@
// issue #25286: a leading comment on the index signature parameter breaks the
// bracket group, keeping `[` on its own line with the comment indented below it
type Config = {
[
// The key name must match the name of the widgets/ subdirectory
// containing the widget on disk, e.g. 'widgets/foo' should be
// called 'foo' here.
widgetName: string
]: WidgetConfig;
};
// block comment fits on one line, group stays flat
interface D {
[/* inline */ k: string]: number;
}
// no break-forcing comment: collapses back to one line
type E = {
[
k: string
]: number;
};
// no comment, but the parameter overflows printWidth: same bracket break
type F = {
[veryLongParameterNameThatGoesOnAndOnForever: SomeVeryLongKeyTypeNameHereToo]: SomeVeryLongValueTypeName;
};
@@ -0,0 +1,96 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// issue #25286: a leading comment on the index signature parameter breaks the
// bracket group, keeping `[` on its own line with the comment indented below it
type Config = {
[
// The key name must match the name of the widgets/ subdirectory
// containing the widget on disk, e.g. 'widgets/foo' should be
// called 'foo' here.
widgetName: string
]: WidgetConfig;
};
// block comment fits on one line, group stays flat
interface D {
[/* inline */ k: string]: number;
}
// no break-forcing comment: collapses back to one line
type E = {
[
k: string
]: number;
};
// no comment, but the parameter overflows printWidth: same bracket break
type F = {
[veryLongParameterNameThatGoesOnAndOnForever: SomeVeryLongKeyTypeNameHereToo]: SomeVeryLongValueTypeName;
};
==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// issue #25286: a leading comment on the index signature parameter breaks the
// bracket group, keeping `[` on its own line with the comment indented below it
type Config = {
[
// The key name must match the name of the widgets/ subdirectory
// containing the widget on disk, e.g. 'widgets/foo' should be
// called 'foo' here.
widgetName: string
]: WidgetConfig;
};
// block comment fits on one line, group stays flat
interface D {
[/* inline */ k: string]: number;
}
// no break-forcing comment: collapses back to one line
type E = {
[k: string]: number;
};
// no comment, but the parameter overflows printWidth: same bracket break
type F = {
[
veryLongParameterNameThatGoesOnAndOnForever: SomeVeryLongKeyTypeNameHereToo
]: SomeVeryLongValueTypeName;
};
-------------------
{ printWidth: 100 }
-------------------
// issue #25286: a leading comment on the index signature parameter breaks the
// bracket group, keeping `[` on its own line with the comment indented below it
type Config = {
[
// The key name must match the name of the widgets/ subdirectory
// containing the widget on disk, e.g. 'widgets/foo' should be
// called 'foo' here.
widgetName: string
]: WidgetConfig;
};
// block comment fits on one line, group stays flat
interface D {
[/* inline */ k: string]: number;
}
// no break-forcing comment: collapses back to one line
type E = {
[k: string]: number;
};
// no comment, but the parameter overflows printWidth: same bracket break
type F = {
[
veryLongParameterNameThatGoesOnAndOnForever: SomeVeryLongKeyTypeNameHereToo
]: SomeVeryLongValueTypeName;
};
===================== End =====================