Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 1 | # import/named |
| 2 | |
| 3 | Verifies that all named imports are part of the set of named exports in the referenced module. |
| 4 | |
| 5 | For `export`, verifies that all named exports exist in the referenced module. |
| 6 | |
| 7 | Note: for packages, the plugin will find exported names |
| 8 | from [`jsnext:main`], if present in `package.json`. |
| 9 | Redux's npm module includes this key, and thereby is lintable, for example. |
| 10 | |
| 11 | A 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 | |
| 20 | Given: |
| 21 | |
| 22 | ```js |
| 23 | // ./foo.js |
| 24 | export const foo = "I'm so foo" |
| 25 | ``` |
| 26 | |
| 27 | The following is considered valid: |
| 28 | |
| 29 | ```js |
| 30 | // ./bar.js |
| 31 | import { foo } from './foo' |
| 32 | |
| 33 | // ES7 proposal |
| 34 | export { foo as bar } from './foo' |
| 35 | |
| 36 | // node_modules without jsnext:main are not analyzed by default |
| 37 | // (import/ignore setting) |
| 38 | import { SomeNonsenseThatDoesntExist } from 'react' |
| 39 | ``` |
| 40 | |
| 41 | ...and the following are reported: |
| 42 | |
| 43 | ```js |
| 44 | // ./baz.js |
| 45 | import { notFoo } from './foo' |
| 46 | |
| 47 | // ES7 proposal |
| 48 | export { notFoo as defNotBar } from './foo' |
| 49 | |
| 50 | // will follow 'jsnext:main', if available |
| 51 | import { dontCreateStore } from 'redux' |
| 52 | ``` |
| 53 | |
| 54 | ### Settings |
| 55 | |
| 56 | [`import/ignore`] can be provided as a setting to ignore certain modules (node_modules, |
| 57 | CoffeeScript, CSS if using Webpack, etc.). |
| 58 | |
| 59 | Given: |
| 60 | |
| 61 | ```yaml |
| 62 | # .eslintrc (YAML) |
| 63 | --- |
| 64 | settings: |
| 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 | |
| 70 | and |
| 71 | |
| 72 | ```coffeescript |
| 73 | # ./whatever.coffee |
| 74 | exports.whatever = (foo) -> console.log foo |
| 75 | ``` |
| 76 | |
| 77 | then the following is not reported: |
| 78 | |
| 79 | ```js |
| 80 | // ./foo.js |
| 81 | |
| 82 | // can't be analyzed, and ignored, so not reported |
| 83 | import { notWhatever } from './whatever' |
| 84 | ``` |
| 85 | |
| 86 | ## When Not To Use It |
| 87 | |
| 88 | If you are using CommonJS and/or modifying the exported namespace of any module at |
| 89 | runtime, 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 |