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
+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,