web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

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