feat: isolate side effect imports

This commit is contained in:
2024-11-08 21:42:30 +08:00
parent 68c1f24db1
commit cb6a241ba1
3 changed files with 24 additions and 1630 deletions
+5 -1627
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -18,7 +18,8 @@
"url": "https://github.com/mattuylee/vsc-sort-import/issues"
},
"engines": {
"vscode": "^1.0.0"
"vscode": "^1.0.0",
"node": "14"
},
"categories": [
"Other"
@@ -46,7 +47,7 @@
"devDependencies": {
"@types/glob": "^7.1.3",
"@types/mocha": "^8.0.4",
"@types/node": "^12.11.7",
"@types/node": "^14.0.0",
"@types/vscode": "^1.0.0",
"glob": "^7.1.6",
"mocha": "^8.2.1",
+16 -1
View File
@@ -25,6 +25,11 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {
return i.moduleName.startsWith('@/');
}
// eg: import 'foobar'
const isSideEffect = (i: IImport) => {
return !Boolean(i.defaultMember) && !Boolean(i.namespaceMember) && !Boolean(i.namedMembers[0]?.name)
}
// comparator function which place non-alphanumeric first
const natural: IComparatorFunction = (a: string, b: string) => {
if (a === b) {
@@ -38,7 +43,8 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {
return sa ? 1 : -1;
}
};
// sort: use member primarily, if member is not avaliable, use module name
// sort: use member primarily, if member is not available, use module name
const memberOrModule: (c: IComparatorFunction) => ISorterFunction = (
comparator: IComparatorFunction
) => {
@@ -88,6 +94,15 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {
};
return [
// side effect import
{
match: isSideEffect,
sort: memberOrModule(natural),
sortNamedMembers: name(natural),
},
{
separator: true,
},
// built-in module, has member
{
match: isNodeModule,