119 lines
12 KiB
JavaScript
119 lines
12 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
||
|
|
||
|
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 _staticRequire = require('../core/staticRequire');
|
||
|
|
||
|
var _staticRequire2 = _interopRequireDefault(_staticRequire);
|
||
|
|
||
|
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: [{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
allow: {
|
||
|
type: 'array',
|
||
|
items: {
|
||
|
type: 'string'
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
additionalProperties: false
|
||
|
}]
|
||
|
},
|
||
|
|
||
|
create: function noReachingInside(context) {
|
||
|
const options = context.options[0] || {};
|
||
|
const allowRegexps = (options.allow || []).map(p => _minimatch2.default.makeRe(p));
|
||
|
|
||
|
// test if reaching to this destination is allowed
|
||
|
function reachingAllowed(importPath) {
|
||
|
return allowRegexps.some(re => re.test(importPath));
|
||
|
}
|
||
|
|
||
|
// 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('/');
|
||
|
}
|
||
|
|
||
|
// find a directory that is being reached into, but which shouldn't be
|
||
|
function isReachViolation(importPath) {
|
||
|
const steps = normalizeSep(importPath).split('/').reduce((acc, step) => {
|
||
|
if (!step || step === '.') {
|
||
|
return acc;
|
||
|
} else if (step === '..') {
|
||
|
return acc.slice(0, -1);
|
||
|
} else {
|
||
|
return acc.concat(step);
|
||
|
}
|
||
|
}, []);
|
||
|
|
||
|
const nonScopeSteps = steps.filter(step => 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
|
||
|
const justSteps = steps.join('/');
|
||
|
if (reachingAllowed(justSteps) || reachingAllowed(`/${justSteps}`)) return false;
|
||
|
|
||
|
// if the import statement doesn't match directly, try to match the
|
||
|
// resolved path if the import is resolvable
|
||
|
const 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 checkImportForReaching(importPath, node) {
|
||
|
const potentialViolationTypes = ['parent', 'index', 'sibling', 'external', 'internal'];
|
||
|
if (potentialViolationTypes.indexOf((0, _importType2.default)(importPath, context)) !== -1 && isReachViolation(importPath)) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `Reaching to "${importPath}" is not allowed.`
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration(node) {
|
||
|
checkImportForReaching(node.source.value, node.source);
|
||
|
},
|
||
|
CallExpression(node) {
|
||
|
if ((0, _staticRequire2.default)(node)) {
|
||
|
var _node$arguments = _slicedToArray(node.arguments, 1);
|
||
|
|
||
|
const firstArgument = _node$arguments[0];
|
||
|
|
||
|
checkImportForReaching(firstArgument.value, firstArgument);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbnRlcm5hbC1tb2R1bGVzLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwidHlwZSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiYWxsb3ciLCJpdGVtcyIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiY3JlYXRlIiwibm9SZWFjaGluZ0luc2lkZSIsImNvbnRleHQiLCJvcHRpb25zIiwiYWxsb3dSZWdleHBzIiwibWFwIiwicCIsIm1pbmltYXRjaCIsIm1ha2VSZSIsInJlYWNoaW5nQWxsb3dlZCIsImltcG9ydFBhdGgiLCJzb21lIiwicmUiLCJ0ZXN0Iiwibm9ybWFsaXplU2VwIiwic29tZVBhdGgiLCJzcGxpdCIsImpvaW4iLCJpc1JlYWNoVmlvbGF0aW9uIiwic3RlcHMiLCJyZWR1Y2UiLCJhY2MiLCJzdGVwIiwic2xpY2UiLCJjb25jYXQiLCJub25TY29wZVN0ZXBzIiwiZmlsdGVyIiwiaW5kZXhPZiIsImxlbmd0aCIsImp1c3RTdGVwcyIsInJlc29sdmVkIiwiY2hlY2tJbXBvcnRGb3JSZWFjaGluZyIsIm5vZGUiLCJwb3RlbnRpYWxWaW9sYXRpb25UeXBlcyIsInJlcG9ydCIsIm1lc3NhZ2UiLCJJbXBvcnREZWNsYXJhdG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