blob: 01ccb14ae70dd0141da64dd664a212374aa60a5d [file] [log] [blame] [view]
Tim van der Lippefdbd42e2020-04-07 14:14:361# import/named
2
3Verifies that all named imports are part of the set of named exports in the referenced module.
4
5For `export`, verifies that all named exports exist in the referenced module.
6
7Note: for packages, the plugin will find exported names
8from [`jsnext:main`], if present in `package.json`.
9Redux's npm module includes this key, and thereby is lintable, for example.
10
11A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported. Note that type imports and exports, as used by [Flow], are always ignored.
12
13[ignored]: ../../README.md#importignore
14[unambiguously an ES module]: https://github.com/bmeck/UnambiguousJavaScriptGrammar
15[Flow]: https://flow.org/
16
17
18## Rule Details
19
20Given:
21
22```js
23// ./foo.js
24export const foo = "I'm so foo"
25```
26
27The following is considered valid:
28
29```js
30// ./bar.js
31import { foo } from './foo'
32
33// ES7 proposal
34export { foo as bar } from './foo'
35
36// node_modules without jsnext:main are not analyzed by default
37// (import/ignore setting)
38import { SomeNonsenseThatDoesntExist } from 'react'
39```
40
41...and the following are reported:
42
43```js
44// ./baz.js
45import { notFoo } from './foo'
46
47// ES7 proposal
48export { notFoo as defNotBar } from './foo'
49
50// will follow 'jsnext:main', if available
51import { dontCreateStore } from 'redux'
52```
53
54### Settings
55
56[`import/ignore`] can be provided as a setting to ignore certain modules (node_modules,
57CoffeeScript, CSS if using Webpack, etc.).
58
59Given:
60
61```yaml
62# .eslintrc (YAML)
63---
64settings:
65 import/ignore:
66 - node_modules # included by default, but replaced if explicitly configured
67 - *.coffee$ # can't parse CoffeeScript (unless a custom polyglot parser was configured)
68```
69
70and
71
72```coffeescript
73# ./whatever.coffee
74exports.whatever = (foo) -> console.log foo
75```
76
77then the following is not reported:
78
79```js
80// ./foo.js
81
82// can't be analyzed, and ignored, so not reported
83import { notWhatever } from './whatever'
84```
85
86## When Not To Use It
87
88If you are using CommonJS and/or modifying the exported namespace of any module at
89runtime, you will likely see false positives with this rule.
90
91## Further Reading
92
93- [`import/ignore`] setting
94- [`jsnext:main`] (Rollup)
95
96
97[`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main
98[`import/ignore`]: ../../README.md#importignore