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

128 lines
15 KiB
JavaScript
Raw Permalink Normal View History

2022-04-25 02:47:15 +00: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 _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);
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.' });
}
var zoneExceptions = matchingZones.map(function (zone) {
var exceptionPaths = zone.except || [];
var absoluteFrom = _path2['default'].resolve(basePath, zone.from);
var absoluteExceptionPaths = exceptionPaths.map(function (exceptionPath) {return _path2['default'].resolve(absoluteFrom, exceptionPath);});
var hasValidExceptionPaths = absoluteExceptionPaths.
every(function (absoluteExceptionPath) {return isValidExceptionPath(absoluteFrom, absoluteExceptionPath);});
return {
absoluteExceptionPaths: absoluteExceptionPaths,
hasValidExceptionPaths: hasValidExceptionPaths };
});
function checkForRestrictedImportPath(importPath, node) {
var absoluteImportPath = (0, _resolve2['default'])(importPath, context);
if (!absoluteImportPath) {
return;
}
matchingZones.forEach(function (zone, index) {
var absoluteFrom = _path2['default'].resolve(basePath, zone.from);
if (!containsPath(absoluteImportPath, absoluteFrom)) {
return;
}var _zoneExceptions$index =
zoneExceptions[index],hasValidExceptionPaths = _zoneExceptions$index.hasValidExceptionPaths,absoluteExceptionPaths = _zoneExceptions$index.absoluteExceptionPaths;
if (!hasValidExceptionPaths) {
reportInvalidExceptionPath(node);
return;
}
var pathIsExcepted = absoluteExceptionPaths.
some(function (absoluteExceptionPath) {return containsPath(absoluteImportPath, absoluteExceptionPath);});
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZXN0cmljdGVkLXBhdGhzLmpzIl0sIm5hbWVzIjpbImNvbnRhaW5zUGF0aCIsImZpbGVwYXRoIiwidGFyZ2V0IiwicmVsYXRpdmUiLCJwYXRoIiwic3RhcnRzV2l0aCIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJwcm9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