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