Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-detect aliases #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 11 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
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,
dotSegmentCount,
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"
Expand All @@ -49,25 +40,23 @@ export default (

// import … from "fs";
{
match: overrideBuiltinModules
? and(isNodeModule, not(isAliasModule))
: isNodeModule,
match: isNodeModule,
sort: moduleName(naturally),
sortNamedMembers: alias(unicode),
},
{ separator: true },

// import … from "foo";
{
match: and(isAbsoluteModule, not(isAliasModule)),
match: and(isAbsoluteModule, isInstalledModule(file)),
sort: moduleName(naturally),
sortNamedMembers: alias(unicode),
},
{ separator: true },

// import … from "{relativeAlias}/foo";
{
match: and(isAbsoluteModule, isAliasModule),
match: and(isAbsoluteModule, not(isExternalModule)),
sort: moduleName(naturally),
sortNamedMembers: alias(unicode),
},
Expand Down