no-internal-modules.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
  2. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  3. var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);
  4. var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
  5. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  6. module.exports = {
  7. meta: {
  8. type: 'suggestion',
  9. docs: {
  10. category: 'Static analysis',
  11. description: 'Forbid importing the submodules of other modules.',
  12. url: (0, _docsUrl2['default'])('no-internal-modules') },
  13. schema: [
  14. {
  15. anyOf: [
  16. {
  17. type: 'object',
  18. properties: {
  19. allow: {
  20. type: 'array',
  21. items: {
  22. type: 'string' } } },
  23. additionalProperties: false },
  24. {
  25. type: 'object',
  26. properties: {
  27. forbid: {
  28. type: 'array',
  29. items: {
  30. type: 'string' } } },
  31. additionalProperties: false }] }] },
  32. create: function () {function noReachingInside(context) {
  33. var options = context.options[0] || {};
  34. var allowRegexps = (options.allow || []).map(function (p) {return _minimatch2['default'].makeRe(p);});
  35. var forbidRegexps = (options.forbid || []).map(function (p) {return _minimatch2['default'].makeRe(p);});
  36. // minimatch patterns are expected to use / path separators, like import
  37. // statements, so normalize paths to use the same
  38. function normalizeSep(somePath) {
  39. return somePath.split('\\').join('/');
  40. }
  41. function toSteps(somePath) {
  42. return normalizeSep(somePath).
  43. split('/').
  44. filter(function (step) {return step && step !== '.';}).
  45. reduce(function (acc, step) {
  46. if (step === '..') {
  47. return acc.slice(0, -1);
  48. }
  49. return acc.concat(step);
  50. }, []);
  51. }
  52. // test if reaching to this destination is allowed
  53. function reachingAllowed(importPath) {
  54. return allowRegexps.some(function (re) {return re.test(importPath);});
  55. }
  56. // test if reaching to this destination is forbidden
  57. function reachingForbidden(importPath) {
  58. return forbidRegexps.some(function (re) {return re.test(importPath);});
  59. }
  60. function isAllowViolation(importPath) {
  61. var steps = toSteps(importPath);
  62. var nonScopeSteps = steps.filter(function (step) {return step.indexOf('@') !== 0;});
  63. if (nonScopeSteps.length <= 1) {return false;}
  64. // before trying to resolve, see if the raw import (with relative
  65. // segments resolved) matches an allowed pattern
  66. var justSteps = steps.join('/');
  67. if (reachingAllowed(justSteps) || reachingAllowed('/' + String(justSteps))) {return false;}
  68. // if the import statement doesn't match directly, try to match the
  69. // resolved path if the import is resolvable
  70. var resolved = (0, _resolve2['default'])(importPath, context);
  71. if (!resolved || reachingAllowed(normalizeSep(resolved))) {return false;}
  72. // this import was not allowed by the allowed paths, and reaches
  73. // so it is a violation
  74. return true;
  75. }
  76. function isForbidViolation(importPath) {
  77. var steps = toSteps(importPath);
  78. // before trying to resolve, see if the raw import (with relative
  79. // segments resolved) matches a forbidden pattern
  80. var justSteps = steps.join('/');
  81. if (reachingForbidden(justSteps) || reachingForbidden('/' + String(justSteps))) {return true;}
  82. // if the import statement doesn't match directly, try to match the
  83. // resolved path if the import is resolvable
  84. var resolved = (0, _resolve2['default'])(importPath, context);
  85. if (resolved && reachingForbidden(normalizeSep(resolved))) {return true;}
  86. // this import was not forbidden by the forbidden paths so it is not a violation
  87. return false;
  88. }
  89. // find a directory that is being reached into, but which shouldn't be
  90. var isReachViolation = options.forbid ? isForbidViolation : isAllowViolation;
  91. function checkImportForReaching(importPath, node) {
  92. var potentialViolationTypes = ['parent', 'index', 'sibling', 'external', 'internal'];
  93. if (
  94. potentialViolationTypes.indexOf((0, _importType2['default'])(importPath, context)) !== -1 &&
  95. isReachViolation(importPath))
  96. {
  97. context.report({
  98. node: node,
  99. message: 'Reaching to "' + String(importPath) + '" is not allowed.' });
  100. }
  101. }
  102. return (0, _moduleVisitor2['default'])(
  103. function (source) {
  104. checkImportForReaching(source.value, source);
  105. },
  106. { commonjs: true });
  107. }return noReachingInside;}() };
  108. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbnRlcm5hbC1tb2R1bGVzLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJjYXRlZ29yeSIsImRlc2NyaXB0aW9uIiwidXJsIiwic2NoZW1hIiwiYW55T2YiLCJwcm9wZXJ0aWVzIiwiYWxsb3ciLCJpdGVtcyIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiZm9yYmlkIiwiY3JlYXRlIiwibm9SZWFjaGluZ0luc2lkZSIsImNvbnRleHQiLCJvcHRpb25zIiwiYWxsb3dSZWdleHBzIiwibWFwIiwicCIsIm1pbmltYXRjaCIsIm1ha2VSZSIsImZvcmJpZFJlZ2V4cHMiLCJub3JtYWxpemVTZXAiLCJzb21lUGF0aCIsInNwbGl0Iiwiam9pbiIsInRvU3RlcHMiLCJmaWx0ZXIiLCJzdGVwIiwicmVkdWNlIiwiYWNjIiwic2xpY2UiLCJjb25jYXQiLCJyZWFjaGluZ0FsbG93ZWQiLCJpbXBvcnRQYXRoIiwic29tZSIsInJlIiwidGVzdCIsInJlYWNoaW5nRm9yYmlkZGVuIiwiaXNBbGxvd1Zpb2xhdGlvbiIsInN0ZXBzIiwibm9uU2NvcGVTdGVwcyIsImluZGV4T2YiLCJsZW5ndGgiLCJqdXN0U3RlcHMiLCJyZXNvbHZlZCIsImlzRm9yYmlkVmlvbGF0aW9uIiwiaXNSZWFjaFZpb2xhdGlvbiIsImNoZWNrSW1wb3J0Rm9yUmVhY2hpbmciLCJub2RlIiwicG90ZW50aWFsVmlvbGF0aW9uVHlwZXMiLCJyZXBv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J21pbmltYXRjaCc7XG5cbmltcG9ydCByZXNvbHZlIGZyb20gJ2VzbGludC1tb2R1bGUtdXRpbHMvcmVzb2x2ZSc7XG5pbXBvcnQgaW1wb3J0VHlwZSBmcm9tICcuLi9jb3JlL2ltcG9ydFR5cGUnO1xuaW1wb3J0IG1vZHVsZVZpc2l0b3IgZnJvbSAnZXNsaW50LW1vZHVsZS11dGlscy9tb2R1bGVWaXNpdG9yJztcbmltcG9ydCBkb2NzVXJsIGZyb20gJy4uL2RvY3NVcmwnO1xuXG5tb2R1bGUuZXhwb3J0cyA9IHtcbiAgbWV0YToge1xuICAgIHR5cGU6ICdzdWdnZXN0aW9uJyxcbiAgICBkb2NzOiB7XG4gICAgICBjYXRlZ29yeTogJ1N0YXRpYyBhbmFseXNpcycsXG4gICAgICBkZXNjcmlwdGlvbjogJ0ZvcmJpZCBpbXBvcnRpbmcgdGhlIHN1Ym1vZHVsZXMgb2Ygb3RoZXIgbW9kdWxlcy4nLFxuICAgICAgdXJsOiBkb2NzVXJsKCduby1pbnRlcm5hbC1tb2R1bGVzJyksXG4gICAgfSxcblxuICAgIHNjaGVtYTogW1xuICAgICAge1xuICAgICAgICBhbnlPZjogW1xuICAgICAgICAgIHtcbiAgICAgICAgICAgIHR5cGU6ICdvYmplY3QnLFxuICAgICAgICAgICAgcHJvcGVydGllczoge1xuICAgICAgICAgICAgICBhbGxvdzoge1xuICAgICAgICAgICAgICAgIHR5cGU6ICdhcnJheScsXG4gICAgICAgICAgICAgICAgaXRlbXM6IHtcbiAgICAgICAgICAgICAgICAgIHR5cGU6ICdzdHJpbmcnLFxuICAgICAgICAgICAgICAgIH0sXG4gICAgICAgICAgICAgIH0sXG4gICAgICAgICAgICB9LFxuICAgICAgICAgICAgYWRkaXRpb25hbFByb3BlcnRpZXM6IGZhbHNlLFxuICAgICAgICAgIH0sXG4gICAgICAgICAge1xuICAgICAgICAgICAgdHlwZTogJ29iamVjdCcsXG4gICAgICAgICAgICBwcm9wZXJ0aWV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