webhook/node_modules/eslint-plugin-import/lib/rules/no-cycle.js
2020-08-26 09:57:08 +10:00

105 lines
No EOL
14 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 _importType = require('../core/importType');
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: {
oneOf: [
{
description: 'maximum dependency depth to traverse',
type: 'integer',
minimum: 1 },
{
enum: ['∞'],
type: 'string' }] },
ignoreExternal: {
description: 'ignore external modules',
type: 'boolean',
default: false } })] },
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 = typeof options.maxDepth === 'number' ? options.maxDepth : Infinity;
const ignoreModule = name => options.ignoreExternal ? (0, _importType.isExternalModule)(name) : false;
function checkSourceValue(sourceNode, importer) {
if (ignoreModule(sourceNode.value)) {
return; // ignore external modules
}
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 (ignoreModule(source.value)) 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jeWNsZS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwic2NoZW1hIiwibWF4RGVwdGgiLCJvbmVPZiIsImRlc2NyaXB0aW9uIiwibWluaW11bSIsImVudW0iLCJpZ25vcmVFeHRlcm5hbCIsImRlZmF1bHQiLCJjcmVhdGUiLCJjb250ZXh0IiwibXlQYXRoIiwiZ2V0RmlsZW5hbWUiLCJvcHRpb25zIiwiSW5maW5pdHkiLCJpZ25vcmVNb2R1bGUiLCJuYW1lIiwiY2hlY2tTb3VyY2VWYWx1ZSIsInNvdXJjZU5vZGUiLCJpbXBvc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SB9XSBvZiBtLmltcG9ydHMpIHtcbiAgICAgICAgICBpZiAocGF0aCA9PT0gbXlQYXRoKSByZXR1cm4gdHJ1ZVxuICAgICAgICAgIGlmICh0cmF2ZXJzZWQuaGFzKHBhdGgpKSBjb250aW51ZVxuICAgICAgICAgIGlmIChpZ25vcmVNb2R1bGUoc291cmNlLnZhbHVlKSkgY29udGludWVcbiAgICAgICAgICBpZiAocm91dGUubGVuZ3RoICsgMSA8IG1heERlcHRoKSB7XG4gICAgICAgICAgICB1bnRyYXZlcnNlZC5wdXNoKHtcbiAgICAgICAgICAgICAgbWdldDogZ2V0dGVyLFxuICAgICAgICAgICAgICByb3V0ZTogcm91dGUuY29uY2F0KHNvdXJjZSksXG4gICAgICAgICAgICB9KVxuICAgICAgICAgIH1cbiAgICAgICAgfVxuICAgICAgfVxuXG4gICAgICB3aGlsZSAodW50cmF2ZXJzZWQubGVuZ3RoID4gMCkge1xuICAgICAgICBjb25zdCBuZXh0ID0gdW50cmF2ZXJzZWQuc2hpZnQoKSAvLyBiZnMhXG4gICAgICAgIGlmIChkZXRlY3RDeWNsZShuZXh0KSkge1xuICAgICAgICAgIGNvbnN0IG1lc3NhZ2UgPSAobmV4dC5yb3V0ZS5sZW5ndGggPiAwXG4gICAgICAgICAgICA/IGBEZXBlbmRlbmN5IGN5Y2xlIHZpYSAke3JvdXRlU3RyaW5nKG5leHQucm91dGUpfWBcbiAgICAgICAgICAgIDogJ0RlcGVuZGVuY3kgY3ljbGUgZGV0ZWN0ZWQuJylcbiAgICAgICAgICBjb250ZXh0LnJlcG9ydChpbXBvcnRlciwgbWVzc2FnZSlcbiAgICAgICAgICByZXR1cm5cbiAgICAgICAgfVxuICAgICAgfVxuICAgIH1cblxuICAgIHJldHVybiBtb2R1bGVWaXNpdG9yKGNoZWNrU291cmNlVmFsdWUsIGNvbnRleHQub3B0aW9uc1swXSlcbiAgfSxcbn1cblxuZnVuY3Rpb24gcm91dGVTdHJpbmcocm91dGUpIHtcbiAgcmV0dXJuIHJvdXRlLm1hcChzID0+IGAke3MudmFsdWV9OiR7cy5sb2Muc3RhcnQubGluZX1gKS5qb2luKCc9PicpXG59XG4iXX0=