Skip to content

fix(formatter): Complete is_complex_type_arguments()#17353

Closed
leaysgur wants to merge 1 commit intomainfrom
12-25-fix_formatter_complete_is_complex_type_arguments
Closed

fix(formatter): Complete is_complex_type_arguments()#17353
leaysgur wants to merge 1 commit intomainfrom
12-25-fix_formatter_complete_is_complex_type_arguments

Conversation

@leaysgur
Copy link
Member

@leaysgur leaysgur commented Dec 25, 2025

Fixes #17275.

At fitst, I just added TSTypeReference after TSMappedType. However it produced another formatting difference (= fails current snapshot tests).

Copilot AI review requested due to automatic review settings December 25, 2025 06:10
@leaysgur leaysgur requested a review from Dunqing as a code owner December 25, 2025 06:10
@github-actions github-actions bot added A-formatter Area - Formatter C-bug Category - Bug labels Dec 25, 2025
Copy link
Member Author

leaysgur commented Dec 25, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@leaysgur leaysgur changed the title fix(formatter): Complete is_complex_type_arguments fix(formatter): Complete is_complex_type_arguments() Dec 25, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the implementation of the is_complex_type_arguments function by adding the previously missing will_break() logic. This allows the formatter to correctly identify complex type arguments in call expressions and apply appropriate formatting.

Key Changes

  • Completed the is_complex_type_arguments function by implementing the will_break() check that was previously marked as TODO
  • Changed function signatures from &Formatter to &mut Formatter to support the new inspect() method
  • Simplified the explicit type matching by removing TSMappedType, which is now handled by the will_break() logic

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
crates/oxc_formatter/src/utils/assignment_like.rs Completed is_complex_type_arguments implementation with will_break() logic and updated function signatures to accept mutable Formatter references
crates/oxc_formatter/tests/fixtures/ts/assignments/complex-type-arguments.ts Added test case for TSTypeReference with nested TSTypeLiteral to verify the fix
crates/oxc_formatter/tests/fixtures/ts/assignments/complex-type-arguments.ts.snap Updated snapshot to include expected formatting output for the new test case

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 25, 2025

CodSpeed Performance Report

Merging #17353 will not alter performance

Comparing 12-25-fix_formatter_complete_is_complex_type_arguments (77f526c) with main (7c3dae3)1

Summary

✅ 38 untouched
⏩ 7 skipped2

Footnotes

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

  2. 7 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.

@leaysgur
Copy link
Member Author

leaysgur commented Dec 25, 2025

Talked with @Dunqing , I will retry this. roughly:

     let is_first_argument_complex = ts_type_argument_list.first().is_some_and(|first_argument| {
+        match first_argument {
+            TSType::TSTypeReference(r) => {
+                if r.type_arguments.as_ref().is_some_and(|type_args| type_args.params.len() > 1) {
+                    return true;
+                }
+
+                return matches!(
+                    r.type_arguments.as_ref().unwrap().params.first().unwrap(),
+                    TSType::TSUnionType(_)
+                | TSType::TSIntersectionType(_)
+                | TSType::TSTypeLiteral(_)
+                // TODO: This is not part of Prettier's logic, but it makes sense to consider mapped types as
+                // complex because it is the same as type literals in terms of structure.
+                // NOTE: Once the `will_break` logic is added, this will have to be revisited.
+                | TSType::TSMappedType(_)
+                );
+            }
+            _ => {}
+        }
+
         matches!(
             first_argument,
             TSType::TSUnionType(_)

Need refactoring though.

NOTE to myself: inspect(f) (and intern(f) internally) consumes comment!

@leaysgur
Copy link
Member Author

leaysgur commented Dec 26, 2025

We discussed concerns that comments might be discarded when attempting to print again, since the results of memoized.inspect(f) are being discarded.

However, even after modifying the code in the example, we couldn't confirm this behavior last night.

@Dunqing After investigating, I understood the reason.

if f.comments().has_comment_in_span(call_expressions[0].span) {
return false;
}

This check runs first, so is_complex_type_arguments() was not called anyway.

But in any case, I think it's better not to use memoized.inspect(f) itself very often, so I'll redo the PR. 👉🏻 #17362

@leaysgur leaysgur closed this Dec 26, 2025
@leaysgur leaysgur deleted the 12-25-fix_formatter_complete_is_complex_type_arguments branch December 26, 2025 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatter: Diff with Prettier on Generic Types

3 participants