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.

206 lines
23 KiB

  1. 'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
  2. var _fs = require('fs');var _fs2 = _interopRequireDefault(_fs);
  3. var _readPkgUp = require('read-pkg-up');var _readPkgUp2 = _interopRequireDefault(_readPkgUp);
  4. var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
  5. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  6. var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
  7. var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);
  8. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  9. function hasKeys() {let obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  10. return Object.keys(obj).length > 0;
  11. }
  12. function arrayOrKeys(arrayOrObject) {
  13. return Array.isArray(arrayOrObject) ? arrayOrObject : Object.keys(arrayOrObject);
  14. }
  15. function extractDepFields(pkg) {
  16. return {
  17. dependencies: pkg.dependencies || {},
  18. devDependencies: pkg.devDependencies || {},
  19. optionalDependencies: pkg.optionalDependencies || {},
  20. peerDependencies: pkg.peerDependencies || {},
  21. // BundledDeps should be in the form of an array, but object notation is also supported by
  22. // `npm`, so we convert it to an array if it is an object
  23. bundledDependencies: arrayOrKeys(pkg.bundleDependencies || pkg.bundledDependencies || []) };
  24. }
  25. function getDependencies(context, packageDir) {
  26. let paths = [];
  27. try {
  28. const packageContent = {
  29. dependencies: {},
  30. devDependencies: {},
  31. optionalDependencies: {},
  32. peerDependencies: {},
  33. bundledDependencies: [] };
  34. if (packageDir && packageDir.length > 0) {
  35. if (!Array.isArray(packageDir)) {
  36. paths = [_path2.default.resolve(packageDir)];
  37. } else {
  38. paths = packageDir.map(dir => _path2.default.resolve(dir));
  39. }
  40. }
  41. if (paths.length > 0) {
  42. // use rule config to find package.json
  43. paths.forEach(dir => {
  44. const _packageContent = extractDepFields(
  45. JSON.parse(_fs2.default.readFileSync(_path2.default.join(dir, 'package.json'), 'utf8')));
  46. Object.keys(packageContent).forEach(depsKey =>
  47. Object.assign(packageContent[depsKey], _packageContent[depsKey]));
  48. });
  49. } else {
  50. // use closest package.json
  51. Object.assign(
  52. packageContent,
  53. extractDepFields(
  54. _readPkgUp2.default.sync({ cwd: context.getFilename(), normalize: false }).pkg));
  55. }
  56. if (![
  57. packageContent.dependencies,
  58. packageContent.devDependencies,
  59. packageContent.optionalDependencies,
  60. packageContent.peerDependencies,
  61. packageContent.bundledDependencies].
  62. some(hasKeys)) {
  63. return null;
  64. }
  65. return packageContent;
  66. } catch (e) {
  67. if (paths.length > 0 && e.code === 'ENOENT') {
  68. context.report({
  69. message: 'The package.json file could not be found.',
  70. loc: { line: 0, column: 0 } });
  71. }
  72. if (e.name === 'JSONError' || e instanceof SyntaxError) {
  73. context.report({
  74. message: 'The package.json file could not be parsed: ' + e.message,
  75. loc: { line: 0, column: 0 } });
  76. }
  77. return null;
  78. }
  79. }
  80. function missingErrorMessage(packageName) {
  81. return `'${packageName}' should be listed in the project's dependencies. ` +
  82. `Run 'npm i -S ${packageName}' to add it`;
  83. }
  84. function devDepErrorMessage(packageName) {
  85. return `'${packageName}' should be listed in the project's dependencies, not devDependencies.`;
  86. }
  87. function optDepErrorMessage(packageName) {
  88. return `'${packageName}' should be listed in the project's dependencies, ` +
  89. `not optionalDependencies.`;
  90. }
  91. function reportIfMissing(context, deps, depsOptions, node, name) {
  92. // Do not report when importing types
  93. if (node.importKind === 'type' || node.parent && node.parent.importKind === 'type') {
  94. return;
  95. }
  96. if ((0, _importType2.default)(name, context) !== 'external') {
  97. return;
  98. }
  99. const resolved = (0, _resolve2.default)(name, context);
  100. if (!resolved) {return;}
  101. const splitName = name.split('/');
  102. const packageName = splitName[0][0] === '@' ?
  103. splitName.slice(0, 2).join('/') :
  104. splitName[0];
  105. const isInDeps = deps.dependencies[packageName] !== undefined;
  106. const isInDevDeps = deps.devDependencies[packageName] !== undefined;
  107. const isInOptDeps = deps.optionalDependencies[packageName] !== undefined;
  108. const isInPeerDeps = deps.peerDependencies[packageName] !== undefined;
  109. const isInBundledDeps = deps.bundledDependencies.indexOf(packageName) !== -1;
  110. if (isInDeps ||
  111. depsOptions.allowDevDeps && isInDevDeps ||
  112. depsOptions.allowPeerDeps && isInPeerDeps ||
  113. depsOptions.allowOptDeps && isInOptDeps ||
  114. depsOptions.allowBundledDeps && isInBundledDeps)
  115. {
  116. return;
  117. }
  118. if (isInDevDeps && !depsOptions.allowDevDeps) {
  119. context.report(node, devDepErrorMessage(packageName));
  120. return;
  121. }
  122. if (isInOptDeps && !depsOptions.allowOptDeps) {
  123. context.report(node, optDepErrorMessage(packageName));
  124. return;
  125. }
  126. context.report(node, missingErrorMessage(packageName));
  127. }
  128. function testConfig(config, filename) {
  129. // Simplest configuration first, either a boolean or nothing.
  130. if (typeof config === 'boolean' || typeof config === 'undefined') {
  131. return config;
  132. }
  133. // Array of globs.
  134. return config.some(c =>
  135. (0, _minimatch2.default)(filename, c) ||
  136. (0, _minimatch2.default)(filename, _path2.default.join(process.cwd(), c)));
  137. }
  138. module.exports = {
  139. meta: {
  140. type: 'problem',
  141. docs: {
  142. url: (0, _docsUrl2.default)('no-extraneous-dependencies') },
  143. schema: [
  144. {
  145. 'type': 'object',
  146. 'properties': {
  147. 'devDependencies': { 'type': ['boolean', 'array'] },
  148. 'optionalDependencies': { 'type': ['boolean', 'array'] },
  149. 'peerDependencies': { 'type': ['boolean', 'array'] },
  150. 'bundledDependencies': { 'type': ['boolean', 'array'] },
  151. 'packageDir': { 'type': ['string', 'array'] } },
  152. 'additionalProperties': false }] },
  153. create: function (context) {
  154. const options = context.options[0] || {};
  155. const filename = context.getFilename();
  156. const deps = getDependencies(context, options.packageDir) || extractDepFields({});
  157. const depsOptions = {
  158. allowDevDeps: testConfig(options.devDependencies, filename) !== false,
  159. allowOptDeps: testConfig(options.optionalDependencies, filename) !== false,
  160. allowPeerDeps: testConfig(options.peerDependencies, filename) !== false,
  161. allowBundledDeps: testConfig(options.bundledDependencies, filename) !== false };
  162. return (0, _moduleVisitor2.default)(node => {
  163. reportIfMissing(context, deps, depsOptions, node, node.value);
  164. }, { commonjs: true });
  165. } };
  166. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1leHRyYW5lb3VzLWRlcGVuZGVuY2llcy5qcyJdLCJuYW1lcyI6WyJoYXNLZXlzIiwib2JqIiwiT2JqZWN0Iiwia2V5cyIsImxlbmd0aCIsImFycmF5T3JLZXlzIiwiYXJyYXlPck9iamVjdCIsIkFycmF5IiwiaXNBcnJheSIsImV4dHJhY3REZXBGaWVsZHMiLCJwa2ciLCJkZXBlbmRlbmNpZXMiLCJkZXZEZXBlbmRlbmNpZXMiLCJvcHRpb25hbERlcGVuZGVuY2llcyIsInBlZXJEZXBlbmRlbmNpZXMiLCJidW5kbGVkRGVwZW5kZW5jaWVzIiwiYnVuZGxlRGVwZW5kZW5jaWVzIiwiZ2V0RGVwZW5kZW5jaWVzIiwiY29udGV4dCIsInBhY2thZ2VEaXIiLCJwYXRocyIsInBhY2thZ2VDb250ZW50IiwicGF0aCIsInJlc29sdmUiLCJtYXAiLCJkaXIiLCJmb3JFYWNoIiwiX3BhY2thZ2VDb250ZW50IiwiSlNPTiIsInBhcnNlIiwiZnMiLCJyZWFkRmlsZVN5bmMiLCJqb2luIiwiZGVwc0tleSIsImFzc2lnbiIsInJlYWRQa2dVcCIsInN5bmMiLCJjd2QiLCJnZXRGaWxlbmFtZSIsIm5vcm1hbGl6ZSIsInNvbWUiLCJlIiwiY29kZSIsInJlcG9ydCIsIm1lc3NhZ2UiLCJsb2MiLCJsaW5lIiwiY29sd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