-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
] | ||
}), | ||
], | ||
}) |