Skip to content

Commit

Permalink
feat(alias): auto-detect aliases
Browse files Browse the repository at this point in the history
Auto-detection relies on isAbsoluteModule and isNodeModule

BREAKING CHANGE: options.alias is no longer required/valid

fix #11
  • Loading branch information
fsmaia committed May 24, 2021
1 parent 621a4aa commit 252610c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
A style for [import-sort](https://github.com/renke/import-sort) that is focused
on modules with relative modules alias support.

## Migrating from v1 to v2

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:

- Aliases are absolute modules: `isAbsoluteModule`
- Aliases aren't node modules: `not(isNodeModule)`

## Options

| 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 |
| Name | Type | Description | Default value |
| ---------------------- | ------- | ----------------------------------------------------------------- | ------------- |
| overrideBuiltInModules | boolean | Whether an alias should override a Node built-in module (e.g. fs) | true |

## Configuration

Expand Down
13 changes: 2 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
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
{ overrideBuiltinModules = true } = {} as Options
): IStyleItem[] => {
const {
alias,
Expand All @@ -32,7 +23,7 @@ export default (
unicode,
} = styleApi;

const isAliasModule = hasAlias(aliases || []);
const isAliasModule = not(isNodeModule);

return [
// import "foo"
Expand Down

0 comments on commit 252610c

Please sign in to comment.