Skip to content

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Oct 17, 2025

The formatting logic of arguments in ImportExpression and CallExpression is the same, but our AST differs between these two nodes. Previously, I tried to pick up some logic from CallExpression to ImportExpression to cover most cases. However, it turns out that it isn't covered very well. In this PR, I tried to convert ImportExpression's source and options to AstNode<Vec<Argument> with a tricky unsafe transmute so that it matches the Prettier behavior.

Copy link
Member Author

Dunqing commented Oct 17, 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.

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 17, 2025

CodSpeed Performance Report

Merging #14712 will not alter performance

Comparing 10-17-feat_formatter_reimplement_formatting_for_importexpression (4d50fde) with main (5ea3bb6)

Summary

✅ 33 untouched
⏩ 4 skipped1

Footnotes

  1. 4 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.

@Dunqing Dunqing force-pushed the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch from 96b58db to 2a4427b Compare October 17, 2025 07:34
@graphite-app graphite-app bot changed the base branch from 10-17-fix_formatter_incorrect_handling_of_objectpattern_as_an_assignmentpattern_of_a_parameter to graphite-base/14712 October 17, 2025 08:05
@graphite-app graphite-app bot force-pushed the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch from 2a4427b to f2b47f5 Compare October 17, 2025 08:17
@graphite-app graphite-app bot force-pushed the graphite-base/14712 branch from 159d2bd to 5ea3bb6 Compare October 17, 2025 08:17
@graphite-app graphite-app bot changed the base branch from graphite-base/14712 to main October 17, 2025 08:18
@graphite-app graphite-app bot force-pushed the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch from f2b47f5 to 2a5c9d0 Compare October 17, 2025 08:18
@Dunqing Dunqing changed the title feat(formatter): reimplement formatting for ImportExpression feat(formatter): reimplement formatting for ImportExpression Oct 17, 2025
@Dunqing Dunqing marked this pull request as ready for review October 17, 2025 08:25
Copilot AI review requested due to automatic review settings October 17, 2025 08:25
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 reimplements the formatting logic for ImportExpression to match Prettier's behavior by reusing the existing CallExpression argument formatting logic. Instead of maintaining separate formatting code, it converts the ImportExpression's source and options into a Vec<Argument> using unsafe transmute operations.

Key changes:

  • Replaced custom ImportExpression formatting with CallExpression argument formatting logic
  • Added unsafe transmute operations to convert Expression to Argument types
  • Modified trailing comma logic to exclude ImportExpression from automatic trailing commas

Reviewed Changes

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

File Description
crates/oxc_formatter/src/write/import_expression.rs Simplified formatting logic to delegate to CallExpression argument formatting
crates/oxc_formatter/src/ast_nodes/node.rs Added to_arguments() method with unsafe transmute to convert ImportExpression to argument list
crates/oxc_formatter/src/write/call_arguments.rs Modified trailing comma logic to exclude ImportExpression
Test files Added new test fixtures for grouped import expressions and comment handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Dunqing Dunqing force-pushed the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch 2 times, most recently from 142025d to 4d50fde Compare October 17, 2025 09:21
@graphite-app
Copy link
Contributor

graphite-app bot commented Oct 17, 2025

Merge activity

The formatting logic of `arguments` in `ImportExpression` and `CallExpression` is the same, but our AST differs between these two nodes. Previously, I tried to pick up some logic from `CallExpression` to `ImportExpression` to cover most cases. However, it turns out that it isn't covered very well. In this PR, I tried to convert `ImportExpression`'s `source` and `options` to `AstNode<Vec<Argument>` with a tricky unsafe transmute so that it matches the Prettier behavior.
@graphite-app graphite-app bot force-pushed the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch from 4d50fde to 6bf8bac Compare October 17, 2025 23:34
@graphite-app graphite-app bot merged commit 6bf8bac into main Oct 17, 2025
20 checks passed
@graphite-app graphite-app bot deleted the 10-17-feat_formatter_reimplement_formatting_for_importexpression branch October 17, 2025 23:40
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Oct 17, 2025
@Boshen Boshen mentioned this pull request Oct 20, 2025
graphite-app bot pushed a commit that referenced this pull request Oct 20, 2025
## [0.6.0] - 2025-10-20

### 🚀 Features

- 7f91a26 oxfmt: Handle ignoring files (#14798) (leaysgur)
- 199a2c6 oxfmt: Support `--with-node-modules` option (#14713) (leaysgur)
- 6bf8bac formatter: Reimplement formatting for `ImportExpression` (#14712) (Dunqing)
- 3f2e036 formatter: Introduce `AstNode<ExpressionStatement>::is_arrow_function_body` (#14709) (Dunqing)
- df225e9 formatter: Add `AstNode::ancestor` and `AstNode::grand_parent` methods (#14700) (Dunqing)
- 26c5f5a oxfmt: Ignore VCS directories by default (#14616) (leaysgur)
- fec2ed9 oxfmt: Use Prettier style config key and value (#14612) (leaysgur)
- 1b58521 oxfmt,language_server: Enable JSX for all JS source type (#14605) (leaysgur)

### 🐛 Bug Fixes

- 21c4285 formatter: Correct printing remaining trailing comments for `TSMappedType` (#14761) (Dunqing)
- 1d1573e formatter: Correct adding semicolons for TypeScript left-hand side nodes (#14760) (Dunqing)
- 4cc3b10 formatter: Improve handling of new lines between comments in `MemberChain` (#14759) (Dunqing)
- e6bce8e formatter: Break the left hand side of AssignmentLike node if it is an `ObjectPattern` with three properties (#14756) (Dunqing)
- dc57a2b formatter: Incorrect handling of `VariableDeclarator` with an `ArrowFunctionExpression` initializer (#14731) (Dunqing)
- 537185d formatter: Should always group the left side of `AssignmentPattern` (#14730) (Dunqing)
- ef02760 oxfmt: Handle relative path starts with dot (#14708) (leaysgur)
- ee37f5d oxfmt: Handle default cwd correctly (#14704) (leaysgur)
- 4283fd8 formatter: Correct printing comments for `JSXAttributeValue` (#14719) (Dunqing)
- 59c9e1b formatter: Avoid conditional being broken in arguments by trailing comments (#14718) (Dunqing)
- 7d64b96 formatter: Should wrap parentheses with JSX arguments of `NewExpression` (#14717) (Dunqing)
- 2068a63 formatter: Should indent TemplateExpression if it is a member expression that is part of `ChainExpression` (#14714) (Dunqing)
- 5ea3bb6 formatter: Incorrect handling of `ObjectPattern` as an `AssignmentPattern` of a parameter (#14711) (Dunqing)
- eb52529 formatter: Incorrect handling of `ObjectPattern` as a parameter (#14670) (Dunqing)
- 8ac10da formatter: Correct checking assignment layout for the right side with ownline comment and it is a `PrivateFieldExpression` (#14664) (Dunqing)
- 6cba9b1 formatter: Should not merge tail with head for MemberChain when its parent is ArrowFunctionExpression (#14663) (Dunqing)
- f44d3c0 formatter: Should not indent BinaryLikeExpression when its parent is `NewExpression` (#14662) (Dunqing)
- bf953b8 formatter: Should group nested test for TSConditionalType (#14661) (Dunqing)
- 63dc57b formatter: Correct handling if a template literal should be printed as a signle line (#14660) (Dunqing)
- 0150ad5 formatter: Should group type parameters and parameters for method-like and function-like nodes (#14659) (Dunqing)
- 392bf74 formatter: Improve handling of dangling comments in if statements (#14658) (Dunqing)
- fd52b10 formatter: Don't print CallExpression as MemberChain style when its only has one argument and it is a TemplateLiteral on its own line (#14657) (Dunqing)
- 29c3c60 formatter: Don't group nested await expression when its is the leftmost expression (#14656) (Dunqing)
- 72c7503 formatter: Correct checking function composition when the arguments have an call expression before function (#14655) (Dunqing)
- 2b645e2 formatter: Don't wrap parenthesis for `type` when its grandparent isn't a `ExpressionStatement` (#14654) (Dunqing)
- e0eb966 formatter: Skip the leading semicolon when calculating leading lines (#14653) (Dunqing)
- 0961c3a oxlint,oxfmt: Skip traversing `.git` directories (#14590) (Boshen)

### 🚜 Refactor

- b7926f3 oxfmt: Update CLI --help details (#14796) (leaysgur)
- 173168b oxfmt: Refactor walk.rs and format.rs relationship (#14795) (leaysgur)
- aea9d79 oxfmt: Pass `PathBuf` from walk.rs to service.rs (#14716) (leaysgur)
- 83e783a formatter: Organize utils structure (#14710) (Dunqing)
- 58dd74a formatter: Remove all `without_parentheses` usages (#14707) (Dunqing)
- 75dfcad formatter: Simplify `ExpressionLeftSide` (#14706) (Dunqing)
- 273f0fe formatter: Remove unnecessary lifetimes for the implementations of `NeedsParentheses` (#14703) (Dunqing)
- bae5f11 formatter: Improve `AstNode` and `AstNodes` (#14686) (Dunqing)

### ⚡ Performance

- c6395c7 formatter: Optimize string reservation in print_text (#14751) (Boshen)

### 🧪 Testing

- 7c42ea0 oxfmt: Remove args from snapshot file name (#14800) (leaysgur)
- 42d8c62 formatter: Add tests for sort-imports (#14685) (leaysgur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants