source: frontend/node_modules/eslint-plugin-import/docs/rules/no-duplicates.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.5 KB
Line 
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
9Reports if a resolved path is imported more than once.
10
11ESLint core has a similar rule ([`no-duplicate-imports`](https://eslint.org/docs/rules/no-duplicate-imports)), but this version
12is different in two key ways:
13
141. 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`)
152. 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
19Valid:
20
21```js
22import SomeDefaultClass, * as names from './mod'
23// Flow `type` import from same module is fine
24import type SomeType from './mod'
25```
26
27...whereas here, both `./mod` imports will be reported:
28
29```js
30import SomeDefaultClass from './mod'
31
32// oops, some other import separated these lines
33import foo from './some-other-mod'
34
35import * as names from './mod'
36
37// will catch this too, assuming it is the same target module
38import { something } from './mod.js'
39```
40
41The motivation is that this is likely a result of two developers importing different
42names from the same module at different times (and potentially largely different
43locations in the file.) This rule brings both (or n-many) to attention.
44
45### Query Strings
46
47By 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
49Config:
50
51```json
52"import/no-duplicates": ["error", {"considerQueryString": true}]
53```
54
55And then the following code becomes valid:
56
57```js
58import minifiedMod from './mod?minify'
59import noCommentsMod from './mod?comments=0'
60import originalMod from './mod'
61```
62
63It will still catch duplicates when using the same module and the exact same query string:
64
65```js
66import SomeDefaultClass from './mod?minify'
67
68// This is invalid, assuming `./mod` and `./mod.js` are the same target:
69import * from './mod.js?minify'
70```
71
72### Inline Type imports
73
74TypeScript 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
76Config:
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
87import { AValue, type AType } from './mama-mia'
88import type { BType } from './mama-mia'
89
90import { CValue } from './papa-mia'
91import type { CType } from './papa-mia'
92```
93
94✅ Valid with `["error", {"prefer-inline": true}]`
95
96```js
97import { AValue, type AType, type BType } from './mama-mia'
98
99import { CValue, type CType } from './papa-mia'
100```
101
102<!--tabs-->
103
104## When Not To Use It
105
106If 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
108If you like to split up imports across lines or may need to import a default and a namespace,
109you may not want to enable this rule.
Note: See TracBrowser for help on using the repository browser.