Skip to content

Commit aef64dc

Browse files
committed
fix(formatter): missing trailing ; operator in TSSignauture
1 parent e03ea5d commit aef64dc

File tree

4 files changed

+81
-107
lines changed

4 files changed

+81
-107
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,8 +1547,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSInterfaceDeclaration<'a>> {
15471547

15481548
impl<'a> FormatWrite<'a> for AstNode<'a, TSInterfaceBody<'a>> {
15491549
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1550-
let source_text = f.context().source_text();
1551-
f.join_nodes_with_soft_line().entries(self.body()).finish()
1550+
self.body().fmt(f)
15521551
}
15531552
}
15541553

@@ -1572,9 +1571,50 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSPropertySignature<'a>> {
15721571
}
15731572
}
15741573

1574+
struct FormatTSSignature<'a, 'b> {
1575+
last: bool,
1576+
signature: &'b AstNode<'a, TSSignature<'a>>,
1577+
}
1578+
1579+
impl GetSpan for FormatTSSignature<'_, '_> {
1580+
fn span(&self) -> Span {
1581+
self.signature.span()
1582+
}
1583+
}
1584+
1585+
impl<'a> Format<'a> for FormatTSSignature<'a, '_> {
1586+
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1587+
self.signature.fmt(f)?;
1588+
1589+
match f.options().semicolons {
1590+
Semicolons::Always => {
1591+
if self.last {
1592+
write!(f, [if_group_breaks(&text(";"))])?;
1593+
} else {
1594+
text(";").fmt(f)?;
1595+
}
1596+
}
1597+
Semicolons::AsNeeded => {
1598+
if !self.last {
1599+
write!(f, [if_group_fits_on_line(&text(";"))])?;
1600+
}
1601+
}
1602+
}
1603+
1604+
Ok(())
1605+
}
1606+
}
1607+
15751608
impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSSignature<'a>>> {
15761609
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1577-
f.join_nodes_with_soft_line().entries(self).finish()
1610+
let last_index = self.len().saturating_sub(1);
1611+
f.join_nodes_with_soft_line()
1612+
.entries(
1613+
self.iter()
1614+
.enumerate()
1615+
.map(|(i, signature)| FormatTSSignature { last: i == last_index, signature }),
1616+
)
1617+
.finish()
15781618
}
15791619
}
15801620

@@ -1584,16 +1624,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSIndexSignature<'a>> {
15841624
write!(f, ["readonly", space()])?;
15851625
}
15861626
// TODO: parameters only have one element for now.
1587-
write!(
1588-
f,
1589-
[
1590-
"[",
1591-
self.parameters().first().unwrap(),
1592-
"]",
1593-
self.type_annotation(),
1594-
OptionalSemicolon
1595-
]
1596-
)
1627+
write!(f, ["[", self.parameters().first().unwrap(), "]", self.type_annotation(),])
15971628
}
15981629
}
15991630

@@ -1610,7 +1641,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSCallSignatureDeclaration<'a>> {
16101641
if let Some(return_type) = &self.return_type() {
16111642
write!(f, return_type)?;
16121643
}
1613-
write!(f, OptionalSemicolon)
1644+
Ok(())
16141645
}
16151646
}
16161647

@@ -1656,7 +1687,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
16561687
if let Some(return_type) = &self.return_type() {
16571688
write!(f, return_type)?;
16581689
}
1659-
write!(f, OptionalSemicolon)
1690+
Ok(())
16601691
}
16611692
}
16621693

@@ -1670,7 +1701,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructSignatureDeclaration<'a>> {
16701701
if let Some(return_type) = self.return_type() {
16711702
write!(f, return_type)?;
16721703
}
1673-
write!(f, OptionalSemicolon)
1704+
Ok(())
16741705
}
16751706
}
16761707

crates/oxc_formatter/src/write/semicolon.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl<'a, 'b> ClassPropertySemicolon<'a, 'b> {
7373
ClassElement::AccessorProperty(def) => {
7474
def.computed && !(def.accessibility.is_some() || def.r#static || def.r#override)
7575
}
76+
ClassElement::TSIndexSignature(_) => true,
7677
_ => false,
7778
}
7879
}
@@ -82,9 +83,7 @@ impl<'a> Format<'a> for ClassPropertySemicolon<'a, '_> {
8283
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
8384
if matches!(
8485
self.element.as_ref(),
85-
ClassElement::StaticBlock(_)
86-
| ClassElement::MethodDefinition(_)
87-
| ClassElement::TSIndexSignature(_)
86+
ClassElement::StaticBlock(_) | ClassElement::MethodDefinition(_)
8887
) {
8988
return Ok(());
9089
}

tasks/coverage/snapshots/formatter_typescript.snap

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

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8751/8816 (99.26%)
6-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/APISample_jsdoc.ts
7-
Expected `]` but found `:`
5+
Positive Passed: 8762/8816 (99.39%)
86
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/aliasInstantiationExpressionGenericIntersectionNoCrash1.ts
97
Unexpected token
108
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
@@ -27,8 +25,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/contextualTypeBa
2725
Unexpected token
2826
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts
2927

30-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPartialReuseComputedProperty.ts
31-
Unexpected token
3228
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPromise.ts
3329
Expected a semicolon or an implicit semicolon after a statement, but found none
3430
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitRecursiveConditionalAliasPreserved.ts
@@ -45,8 +41,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexp
4541

4642
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceImplicitImportJSXNamespace.tsx
4743

48-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/keyRemappingKeyofResult.ts
49-
Unexpected token
5044
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/multiSignatureTypeInference.ts
5145
Unexpected token
5246
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/narrowingByTypeofInSwitch.ts
@@ -57,8 +51,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/ramdaToolsNoInfi
5751
Unexpected token
5852
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/readonlyAssignmentInSubclassOfClassExpression.ts
5953
Expected `{` but found `as`
60-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reducibleIndexedAccessTypes.ts
61-
Expected `]` but found `:`
6254
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/referenceSatisfiesExpression.ts
6355
Expected a semicolon or an implicit semicolon after a statement, but found none
6456
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reverseMappedTypeInferenceSameSource1.ts
@@ -75,8 +67,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/superAccessCaste
7567
Expected a semicolon or an implicit semicolon after a statement, but found none
7668
Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts
7769
78-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeName1.ts
79-
Expected `]` but found `:`
8070
Mismatch: tasks/coverage/typescript/tests/cases/compiler/typePredicateFreshLiteralWidening.ts
8171
8272
Mismatch: tasks/coverage/typescript/tests/cases/compiler/unionSignaturesWithThisParameter.ts
@@ -115,21 +105,9 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interface
115105
116106
Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts
117107
118-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts
119-
Expected `]` but found `:`
120108
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
121109
Expected `,` but found `(`
122110
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/literal/templateLiteralTypes4.ts
123111
124-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeModifiers.ts
125-
Expected `]` but found `:`
126-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/objectTypeLiteral/propertySignatures/propertyNamesOfReservedWords.ts
127-
'static' modifier cannot appear on a type member.'static' modifier cannot appear on a type member.
128-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/rest/genericObjectRest.ts
129-
Unexpected token
130112
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts
131113
132-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts
133-
Unexpected token
134-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsDeclarations.ts
135-
Unexpected token

0 commit comments

Comments
 (0)