130 lines
19 KiB
JavaScript
130 lines
19 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 _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
|
||
|
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 };}function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {arr2[i] = arr[i];}return arr2;} else {return Array.from(arr);}}
|
||
|
|
||
|
// 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 () {function create(context) {
|
||
|
var myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
|
||
|
if (myPath === '<text>') return {}; // can't cycle-check a non-file
|
||
|
|
||
|
var options = context.options[0] || {};
|
||
|
var maxDepth = typeof options.maxDepth === 'number' ? options.maxDepth : Infinity;
|
||
|
var ignoreModule = function () {function ignoreModule(name) {return options.ignoreExternal && (0, _importType.isExternalModule)(
|
||
|
name,
|
||
|
(0, _resolve2['default'])(name, context),
|
||
|
context);}return ignoreModule;}();
|
||
|
|
||
|
|
||
|
function checkSourceValue(sourceNode, importer) {
|
||
|
if (ignoreModule(sourceNode.value)) {
|
||
|
return; // ignore external modules
|
||
|
}
|
||
|
|
||
|
if (
|
||
|
importer.type === 'ImportDeclaration' && (
|
||
|
// import type { Foo } (TS and Flow)
|
||
|
importer.importKind === 'type' ||
|
||
|
// import { type Foo } (Flow)
|
||
|
importer.specifiers.every(function (_ref) {var importKind = _ref.importKind;return importKind === 'type';})))
|
||
|
|
||
|
{
|
||
|
return; // ignore type imports
|
||
|
}
|
||
|
|
||
|
var imported = _ExportMap2['default'].get(sourceNode.value, context);
|
||
|
|
||
|
if (imported == null) {
|
||
|
return; // no-unresolved territory
|
||
|
}
|
||
|
|
||
|
if (imported.path === myPath) {
|
||
|
return; // no-self-import territory
|
||
|
}
|
||
|
|
||
|
var untraversed = [{ mget: function () {function mget() {return imported;}return mget;}(), route: [] }];
|
||
|
var traversed = new Set();
|
||
|
function detectCycle(_ref2) {var mget = _ref2.mget,route = _ref2.route;
|
||
|
var m = mget();
|
||
|
if (m == null) return;
|
||
|
if (traversed.has(m.path)) return;
|
||
|
traversed.add(m.path);var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
|
||
|
|
||
|
for (var _iterator = m.imports[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var _ref3 = _step.value;var _ref4 = _slicedToArray(_ref3, 2);var path = _ref4[0];var _ref4$ = _ref4[1];var getter = _ref4$.getter;var declarations = _ref4$.declarations;
|
||
|
if (traversed.has(path)) continue;
|
||
|
var toTraverse = [].concat(_toConsumableArray(declarations)).filter(function (_ref5) {var source = _ref5.source,isOnlyImportingTypes = _ref5.isOnlyImportingTypes;return (
|
||
|
!ignoreModule(source.value) &&
|
||
|
// Ignore only type imports
|
||
|
!isOnlyImportingTypes);});
|
||
|
|
||
|
/*
|
||
|
Only report as a cycle if there are any import declarations that are considered by
|
||
|
the rule. For example:
|
||
|
a.ts:
|
||
|
import { foo } from './b' // should not be reported as a cycle
|
||
|
b.ts:
|
||
|
import type { Bar } from './a'
|
||
|
*/
|
||
|
|
||
|
|
||
|
if (path === myPath && toTraverse.length > 0) return true;
|
||
|
if (route.length + 1 < maxDepth) {var _iteratorNormalCompletion2 = true;var _didIteratorError2 = false;var _iteratorError2 = undefined;try {
|
||
|
for (var _iterator2 = toTraverse[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {var _ref6 = _step2.value;var source = _ref6.source;
|
||
|
untraversed.push({ mget: getter, route: route.concat(source) });
|
||
|
}} catch (err) {_didIteratorError2 = true;_iteratorError2 = err;} finally {try {if (!_iteratorNormalCompletion2 && _iterator2['return']) {_iterator2['return']();}} finally {if (_didIteratorError2) {throw _iteratorError2;}}}
|
||
|
}
|
||
|
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
|
||
|
}
|
||
|
|
||
|
while (untraversed.length > 0) {
|
||
|
var next = untraversed.shift(); // bfs!
|
||
|
if (detectCycle(next)) {
|
||
|
var message = next.route.length > 0 ? 'Dependency cycle via ' + String(
|
||
|
routeString(next.route)) :
|
||
|
'Dependency cycle detected.';
|
||
|
context.report(importer, message);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return (0, _moduleVisitor2['default'])(checkSourceValue, context.options[0]);
|
||
|
}return create;}() };
|
||
|
|
||
|
|
||
|
function routeString(route) {
|
||
|
return route.map(function (s) {return String(s.value) + ':' + String(s.loc.start.line);}).join('=>');
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jeWNsZS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwic2NoZW1hIiwibWF4RGVwdGgiLCJvbmVPZiIsImRlc2NyaXB0aW9uIiwibWluaW11bSIsImlnbm9yZUV4dGVybmFsIiwiY3JlYXRlIiwiY29udGV4dCIsIm15UGF0aCIsImdldFBoeXNpY2FsRmlsZW5hbWUiLCJnZXRGaWxlbmFtZSIsIm9wdGlvbnMiLCJJbmZpbml0eSIsImlnbm9yZU1vZHVsZSIsIm5hbWUiLCJjaGVja1NvdXJjZVZhbHVlIiwic291cmNlTm9kZSIsImltcG9ydGVyIiwidmFsdWUiLCJpbXBvcnRLaW5kIiwic3BlY2lmaWVycyIsImV2ZXJ5IiwiaW1wb3J0ZWQiLCJFeHBvcnRzIiwiZ2V0IiwicGF0aCIsInVudHJhdmVyc2VkIiwibWdldCIsInJvdXRlIiwidHJhdmVyc2VkIiwiU2V0IiwiZGV0ZWN0Q3ljbGUiLCJtIiwiaGFzIiwiYWRkIiwiaW1wb3J0cyIsImdldHRlciIsImRlY2xhcmF0aW9ucyIsInRvVHJhdmVyc2UiLCJmaWx0ZXIiLCJzb3VyY2UiLCJpc09ubHlJbXBvcnRpbmdUeX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