Skip to content

Commit

Permalink
Added no-self-import rule
Browse files Browse the repository at this point in the history
New rule checks against importing from the current file. Ref #447
  • Loading branch information
Pavel Žák committed Jul 21, 2016
1 parent 21798a8 commit 85b2e71
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const rules = {
'no-mutable-exports': require('./rules/no-mutable-exports'),
'extensions': require('./rules/extensions'),
'no-restricted-paths': require('./rules/no-restricted-paths'),
'no-self-import': require('./rules/no-self-import'),

'no-named-as-default': require('./rules/no-named-as-default'),
'no-named-as-default-member': require('./rules/no-named-as-default-member'),
Expand Down
17 changes: 17 additions & 0 deletions src/rules/no-self-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var path = require('path')

module.exports = function (context) {
function checkSelfImport(node) {
var fileName = path.basename(context.getFilename())
var fileNameNoExtension = path.basename(fileName, path.extname(fileName))
var badPaths = ['./' + fileName, './' + fileNameNoExtension]

if (~badPaths.indexOf(node.source.value)) {
context.report(node, 'Importing from the current file.')
}
}

return {
'ImportDeclaration': checkSelfImport.bind(null),
}
}
73 changes: 73 additions & 0 deletions tests/src/rules/no-self-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { test, SYNTAX_CASES } from '../utils'
import { RuleTester } from 'eslint'

const ruleTester = new RuleTester()
, rule = require('rules/no-self-import')

ruleTester.run('no-self-import', rule, {
valid: [
test({ filename: 'foo', code: 'import "./bar"' }),
test({ filename: 'foo', code: 'import "./bar.js"' }),
test({ filename: 'foo.js', code: 'import "./bar"' }),
test({ filename: 'foo.js', code: 'import "./bar.js"' }),
test({ filename: 'foo.jsx', code: 'import "./bar"' }),
test({ filename: 'foo.jsx', code: 'import "./bar.jsx"' }),
test({ filename: 'foo.jsx', code: 'import "./foo.js"' }),

// es7
test({ filename: 'foo.js', code: 'export bar, { foo } from "./bar";', parser: 'babel-eslint' }),
test({ filename: 'foo.js', code: 'export bar from "./bar";', parser: 'babel-eslint' }),

...SYNTAX_CASES,
],

invalid: [
test({
code: 'import "./foo";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),

test({
code: 'import "./foo.jsx";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),

test({
code: 'import foo from "./foo";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),

test({
code: 'import foo from "./foo.jsx";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),

test({
code: 'import { foo } from "./foo";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),

test({
code: 'import { foo } from "./foo.jsx";',
filename: 'foo.jsx',
errors: [
{ message: 'Importing from the current file.', type: 'ImportDeclaration' },
]
}),
],
})

0 comments on commit 85b2e71

Please sign in to comment.