π Enforce specific import styles per module.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
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'orrequire('foo')default-import path from 'path'orconst path = require('path')namespace-import * as path from 'path'orconst path = require('path')named-import {inspect} from 'util'orconst {inspect} = require('util')
// β
const util = require('node:util');
// β
const {promisify} = require('node:util');// β
import util from 'node:util';
// β
import * as util from 'node:util';
// β
import {promisify} from 'node:util';Type: object
You can extend default import styles per module by passing the styles option.
Default options per module are:
util-namedonlypath-defaultonlychalk-defaultonly
The example below:
- Disables any restrictions on the
utilmodule imports. - Allows
namedimport (leavingdefaultallowed too) from thepathmodule (by default onlydefaultimport ofpathis allowed).
"unicorn/import-style": [
"error",
{
"styles": {
"util": false,
"path": {
"named": true
}
}
}
]Type: boolean
Default: true
Pass "extendDefaultStyles": false to override the default styles option completely.
Type: boolean
Default: true
Pass "checkImport": false to disable linting of static import statements (like import ... from 'foo' or import 'foo') completely.
Type: boolean
Default: true
Pass "checkDynamicImport": false to disable linting of dynamic import statements (like await import('foo')) completely.
Type: boolean
Default: false
Pass "checkExportFrom": true to enable linting of export-from statements (like export ... from 'foo').
Type: boolean
Default: true
Pass "checkRequire": false to disable linting of require calls completely.