noUnusedImports
¥Summary
-
规则生效日期:
v1.3.0¥Rule available since:
v1.3.0 -
诊断类别:
lint/correctness/noUnusedImports¥Diagnostic Category:
lint/correctness/noUnusedImports -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "correctness": { "noUnusedImports": "error" } } }}¥Description
禁止未使用的导入。
¥Disallow unused imports.
未使用的导入可能是重构不完整造成的。代码修复可以删除与 import 相关的注释。参见最后一个无效示例。
¥Unused imports might be the result of an incomplete refactoring.
The code fix can remove comments associated with an import.
See the last invalid example.
请注意,未使用的导入前面的琐碎内容(例如注释或换行符)也将被删除。这样像 @ts-expect-error 这样的注释指令就不会被转移到错误的地方。
¥Note that the leading trivia, e.g., comments or newlines preceding
the unused imports will also be removed. So that comment directives
like @ts-expect-error won’t be transferred to a wrong place.
¥Options
此规则尊重 jsxRuntime 设置,如果设置为 "reactClassic",则会对 React 全局变量进行例外处理。
¥This rule respects the jsxRuntime
setting and will make an exception for React globals if it is set to
"reactClassic".
¥Examples
¥Invalid
import A from 'mod';code-block.js:1:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This import is unused.
> 1 │ import A from ‘mod’;
│ ^
2 │
ℹ Unused imports might be the result of an incomplete refactoring.
ℹ Unsafe fix: Remove the unused imports.
1 │ import·A·from·‘mod’;
│ --------------------
import * as A from 'mod';code-block.js:1:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This import is unused.
> 1 │ import * as A from ‘mod’;
│ ^
2 │
ℹ Unused imports might be the result of an incomplete refactoring.
ℹ Unsafe fix: Remove the unused imports.
1 │ import·*·as·A·from·‘mod’;
│ -------------------------
import { type A, B } from 'mod';
export { B }code-block.ts:1:10 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Several of these imports are unused.
> 1 │ import { type A, B } from ‘mod’;
│ ^^^^^^
2 │
3 │ export { B }
ℹ Unused imports might be the result of an incomplete refactoring.
ℹ Unsafe fix: Remove the unused imports.
1 │ import·{·type·A,·B·}·from·‘mod’;
│ --------
// Header commentimport /*inner comment */ A from 'mod'; // Associated commentcode-block.ts:2:27 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This import is unused.
1 │ // Header comment
> 2 │ import /*inner comment */ A from ‘mod’; // Associated comment
│ ^
3 │
ℹ Unused imports might be the result of an incomplete refactoring.
ℹ Unsafe fix: Remove the unused imports.
1 │ - //·Header·comment
2 │ - import·/*inner·comment·*/·A·from·‘mod’;·//·Associated·comment
1 │ +
3 2 │
// Another header commentimport { // A's header comment type A, // A's comment // B's header comment B,} from 'mod';
export { B }code-block.ts:4:5 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Several of these imports are unused.
2 │ import {
3 │ // A’s header comment
> 4 │ type A, // A’s comment
│ ^^^^^^
5 │ // B’s header comment
6 │ B,
ℹ Unused imports might be the result of an incomplete refactoring.
ℹ Unsafe fix: Remove the unused imports.
1 1 │ // Another header comment
2 2 │ import {
3 │ - ····//·A’s·header·comment
4 │ - ····type·A,·//·A’s·comment
5 3 │ // B’s header comment
6 4 │ B,
¥Valid
import { A, type B } from 'mod';
function f(arg: B): A { return new A(arg);}一个值得注意的例外情况是,当导入用于类型增强时。
¥One notable exception is when the import is intended to be used for type augmentation.
import type {} from '@mui/lab/themeAugmentation';¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号