Skip to content

Commit f6dc981

Browse files
committed
feat(formatter): implement formatting for TSTupletype (#14019)
1 parent c2f7459 commit f6dc981

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod sequence_expression;
2626
mod switch_statement;
2727
mod template;
2828
mod try_statement;
29+
mod tuple_type;
2930
mod type_parameters;
3031
mod union_type;
3132
mod utils;
@@ -45,7 +46,7 @@ use oxc_ast::{AstKind, ast::*};
4546
use oxc_span::GetSpan;
4647

4748
use crate::{
48-
best_fitting, format_args,
49+
Expand, best_fitting, format_args,
4950
formatter::{
5051
Buffer, Format, FormatResult, Formatter,
5152
prelude::*,
@@ -1246,19 +1247,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSIndexedAccessType<'a>> {
12461247
}
12471248
}
12481249

1249-
impl<'a> FormatWrite<'a> for AstNode<'a, TSTupleType<'a>> {
1250-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1251-
write!(f, "[")?;
1252-
for (i, ty) in self.element_types().iter().enumerate() {
1253-
if i != 0 {
1254-
write!(f, [",", space()])?;
1255-
}
1256-
write!(f, ty)?;
1257-
}
1258-
write!(f, "]")
1259-
}
1260-
}
1261-
12621250
impl<'a> FormatWrite<'a> for AstNode<'a, TSNamedTupleMember<'a>> {
12631251
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
12641252
write!(f, self.label())?;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use oxc_allocator::Vec;
2+
use oxc_ast::ast::*;
3+
4+
use crate::{
5+
Expand, Format, FormatResult, FormatTrailingCommas,
6+
formatter::{Formatter, prelude::*, trivia::format_dangling_comments},
7+
generated::ast_nodes::AstNode,
8+
write,
9+
};
10+
11+
use super::FormatWrite;
12+
13+
impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSTupleElement<'a>>> {
14+
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
15+
let trailing_separator = FormatTrailingCommas::ES5.trailing_separator(f.options());
16+
f.join_nodes_with_soft_line()
17+
.entries_with_trailing_separator(self.iter(), ",", trailing_separator)
18+
.finish()
19+
}
20+
}
21+
22+
impl<'a> FormatWrite<'a> for AstNode<'a, TSTupleType<'a>> {
23+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
24+
write!(f, "[")?;
25+
26+
let element_types = self.element_types();
27+
if element_types.is_empty() {
28+
write!(f, [format_dangling_comments(self.span).with_block_indent()])?;
29+
} else {
30+
let should_expand = f.options().expand == Expand::Always;
31+
32+
write!(f, [group(&soft_block_indent(&element_types)).should_expand(should_expand)])?;
33+
}
34+
35+
write!(f, "]")
36+
}
37+
}

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8791/8816 (99.72%)
5+
Positive Passed: 8792/8816 (99.73%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
@@ -51,5 +51,3 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interface
5151

5252
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
5353

54-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts
55-

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 467/573 (81.50%)
1+
ts compatibility: 473/573 (82.55%)
22

33
# Failed
44

@@ -14,7 +14,6 @@ ts compatibility: 467/573 (81.50%)
1414
| typescript/angular-component-examples/test.component.ts | 💥💥 | 41.18% |
1515
| typescript/arrow/16067.ts | 💥💥 | 95.92% |
1616
| typescript/arrow/comments.ts | 💥✨ | 44.44% |
17-
| typescript/as/as.ts | 💥 | 96.18% |
1817
| typescript/as/expression-statement.ts | 💥 | 75.00% |
1918
| typescript/assignment/issue-10848.tsx | 💥 | 63.86% |
2019
| typescript/assignment/issue-10850.ts | 💥 | 50.00% |
@@ -91,14 +90,10 @@ ts compatibility: 467/573 (81.50%)
9190
| typescript/prettier-ignore/prettier-ignore-nested-unions.ts | 💥 | 29.17% |
9291
| typescript/prettier-ignore/prettier-ignore-parenthesized-type.ts | 💥 | 0.00% |
9392
| typescript/rest-type/complex.ts | 💥 | 0.00% |
94-
| typescript/rest-type/infer-type.ts | 💥 | 64.00% |
9593
| typescript/satisfies-operators/expression-statement.ts | 💥💥 | 78.38% |
9694
| typescript/satisfies-operators/lhs.ts | 💥✨ | 35.00% |
97-
| typescript/tuple/dangling-comments.ts | 💥💥💥 | 21.28% |
9895
| typescript/tuple/trailing-comma-for-empty-tuples.ts | 💥💥💥 | 16.67% |
99-
| typescript/tuple/trailing-comma-trailing-rest.ts | 💥💥💥 | 0.00% |
100-
| typescript/tuple/trailing-comma.ts | 💥💥💥 | 61.54% |
101-
| typescript/tuple/tuple.ts | 💥💥💥 | 0.00% |
96+
| typescript/tuple/tuple.ts | 💥💥💥 | 87.50% |
10297
| typescript/type-alias/conditional.ts | 💥 | 38.10% |
10398
| typescript/type-alias/issue-9874.ts | 💥 | 0.00% |
10499
| typescript/type-arguments-bit-shift-left-like/3.ts | 💥 | 0.00% |
@@ -108,5 +103,4 @@ ts compatibility: 467/573 (81.50%)
108103
| typescript/union/inlining.ts | 💥 | 79.70% |
109104
| typescript/union/union-parens.ts | 💥 | 92.59% |
110105
| typescript/union/consistent-with-flow/prettier-ignore.ts | 💥 | 38.30% |
111-
| typescript/union/consistent-with-flow/within-tuple.ts | 💥 | 17.50% |
112106
| typescript/union/single-type/single-type.ts | 💥 | 66.67% |

0 commit comments

Comments
 (0)