forked from import-js/eslint-plugin-import
/
no-self-import.js
38 lines (33 loc) · 934 Bytes
/
no-self-import.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
/**
* @fileOverview Forbids a module from importing itself
* @author Gio d'Amelio
*/
import resolve from 'eslint-module-utils/resolve';
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';
function isImportingSelf(context, node, requireName) {
const filePath = context.getFilename();
// If the input is from stdin, this test can't fail
if (filePath !== '<text>' && filePath === resolve(requireName, context)) {
context.report({
node,
message: 'Module imports itself.',
});
}
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Forbid a module from importing itself',
recommended: true,
url: docsUrl('no-self-import'),
},
schema: [],
},
create: function (context) {
return moduleVisitor((source, node) => {
isImportingSelf(context, node, source.value);
}, { commonjs: true });
},
};