diff --git a/README.md b/README.md index fe43224..0f739ed 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,15 @@ A style for [import-sort](https://github.com/renke/import-sort) that is focused on modules with relative modules alias support. -## Options +## Migrating from v1 to v2 -| Name | Type | Description | Default value | -| ---------------------- | -------- | ----------------------------------------------------------------- | ------------- | -| alias | string[] | List of resolver aliases | [] | -| overrideBuiltInModules | boolean | Whether an alias should override a Node built-in module (e.g. fs) | true | +Previously, we had to fill an `alias` option with the aliases list (e.g. `["components", "modules"]`). + +Now it auto-detects aliases using the following combined rules: + +- Aliased imports are absolute modules (e.g. `foo`): `isAbsoluteModule` +- Aliased imports aren't installed modules (e.g. `react`): `not(isInstalledModule(file))` +- Aliased imports aren't node modules (e.g. `fs`): `not(isNodeModule)` ## Configuration diff --git a/src/index.ts b/src/index.ts index b073455..1f95c93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,23 +1,10 @@ -import { IImport } from 'import-sort-parser'; import { IStyleAPI, IStyleItem } from 'import-sort-style'; export interface Options { - alias: string[]; overrideBuiltinModules: boolean; } -const hasAlias = (aliases: string[]) => (imported: IImport): boolean => - aliases.some( - (alias: string): boolean => - imported.moduleName === alias || - imported.moduleName.indexOf(`${alias}/`) === 0 - ); - -export default ( - styleApi: IStyleAPI, - _file?: string, - { alias: aliases = [], overrideBuiltinModules = true } = {} as Options -): IStyleItem[] => { +export default (styleApi: IStyleAPI, file: string): IStyleItem[] => { const { alias, and, @@ -25,22 +12,26 @@ export default ( hasNoMember, isNodeModule, isAbsoluteModule, + isInstalledModule, isRelativeModule, moduleName, naturally, not, + or, unicode, } = styleApi; - const isAliasModule = hasAlias(aliases || []); + const isExternalModule = or(isNodeModule, isInstalledModule(file)); return [ // import "foo" - { match: and(hasNoMember, isAbsoluteModule, not(isAliasModule)) }, + { + match: and(hasNoMember, isAbsoluteModule, isExternalModule), + }, { separator: true }, // import "{relativeAlias}/foo" - { match: and(hasNoMember, isAbsoluteModule, isAliasModule) }, + { match: and(hasNoMember, isAbsoluteModule, not(isExternalModule)) }, { separator: true }, // import "./foo" @@ -49,9 +40,7 @@ export default ( // import … from "fs"; { - match: overrideBuiltinModules - ? and(isNodeModule, not(isAliasModule)) - : isNodeModule, + match: isNodeModule, sort: moduleName(naturally), sortNamedMembers: alias(unicode), }, @@ -59,7 +48,7 @@ export default ( // import … from "foo"; { - match: and(isAbsoluteModule, not(isAliasModule)), + match: and(isAbsoluteModule, isInstalledModule(file)), sort: moduleName(naturally), sortNamedMembers: alias(unicode), }, @@ -67,7 +56,7 @@ export default ( // import … from "{relativeAlias}/foo"; { - match: and(isAbsoluteModule, isAliasModule), + match: and(isAbsoluteModule, not(isExternalModule)), sort: moduleName(naturally), sortNamedMembers: alias(unicode), },