Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/oxfmt/src-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ export type SortImportsOptions = {
/** Glob patterns to identify internal imports. */
internalPattern?: string[];
/**
* Custom groups configuration for organizing imports.
* Groups configuration for organizing imports.
* Each array element represents a group, and multiple group names in the same array are treated as one.
* Accepts both `string` and `string[]` as group elements.
*/
groups?: (string | string[])[];
/** Define custom groups for matching specific imports. */
customGroups?: { groupName: string; elementNamePattern: string[] }[];
};

/**
Expand Down
9 changes: 9 additions & 0 deletions apps/oxfmt/src/core/oxfmtrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ impl FormatConfig {
if let Some(v) = config.groups {
sort_imports.groups = v.into_iter().map(SortGroupItemConfig::into_vec).collect();
}
if let Some(v) = config.custom_groups {
sort_imports.custom_groups = v
.into_iter()
.map(|c| CustomGroupDefinition {
group_name: c.group_name,
element_name_pattern: c.element_name_pattern,
})
.collect();
}

// `partition_by_newline: true` and `newlines_between: true` cannot be used together
if sort_imports.partition_by_newline && sort_imports.newlines_between {
Expand Down
24 changes: 24 additions & 0 deletions apps/oxfmt/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@ describe("API Tests", () => {
);
expect(result3.errors).toStrictEqual([]);
});

test("should sort imports with customGroups", async () => {
const input = `import { foo } from "./foo";
import { util } from "~/utils/util";
import { store } from "~/stores/store";
`;
const result = await format("a.ts", input, {
experimentalSortImports: {
customGroups: [
{ elementNamePattern: ["~/stores/"], groupName: "stores" },
{ elementNamePattern: ["~/utils/"], groupName: "utils" },
],
groups: ["stores", "utils", "sibling"],
},
});

expect(result.code).toBe(`import { store } from "~/stores/store";

import { util } from "~/utils/util";

import { foo } from "./foo";
`);
expect(result.errors).toStrictEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"experimentalSortImports": {
"customGroups": [
{ "elementNamePattern": ["~/stores/"], "groupName": "stores" },
{ "elementNamePattern": ["~/utils/"], "groupName": "utils" }
],
"groups": ["stores", "utils", "sibling"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { store } from "~/stores/store";

import { util } from "~/utils/util";

import { foo } from "./foo";
14 changes: 14 additions & 0 deletions apps/oxfmt/test/cli/sort_imports/sort_imports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { join } from "node:path";
import { runCli } from "../utils";

const fixturesDir = join(import.meta.dirname, "fixtures");

describe("sort_imports", () => {
it("should sort imports with customGroups", async () => {
const cwd = join(fixturesDir, "custom_groups");
const result = await runCli(cwd, ["--check", "input.ts"]);

expect(result.exitCode).toBe(0);
});
});
Loading