Skip to content

Commit 7cbd06e

Browse files
committed
feat(formatter): support TSTypePredicate (#13742)
Do not know if the parser is wrong, or the description of `TSTypeAnnotation`: ``` pub struct TSTypeAnnotation<'a> { /// starts at the `:` token and ends at the end of the type annotation pub span: Span, /// The actual type in the annotation pub type_annotation: TSType<'a>, } ``` having this code: `tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/input.ts` ``` function asserts1 (value: unknown): asserts value is string {} function asserts2 (value: unknown): asserts value {} function asserts3 (value: unknown): asserts {} ``` there are 2 `TSTypeAnnotation`s here: <img width="928" height="912" alt="grafik" src="https://github.com/user-attachments/assets/b0d12446-7e6b-4e59-a426-8eee9d7f2bb0" />
1 parent a49d7cf commit 7cbd06e

File tree

4 files changed

+16
-252
lines changed

4 files changed

+16
-252
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,11 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSEnumMember<'a>> {
11761176

11771177
impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAnnotation<'a>> {
11781178
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1179-
write!(f, [":", space(), self.type_annotation()])
1179+
if matches!(self.parent, AstNodes::TSTypePredicate(_)) {
1180+
write!(f, [self.type_annotation()])
1181+
} else {
1182+
write!(f, [":", space(), self.type_annotation()])
1183+
}
11801184
}
11811185
}
11821186

@@ -1687,6 +1691,13 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSInterfaceHeritage<'a>> {
16871691

16881692
impl<'a> FormatWrite<'a> for AstNode<'a, TSTypePredicate<'a>> {
16891693
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1694+
if self.asserts() {
1695+
write!(f, ["asserts", space()])?;
1696+
}
1697+
write!(f, [self.parameter_name()])?;
1698+
if let Some(type_annotation) = self.type_annotation() {
1699+
write!(f, [space(), "is", space(), type_annotation])?;
1700+
}
16901701
Ok(())
16911702
}
16921703
}

tasks/coverage/snapshots/formatter_babel.snap

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

33
formatter_babel Summary:
44
AST Parsed : 2423/2423 (100.00%)
5-
Positive Passed: 2403/2423 (99.17%)
5+
Positive Passed: 2414/2423 (99.63%)
66
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js
77

88
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/regression/13750/input.js
@@ -11,28 +11,6 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2015
1111

1212
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/private-in/private-in-class-heritage/input.js
1313

14-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/loc-index-property/input.js
15-
16-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/loc-index-property-babel-7/input.js
17-
18-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/predicate-types/input.ts
19-
20-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/input.ts
21-
22-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/input.ts
23-
24-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts
25-
26-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts
27-
28-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts
29-
30-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/input.ts
31-
32-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/predicate-types/input.ts
33-
34-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/function/predicate-types/input.ts
35-
3614
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/conditional-infer/input.ts
3715

3816
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/conditional-infer-babel-7/input.ts

0 commit comments

Comments
 (0)