Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-cycle.js
829 views
1
/**
2
* @fileOverview Ensures that no imported module imports the linted module.
3
* @author Ben Mosher
4
*/
5
6
import resolve from 'eslint-module-utils/resolve';
7
import Exports from '../ExportMap';
8
import { isExternalModule } from '../core/importType';
9
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
10
import docsUrl from '../docsUrl';
11
12
// todo: cache cycles / deep relationships for faster repeat evaluation
13
module.exports = {
14
meta: {
15
type: 'suggestion',
16
docs: { url: docsUrl('no-cycle') },
17
schema: [makeOptionsSchema({
18
maxDepth: {
19
oneOf: [
20
{
21
description: 'maximum dependency depth to traverse',
22
type: 'integer',
23
minimum: 1,
24
},
25
{
26
enum: ['∞'],
27
type: 'string',
28
},
29
],
30
},
31
ignoreExternal: {
32
description: 'ignore external modules',
33
type: 'boolean',
34
default: false,
35
},
36
})],
37
},
38
39
create(context) {
40
const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
41
if (myPath === '<text>') return {}; // can't cycle-check a non-file
42
43
const options = context.options[0] || {};
44
const maxDepth = typeof options.maxDepth === 'number' ? options.maxDepth : Infinity;
45
const ignoreModule = (name) => options.ignoreExternal && isExternalModule(
46
name,
47
context.settings,
48
resolve(name, context),
49
context,
50
);
51
52
function checkSourceValue(sourceNode, importer) {
53
if (ignoreModule(sourceNode.value)) {
54
return; // ignore external modules
55
}
56
57
if (
58
importer.type === 'ImportDeclaration' && (
59
// import type { Foo } (TS and Flow)
60
importer.importKind === 'type' ||
61
// import { type Foo } (Flow)
62
importer.specifiers.every(({ importKind }) => importKind === 'type')
63
)
64
) {
65
return; // ignore type imports
66
}
67
68
const imported = Exports.get(sourceNode.value, context);
69
70
if (imported == null) {
71
return; // no-unresolved territory
72
}
73
74
if (imported.path === myPath) {
75
return; // no-self-import territory
76
}
77
78
const untraversed = [{ mget: () => imported, route:[] }];
79
const traversed = new Set();
80
function detectCycle({ mget, route }) {
81
const m = mget();
82
if (m == null) return;
83
if (traversed.has(m.path)) return;
84
traversed.add(m.path);
85
86
for (const [path, { getter, declarations }] of m.imports) {
87
if (traversed.has(path)) continue;
88
const toTraverse = [...declarations].filter(({ source, isOnlyImportingTypes }) =>
89
!ignoreModule(source.value) &&
90
// Ignore only type imports
91
!isOnlyImportingTypes,
92
);
93
/*
94
Only report as a cycle if there are any import declarations that are considered by
95
the rule. For example:
96
97
a.ts:
98
import { foo } from './b' // should not be reported as a cycle
99
100
b.ts:
101
import type { Bar } from './a'
102
*/
103
if (path === myPath && toTraverse.length > 0) return true;
104
if (route.length + 1 < maxDepth) {
105
for (const { source } of toTraverse) {
106
untraversed.push({ mget: getter, route: route.concat(source) });
107
}
108
}
109
}
110
}
111
112
while (untraversed.length > 0) {
113
const next = untraversed.shift(); // bfs!
114
if (detectCycle(next)) {
115
const message = (next.route.length > 0
116
? `Dependency cycle via ${routeString(next.route)}`
117
: 'Dependency cycle detected.');
118
context.report(importer, message);
119
return;
120
}
121
}
122
}
123
124
return moduleVisitor(checkSourceValue, context.options[0]);
125
},
126
};
127
128
function routeString(route) {
129
return route.map(s => `${s.value}:${s.loc.start.line}`).join('=>');
130
}
131
132