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.

144 lines
19 KiB

  1. 'use strict';
  2. var _ignore = require('eslint-module-utils/ignore');
  3. var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
  4. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  5. var _path = require('path');var _path2 = _interopRequireDefault(_path);
  6. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  7. /**
  8. * convert a potentially relative path from node utils into a true
  9. * relative path.
  10. *
  11. * ../ -> ..
  12. * ./ -> .
  13. * .foo/bar -> ./.foo/bar
  14. * ..foo/bar -> ./..foo/bar
  15. * foo/bar -> ./foo/bar
  16. *
  17. * @param relativePath {string} relative posix path potentially missing leading './'
  18. * @returns {string} relative posix path that always starts with a ./
  19. **/
  20. function toRelativePath(relativePath) {
  21. const stripped = relativePath.replace(/\/$/g, ''); // Remove trailing /
  22. return (/^((\.\.)|(\.))($|\/)/.test(stripped) ? stripped : `./${stripped}`);
  23. } /**
  24. * @fileOverview Ensures that there are no useless path segments
  25. * @author Thomas Grainger
  26. */function normalize(fn) {return toRelativePath(_path2.default.posix.normalize(fn));
  27. }
  28. function countRelativeParents(pathSegments) {
  29. return pathSegments.reduce((sum, pathSegment) => pathSegment === '..' ? sum + 1 : sum, 0);
  30. }
  31. module.exports = {
  32. meta: {
  33. type: 'suggestion',
  34. docs: {
  35. url: (0, _docsUrl2.default)('no-useless-path-segments') },
  36. fixable: 'code',
  37. schema: [
  38. {
  39. type: 'object',
  40. properties: {
  41. commonjs: { type: 'boolean' },
  42. noUselessIndex: { type: 'boolean' } },
  43. additionalProperties: false }] },
  44. create(context) {
  45. const currentDir = _path2.default.dirname(context.getFilename());
  46. const options = context.options[0];
  47. function checkSourceValue(source) {const
  48. importPath = source.value;
  49. function reportWithProposedPath(proposedPath) {
  50. context.report({
  51. node: source,
  52. // Note: Using messageIds is not possible due to the support for ESLint 2 and 3
  53. message: `Useless path segments for "${importPath}", should be "${proposedPath}"`,
  54. fix: fixer => proposedPath && fixer.replaceText(source, JSON.stringify(proposedPath)) });
  55. }
  56. // Only relative imports are relevant for this rule --> Skip checking
  57. if (!importPath.startsWith('.')) {
  58. return;
  59. }
  60. // Report rule violation if path is not the shortest possible
  61. const resolvedPath = (0, _resolve2.default)(importPath, context);
  62. const normedPath = normalize(importPath);
  63. const resolvedNormedPath = (0, _resolve2.default)(normedPath, context);
  64. if (normedPath !== importPath && resolvedPath === resolvedNormedPath) {
  65. return reportWithProposedPath(normedPath);
  66. }
  67. const fileExtensions = (0, _ignore.getFileExtensions)(context.settings);
  68. const regexUnnecessaryIndex = new RegExp(
  69. `.*\\/index(\\${Array.from(fileExtensions).join('|\\')})?$`);
  70. // Check if path contains unnecessary index (including a configured extension)
  71. if (options && options.noUselessIndex && regexUnnecessaryIndex.test(importPath)) {
  72. const parentDirectory = _path2.default.dirname(importPath);
  73. // Try to find ambiguous imports
  74. if (parentDirectory !== '.' && parentDirectory !== '..') {
  75. for (let fileExtension of fileExtensions) {
  76. if ((0, _resolve2.default)(`${parentDirectory}${fileExtension}`, context)) {
  77. return reportWithProposedPath(`${parentDirectory}/`);
  78. }
  79. }
  80. }
  81. return reportWithProposedPath(parentDirectory);
  82. }
  83. // Path is shortest possible + starts from the current directory --> Return directly
  84. if (importPath.startsWith('./')) {
  85. return;
  86. }
  87. // Path is not existing --> Return directly (following code requires path to be defined)
  88. if (resolvedPath === undefined) {
  89. return;
  90. }
  91. const expected = _path2.default.relative(currentDir, resolvedPath); // Expected import path
  92. const expectedSplit = expected.split(_path2.default.sep); // Split by / or \ (depending on OS)
  93. const importPathSplit = importPath.replace(/^\.\//, '').split('/');
  94. const countImportPathRelativeParents = countRelativeParents(importPathSplit);
  95. const countExpectedRelativeParents = countRelativeParents(expectedSplit);
  96. const diff = countImportPathRelativeParents - countExpectedRelativeParents;
  97. // Same number of relative parents --> Paths are the same --> Return directly
  98. if (diff <= 0) {
  99. return;
  100. }
  101. // Report and propose minimal number of required relative parents
  102. return reportWithProposedPath(
  103. toRelativePath(
  104. importPathSplit.
  105. slice(0, countExpectedRelativeParents).
  106. concat(importPathSplit.slice(countImportPathRelativeParents + diff)).
  107. join('/')));
  108. }
  109. return (0, _moduleVisitor2.default)(checkSourceValue, options);
  110. } };
  111. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11c2VsZXNzLXBhdGgtc2VnbWVudHMuanMiXSwibmFtZXMiOlsidG9SZWxhdGl2ZVBhdGgiLCJyZWxhdGl2ZVBhdGgiLCJzdHJpcHBlZCIsInJlcGxhY2UiLCJ0ZXN0Iiwibm9ybWFsaXplIiwiZm4iLCJwYXRoIiwicG9zaXgiLCJjb3VudFJlbGF0aXZlUGFyZW50cyIsInBhdGhTZWdtZW50cyIsInJlZHVjZSIsInN1bSIsInBhdGhTZWdtZW50IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiY29tbW9uanMiLCJub1VzZWxlc3NJbmRleCIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiY3JlYXRlIiwiY29udGV4dCIsImN1cnJlbnREaXIiLCJkaXJuYW1lIiwiZ2V0RmlsZW5hbWUiLCJvcHRpb25zIiwiY2hlY2tTb3VyY2VWYWx1ZSIsInNvdXJjZSIsImltcG9ydFBhdGgiLCJ2YWx1ZSIsInJlcG9ydFdpdGhQcm9wb3NlZFBhdGgiLCJwcm9wb3NlZFBhdGgiLCJyZXBvcnQiLCJub2RlIiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwicmVwbGFjZVRleHQiLCJKU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