123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 'use strict';var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
- var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
- var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);
- var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
- var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
- module.exports = {
- meta: {
- type: 'suggestion',
- docs: {
- category: 'Static analysis',
- description: 'Forbid importing the submodules of other modules.',
- url: (0, _docsUrl2['default'])('no-internal-modules') },
- schema: [
- {
- anyOf: [
- {
- type: 'object',
- properties: {
- allow: {
- type: 'array',
- items: {
- type: 'string' } } },
- additionalProperties: false },
- {
- type: 'object',
- properties: {
- forbid: {
- type: 'array',
- items: {
- type: 'string' } } },
- additionalProperties: false }] }] },
- create: function () {function noReachingInside(context) {
- var options = context.options[0] || {};
- var allowRegexps = (options.allow || []).map(function (p) {return _minimatch2['default'].makeRe(p);});
- var forbidRegexps = (options.forbid || []).map(function (p) {return _minimatch2['default'].makeRe(p);});
-
-
- function normalizeSep(somePath) {
- return somePath.split('\\').join('/');
- }
- function toSteps(somePath) {
- return normalizeSep(somePath).
- split('/').
- filter(function (step) {return step && step !== '.';}).
- reduce(function (acc, step) {
- if (step === '..') {
- return acc.slice(0, -1);
- }
- return acc.concat(step);
- }, []);
- }
-
- function reachingAllowed(importPath) {
- return allowRegexps.some(function (re) {return re.test(importPath);});
- }
-
- function reachingForbidden(importPath) {
- return forbidRegexps.some(function (re) {return re.test(importPath);});
- }
- function isAllowViolation(importPath) {
- var steps = toSteps(importPath);
- var nonScopeSteps = steps.filter(function (step) {return step.indexOf('@') !== 0;});
- if (nonScopeSteps.length <= 1) {return false;}
-
-
- var justSteps = steps.join('/');
- if (reachingAllowed(justSteps) || reachingAllowed('/' + String(justSteps))) {return false;}
-
-
- var resolved = (0, _resolve2['default'])(importPath, context);
- if (!resolved || reachingAllowed(normalizeSep(resolved))) {return false;}
-
-
- return true;
- }
- function isForbidViolation(importPath) {
- var steps = toSteps(importPath);
-
-
- var justSteps = steps.join('/');
- if (reachingForbidden(justSteps) || reachingForbidden('/' + String(justSteps))) {return true;}
-
-
- var resolved = (0, _resolve2['default'])(importPath, context);
- if (resolved && reachingForbidden(normalizeSep(resolved))) {return true;}
-
- return false;
- }
-
- var isReachViolation = options.forbid ? isForbidViolation : isAllowViolation;
- function checkImportForReaching(importPath, node) {
- var potentialViolationTypes = ['parent', 'index', 'sibling', 'external', 'internal'];
- if (
- potentialViolationTypes.indexOf((0, _importType2['default'])(importPath, context)) !== -1 &&
- isReachViolation(importPath))
- {
- context.report({
- node: node,
- message: 'Reaching to "' + String(importPath) + '" is not allowed.' });
- }
- }
- return (0, _moduleVisitor2['default'])(
- function (source) {
- checkImportForReaching(source.value, source);
- },
- { commonjs: true });
- }return noReachingInside;}() };
|