source: frontend/node_modules/eslint-plugin-import/docs/rules/namespace.md@ 9af201e

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
Line 
1# import/namespace
2
3💼 This rule is enabled in the following configs: ❗ `errors`, ☑️ `recommended`.
4
5<!-- end auto-generated rule header -->
6
7Enforces 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
9Will report at the import declaration if there are _no_ exported names found.
10
11Also, will report for computed references (i.e. `foo["bar"]()`).
12
13Reports on assignment to a member of an imported namespace.
14
15Note: for packages, the plugin will find exported names
16from [`jsnext:main`], if present in `package.json`.
17Redux's npm module includes this key, and thereby is lintable, for example.
18
19A 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
26Currently, this rule does not check for possible
27redefinition 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
30For [ES7], reports if an exported namespace would be empty (no names exported from the referenced module.)
31
32Given:
33
34```js
35// @module ./named-exports
36export const a = 1
37const b = 2
38export { b }
39
40const c = 3
41export { c as d }
42
43export class ExportedClass { }
44
45// ES7
46export * as deep from './deep'
47```
48
49and:
50
51```js
52// @module ./deep
53export const e = "MC2"
54```
55
56See what is valid and reported:
57
58```js
59// @module ./foo
60import * as names from './named-exports'
61
62function great() {
63 return names.a + names.b // so great https://youtu.be/ei7mb8UxEl8
64}
65
66function 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!
73function 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
84Defaults to `false`. When false, will report the following:
85
86```js
87/*eslint import/namespace: [2, { allowComputed: false }]*/
88import * as a from './a'
89
90function f(x) {
91 return a[x] // Unable to validate computed reference to imported namespace 'a'.
92}
93```
94
95When set to `true`, the above computed namespace member reference is allowed, but
96still 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
Note: See TracBrowser for help on using the repository browser.