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.

140 lines
16 KiB

2 years ago
'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: {
url: (0, _docsUrl2['default'])('no-internal-modules') },
schema: [
{
oneOf: [
{
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);});
// minimatch patterns are expected to use / path separators, like import
// statements, so normalize paths to use the same
function normalizeSep(somePath) {
return somePath.split('\\').join('/');
}
function toSteps(somePath) {
return normalizeSep(somePath).
split('/').
reduce(function (acc, step) {
if (!step || step === '.') {
return acc;
} else if (step === '..') {
return acc.slice(0, -1);
} else {
return acc.concat(step);
}
}, []);
}
// test if reaching to this destination is allowed
function reachingAllowed(importPath) {
return allowRegexps.some(function (re) {return re.test(importPath);});
}
// test if reaching to this destination is forbidden
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;
// before trying to resolve, see if the raw import (with relative
// segments resolved) matches an allowed pattern
var justSteps = steps.join('/');
if (reachingAllowed(justSteps) || reachingAllowed('/' + String(justSteps))) return false;
// if the import statement doesn't match directly, try to match the
// resolved path if the import is resolvable
var resolved = (0, _resolve2['default'])(importPath, context);
if (!resolved || reachingAllowed(normalizeSep(resolved))) return false;
// this import was not allowed by the allowed paths, and reaches
// so it is a violation
return true;
}
function isForbidViolation(importPath) {
var steps = toSteps(importPath);
// before trying to resolve, see if the raw import (with relative
// segments resolved) matches a forbidden pattern
var justSteps = steps.join('/');
if (reachingForbidden(justSteps) || reachingForbidden('/' + String(justSteps))) return true;
// if the import statement doesn't match directly, try to match the
// resolved path if the import is resolvable
var resolved = (0, _resolve2['default'])(importPath, context);
if (resolved && reachingForbidden(normalizeSep(resolved))) return true;
// this import was not forbidden by the forbidden paths so it is not a violation
return false;
}
// find a directory that is being reached into, but which shouldn't be
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;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbnRlcm5hbC1tb2R1bGVzLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJvbmVPZiIsInByb3BlcnRpZXMiLCJhbGxvdyIsIml0ZW1zIiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJmb3JiaWQiLCJjcmVhdGUiLCJub1JlYWNoaW5nSW5za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