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.

124 lines
3.9 KiB

  1. # import/no-extraneous-dependencies: Forbid the use of extraneous packages
  2. Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, or `bundledDependencies`.
  3. The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. This behaviour can be changed with the rule option `packageDir`.
  4. Modules have to be installed for this rule to work.
  5. ### Options
  6. This rule supports the following options:
  7. `devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`.
  8. `optionalDependencies`: If set to `false`, then the rule will show an error when `optionalDependencies` are imported. Defaults to `true`.
  9. `peerDependencies`: If set to `false`, then the rule will show an error when `peerDependencies` are imported. Defaults to `false`.
  10. `bundledDependencies`: If set to `false`, then the rule will show an error when `bundledDependencies` are imported. Defaults to `true`.
  11. You can set the options like this:
  12. ```js
  13. "import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}]
  14. ```
  15. You can also use an array of globs instead of literal booleans:
  16. ```js
  17. "import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.js", "**/*.spec.js"]}]
  18. ```
  19. When using an array of globs, the setting will be set to `true` (no errors reported) if the name of the file being linted matches a single glob in the array, and `false` otherwise.
  20. Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json.
  21. If provided as a relative path string, will be computed relative to the current working directory at linter execution time. If this is not ideal (does not work with some editor integrations), consider using `__dirname` to provide a path relative to your configuration.
  22. ```js
  23. "import/no-extraneous-dependencies": ["error", {"packageDir": './some-dir/'}]
  24. // or
  25. "import/no-extraneous-dependencies": ["error", {"packageDir": path.join(__dirname, 'some-dir')}]
  26. ```
  27. It may also be an array of multiple paths, to support monorepos or other novel project
  28. folder layouts:
  29. ```js
  30. "import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
  31. ```
  32. ## Rule Details
  33. Given the following `package.json`:
  34. ```json
  35. {
  36. "name": "my-project",
  37. "...": "...",
  38. "dependencies": {
  39. "builtin-modules": "^1.1.1",
  40. "lodash.cond": "^4.2.0",
  41. "lodash.find": "^4.2.0",
  42. "pkg-up": "^1.0.0"
  43. },
  44. "devDependencies": {
  45. "ava": "^0.13.0",
  46. "eslint": "^2.4.0",
  47. "eslint-plugin-ava": "^1.3.0",
  48. "xo": "^0.13.0"
  49. },
  50. "optionalDependencies": {
  51. "lodash.isarray": "^4.0.0"
  52. },
  53. "peerDependencies": {
  54. "react": ">=15.0.0 <16.0.0"
  55. },
  56. "bundledDependencies": [
  57. "@generated/foo",
  58. ]
  59. }
  60. ```
  61. ## Fail
  62. ```js
  63. var _ = require('lodash');
  64. import _ from 'lodash';
  65. import react from 'react';
  66. /* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": false}] */
  67. import test from 'ava';
  68. var test = require('ava');
  69. /* eslint import/no-extraneous-dependencies: ["error", {"optionalDependencies": false}] */
  70. import isArray from 'lodash.isarray';
  71. var isArray = require('lodash.isarray');
  72. /* eslint import/no-extraneous-dependencies: ["error", {"bundledDependencies": false}] */
  73. import foo from '"@generated/foo"';
  74. var foo = require('"@generated/foo"');
  75. ```
  76. ## Pass
  77. ```js
  78. // Builtin and internal modules are fine
  79. var path = require('path');
  80. var foo = require('./foo');
  81. import test from 'ava';
  82. import find from 'lodash.find';
  83. import isArray from 'lodash.isarray';
  84. import foo from '"@generated/foo"';
  85. /* eslint import/no-extraneous-dependencies: ["error", {"peerDependencies": true}] */
  86. import react from 'react';
  87. ```
  88. ## When Not To Use It
  89. If you do not have a `package.json` file in your project.