| 1 | # import/namespace
|
|---|
| 2 |
|
|---|
| 3 | 💼 This rule is enabled in the following configs: ❗ `errors`, ☑️ `recommended`.
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Enforces names exist at the time they are dereferenced, when imported as a full namespace (i.e. `import * as foo from './foo'; foo.bar();` will report if `bar` is not exported by `./foo`.).
|
|---|
| 8 |
|
|---|
| 9 | Will report at the import declaration if there are _no_ exported names found.
|
|---|
| 10 |
|
|---|
| 11 | Also, will report for computed references (i.e. `foo["bar"]()`).
|
|---|
| 12 |
|
|---|
| 13 | Reports on assignment to a member of an imported namespace.
|
|---|
| 14 |
|
|---|
| 15 | Note: for packages, the plugin will find exported names
|
|---|
| 16 | from [`jsnext:main`], if present in `package.json`.
|
|---|
| 17 | Redux's npm module includes this key, and thereby is lintable, for example.
|
|---|
| 18 |
|
|---|
| 19 | A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported.
|
|---|
| 20 |
|
|---|
| 21 | [ignored]: ../README.md#importignore
|
|---|
| 22 | [unambiguously an ES module]: https://github.com/bmeck/UnambiguousJavaScriptGrammar
|
|---|
| 23 |
|
|---|
| 24 | ## Rule Details
|
|---|
| 25 |
|
|---|
| 26 | Currently, this rule does not check for possible
|
|---|
| 27 | redefinition of the namespace in an intermediate scope. Adherence to the ESLint
|
|---|
| 28 | `no-shadow` rule for namespaces will prevent this from being a problem.
|
|---|
| 29 |
|
|---|
| 30 | For [ES7], reports if an exported namespace would be empty (no names exported from the referenced module.)
|
|---|
| 31 |
|
|---|
| 32 | Given:
|
|---|
| 33 |
|
|---|
| 34 | ```js
|
|---|
| 35 | // @module ./named-exports
|
|---|
| 36 | export const a = 1
|
|---|
| 37 | const b = 2
|
|---|
| 38 | export { b }
|
|---|
| 39 |
|
|---|
| 40 | const c = 3
|
|---|
| 41 | export { c as d }
|
|---|
| 42 |
|
|---|
| 43 | export class ExportedClass { }
|
|---|
| 44 |
|
|---|
| 45 | // ES7
|
|---|
| 46 | export * as deep from './deep'
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | and:
|
|---|
| 50 |
|
|---|
| 51 | ```js
|
|---|
| 52 | // @module ./deep
|
|---|
| 53 | export const e = "MC2"
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | See what is valid and reported:
|
|---|
| 57 |
|
|---|
| 58 | ```js
|
|---|
| 59 | // @module ./foo
|
|---|
| 60 | import * as names from './named-exports'
|
|---|
| 61 |
|
|---|
| 62 | function great() {
|
|---|
| 63 | return names.a + names.b // so great https://youtu.be/ei7mb8UxEl8
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | function notGreat() {
|
|---|
| 67 | doSomethingWith(names.c) // Reported: 'c' not found in imported namespace 'names'.
|
|---|
| 68 |
|
|---|
| 69 | const { a, b, c } = names // also reported, only for 'c'
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // also tunnels through re-exported namespaces!
|
|---|
| 73 | function deepTrouble() {
|
|---|
| 74 | doSomethingWith(names.deep.e) // fine
|
|---|
| 75 | doSomethingWith(names.deep.f) // Reported: 'f' not found in deeply imported namespace 'names.deep'.
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | ```
|
|---|
| 79 |
|
|---|
| 80 | ### Options
|
|---|
| 81 |
|
|---|
| 82 | #### `allowComputed`
|
|---|
| 83 |
|
|---|
| 84 | Defaults to `false`. When false, will report the following:
|
|---|
| 85 |
|
|---|
| 86 | ```js
|
|---|
| 87 | /*eslint import/namespace: [2, { allowComputed: false }]*/
|
|---|
| 88 | import * as a from './a'
|
|---|
| 89 |
|
|---|
| 90 | function f(x) {
|
|---|
| 91 | return a[x] // Unable to validate computed reference to imported namespace 'a'.
|
|---|
| 92 | }
|
|---|
| 93 | ```
|
|---|
| 94 |
|
|---|
| 95 | When set to `true`, the above computed namespace member reference is allowed, but
|
|---|
| 96 | still can't be statically analyzed any further.
|
|---|
| 97 |
|
|---|
| 98 | ## Further Reading
|
|---|
| 99 |
|
|---|
| 100 | - Lee Byron's [ES7] export proposal
|
|---|
| 101 | - [`import/ignore`] setting
|
|---|
| 102 | - [`jsnext:main`](Rollup)
|
|---|
| 103 |
|
|---|
| 104 | [ES7]: https://github.com/leebyron/ecmascript-more-export-from
|
|---|
| 105 | [`import/ignore`]: ../../README.md#importignore
|
|---|
| 106 | [`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main
|
|---|