no-internal-modules.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. url: (0, _docsUrl2.default)('no-internal-modules') },
  11. schema: [
  12. {
  13. oneOf: [
  14. {
  15. type: 'object',
  16. properties: {
  17. allow: {
  18. type: 'array',
  19. items: {
  20. type: 'string' } } },
  21. additionalProperties: false },
  22. {
  23. type: 'object',
  24. properties: {
  25. forbid: {
  26. type: 'array',
  27. items: {
  28. type: 'string' } } },
  29. additionalProperties: false }] }] },
  30. create: function noReachingInside(context) {
  31. const options = context.options[0] || {};
  32. const allowRegexps = (options.allow || []).map(p => _minimatch2.default.makeRe(p));
  33. const forbidRegexps = (options.forbid || []).map(p => _minimatch2.default.makeRe(p));
  34. // minimatch patterns are expected to use / path separators, like import
  35. // statements, so normalize paths to use the same
  36. function normalizeSep(somePath) {
  37. return somePath.split('\\').join('/');
  38. }
  39. function toSteps(somePath) {
  40. return normalizeSep(somePath).
  41. split('/').
  42. reduce((acc, step) => {
  43. if (!step || step === '.') {
  44. return acc;
  45. } else if (step === '..') {
  46. return acc.slice(0, -1);
  47. } else {
  48. return acc.concat(step);
  49. }
  50. }, []);
  51. }
  52. // test if reaching to this destination is allowed
  53. function reachingAllowed(importPath) {
  54. return allowRegexps.some(re => re.test(importPath));
  55. }
  56. // test if reaching to this destination is forbidden
  57. function reachingForbidden(importPath) {
  58. return forbidRegexps.some(re => re.test(importPath));
  59. }
  60. function isAllowViolation(importPath) {
  61. const steps = toSteps(importPath);
  62. const nonScopeSteps = steps.filter(step => 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. const justSteps = steps.join('/');
  67. if (reachingAllowed(justSteps) || reachingAllowed(`/${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. const 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. const steps = toSteps(importPath);
  78. // before trying to resolve, see if the raw import (with relative
  79. // segments resolved) matches a forbidden pattern
  80. const justSteps = steps.join('/');
  81. if (reachingForbidden(justSteps) || reachingForbidden(`/${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. const 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. const isReachViolation = options.forbid ? isForbidViolation : isAllowViolation;
  91. function checkImportForReaching(importPath, node) {
  92. const potentialViolationTypes = ['parent', 'index', 'sibling', 'external', 'internal'];
  93. if (potentialViolationTypes.indexOf((0, _importType2.default)(importPath, context)) !== -1 &&
  94. isReachViolation(importPath))
  95. {
  96. context.report({
  97. node,
  98. message: `Reaching to "${importPath}" is not allowed.` });
  99. }
  100. }
  101. return (0, _moduleVisitor2.default)(source => {
  102. checkImportForReaching(source.value, source);
  103. }, { commonjs: true });
  104. } };
  105. //#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pb24nLFxuICAgIGRvY3M6IHtcbiAgICAgIHVybDogZG9jc1VybCgnbm8taW50ZXJuYWwtbW9kdWxlcycpLFxuICAgIH0sXG5cbiAgICBzY2hlbWE6IFtcbiAgICAgIHtcbiAgICAgICAgb25lT2Y6IFtcbiAgICAgICAgICB7XG4gICAgICAgICAgICB0eXBlOiAnb2JqZWN0JyxcbiAgICAgICAgICAgIHByb3BlcnRpZXM6IHtcbiAgICAgICAgICAgICAgYWxsb3c6IHtcbiAgICAgICAgICAgICAgICB0eXBlOiAnYXJyYXknLFxuICAgICAgICAgICAgICAgIGl0ZW1zOiB7XG4gICAgICAgICAgICAgICAgICB0eXBlOiAnc3RyaW5nJyxcbiAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgfSxcbiAgICAgICAgICAgIGFkZGl0aW9uYWxQcm9wZXJ0aWVzOiBmYWxzZSxcbiAgICAgICAgICB9LFxuICAgICAgICAgIHtcbiAgICAgICAgICAgIHR5cGU6ICdvYmplY3QnLFxuICAgICAgICAgICAgcHJvcGVydGllczoge1xuICAgICAgICAgICAgICBmb3JiaWQ6IHtcbiAgICAgICAgICAgICAgICB0eXBlOiAnYXJyYXknLFxuICAgICAgICAgICAgICAgIGl0ZW1zOiB7XG4gICAgICAgICAgICAgICAgICB0eXBlOiAnc3RyaW5nJyxcbiAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgfSxcbiAgICAgICAgICAgIGFkZGl0aW9uYWxQcm9wZXJ0aWVzOiBmYWxzZSxcbiAgICAgICAgICB9LFxuICAgICAgICBdLFxuICAgICAgfSxcbiAgICBdLFxuICB9LFxuXG4gIGNyZWF0ZTogZnVuY3Rpb24gbm9SZWFjaGluZ0luc2lkZShjb250ZXh0KSB7XG4gICAgY29uc3Qgb3B0aW9ucyA9IGNvbnRleHQub3B0aW9uc1swXSB8fCB7fTtcbiAgICBjb25zdCBhbGxvd1JlZ2V4cHMgPSA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