Skip to content

Commit 43b9978

Browse files
committed
fix(formatter/sort_imports): Treat subpath imports as internal (#22440)
Closes #22371, follows azat-io/eslint-plugin-perfectionist#732 --- As a baseline, while the result effectively follows the behavior of a perfectionist, there is no fundamental reason why it must be done that way. Node.js subpaths are more flexible and aren't necessarily "internal", you can also alias external modules. However, I do think it's consistent in the sense that it treats them the same way as other conventions like `@`.
1 parent 21d8804 commit 43b9978

8 files changed

Lines changed: 33 additions & 11 deletions

File tree

apps/oxfmt/src-js/config.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export interface SortImportsConfig {
615615
*
616616
* This is useful for distinguishing your own modules from external dependencies.
617617
*
618-
* - Default: `["~/", "@/"]`
618+
* - Default: `["~/", "@/", "#"]`
619619
*/
620620
internalPattern?: string[];
621621
/**

apps/oxfmt/src/core/oxfmtrc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ pub struct SortImportsConfig {
477477
///
478478
/// This is useful for distinguishing your own modules from external dependencies.
479479
///
480-
/// - Default: `["~/", "@/"]`
480+
/// - Default: `["~/", "@/", "#"]`
481481
#[serde(skip_serializing_if = "Option::is_none")]
482482
pub internal_pattern: Option<Vec<String>>,
483483
/// Specifies a list of predefined import groups for sorting.

crates/oxc_formatter/src/ir_transform/sort_imports/compute_metadata.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ fn to_path_kind(source: &str, options: &SortImportsOptions) -> ImportPathKind {
219219
return ImportPathKind::Internal;
220220
}
221221

222-
// Subpath imports (e.g., `#foo`) are also considered external
223222
ImportPathKind::External
224223
}
225224

crates/oxc_formatter/src/ir_transform/sort_imports/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ pub struct CustomGroupDefinition {
134134
pub modifiers: Vec<ImportModifier>,
135135
}
136136

137-
/// Returns default prefixes for identifying internal imports: `["~/", "@/"]`.
137+
/// Returns default prefixes for identifying internal imports: `["~/", "@/", "#"]`.
138138
pub fn default_internal_patterns() -> Vec<String> {
139-
["~/", "@/"].iter().map(|s| (*s).to_string()).collect()
139+
["~/", "@/", "#"].iter().map(|s| (*s).to_string()).collect()
140140
}
141141

142142
/// Returns default groups configuration for organizing imports.

crates/oxc_formatter/tests/ir_transform/sort_imports/basic.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,29 @@ import { z } from "z";
10771077

10781078
// ---
10791079

1080+
#[test]
1081+
fn should_treat_subpath_imports_as_internal_by_default() {
1082+
assert_format(
1083+
r##"
1084+
import foo from "#utils";
1085+
import bar from "react";
1086+
import baz from "~/local";
1087+
import qux from "node:fs";
1088+
"##,
1089+
r#"{ "sortImports": {} }"#,
1090+
r##"
1091+
import qux from "node:fs";
1092+
1093+
import bar from "react";
1094+
1095+
import foo from "#utils";
1096+
import baz from "~/local";
1097+
"##,
1098+
);
1099+
}
1100+
1101+
// ---
1102+
10801103
#[test]
10811104
fn should_support_internal_pattern_option() {
10821105
assert_format(

npm/oxfmt/configuration_schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,12 @@
645645
"markdownDescription": "Specifies whether sorting should be case-sensitive.\n\n- Default: `true`"
646646
},
647647
"internalPattern": {
648-
"description": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\"]`",
648+
"description": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\", \"#\"]`",
649649
"type": "array",
650650
"items": {
651651
"type": "string"
652652
},
653-
"markdownDescription": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\"]`"
653+
"markdownDescription": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\", \"#\"]`"
654654
},
655655
"newlinesBetween": {
656656
"description": "Specifies whether to add newlines between groups.\n\nWhen `false`, no newlines are added between groups.\n\n- Default: `true`",

tasks/website_formatter/src/snapshots/schema_json.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,12 @@ expression: json
649649
"markdownDescription": "Specifies whether sorting should be case-sensitive.\n\n- Default: `true`"
650650
},
651651
"internalPattern": {
652-
"description": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\"]`",
652+
"description": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\", \"#\"]`",
653653
"type": "array",
654654
"items": {
655655
"type": "string"
656656
},
657-
"markdownDescription": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\"]`"
657+
"markdownDescription": "Specifies a prefix for identifying internal imports.\n\nThis is useful for distinguishing your own modules from external dependencies.\n\n- Default: `[\"~/\", \"@/\", \"#\"]`"
658658
},
659659
"newlinesBetween": {
660660
"description": "Specifies whether to add newlines between groups.\n\nWhen `false`, no newlines are added between groups.\n\n- Default: `true`",

tasks/website_formatter/src/snapshots/schema_markdown.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ Specifies a prefix for identifying internal imports.
789789
790790
This is useful for distinguishing your own modules from external dependencies.
791791
792-
- Default: `["~/", "@/"]`
792+
- Default: `["~/", "@/", "#"]`
793793
794794
795795
###### overrides[n].options.sortImports.newlinesBetween
@@ -1320,7 +1320,7 @@ Specifies a prefix for identifying internal imports.
13201320

13211321
This is useful for distinguishing your own modules from external dependencies.
13221322

1323-
- Default: `["~/", "@/"]`
1323+
- Default: `["~/", "@/", "#"]`
13241324

13251325

13261326
### sortImports.newlinesBetween

0 commit comments

Comments
 (0)