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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/oxfmt/src/core/oxfmtrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl SortGroupItemConfig {
pub struct CustomGroupItemConfig {
/// Name of the custom group, used in the `groups` option.
pub group_name: String,
/// List of import name prefixes to match for this group.
/// List of glob patterns to match import sources for this group.
pub element_name_pattern: Vec<String>,
}

Expand Down
4 changes: 2 additions & 2 deletions apps/oxfmt/test/api/sort_imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { store } from "~/stores/store";
experimentalSortImports: {
newlinesBetween: false,
customGroups: [
{ elementNamePattern: ["~/stores/"], groupName: "stores" },
{ elementNamePattern: ["~/utils/"], groupName: "utils" },
{ elementNamePattern: ["~/stores/*"], groupName: "stores" },
{ elementNamePattern: ["~/utils/*"], groupName: "utils" },
Copy link
Contributor

Choose a reason for hiding this comment

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

I know I'm a little late, but…

I think those test globs should rather be like ~/utils/** with double asterisk. Makes more sense semantically. Not strictly relevant for the tests, obviously, but at least something to keep in mind when updating the docs ;-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. Thanks, I'll fix the test and docs later. 🙏🏻

],
groups: ["stores", "utils", "sibling"],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"experimentalSortImports": {
"customGroups": [
{ "elementNamePattern": ["~/stores/"], "groupName": "stores" },
{ "elementNamePattern": ["~/utils/"], "groupName": "utils" }
{ "elementNamePattern": ["~/stores/*"], "groupName": "stores" },
{ "elementNamePattern": ["~/utils/*"], "groupName": "utils" }
],
"groups": ["stores", "utils", "sibling"]
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

cow-utils = { workspace = true }
fast-glob = { workspace = true }
natord = { workspace = true }
nodejs-built-in-modules = { workspace = true }
phf = { workspace = true, features = ["macros"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl GroupMatcher {
let is_match = custom_group
.element_name_pattern
.iter()
.any(|pattern| import_metadata.source.starts_with(pattern));
.any(|pattern| fast_glob::glob_match(pattern, import_metadata.source));
if is_match {
return *index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ impl fmt::Display for SortOrder {

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct CustomGroupDefinition {
/// The identifier used in `groups` representing this group.
pub group_name: String,
/// List of glob patterns to match import sources for this group.
pub element_name_pattern: Vec<String>,
}

Expand Down
139 changes: 117 additions & 22 deletions crates/oxc_formatter/tests/ir_transform/sort_imports/custom_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,47 +81,47 @@ import CartComponentB from "./cart/CartComponentB.vue";
"experimentalSortImports": {
"customGroups": [
{
"elementNamePattern": ["~/validators/"],
"elementNamePattern": ["~/validators/*"],
"groupName": "validators"
},
{
"elementNamePattern": ["~/composable/"],
"elementNamePattern": ["~/composable/*"],
"groupName": "composable"
},
{
"elementNamePattern": ["~/components/"],
"elementNamePattern": ["~/components/*"],
"groupName": "components"
},
{
"elementNamePattern": ["~/services/"],
"elementNamePattern": ["~/services/*"],
"groupName": "services"
},
{
"elementNamePattern": ["~/widgets/"],
"elementNamePattern": ["~/widgets/*"],
"groupName": "widgets"
},
{
"elementNamePattern": ["~/stores/"],
"elementNamePattern": ["~/stores/*"],
"groupName": "stores"
},
{
"elementNamePattern": ["~/logics/"],
"elementNamePattern": ["~/logics/*"],
"groupName": "logics"
},
{
"elementNamePattern": ["~/assets/"],
"elementNamePattern": ["~/assets/*"],
"groupName": "assets"
},
{
"elementNamePattern": ["~/utils/"],
"elementNamePattern": ["~/utils/*"],
"groupName": "utils"
},
{
"elementNamePattern": ["~/pages/"],
"elementNamePattern": ["~/pages/*"],
"groupName": "pages"
},
{
"elementNamePattern": ["~/ui/"],
"elementNamePattern": ["~/ui/*"],
"groupName": "ui"
}
],
Expand Down Expand Up @@ -196,47 +196,47 @@ import ComponentC from "~/components/ComponentC.vue";
"experimentalSortImports": {
"customGroups": [
{
"elementNamePattern": ["~/validators/"],
"elementNamePattern": ["~/validators/*"],
"groupName": "validators"
},
{
"elementNamePattern": ["~/composable/"],
"elementNamePattern": ["~/composable/*"],
"groupName": "composable"
},
{
"elementNamePattern": ["~/components/"],
"elementNamePattern": ["~/components/*"],
"groupName": "components"
},
{
"elementNamePattern": ["~/services/"],
"elementNamePattern": ["~/services/*"],
"groupName": "services"
},
{
"elementNamePattern": ["~/widgets/"],
"elementNamePattern": ["~/widgets/*"],
"groupName": "widgets"
},
{
"elementNamePattern": ["~/stores/"],
"elementNamePattern": ["~/stores/*"],
"groupName": "stores"
},
{
"elementNamePattern": ["~/logics/"],
"elementNamePattern": ["~/logics/*"],
"groupName": "logics"
},
{
"elementNamePattern": ["~/assets/"],
"elementNamePattern": ["~/assets/*"],
"groupName": "assets"
},
{
"elementNamePattern": ["~/utils/"],
"elementNamePattern": ["~/utils/*"],
"groupName": "utils"
},
{
"elementNamePattern": ["~/pages/"],
"elementNamePattern": ["~/pages/*"],
"groupName": "pages"
},
{
"elementNamePattern": ["~/ui/"],
"elementNamePattern": ["~/ui/*"],
"groupName": "ui"
}
],
Expand Down Expand Up @@ -286,3 +286,98 @@ import CartComponentB from "./cart/CartComponentB.vue";
"#,
);
}

#[test]
fn glob_pattern_suffix_matching() {
assert_format(
r#"
import { setup } from "./setup.mock.ts";
import { a } from "./a.ts";
"#,
r#"
{
"experimentalSortImports": {
"customGroups": [
{
"groupName": "mocks",
"elementNamePattern": ["**/*.mock.ts"]
}
],
"groups": [
"mocks",
"unknown"
]
}
}
"#,
r#"
import { setup } from "./setup.mock.ts";

import { a } from "./a.ts";
"#,
);
}

#[test]
fn glob_pattern_brace_expansion() {
assert_format(
r#"
import { createApp } from "vue";
import React from "react";
import Vuetify from "vuetify";
"#,
r#"
{
"experimentalSortImports": {
"customGroups": [
{
"groupName": "frameworks",
"elementNamePattern": ["{react,vue}"]
}
],
"groups": [
"frameworks",
"unknown"
]
}
}
"#,
r#"
import React from "react";
import { createApp } from "vue";

import Vuetify from "vuetify";
"#,
);
}

#[test]
fn glob_pattern_exact_match() {
assert_format(
r#"
import { createApp } from "vue";
import Vuetify from "vuetify";
"#,
r#"
{
"experimentalSortImports": {
"customGroups": [
{
"groupName": "vue-core",
"elementNamePattern": ["vue"]
}
],
"groups": [
"vue-core",
"unknown"
]
}
}
"#,
r#"
import { createApp } from "vue";

import Vuetify from "vuetify";
"#,
);
}
4 changes: 2 additions & 2 deletions npm/oxfmt/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@
"type": "object",
"properties": {
"elementNamePattern": {
"description": "List of import name prefixes to match for this group.",
"description": "List of glob patterns to match import sources for this group.",
"default": [],
"type": "array",
"items": {
"type": "string"
},
"markdownDescription": "List of import name prefixes to match for this group."
"markdownDescription": "List of glob patterns to match import sources for this group."
},
"groupName": {
"description": "Name of the custom group, used in the `groups` option.",
Expand Down
4 changes: 2 additions & 2 deletions tasks/website_formatter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ expression: json
"type": "object",
"properties": {
"elementNamePattern": {
"description": "List of import name prefixes to match for this group.",
"description": "List of glob patterns to match import sources for this group.",
"default": [],
"type": "array",
"items": {
"type": "string"
},
"markdownDescription": "List of import name prefixes to match for this group."
"markdownDescription": "List of glob patterns to match import sources for this group."
},
"groupName": {
"description": "Name of the custom group, used in the `groups` option.",
Expand Down
4 changes: 2 additions & 2 deletions tasks/website_formatter/src/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type: `string[]`

default: `[]`

List of import name prefixes to match for this group.
List of glob patterns to match import sources for this group.


##### experimentalSortImports.customGroups[n].groupName
Expand Down Expand Up @@ -599,7 +599,7 @@ type: `string[]`
default: `[]`
List of import name prefixes to match for this group.
List of glob patterns to match import sources for this group.
######## overrides[n].options.experimentalSortImports.customGroups[n].groupName
Expand Down
Loading