Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ongyuxing
Copy link
Contributor

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

@changeset-bot
Copy link

changeset-bot bot commented Nov 23, 2025

🦋 Changeset detected

Latest commit: a9b0a3f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

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

@github-actions github-actions bot added A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages labels Nov 23, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 23, 2025

Walkthrough

Adds a patch-level changeset and implements formatting logic so conditional TypeScript types on the right side of assignment-like expressions break consistently. Refactors should_break_after_operator from a RightAssignmentLike impl method to a crate-level function with a new signature that accepts AnyJsExpression and JsComments. Introduces internal helpers is_generic and should_break_before_conditional_type, updates imports to include TsConditionalType, and extends the breaking logic to handle TsConditionalType. Adds new formatter tests (including an Echo<T> alias and multiple conditional-type cases) exercising the change for issue #7806.

Suggested reviewers

  • ematipico
  • arendjr
  • dyc3

Pre-merge checks and finishing touches

✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: aligning conditional type formatting with Prettier's logic for assignment operator breaks.
Description check ✅ Passed The description is well-related to the changeset, explaining the motivation, referencing the linked issue (#7806), and noting that snapshot tests were updated.
Linked Issues check ✅ Passed The PR implementation aligns with issue #7806's objective by inspecting checkType and extendsType of conditional types and preferring breaks after the assignment operator when generics are involved, matching Prettier's behavior.
Out of Scope Changes check ✅ Passed All changes are scoped to the conditional type formatting fix: changelog entry, formatter logic updates, and test case additions—no unrelated modifications detected.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 2e70165 and ac098ab.

⛔ Files ignored due to path filters (1)
  • crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snap is excluded by !**/*.snap and 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_type or extends_type involves generics. Clean and straightforward implementation.

Copy link
Contributor

@dyc3 dyc3 left a 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-hq
Copy link

codspeed-hq bot commented Nov 23, 2025

CodSpeed Performance Report

Merging #8225 will not alter performance

Comparing ongyuxing:fix/conditional-type (a9b0a3f) with main (abfeb30)1

Summary

✅ 58 untouched
⏩ 95 skipped2

Footnotes

  1. No successful run was found on main (c485f96) during the generation of this report, so abfeb30 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 95 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ongyuxing ongyuxing force-pushed the fix/conditional-type branch from ac098ab to de88dff Compare November 23, 2025 23:14
@ongyuxing
Copy link
Contributor Author

We should add some of our own snapshot tests too. IIRC the prettier snapshots are a bit out of date.

I added a few test cases and checked the Prettier snapshots. The relevant snapshots have not changed since the last update.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between ac098ab and de88dff.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snap is excluded by !**/*.snap and 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 types

The 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 correctly

The added TsConditionalType / TS property member imports line up with the usages further down in AnyJsAssignmentLike and the new helpers; no issues here.

@ongyuxing ongyuxing force-pushed the fix/conditional-type branch from de88dff to 3714818 Compare November 24, 2025 00:01
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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_generic heuristic 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

📥 Commits

Reviewing files that changed from the base of the PR and between de88dff and 3714818.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snap is excluded by !**/*.snap and 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 TsConditionalType and 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 JsInitializerClause approach above.


953-960: LGTM!

The helper correctly checks whether the conditional type's check_type or extends_type involves 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. The Echo<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.

@ongyuxing ongyuxing force-pushed the fix/conditional-type branch 2 times, most recently from 0084871 to f67198f Compare November 24, 2025 15:38
@ongyuxing ongyuxing force-pushed the fix/conditional-type branch from f67198f to f740a75 Compare November 24, 2025 15:42
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between f67198f and f740a75.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_formatter/tests/specs/ts/type/conditional.ts.snap is excluded by !**/*.snap and 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.snap contains 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.

@ematipico ematipico merged commit 98ca2ae into biomejs:main Nov 24, 2025
17 checks passed
This was referenced Nov 24, 2025
l0ngvh pushed a commit to l0ngvh/biome that referenced this pull request Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📝 Inconsistent formatting of conditional types with Prettier

3 participants