108 lines
12 KiB
JavaScript
108 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"); } }; }(); /**
|
|
* @fileOverview Ensures that no imported module imports the linted module.
|
|
* @author Ben Mosher
|
|
*/
|
|
|
|
var _ExportMap = require('../ExportMap');
|
|
|
|
var _ExportMap2 = _interopRequireDefault(_ExportMap);
|
|
|
|
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 }; }
|
|
|
|
// todo: cache cycles / deep relationships for faster repeat evaluation
|
|
module.exports = {
|
|
meta: {
|
|
docs: { url: (0, _docsUrl2.default)('no-cycle') },
|
|
schema: [(0, _moduleVisitor.makeOptionsSchema)({
|
|
maxDepth: {
|
|
description: 'maximum dependency depth to traverse',
|
|
type: 'integer',
|
|
minimum: 1
|
|
}
|
|
})]
|
|
},
|
|
|
|
create: function (context) {
|
|
const myPath = context.getFilename();
|
|
if (myPath === '<text>') return {}; // can't cycle-check a non-file
|
|
|
|
const options = context.options[0] || {};
|
|
const maxDepth = options.maxDepth || Infinity;
|
|
|
|
function checkSourceValue(sourceNode, importer) {
|
|
const imported = _ExportMap2.default.get(sourceNode.value, context);
|
|
|
|
if (sourceNode.parent && sourceNode.parent.importKind === 'type') {
|
|
return; // no Flow import resolution
|
|
}
|
|
|
|
if (sourceNode._babelType === 'Literal') {
|
|
return; // no Flow import resolution, workaround for ESLint < 5.x
|
|
}
|
|
|
|
if (imported == null) {
|
|
return; // no-unresolved territory
|
|
}
|
|
|
|
if (imported.path === myPath) {
|
|
return; // no-self-import territory
|
|
}
|
|
|
|
const untraversed = [{ mget: () => imported, route: [] }];
|
|
const traversed = new Set();
|
|
function detectCycle(_ref) {
|
|
let mget = _ref.mget,
|
|
route = _ref.route;
|
|
|
|
const m = mget();
|
|
if (m == null) return;
|
|
if (traversed.has(m.path)) return;
|
|
traversed.add(m.path);
|
|
|
|
for (let _ref2 of m.imports) {
|
|
var _ref3 = _slicedToArray(_ref2, 2);
|
|
|
|
let path = _ref3[0];
|
|
var _ref3$ = _ref3[1];
|
|
let getter = _ref3$.getter;
|
|
let source = _ref3$.source;
|
|
|
|
if (path === myPath) return true;
|
|
if (traversed.has(path)) continue;
|
|
if (route.length + 1 < maxDepth) {
|
|
untraversed.push({
|
|
mget: getter,
|
|
route: route.concat(source)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
while (untraversed.length > 0) {
|
|
const next = untraversed.shift(); // bfs!
|
|
if (detectCycle(next)) {
|
|
const message = next.route.length > 0 ? `Dependency cycle via ${routeString(next.route)}` : 'Dependency cycle detected.';
|
|
context.report(importer, message);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (0, _moduleVisitor2.default)(checkSourceValue, context.options[0]);
|
|
}
|
|
};
|
|
|
|
function routeString(route) {
|
|
return route.map(s => `${s.value}:${s.loc.start.line}`).join('=>');
|
|
}
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLWN5Y2xlLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsInNjaGVtYSIsIm1heERl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