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.

69 lines
2.5 KiB

  1. # import/no-duplicates
  2. Reports if a resolved path is imported more than once.
  3. +(fixable) The `--fix` option on the [command line] automatically fixes some problems reported by this rule.
  4. ESLint core has a similar rule ([`no-duplicate-imports`](http://eslint.org/docs/rules/no-duplicate-imports)), but this version
  5. is different in two key ways:
  6. 1. the paths in the source code don't have to exactly match, they just have to point to the same module on the filesystem. (i.e. `./foo` and `./foo.js`)
  7. 2. this version distinguishes Flow `type` imports from standard imports. ([#334](https://github.com/benmosher/eslint-plugin-import/pull/334))
  8. ## Rule Details
  9. Valid:
  10. ```js
  11. import SomeDefaultClass, * as names from './mod'
  12. // Flow `type` import from same module is fine
  13. import type SomeType from './mod'
  14. ```
  15. ...whereas here, both `./mod` imports will be reported:
  16. ```js
  17. import SomeDefaultClass from './mod'
  18. // oops, some other import separated these lines
  19. import foo from './some-other-mod'
  20. import * as names from './mod'
  21. // will catch this too, assuming it is the same target module
  22. import { something } from './mod.js'
  23. ```
  24. The motivation is that this is likely a result of two developers importing different
  25. names from the same module at different times (and potentially largely different
  26. locations in the file.) This rule brings both (or n-many) to attention.
  27. ### Query Strings
  28. By default, this rule ignores query strings (i.e. paths followed by a question mark), and thus imports from `./mod?a` and `./mod?b` will be considered as duplicates. However you can use the option `considerQueryString` to handle them as different (primarily because browsers will resolve those imports differently).
  29. Config:
  30. ```json
  31. "import/no-duplicates": ["error", {"considerQueryString": true}]
  32. ```
  33. And then the following code becomes valid:
  34. ```js
  35. import minifiedMod from './mod?minify'
  36. import noCommentsMod from './mod?comments=0'
  37. import originalMod from './mod'
  38. ```
  39. It will still catch duplicates when using the same module and the exact same query string:
  40. ```js
  41. import SomeDefaultClass from './mod?minify'
  42. // This is invalid, assuming `./mod` and `./mod.js` are the same target:
  43. import * from './mod.js?minify'
  44. ```
  45. ## When Not To Use It
  46. If the core ESLint version is good enough (i.e. you're _not_ using Flow and you _are_ using [`import/extensions`](./extensions.md)), keep it and don't use this.
  47. If you like to split up imports across lines or may need to import a default and a namespace,
  48. you may not want to enable this rule.