-
Notifications
You must be signed in to change notification settings - Fork 15
/
no-cycles.js
231 lines (203 loc) · 5.78 KB
/
no-cycles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
var fs = require('fs');
var path = require('path');
var helpers = require('./_helpers');
/**
* A reference to the eslint module is needed to be able to require the same
* parser that used to parse the file being linted, as well as to use eslint's
* own traverser.
*/
var eslintModule = (function() {
var parent = module.parent;
var eslintLibRe = new RegExp(
'\\' + path.sep + 'node_modules' +
'\\' + path.sep + 'eslint' +
'\\' + path.sep + 'lib' +
'\\' + path.sep + '[^\\' + path.sep + ']+\\.js$'
);
do {
if (eslintLibRe.test(parent.filename)) {
return parent;
}
} while ((parent = parent.parent));
throw new Error('Could not find eslint');
})();
var traverser = (function() {
var Traverser;
try {
// ESLint >=6.0.0
Traverser = eslintModule.require('./shared/traverser');
} catch (err) {
// ESLint <6.0.0
Traverser = eslintModule.require('./util/traverser');
}
return new Traverser();
})();
//------------------------------------------------------------------------------
// Utils
//------------------------------------------------------------------------------
var externalRe = /^[^./]/;
var skipExts = /\.(?:json|node)$/;
var searchRe = /\b(?:require|import|export)\b/;
var NoopVisitor = {
Program: function() {},
};
function parse(src, parserPath, parserOptions) {
var parser = eslintModule.require(parserPath);
try {
return parser.parse(src, parserOptions);
} catch (err) {
return null;
}
}
function read(filename) {
try {
return fs.readFileSync(filename, 'utf8');
} catch(err) {
return null;
}
}
function resolver(name, basedir) {
if (!externalRe.test(name)) {
var opts = {
basedir: basedir,
extensions: ['.js', '.json', '.node'],
};
var resolved = helpers.resolveSync(name, opts);
if (resolved && !skipExts.test(resolved)) {
return resolved;
}
}
return null;
}
function relativizeTrace(trace, basedir) {
var out = [];
for (var i = 0; i < trace.length; i++) {
out.push(path.relative(basedir, trace[i]));
basedir = path.dirname(trace[i]);
}
return out;
}
var _depsCache = helpers.oneTickCache();
function getDeps(filename, src, ast, context, options) {
var depsCache = _depsCache();
if (depsCache[filename]) return depsCache[filename];
var found = depsCache[filename] = [];
if (skipExts.test(filename)) return found;
if (!src) src = read(filename);
if (!src) return found;
if (!searchRe.test(src)) return found;
if (!ast) ast = parse(src, context.parserPath, context.parserOptions);
if (!ast) return found;
var basedir = path.dirname(filename);
traverser.traverse(ast, {
enter: function(node, parent) {
var start = node.range ? node.range[0] : node.start;
var end = node.range ? node.range[1] : node.end;
var section = src.slice(start, end);
if (!searchRe.test(section)) return this.skip();
if (helpers.isRequireCall(node) ||
helpers.isImport(node) ||
(options.types && helpers.isImportType(node)) ||
helpers.isExportFrom(node)) {
var id = helpers.getModuleId(node);
var resolved = resolver(id, basedir);
if (resolved) found.push(resolved);
}
},
});
return found;
}
//------------------------------------------------------------------------------
// Rule
//------------------------------------------------------------------------------
module.exports = function(context) {
var target = context.getFilename();
var options = context.options[0] || {};
var skip = options.skip;
var shouldSkip = skip && skip.some(function(pattern) {
return RegExp(pattern).test(target);
});
if (shouldSkip) {
return NoopVisitor;
}
var seen = new helpers.StorageObject();
var basedir = path.dirname(target);
function trace(filename, depth, refs) {
if (!depth) depth = [];
if (!refs) refs = [];
var deps = getDeps(filename, null, null, context, options);
depth.push(filename);
for (var i = 0; i < deps.length; i++) {
filename = deps[i];
if (filename === target) {
refs.push(depth.slice());
} else if (!seen[filename]) {
seen[filename] = true;
trace(filename, depth, refs);
}
}
depth.pop();
return refs;
}
function validate(node) {
var id = helpers.getModuleId(node);
var resolved = resolver(id, basedir);
if (resolved === target) {
context.report({
node: node,
message: 'Self-reference cycle.',
});
} else if (resolved) {
var refs = trace(resolved);
for (var i = 0; i < refs.length; i++) {
var prettyTrace = relativizeTrace(refs[i], basedir).join(' => ');
context.report({
node: node,
data: {trace: prettyTrace},
message: 'Cycle in {{trace}}.',
});
}
}
}
return {
CallExpression: function(node) {
// no-cycles doesn't test for "require.resolve"
if (helpers.isRequireCall(node)) {
validate(node);
}
},
ImportDeclaration: function(node) {
if (helpers.isImport(node) ||
(options.types && helpers.isImportType(node))) {
validate(node);
}
},
ExportAllDeclaration: function(node) {
if (helpers.isExportFrom(node)) {
validate(node);
}
},
ExportNamedDeclaration: function(node) {
if (helpers.isExportFrom(node)) {
validate(node);
}
},
'Program:exit': function(node) {
// since this ast has already been built, and traversing is cheap,
// run it through references.deps so it's cached for future runs.
getDeps(target, context.getSourceCode().text, node, context, options);
},
};
};
module.exports.schema = {
skip: {
type: 'array',
items: {
type: 'string',
},
},
types: {
type: 'boolean'
}
};