版本

no-duplicate-imports

禁止重复模块导入

每个模块使用单个 import 语句将使代码更清晰,因为您可以在一行上看到从该模块导入的所有内容。

在以下示例中,第 1 行的 module 导入在第 3 行重复。这些可以组合起来,使导入列表更简洁。

import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';

规则详情

此规则要求来自单个模块的所有可以合并的导入都存在于单个 import 语句中。

此规则的错误代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: "error"*/

import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';

此规则的正确代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: "error"*/

import { merge, find } from 'module';
import something from 'another-module';

此规则的正确代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: "error"*/

// not mergeable
import { merge } from 'module';
import * as something from 'module';

选项

此规则接受一个可选参数,一个带有单个键的对象 includeExports,它是一个 boolean。它默认为 false

如果从导入的模块重新导出,您应该将导入添加到 import 语句中,并直接导出,而不是使用 export ... from

此规则使用 { "includeExports": true } 选项的错误代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge } from 'module';

export { find } from 'module';

此规则使用 { "includeExports": true } 选项的正确代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

export { find };

此规则使用 { "includeExports": true } 选项的正确代码示例

在 Playground 中打开
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

// cannot be merged with the above import
export * as something from 'module';

// cannot be written differently
export * from 'module';

版本

此规则在 ESLint v2.5.0 中引入。

资源

更改语言