Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit bcce23f

Browse files
authored
Merge pull request #1 from leanix/feature/drgn-93-make-configurable-and-add-testing
DRGN-93 Add testing and make prefixes configurable
2 parents ea1edf4 + 9f1dfc4 commit bcce23f

File tree

11 files changed

+5237
-66
lines changed

11 files changed

+5237
-66
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

README.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,41 @@
22

33
This [import-sort](https://github.com/renke/import-sort) is based on [import-sort-style-module](https://github.com/renke/import-sort/tree/master/packages/import-sort-style-module).
44

5-
```js
6-
// Absolute modules with side effects (not sorted because order may matter)
7-
import 'a';
8-
import 'c';
9-
import 'b';
10-
11-
// Relative modules with side effects (not sorted because order may matter)
12-
import './a';
13-
import './c';
14-
import './b';
15-
16-
// Third-party modules sorted by name
17-
import aa from 'aa';
18-
import bb from 'bb';
19-
import cc from 'cc';
20-
// Modules from the Node.js "standard" library sorted by name
21-
import { readFile, writeFile } from 'fs';
22-
import * as path from 'path';
23-
// First-party modules sorted by "relative depth" and then by name
24-
import aaa from '../../aaa';
25-
import bbb from '../../bbb';
26-
import aaaa from '../aaaa';
27-
import bbbb from '../bbbb';
28-
import aaaaa from './aaaaa';
29-
import bbbbb from './bbbbb';
5+
## Installation
6+
7+
- Install with `npm i -D @leanix/import-sort-style`
8+
- If used with Prettier, install `npm i -D prettier-plugin-import-sort@0.0.3` for Prettier@v1 and `npm i -D prettier-plugin-import-sort` for Prettier@v2
9+
10+
## Configuration
11+
12+
- Add a configuration like the following to your package.json to specify the parser (which may need to be installed separately) and the corresponding extensions:
13+
14+
```
15+
"importSort": {
16+
".js, .ts": {
17+
"style": "@leanix/import-sort-style",
18+
"parser": "typescript",
19+
"options": {
20+
"prefixes": [
21+
"@app",
22+
"@lib"
23+
]
24+
}
25+
}
26+
}
3027
```
28+
- The list of local prefixes/aliases is optional.
29+
30+
## Sort order
31+
32+
The order of imports is:
33+
34+
1. Third-party modules with side effects are not sorted because order may matter, e.g. `import 'polyfills';`
35+
2. Local, absolute modules with side-effects are not sorted because order may matter, e.g. `import '@app/polyfills';`
36+
3. Local, relative modules with side effects are not sorted because order may matter, e.g. `import './polyfills';`
37+
4. Third-party modules are sorted by name, e.g. `import { endOfMonth } from 'date-fns';`
38+
5. Built-in Node.js modules are sorted by name, e.g. `import * as fs from 'fs';`
39+
6. Local, absolute modules are sorted by the prefix-order provided and then by name `import { LIMIT } from '@app/app.constants';`
40+
7. Local, relative modules are sorted by "relative depth" and then by name `import { LIMIT } from './app.constants';`
41+
42+
See the [tests](./test/main.spec.ts) for more examples.

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
};

0 commit comments

Comments
 (0)