odin-js-fundamentals-part-5/node_modules/eslint-plugin-import/lib/rules/no-internal-modules.js

144 lines
16 KiB
JavaScript
Raw Normal View History

2024-01-11 08:52:05 +00:00
'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);});
// 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('/').
filter(function (step) {return step && step !== '.';}).
reduce(function (acc, step) {
if (step === '..') {
return acc.slice(0, -1);
}
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbnRlcm5hbC1tb2R1bGVzLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJjYXRlZ29yeSIsImRlc2NyaXB0aW9uIiwidXJsIiwic2NoZW1hIiwiYW55T2YiLCJwcm9wZXJ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