105 lines
12 KiB
JavaScript
105 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: {
|
|
type: 'suggestion',
|
|
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 (importer.importKind === 'type') {
|
|
return; // no Flow import resolution
|
|
}
|
|
|
|
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jeWNsZS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwic2NoZW1hIiwibWF4RGVwdGgiLCJkZXNjcmlwdGlvbiIsIm1pbmltdW0iLCJjcmV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