| 1 | # import/no-duplicates
|
|---|
| 2 |
|
|---|
| 3 | ⚠️ This rule _warns_ in the following configs: ☑️ `recommended`, 🚸 `warnings`.
|
|---|
| 4 |
|
|---|
| 5 | 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|---|
| 6 |
|
|---|
| 7 | <!-- end auto-generated rule header -->
|
|---|
| 8 |
|
|---|
| 9 | Reports if a resolved path is imported more than once.
|
|---|
| 10 |
|
|---|
| 11 | ESLint core has a similar rule ([`no-duplicate-imports`](https://eslint.org/docs/rules/no-duplicate-imports)), but this version
|
|---|
| 12 | is different in two key ways:
|
|---|
| 13 |
|
|---|
| 14 | 1. the paths in the source code don't have to exactly match, they just have to point to the same module on the filesystem. (i.e. `./foo` and `./foo.js`)
|
|---|
| 15 | 2. this version distinguishes Flow `type` imports from standard imports. ([#334](https://github.com/import-js/eslint-plugin-import/pull/334))
|
|---|
| 16 |
|
|---|
| 17 | ## Rule Details
|
|---|
| 18 |
|
|---|
| 19 | Valid:
|
|---|
| 20 |
|
|---|
| 21 | ```js
|
|---|
| 22 | import SomeDefaultClass, * as names from './mod'
|
|---|
| 23 | // Flow `type` import from same module is fine
|
|---|
| 24 | import type SomeType from './mod'
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | ...whereas here, both `./mod` imports will be reported:
|
|---|
| 28 |
|
|---|
| 29 | ```js
|
|---|
| 30 | import SomeDefaultClass from './mod'
|
|---|
| 31 |
|
|---|
| 32 | // oops, some other import separated these lines
|
|---|
| 33 | import foo from './some-other-mod'
|
|---|
| 34 |
|
|---|
| 35 | import * as names from './mod'
|
|---|
| 36 |
|
|---|
| 37 | // will catch this too, assuming it is the same target module
|
|---|
| 38 | import { something } from './mod.js'
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | The motivation is that this is likely a result of two developers importing different
|
|---|
| 42 | names from the same module at different times (and potentially largely different
|
|---|
| 43 | locations in the file.) This rule brings both (or n-many) to attention.
|
|---|
| 44 |
|
|---|
| 45 | ### Query Strings
|
|---|
| 46 |
|
|---|
| 47 | By default, this rule ignores query strings (i.e. paths followed by a question mark), and thus imports from `./mod?a` and `./mod?b` will be considered as duplicates. However you can use the option `considerQueryString` to handle them as different (primarily because browsers will resolve those imports differently).
|
|---|
| 48 |
|
|---|
| 49 | Config:
|
|---|
| 50 |
|
|---|
| 51 | ```json
|
|---|
| 52 | "import/no-duplicates": ["error", {"considerQueryString": true}]
|
|---|
| 53 | ```
|
|---|
| 54 |
|
|---|
| 55 | And then the following code becomes valid:
|
|---|
| 56 |
|
|---|
| 57 | ```js
|
|---|
| 58 | import minifiedMod from './mod?minify'
|
|---|
| 59 | import noCommentsMod from './mod?comments=0'
|
|---|
| 60 | import originalMod from './mod'
|
|---|
| 61 | ```
|
|---|
| 62 |
|
|---|
| 63 | It will still catch duplicates when using the same module and the exact same query string:
|
|---|
| 64 |
|
|---|
| 65 | ```js
|
|---|
| 66 | import SomeDefaultClass from './mod?minify'
|
|---|
| 67 |
|
|---|
| 68 | // This is invalid, assuming `./mod` and `./mod.js` are the same target:
|
|---|
| 69 | import * from './mod.js?minify'
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | ### Inline Type imports
|
|---|
| 73 |
|
|---|
| 74 | TypeScript 4.5 introduced a new [feature](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names) that allows mixing of named value and type imports. In order to support fixing to an inline type import when duplicate imports are detected, `prefer-inline` can be set to true.
|
|---|
| 75 |
|
|---|
| 76 | Config:
|
|---|
| 77 |
|
|---|
| 78 | ```json
|
|---|
| 79 | "import/no-duplicates": ["error", {"prefer-inline": true}]
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | <!--tabs-->
|
|---|
| 83 |
|
|---|
| 84 | ❌ Invalid `["error", {"prefer-inline": true}]`
|
|---|
| 85 |
|
|---|
| 86 | ```js
|
|---|
| 87 | import { AValue, type AType } from './mama-mia'
|
|---|
| 88 | import type { BType } from './mama-mia'
|
|---|
| 89 |
|
|---|
| 90 | import { CValue } from './papa-mia'
|
|---|
| 91 | import type { CType } from './papa-mia'
|
|---|
| 92 | ```
|
|---|
| 93 |
|
|---|
| 94 | ✅ Valid with `["error", {"prefer-inline": true}]`
|
|---|
| 95 |
|
|---|
| 96 | ```js
|
|---|
| 97 | import { AValue, type AType, type BType } from './mama-mia'
|
|---|
| 98 |
|
|---|
| 99 | import { CValue, type CType } from './papa-mia'
|
|---|
| 100 | ```
|
|---|
| 101 |
|
|---|
| 102 | <!--tabs-->
|
|---|
| 103 |
|
|---|
| 104 | ## When Not To Use It
|
|---|
| 105 |
|
|---|
| 106 | If the core ESLint version is good enough (i.e. you're _not_ using Flow and you _are_ using [`import/extensions`](./extensions.md)), keep it and don't use this.
|
|---|
| 107 |
|
|---|
| 108 | If you like to split up imports across lines or may need to import a default and a namespace,
|
|---|
| 109 | you may not want to enable this rule.
|
|---|