Constnium/node_modules/eslint-plugin-import/lib/rules/no-restricted-paths.js

180 lines
21 KiB
JavaScript
Raw Normal View History

2022-06-23 02:27:43 +02:00
'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
var _isGlob = require('is-glob');var _isGlob2 = _interopRequireDefault(_isGlob);
var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
var containsPath = function containsPath(filepath, target) {
var relative = _path2['default'].relative(target, filepath);
return relative === '' || !relative.startsWith('..');
};
module.exports = {
meta: {
type: 'problem',
docs: {
url: (0, _docsUrl2['default'])('no-restricted-paths') },
schema: [
{
type: 'object',
properties: {
zones: {
type: 'array',
minItems: 1,
items: {
type: 'object',
properties: {
target: { type: 'string' },
from: { type: 'string' },
except: {
type: 'array',
items: {
type: 'string' },
uniqueItems: true },
message: { type: 'string' } },
additionalProperties: false } },
basePath: { type: 'string' } },
additionalProperties: false }] },
create: function () {function noRestrictedPaths(context) {
var options = context.options[0] || {};
var restrictedPaths = options.zones || [];
var basePath = options.basePath || process.cwd();
var currentFilename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
var matchingZones = restrictedPaths.filter(function (zone) {
var targetPath = _path2['default'].resolve(basePath, zone.target);
if ((0, _isGlob2['default'])(targetPath)) {
return (0, _minimatch2['default'])(currentFilename, targetPath);
}
return containsPath(currentFilename, targetPath);
});
function isValidExceptionPath(absoluteFromPath, absoluteExceptionPath) {
var relativeExceptionPath = _path2['default'].relative(absoluteFromPath, absoluteExceptionPath);
return (0, _importType2['default'])(relativeExceptionPath, context) !== 'parent';
}
function reportInvalidExceptionPath(node) {
context.report({
node: node,
message: 'Restricted path exceptions must be descendants of the configured `from` path for that zone.' });
}
function reportInvalidExceptionGlob(node) {
context.report({
node: node,
message: 'Restricted path exceptions must be glob patterns when`from` is a glob pattern' });
}
var makePathValidator = function () {function makePathValidator(zoneFrom) {var zoneExcept = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var absoluteFrom = _path2['default'].resolve(basePath, zoneFrom);
var isGlobPattern = (0, _isGlob2['default'])(zoneFrom);
var isPathRestricted = void 0;
var hasValidExceptions = void 0;
var isPathException = void 0;
var reportInvalidException = void 0;
if (isGlobPattern) {
var mm = new _minimatch.Minimatch(absoluteFrom);
isPathRestricted = function () {function isPathRestricted(absoluteImportPath) {return mm.match(absoluteImportPath);}return isPathRestricted;}();
hasValidExceptions = zoneExcept.every(_isGlob2['default']);
if (hasValidExceptions) {
var exceptionsMm = zoneExcept.map(function (except) {return new _minimatch.Minimatch(except);});
isPathException = function () {function isPathException(absoluteImportPath) {return exceptionsMm.some(function (mm) {return mm.match(absoluteImportPath);});}return isPathException;}();
}
reportInvalidException = reportInvalidExceptionGlob;
} else {
isPathRestricted = function () {function isPathRestricted(absoluteImportPath) {return containsPath(absoluteImportPath, absoluteFrom);}return isPathRestricted;}();
var absoluteExceptionPaths = zoneExcept.
map(function (exceptionPath) {return _path2['default'].resolve(absoluteFrom, exceptionPath);});
hasValidExceptions = absoluteExceptionPaths.
every(function (absoluteExceptionPath) {return isValidExceptionPath(absoluteFrom, absoluteExceptionPath);});
if (hasValidExceptions) {
isPathException = function () {function isPathException(absoluteImportPath) {return absoluteExceptionPaths.some(
function (absoluteExceptionPath) {return containsPath(absoluteImportPath, absoluteExceptionPath);});}return isPathException;}();
}
reportInvalidException = reportInvalidExceptionPath;
}
return {
isPathRestricted: isPathRestricted,
hasValidExceptions: hasValidExceptions,
isPathException: isPathException,
reportInvalidException: reportInvalidException };
}return makePathValidator;}();
var validators = [];
function checkForRestrictedImportPath(importPath, node) {
var absoluteImportPath = (0, _resolve2['default'])(importPath, context);
if (!absoluteImportPath) {
return;
}
matchingZones.forEach(function (zone, index) {
if (!validators[index]) {
validators[index] = makePathValidator(zone.from, zone.except);
}var _validators$index =
validators[index],isPathRestricted = _validators$index.isPathRestricted,hasValidExceptions = _validators$index.hasValidExceptions,isPathException = _validators$index.isPathException,reportInvalidException = _validators$index.reportInvalidException;
if (!isPathRestricted(absoluteImportPath)) {
return;
}
if (!hasValidExceptions) {
reportInvalidException(node);
return;
}
var pathIsExcepted = isPathException(absoluteImportPath);
if (pathIsExcepted) {
return;
}
context.report({
node: node,
message: 'Unexpected path "{{importPath}}" imported in restricted zone.' + (zone.message ? ' ' + String(zone.message) : ''),
data: { importPath: importPath } });
});
}
return (0, _moduleVisitor2['default'])(function (source) {
checkForRestrictedImportPath(source.value, source);
}, { commonjs: true });
}return noRestrictedPaths;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZXN0cmljdGVkLXBhdGhzLmpzIl0sIm5hbWVzIjpbImNvbnRhaW5zUGF0aCIsImZpbGVwYXRoIiwidGFyZ2V0IiwicmVsYXRpdmUiLCJwYXRoIiwic3RhcnRzV2l0aCIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiem9uZXMiLCJtaW5JdGVtcyIsIml0ZW1zIiwiZnJvbSIsImV4Y2VwdCIsInVuaXF1ZUl0ZW1zIiwibWVzc2FnZSIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiYmFzZVBhdGgiLCJjcmVhdGUiLCJub1Jlc3RyaWN0ZWRQYXRocyIsImNvbnRleHQiLCJvcHRpb25zIiwicmVzdHJpY3RlZFBhdGhzIiwicHJvY2VzcyIsImN3ZCIsImN1cnJlbnRGaWx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