Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added no-self-import rule #449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' },
]
}),
],
})