-
-
Notifications
You must be signed in to change notification settings - Fork 794
fix(formatter): align conditional type formatting with Prettier #8225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: a9b0a3f The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughAdds a patch-level changeset and implements formatting logic so conditional TypeScript types on the right side of assignment-like expressions break consistently. Refactors Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/biome_js_formatter/src/utils/assignment_like.rs (1)
944-950: Consider expanding type coverage.Whilst the current implementation handles the common cases (reference types with type arguments and function types with type parameters), other TypeScript type constructs can also involve generics (e.g.,
TsConditionalType,TsInferType,TsMappedType). However, for the specific use case of checking conditional type operands, this might be sufficient.If you'd like to verify whether Prettier handles additional type variants, I can search for the specific logic in their implementation.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (2)
.changeset/honest-eagles-learn.md(1 hunks)crates/biome_js_formatter/src/utils/assignment_like.rs(2 hunks)
🔇 Additional comments (4)
.changeset/honest-eagles-learn.md (1)
1-5: LGTM!The changeset is properly formatted and accurately describes the fix.
crates/biome_js_formatter/src/utils/assignment_like.rs (3)
19-21: LGTM!The import additions are appropriate for the new conditional type handling.
934-936: LGTM!The integration of conditional type handling into the match expression is clean and correctly placed.
952-959: LGTM!The logic correctly implements Prettier's behaviour: breaking after the operator when either the
check_typeorextends_typeinvolves generics. Clean and straightforward implementation.
dyc3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add some of our own snapshot tests too. IIRC the prettier snapshots are a bit out of date.
CodSpeed Performance ReportMerging #8225 will not alter performanceComparing Summary
Footnotes
|
ac098ab to
de88dff
Compare
I added a few test cases and checked the Prettier snapshots. The relevant snapshots have not changed since the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (3)
.changeset/honest-eagles-learn.md(1 hunks)crates/biome_js_formatter/src/utils/assignment_like.rs(2 hunks)crates/biome_js_formatter/tests/specs/ts/type/conditional.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- .changeset/honest-eagles-learn.md
🧰 Additional context used
🧬 Code graph analysis (1)
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (1)
crates/biome_js_analyze/tests/specs/style/useImportType/invalid-inline-type.ts (4)
T5(9-9)T6(9-9)T7(13-13)T8(13-13)
🔇 Additional comments (2)
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (1)
13-31: Echo-based cases nicely exercise generic conditional typesThe updated T4–T8 snapshots (with
Echo<T>and inline comments) give good coverage for generic vs non-generic conditional types and should catch regressions in the new break-after-operator logic; nothing to change from my side.crates/biome_js_formatter/src/utils/assignment_like.rs (1)
19-21: TS imports wired up correctlyThe added
TsConditionalType/ TS property member imports line up with the usages further down inAnyJsAssignmentLikeand the new helpers; no issues here.
de88dff to
3714818
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/biome_js_formatter/src/utils/assignment_like.rs (1)
945-951: Consider adding a brief doc comment for maintainability.The
is_genericheuristic correctly identifies reference types with type arguments and function types with type parameters, matching Prettier's logic. However, a brief comment explaining the intentional simplicity (e.g., "Matches Prettier's heuristic for detecting generic types") would aid future maintainers.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (3)
.changeset/honest-eagles-learn.md(1 hunks)crates/biome_js_formatter/src/utils/assignment_like.rs(2 hunks)crates/biome_js_formatter/tests/specs/ts/type/conditional.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
- GitHub Check: Bench (biome_js_analyze)
- GitHub Check: Bench (biome_js_parser)
- GitHub Check: Bench (biome_js_formatter)
- GitHub Check: autofix
- GitHub Check: Documentation
- GitHub Check: Lint project (depot-ubuntu-24.04-arm-16)
- GitHub Check: Lint project (depot-windows-2022)
- GitHub Check: Test (depot-windows-2022-16)
- GitHub Check: Check Dependencies
- GitHub Check: Test Node.js API
- GitHub Check: Test (depot-ubuntu-24.04-arm-16)
- GitHub Check: End-to-end tests
- GitHub Check: lint
🔇 Additional comments (5)
crates/biome_js_formatter/src/utils/assignment_like.rs (3)
19-21: LGTM!The import additions for
TsConditionalTypeand related TypeScript types are appropriate for the new conditional type handling logic.
934-937: LGTM!The conditional type handling correctly combines the leading comment check with the generic heuristic, preserving prior formatting behaviour whilst adding the new generic-based breaking logic. The pattern properly matches the
JsInitializerClauseapproach above.
953-960: LGTM!The helper correctly checks whether the conditional type's
check_typeorextends_typeinvolves generics, aligning with Prettier's breaking behaviour as described in the PR objectives.crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (1)
19-43: LGTM!The new test cases provide solid coverage of conditional type formatting with generics (
Echo<string>) and leading comments. TheEcho<T>identity type is a sensible test fixture..changeset/honest-eagles-learn.md (1)
1-5: LGTM!The changeset entry properly documents the patch-level fix and references the relevant issue.
0084871 to
f67198f
Compare
f67198f to
f740a75
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
.changeset/honest-eagles-learn.md (1)
1-22: Past review feedback addressed.The code snippet requested in the previous review has been successfully added. Well done!
🧹 Nitpick comments (1)
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (1)
19-22: Consider clarifying T5's purpose.T5 appears to test a conditional type without generics but with a comment. Since the PR focuses on generic-conditional formatting, it might help future maintainers to add a brief inline comment explaining why this case is included (e.g., to verify non-generic conditionals aren't affected).
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (3)
.changeset/honest-eagles-learn.md(1 hunks)crates/biome_js_formatter/src/utils/assignment_like.rs(2 hunks)crates/biome_js_formatter/tests/specs/ts/type/conditional.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/biome_js_formatter/src/utils/assignment_like.rs
🧰 Additional context used
🧬 Code graph analysis (1)
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (1)
crates/biome_js_analyze/tests/specs/style/useImportType/invalid-inline-type.ts (6)
T5(9-9)T6(9-9)T7(13-13)T8(13-13)T9(17-17)T10(21-21)
🔇 Additional comments (4)
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts (3)
23-23: Excellent test helper!The
Echo<T>alias is a clean way to verify that the formatter correctly detects generics in conditional types. Well done.
25-43: Thorough test coverage!T6–T10 nicely cover the core scenarios: inline conditionals, various comment placements, and different line-break positions when generics are involved. This should give good confidence in the formatting logic.
25-43: Snapshots are already in place.The snapshot file at
crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snapcontains both the Input and Output sections for test cases T5–T10. All are present and properly formatted..changeset/honest-eagles-learn.md (1)
7-22: Clear documentation with a helpful diff.The changeset clearly documents the fix and the included diff effectively demonstrates the formatting change. The before/after example shows how conditional types with generics now break after the assignment operator rather than mid-generic, which aligns with the PR objective.
…ejs#8225) Co-authored-by: Emanuele Stoppa <[email protected]>
Summary
Closes #7806
Prettier implements specific logic to decide whether to break after the assignment operator when the right-hand side is a conditional type.
It inspects the checkType and extendsType of the conditional type. If either of them involves generics, Prettier prefers to break after the operator.
You can find the relevant logic in Prettier's repo here: src/language-js/print/assignment.js.
Test Plan
Updated snapshot tests
Docs