Skip to content

Commit

Permalink
Add import-style rule (#789)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
futpib and sindresorhus committed Jul 30, 2020
1 parent 5bf60cc commit 0c7a199
Show file tree
Hide file tree
Showing 8 changed files with 1,143 additions and 1 deletion.
93 changes: 93 additions & 0 deletions docs/rules/import-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Enforce specific import styles per module

Sometimes a module contains unrelated functions, like `util`, thus it is a good practice to enforce destructuring or named imports here. Other times, in modules like `path`, it is good to use default import as they have similar functions, all likely to be utilized.

This rule defines 4 import styles:
- `unassigned` - `import 'foo'` or `require('foo')`
- `default` - `import path from 'path'` or `const path = require('path')`
- `namespace` - `import * as path from 'path'` or `const path = require('path')`
- `named` - `import {inspect} from 'util'` or `const {inspect} = require('util')`

## Fail

```js
const util = require('util');

import util from 'util';

import * as util from 'util';
```

## Pass

```js
const {promisify} = require('util');

import {promisify} from 'util';
```

## Options

### styles

Type: `object`

You can extend default import styles per module by passing the `styles` option.

Default options per module are:
- `util` - `named` only
- `path` - `default` only
- `chalk` - `default` only

The example below:
- Disables any restrictions on the `util` module imports.
- Allows `named` import (leaving `default` allowed too) from the `path` module (by default only `default` import of `path` is allowed).

```js
"unicorn/import-style": [
"error",
{
"styles": {
"util": false,
"path": {
"named": true
}
}
}
]
```

### extendDefaultStyles

Type: `boolean`\
Default: `true`

Pass `"extendDefaultStyles": false` to override the default `styles` option completely.

### checkImport

Type: `boolean`\
Default: `true`

Pass `"checkImport": false` to disable linting of static import statements (like `import ... from 'foo'` or `import 'foo'`) completely.

### checkDynamicImport

Type: `boolean`\
Default: `true`

Pass `"checkDynamicImport": false` to disable linting of dynamic import statements (like `await import('foo')`) completely.

### checkExportFrom

Type: `boolean`\
Default: `false`

Pass `"checkExportFrom": true` to enable linting of export-from statements (like `export ... from 'foo'`).

### checkRequire

Type: `boolean`\
Default: `true`

Pass `"checkRequire": false` to disable linting of `require` calls completely.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
'unicorn/explicit-length-check': 'error',
'unicorn/filename-case': 'error',
'unicorn/import-index': 'error',
'unicorn/import-style': 'error',
'unicorn/new-for-builtins': 'error',
'unicorn/no-abusive-eslint-disable': 'error',
'unicorn/no-array-instanceof': 'error',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ci-info": "^2.0.0",
"clean-regexp": "^1.0.0",
"eslint-ast-utils": "^1.1.0",
"eslint-template-visitor": "^2.0.0",
"eslint-template-visitor": "^2.2.1",
"eslint-utils": "^2.1.0",
"import-modules": "^2.0.0",
"lodash": "^4.17.15",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Configure it in `package.json`.
"unicorn/explicit-length-check": "error",
"unicorn/filename-case": "error",
"unicorn/import-index": "error",
"unicorn/import-style": "error",
"unicorn/new-for-builtins": "error",
"unicorn/no-abusive-eslint-disable": "error",
"unicorn/no-array-instanceof": "error",
Expand Down Expand Up @@ -106,6 +107,7 @@ Configure it in `package.json`.
- [explicit-length-check](docs/rules/explicit-length-check.md) - Enforce explicitly comparing the `length` property of a value. *(partly fixable)*
- [filename-case](docs/rules/filename-case.md) - Enforce a case style for filenames.
- [import-index](docs/rules/import-index.md) - Enforce importing index files with `.`. *(fixable)*
- [import-style](docs/rules/import-style.md) - Enforce specific import styles per module.
- [new-for-builtins](docs/rules/new-for-builtins.md) - Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. *(fixable)*
- [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) - Enforce specifying rules to disable in `eslint-disable` comments.
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Require `Array.isArray()` instead of `instanceof Array`. *(fixable)*
Expand Down

0 comments on commit 0c7a199

Please sign in to comment.