noRestrictedImports
Diagnostic Category: lint/nursery/noRestrictedImports
Since: v1.6.0
Sources:
- Same as:
no-restricted-imports
- Same as:
@typescript-eslint/no-restricted-imports
Disallow specified modules when loaded by import or require.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:8 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Using lodash is not encouraged.
> 1 │ import “lodash”;
│ ^^^^^^^^
2 │ import “allowed-import”;
3 │
code-block.js:1:33 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Do not import ‘underscore’.
> 1 │ const underscore = await import(“underscore”);
│ ^^^^^^^^^^^^
2 │
code-block.js:1:24 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Using lodash is not encouraged.
> 1 │ const lodash = require(“lodash”);
│ ^^^^^^^^
2 │
Valid
Section titled ValidSupported Import Syntaxes
Section titled Supported Import SyntaxesThe rule tries to parse the context of the import to see if only one or more of the allowed import names have been imported from a given module.
All of the following import syntaxes are supported:
Static import
(and re-export
) declarations
Section titled Static import (and re-export) declarationsNormal static ESM import
declarations are supported:
The TypeScript-specific type-only imports are also supported:
Dynamic import()
calls
Section titled Dynamic import() callsDynamic ESM import()
calls are also supported.
Because the import is performed at runtime, it is not always possible to determine which import names are being used.
Nevertheless, the rule tries to detect the following common usage patterns where the set of imported names is determined statically:
Dynamic require()
calls
Section titled Dynamic require() callsNodeJS-style require()
calls are also supported.
Due to the way require()
works, these are always treated as default imports.
Options
Section titled OptionsUse the options to specify the import paths and/or specific import names within them that you want to restrict in your source code.
paths
Section titled pathsAn object that lists the import paths that are either wholly or partially restricted.
The keys of the object are the import paths to restrict, and the values can be:
- A string with a custom message to show in the diagnostic when any
- An object with additional options, as explained below.
In the example below, we restrict the two paths services-deprecated
and constants
, with two particular messages.
Importing services-deprecated
will emit the message Use services instead.
.
Importing constants
will emit the message This file will be deleted soon.
:
code-block.js:1:33 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use services instead.
> 1 │ import * as namespaceAlias from ‘services-deprecated’;
│ ^^^^^^^^^^^^^^^^^^^^^
2 │
code-block.js:1:25 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This file will be deleted soon.
> 1 │ import { export1 } from ‘constants’;
│ ^^^^^^^^^^^
2 │
paths.<import>.message
Section titled paths.<import>.messageSpecifies the message to be shown when the restricted import is used.
A default message will be generated if message
is empty or not specified:
code-block.js:1:20 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ expected from
but instead found 'import-foo'
> 1 │ import { export1 } ‘import-foo’;
│ ^^^^^^^^^^^^
2 │
ℹ Remove ‘import-foo’
paths.<import>.importNames
Section titled paths.<import>.importNamesSpecifies the array of import names that should be explicitly forbidden. The following import name specifiers are supported:
- Named import:
"someIdentifier"
(import { someIdentifier } from 'named-import'
) - Default import:
"default"
(import defaultExport from 'default-import'
) - Namespace import:
"*"
(import * as alias1 from 'namespace-import'
) - Side effect/Bare import:
""
(import "sideeffect-import"
)
Only one of importNames
and allowImportNames
must be specified.
Invalid
Section titled Invalidcode-block.js:1:10 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Please use Bar from /import-bar/baz/ instead.
> 1 │ import { Bar } from ‘import-foo’;
│ ^^^
2 │
Valid
Section titled Validpaths.<import>.allowImportNames
Section titled paths.<import>.allowImportNamesSpecifies the set of import names that should be explicitly allowed.
See importNames
for the set of supported import name specifiers.
Only one of importNames
and allowImportNames
must be specified.
Invalid
Section titled Invalidcode-block.js:1:10 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Do not import ‘Baz’ from ‘import-bar’.
> 1 │ import { Baz } from ‘import-bar’;
│ ^^^
2 │
ℹ Only the following imports from ‘import-bar’ are allowed:
- Bar