Skip to content

feat(oxfmt): support boolean shorthand for sortImports#20672

Closed
Mister-Hope wants to merge 1 commit intooxc-project:mainfrom
Mister-Hope:main
Closed

feat(oxfmt): support boolean shorthand for sortImports#20672
Mister-Hope wants to merge 1 commit intooxc-project:mainfrom
Mister-Hope:main

Conversation

@Mister-Hope
Copy link
Copy Markdown

@Mister-Hope Mister-Hope commented Mar 24, 2026

This PR supports a boolean option for sortImports similarly like sortPackageJson, allowing people to enable the feature with boolean flags:

import { defineConfig } from "oxfmt";

export const config = defineConfig({
  sortPackageJson: true,
- sortImports: {},
+ sortImports: true,
});

Note: currently all tests are still using experimental prefix, so I followed them. I am expecting some warnings from the copilot review.

I think the changes could be done in a seperate PR, I would like to create one if any maintainer requests this.

Copilot AI review requested due to automatic review settings March 24, 2026 01:07
@github-actions github-actions bot added A-cli Area - CLI A-formatter Area - Formatter C-enhancement Category - New feature or request labels Mar 24, 2026
Copy link
Copy Markdown
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

Updates Oxfmt’s configuration surface to accept a boolean shorthand for sortImports, and refreshes the generated schemas/docs and tests accordingly.

Changes:

  • Extend sortImports config to accept boolean | object via a new SortImportsUserConfig union.
  • Update generated JSON/markdown schemas (and npm-distributed schema) to reflect the new type.
  • Add CLI + API tests/fixtures for the boolean form.

Reviewed changes

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

Show a summary per file
File Description
tasks/website_formatter/src/snapshots/schema_markdown.snap Updates rendered markdown docs to show sortImports as `object
tasks/website_formatter/src/snapshots/schema_json.snap Updates schema snapshot to reference SortImportsUserConfig and adds its definition.
npm/oxfmt/configuration_schema.json Updates shipped schema to reference SortImportsUserConfig and adds its definition.
apps/oxfmt/test/cli/sort_imports/sort_imports.test.ts Adds a CLI test case for boolean true.
apps/oxfmt/test/cli/sort_imports/fixtures/boolean_true/input.ts Adds a new CLI fixture input for the boolean config case.
apps/oxfmt/test/cli/sort_imports/fixtures/boolean_true/.oxfmtrc.json Adds a new CLI fixture config enabling sort imports with true.
apps/oxfmt/test/api/sort_imports.test.ts Adds an API test case for boolean true.
apps/oxfmt/src/core/oxfmtrc/to_oxfmt_options.rs Adds parsing/mapping logic for SortImportsUserConfig into formatter options.
apps/oxfmt/src/core/oxfmtrc/format_config.rs Changes sort_imports type to Option<SortImportsUserConfig> and introduces the untagged enum.
apps/oxfmt/src-js/config.generated.ts Updates generated TS types so sortImports accepts `boolean

Comment thread apps/oxfmt/test/api/sort_imports.test.ts
Comment thread apps/oxfmt/test/cli/sort_imports/sort_imports.test.ts
Comment thread apps/oxfmt/test/cli/sort_imports/fixtures/boolean_true/input.ts Outdated
Copy link
Copy Markdown
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

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

Comment on lines +5 to +12
it("should sort with `true` (boolean)", async () => {
const input = `import { b } from "b";
import { a } from "a";
`;
const result = await format("a.ts", input, {
experimentalSortImports: true,
});

Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

This new API test only exercises the deprecated alias key (experimentalSortImports) with a boolean. Since the PR’s goal is boolean shorthand for sortImports, add a test that passes sortImports: true to format() so the primary (non-experimental) option name is covered and can’t regress.

Copilot uses AI. Check for mistakes.
const snapshot = await runWriteModeAndSnapshot(cwd, ["input.ts"]);
expect(snapshot).toMatchSnapshot();
});

Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

The new CLI test validates boolean enablement, but the fixture config uses the deprecated alias key experimentalSortImports. Since this PR advertises sortImports: true support, add a fixture/test that uses sortImports: true in .oxfmtrc.json so the documented key is covered at the CLI/config-file level as well.

Suggested change
it("should sort imports when enabled via config with `sortImports: true`", async () => {
const cwd = join(fixturesDir, "boolean_true_sort_imports_config");
const snapshot = await runWriteModeAndSnapshot(cwd, ["input.ts"]);
expect(snapshot).toMatchSnapshot();
});

Copilot uses AI. Check for mistakes.
Comment on lines +164 to +167
SortImportsUserConfig::Bool(true) => {
format_options.sort_imports = Some(SortImportsOptions::default());
None
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

In the boolean shorthand handling for sort_imports, the true path sets format_options.sort_imports directly and skips the shared object-processing path (including validate()). Consider mapping Bool(true) to an empty/default SortImportsConfig and running the same conversion+validation logic as {} so {} and true can’t diverge over time and there’s only one place to maintain defaults/validation.

Suggested change
SortImportsUserConfig::Bool(true) => {
format_options.sort_imports = Some(SortImportsOptions::default());
None
}
SortImportsUserConfig::Bool(true) => Some(Default::default()),

Copilot uses AI. Check for mistakes.
@leaysgur
Copy link
Copy Markdown
Member

Thank you for your time.

However, I would like to close this because I believe the drawback of the increased amount of code to maintain outweighs the benefits gained from this change.

Also, unlike other options, sortPackageJson is enabled by default, so it is not necessarily the case that they are all the same.

@Mister-Hope
Copy link
Copy Markdown
Author

I understand that you don't like it.

Just say you don't need it is enough.

I believe the drawback of the increased amount of code to maintain outweighs the benefits gained from this change.

The change only adds less than 30 lines in actual codes. So I am a little surprised about this expression.

Basically, a true flag can be a shortcut resetting to sortImport defaults while extending.

Is this reason strong enough for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants