Skip to content

Commit 8659498

Browse files
authored
fix(formatter): should parenthesize TSInferType when wrapped with TSArrayType (#13756)
1 parent 8c19b18 commit 8659498

File tree

7 files changed

+22
-15
lines changed

7 files changed

+22
-15
lines changed

crates/oxc_formatter/src/generated/format.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,9 +4461,19 @@ impl<'a> Format<'a> for AstNode<'a, TSTypeLiteral<'a>> {
44614461
impl<'a> Format<'a> for AstNode<'a, TSInferType<'a>> {
44624462
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
44634463
let is_suppressed = f.comments().is_suppressed(self.span().start);
4464+
if !is_suppressed && format_type_cast_comment_node(self, false, f)? {
4465+
return Ok(());
4466+
}
44644467
self.format_leading_comments(f)?;
4468+
let needs_parentheses = self.needs_parentheses(f);
4469+
if needs_parentheses {
4470+
"(".fmt(f)?;
4471+
}
44654472
let result =
44664473
if is_suppressed { FormatSuppressedNode(self.span()).fmt(f) } else { self.write(f) };
4474+
if needs_parentheses {
4475+
")".fmt(f)?;
4476+
}
44674477
self.format_trailing_comments(f)?;
44684478
result
44694479
}

crates/oxc_formatter/src/parentheses/ts_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSType<'a>> {
1212
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
1313
match self.as_ast_nodes() {
1414
AstNodes::TSFunctionType(it) => it.needs_parentheses(f),
15+
AstNodes::TSInferType(it) => it.needs_parentheses(f),
1516
_ => {
1617
// TODO: incomplete
1718
false
@@ -26,3 +27,9 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSFunctionType<'a>> {
2627
matches!(self.parent, AstNodes::TSUnionType(_))
2728
}
2829
}
30+
31+
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSInferType<'a>> {
32+
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
33+
matches!(self.parent, AstNodes::TSArrayType(_))
34+
}
35+
}

tasks/ast_tools/src/generators/formatter/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const AST_NODE_WITHOUT_PRINTING_COMMENTS_LIST: &[&str] = &[
3737
"ExportSpecifier",
3838
];
3939

40-
const AST_NODE_NEEDS_PARENTHESES: &[&str] = &["TSTypeAssertion"];
40+
const AST_NODE_NEEDS_PARENTHESES: &[&str] = &["TSTypeAssertion", "TSInferType"];
4141

4242
const NEEDS_IMPLEMENTING_FMT_WITH_OPTIONS: phf::Map<&'static str, &'static str> = phf::phf_map! {
4343
"ArrowFunctionExpression" => "FormatJsArrowFunctionExpressionOptions",

tasks/coverage/snapshots/formatter_babel.snap

Lines changed: 1 addition & 7 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: 2414/2423 (99.63%)
5+
Positive Passed: 2417/2423 (99.75%)
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,12 +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/typescript/types/conditional-infer/input.ts
15-
16-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/conditional-infer-babel-7/input.ts
17-
18-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/conditional-infer-parenthesized-expressions-true/input.ts
19-
2014
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/infer-with-constraints/input.ts
2115

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

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 5 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: 8727/8816 (98.99%)
5+
Positive Passed: 8729/8816 (99.01%)
66
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/APISample_jsdoc.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/aliasInstantiationExpressionGenericIntersectionNoCrash1.ts
@@ -29,8 +29,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/contextualTypeBa
2929

3030
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts
3131

32-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitNestedGenerics.ts
33-
3432
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPartialReuseComputedProperty.ts
3533

3634
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPromise.ts
@@ -47,8 +45,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericRestTypes
4745

4846
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/identityRelationNeverTypes.ts
4947

50-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/inferTypesWithFixedTupleExtendsAtVariadicPosition.ts
51-
5248
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/infiniteConstraints.ts
5349

5450
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/intersectionConstraintReduction.ts

tasks/coverage/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() {
2929
"parser" => args.run_parser(),
3030
"semantic" => args.run_semantic(),
3131
"codegen" => args.run_codegen(),
32-
// "formatter" => args.run_formatter(),
32+
"formatter" => args.run_formatter(),
3333
"transformer" => args.run_transformer(),
3434
"transpiler" => args.run_transpiler(),
3535
"minifier" => args.run_minifier(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ ts compatibility: 349/573 (60.91%)
179179
| typescript/prettier-ignore/prettier-ignore-parenthesized-type.ts | 💥 | 0.00% |
180180
| typescript/quote-props/types.ts | 💥💥💥 | 55.56% |
181181
| typescript/rest-type/complex.ts | 💥 | 0.00% |
182-
| typescript/rest-type/infer-type.ts | 💥 | 72.00% |
182+
| typescript/rest-type/infer-type.ts | 💥 | 80.00% |
183183
| typescript/satisfies-operators/argument-expansion.ts | 💥✨ | 46.77% |
184184
| typescript/satisfies-operators/assignment.ts | 💥💥 | 72.73% |
185185
| typescript/satisfies-operators/basic.ts | 💥✨ | 45.00% |

0 commit comments

Comments
 (0)