Skip to content
Merged
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
63 changes: 47 additions & 16 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSInterfaceDeclaration<'a>> {

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

Expand All @@ -1572,9 +1571,50 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSPropertySignature<'a>> {
}
}

struct FormatTSSignature<'a, 'b> {
last: bool,
signature: &'b AstNode<'a, TSSignature<'a>>,
}

impl GetSpan for FormatTSSignature<'_, '_> {
fn span(&self) -> Span {
self.signature.span()
}
}

impl<'a> Format<'a> for FormatTSSignature<'a, '_> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
self.signature.fmt(f)?;

match f.options().semicolons {
Semicolons::Always => {
if self.last {
write!(f, [if_group_breaks(&text(";"))])?;
} else {
text(";").fmt(f)?;
}
}
Semicolons::AsNeeded => {
if !self.last {
write!(f, [if_group_fits_on_line(&text(";"))])?;
}
}
}

Ok(())
}
}

impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSSignature<'a>>> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
f.join_nodes_with_soft_line().entries(self).finish()
let last_index = self.len().saturating_sub(1);
f.join_nodes_with_soft_line()
.entries(
self.iter()
.enumerate()
.map(|(i, signature)| FormatTSSignature { last: i == last_index, signature }),
)
.finish()
}
}

Expand All @@ -1584,16 +1624,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSIndexSignature<'a>> {
write!(f, ["readonly", space()])?;
}
// TODO: parameters only have one element for now.
write!(
f,
[
"[",
self.parameters().first().unwrap(),
"]",
self.type_annotation(),
OptionalSemicolon
]
)
write!(f, ["[", self.parameters().first().unwrap(), "]", self.type_annotation(),])
}
}

Expand All @@ -1610,7 +1641,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSCallSignatureDeclaration<'a>> {
if let Some(return_type) = &self.return_type() {
write!(f, return_type)?;
}
write!(f, OptionalSemicolon)
Ok(())
}
}

Expand Down Expand Up @@ -1656,7 +1687,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
if let Some(return_type) = &self.return_type() {
write!(f, return_type)?;
}
write!(f, OptionalSemicolon)
Ok(())
}
}

Expand All @@ -1670,7 +1701,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructSignatureDeclaration<'a>> {
if let Some(return_type) = self.return_type() {
write!(f, return_type)?;
}
write!(f, OptionalSemicolon)
Ok(())
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_formatter/src/write/semicolon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl<'a, 'b> ClassPropertySemicolon<'a, 'b> {
ClassElement::AccessorProperty(def) => {
def.computed && !(def.accessibility.is_some() || def.r#static || def.r#override)
}
ClassElement::TSIndexSignature(_) => true,
_ => false,
}
}
Expand All @@ -82,9 +83,7 @@ impl<'a> Format<'a> for ClassPropertySemicolon<'a, '_> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
if matches!(
self.element.as_ref(),
ClassElement::StaticBlock(_)
| ClassElement::MethodDefinition(_)
| ClassElement::TSIndexSignature(_)
ClassElement::StaticBlock(_) | ClassElement::MethodDefinition(_)
) {
return Ok(());
}
Expand Down
24 changes: 1 addition & 23 deletions tasks/coverage/snapshots/formatter_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ commit: 261630d6

formatter_typescript Summary:
AST Parsed : 8816/8816 (100.00%)
Positive Passed: 8763/8816 (99.40%)
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/APISample_jsdoc.ts
Expected `]` but found `:`
Positive Passed: 8774/8816 (99.52%)
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
Expand All @@ -21,8 +19,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/complexNarrowingWithAny

Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPartialReuseComputedProperty.ts
Unexpected token
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPromise.ts
Expected a semicolon or an implicit semicolon after a statement, but found none
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitRecursiveConditionalAliasPreserved.ts
Expand All @@ -37,16 +33,12 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexp

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

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/keyRemappingKeyofResult.ts
Unexpected token
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/narrowingByTypeofInSwitch.ts
Expected a semicolon or an implicit semicolon after a statement, but found none
Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/readonlyAssignmentInSubclassOfClassExpression.ts
Expected `{` but found `as`
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reducibleIndexedAccessTypes.ts
Expected `]` but found `:`
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reverseMappedTypeInferenceSameSource1.ts
An implementation cannot be declared in ambient contexts.
Mismatch: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts
Expand All @@ -59,8 +51,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/superAccessCaste
Expected a semicolon or an implicit semicolon after a statement, but found none
Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeName1.ts
Expected `]` but found `:`
Mismatch: tasks/coverage/typescript/tests/cases/compiler/unionSignaturesWithThisParameter.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/overrideInterfaceProperty.ts
Expand Down Expand Up @@ -93,19 +83,7 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interface

Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts
Expected `]` but found `:`
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
Expected `,` but found `(`
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeModifiers.ts
Expected `]` but found `:`
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/objectTypeLiteral/propertySignatures/propertyNamesOfReservedWords.ts
'static' modifier cannot appear on a type member.'static' modifier cannot appear on a type member.
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/rest/genericObjectRest.ts
Unexpected token
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts
Unexpected token
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsDeclarations.ts
Unexpected token
Loading
Loading