no-duplicate-imports
禁止重复模块导入
每个模块使用单个 import
语句将使代码更清晰,因为你可以在一行中看到从该模块导入的所有内容。
¥Using a single import
statement per module will make the code clearer because you can see everything being imported from that module on one line.
在以下示例中,第 1 行的 module
导入在第 3 行重复。这些可以结合起来使导入清单更加简洁。
¥In the following example the module
import on line 1 is repeated on line 3. These can be combined to make the list of imports more succinct.
import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';
规则详情
¥Rule Details
此规则要求来自单个模块的所有可以合并的导入都存在于单个 import
语句中。
¥This rule requires that all imports from a single module that can be merged exist in a single import
statement.
此规则的错误代码示例:
¥Example of incorrect code for this rule:
/*eslint no-duplicate-imports: "error"*/
import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';
此规则的正确代码示例:
¥Example of correct code for this rule:
/*eslint no-duplicate-imports: "error"*/
import { merge, find } from 'module';
import something from 'another-module';
此规则的正确代码示例:
¥Example of correct code for this rule:
/*eslint no-duplicate-imports: "error"*/
// not mergeable
import { merge } from 'module';
import * as something from 'module';
选项
¥Options
此规则采用一个可选参数,即具有单个键的对象,includeExports
是 boolean
。默认为 false
。
¥This rule takes one optional argument, an object with a single key, includeExports
which is a boolean
. It defaults to false
.
如果从导入的模块重新导出,你应该将导入添加到 import
语句中,然后直接导出,而不是使用 export ... from
。
¥If re-exporting from an imported module, you should add the imports to the import
-statement, and export that directly, not use export ... from
.
使用 { "includeExports": true }
选项的此规则的错误代码示例:
¥Example of incorrect code for this rule with the { "includeExports": true }
option:
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/
import { merge } from 'module';
export { find } from 'module';
使用 { "includeExports": true }
选项的此规则的正确代码示例:
¥Example of correct code for this rule with the { "includeExports": true }
option:
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/
import { merge, find } from 'module';
export { find };
使用 { "includeExports": true }
选项的此规则的正确代码示例:
¥Example of correct code for this rule with the { "includeExports": true }
option:
/*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 中引入。