版本

no-duplicate-imports

禁止重複的模組匯入

每個模組使用單一 import 語句會使程式碼更清晰,因為您可以一行看到從該模組匯入的所有內容。

在以下範例中,第 1 行的 module 匯入在第 3 行重複。可以將它們合併,使匯入列表更簡潔。

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

規則詳情

此規則要求來自單一模組的所有可合併匯入都存在於單一 import 語句中。

此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint no-duplicate-imports: "error"*/

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

此規則的正確程式碼範例

在遊樂場中開啟
/*eslint no-duplicate-imports: "error"*/

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

此規則的正確程式碼範例

在遊樂場中開啟
/*eslint no-duplicate-imports: "error"*/

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

選項

此規則採用一個可選引數,一個帶有單一鍵 includeExports 的物件,該鍵為 boolean。它預設為 false

如果從匯入的模組重新匯出,您應該將匯入新增至 import 語句,並直接匯出,而不是使用 export ... from

使用 { "includeExports": true } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge } from 'module';

export { find } from 'module';

使用 { "includeExports": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

export { find };

使用 { "includeExports": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*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 中引入。

資源

變更語言