import resolve from 'eslint-module-utils/resolve';
import Exports from '../ExportMap';
import { isExternalModule } from '../core/importType';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'suggestion',
docs: { url: docsUrl('no-cycle') },
schema: [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(context) {
const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
if (myPath === '<text>') return {};
const options = context.options[0] || {};
const maxDepth = typeof options.maxDepth === 'number' ? options.maxDepth : Infinity;
const ignoreModule = (name) => options.ignoreExternal && isExternalModule(
name,
context.settings,
resolve(name, context),
context,
);
function checkSourceValue(sourceNode, importer) {
if (ignoreModule(sourceNode.value)) {
return;
}
if (
importer.type === 'ImportDeclaration' && (
importer.importKind === 'type' ||
importer.specifiers.every(({ importKind }) => importKind === 'type')
)
) {
return;
}
const imported = Exports.get(sourceNode.value, context);
if (imported == null) {
return;
}
if (imported.path === myPath) {
return;
}
const untraversed = [{ mget: () => imported, route:[] }];
const traversed = new Set();
function detectCycle({ mget, route }) {
const m = mget();
if (m == null) return;
if (traversed.has(m.path)) return;
traversed.add(m.path);
for (const [path, { getter, declarations }] of m.imports) {
if (traversed.has(path)) continue;
const toTraverse = [...declarations].filter(({ source, isOnlyImportingTypes }) =>
!ignoreModule(source.value) &&
!isOnlyImportingTypes,
);
if (path === myPath && toTraverse.length > 0) return true;
if (route.length + 1 < maxDepth) {
for (const { source } of toTraverse) {
untraversed.push({ mget: getter, route: route.concat(source) });
}
}
}
}
while (untraversed.length > 0) {
const next = untraversed.shift();
if (detectCycle(next)) {
const message = (next.route.length > 0
? `Dependency cycle via ${routeString(next.route)}`
: 'Dependency cycle detected.');
context.report(importer, message);
return;
}
}
}
return moduleVisitor(checkSourceValue, context.options[0]);
},
};
function routeString(route) {
return route.map(s => `${s.value}:${s.loc.start.line}`).join('=>');
}