Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-self-import.js
829 views
1
/**
2
* @fileOverview Forbids a module from importing itself
3
* @author Gio d'Amelio
4
*/
5
6
import resolve from 'eslint-module-utils/resolve';
7
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
8
import docsUrl from '../docsUrl';
9
10
function isImportingSelf(context, node, requireName) {
11
const filePath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
12
13
// If the input is from stdin, this test can't fail
14
if (filePath !== '<text>' && filePath === resolve(requireName, context)) {
15
context.report({
16
node,
17
message: 'Module imports itself.',
18
});
19
}
20
}
21
22
module.exports = {
23
meta: {
24
type: 'problem',
25
docs: {
26
description: 'Forbid a module from importing itself',
27
recommended: true,
28
url: docsUrl('no-self-import'),
29
},
30
31
schema: [],
32
},
33
create(context) {
34
return moduleVisitor((source, node) => {
35
isImportingSelf(context, node, source.value);
36
}, { commonjs: true });
37
},
38
};
39
40