Skip to content

Commit 85b2e71

Browse files
author
Pavel Žák
committed
Added no-self-import rule
New rule checks against importing from the current file. Ref #447
1 parent 21798a8 commit 85b2e71

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const rules = {
88
'no-mutable-exports': require('./rules/no-mutable-exports'),
99
'extensions': require('./rules/extensions'),
1010
'no-restricted-paths': require('./rules/no-restricted-paths'),
11+
'no-self-import': require('./rules/no-self-import'),
1112

1213
'no-named-as-default': require('./rules/no-named-as-default'),
1314
'no-named-as-default-member': require('./rules/no-named-as-default-member'),

src/rules/no-self-import.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var path = require('path')
2+
3+
module.exports = function (context) {
4+
function checkSelfImport(node) {
5+
var fileName = path.basename(context.getFilename())
6+
var fileNameNoExtension = path.basename(fileName, path.extname(fileName))
7+
var badPaths = ['./' + fileName, './' + fileNameNoExtension]
8+
9+
if (~badPaths.indexOf(node.source.value)) {
10+
context.report(node, 'Importing from the current file.')
11+
}
12+
}
13+
14+
return {
15+
'ImportDeclaration': checkSelfImport.bind(null),
16+
}
17+
}

tests/src/rules/no-self-import.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { test, SYNTAX_CASES } from '../utils'
2+
import { RuleTester } from 'eslint'
3+
4+
const ruleTester = new RuleTester()
5+
, rule = require('rules/no-self-import')
6+
7+
ruleTester.run('no-self-import', rule, {
8+
valid: [
9+
test({ filename: 'foo', code: 'import "./bar"' }),
10+
test({ filename: 'foo', code: 'import "./bar.js"' }),
11+
test({ filename: 'foo.js', code: 'import "./bar"' }),
12+
test({ filename: 'foo.js', code: 'import "./bar.js"' }),
13+
test({ filename: 'foo.jsx', code: 'import "./bar"' }),
14+
test({ filename: 'foo.jsx', code: 'import "./bar.jsx"' }),
15+
test({ filename: 'foo.jsx', code: 'import "./foo.js"' }),
16+
17+
// es7
18+
test({ filename: 'foo.js', code: 'export bar, { foo } from "./bar";', parser: 'babel-eslint' }),
19+
test({ filename: 'foo.js', code: 'export bar from "./bar";', parser: 'babel-eslint' }),
20+
21+
...SYNTAX_CASES,
22+
],
23+
24+
invalid: [
25+
test({
26+
code: 'import "./foo";',
27+
filename: 'foo.jsx',
28+
errors: [
29+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
30+
]
31+
}),
32+
33+
test({
34+
code: 'import "./foo.jsx";',
35+
filename: 'foo.jsx',
36+
errors: [
37+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
38+
]
39+
}),
40+
41+
test({
42+
code: 'import foo from "./foo";',
43+
filename: 'foo.jsx',
44+
errors: [
45+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
46+
]
47+
}),
48+
49+
test({
50+
code: 'import foo from "./foo.jsx";',
51+
filename: 'foo.jsx',
52+
errors: [
53+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
54+
]
55+
}),
56+
57+
test({
58+
code: 'import { foo } from "./foo";',
59+
filename: 'foo.jsx',
60+
errors: [
61+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
62+
]
63+
}),
64+
65+
test({
66+
code: 'import { foo } from "./foo.jsx";',
67+
filename: 'foo.jsx',
68+
errors: [
69+
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
70+
]
71+
}),
72+
],
73+
})

0 commit comments

Comments
 (0)