Created
August 9, 2017 22:29
-
-
Save j-f1/d4beb130f6f84e868e6bdb6c3afbd73a to your computer and use it in GitHub Desktop.
Require sorting imports by length
This file contains hidden or 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
export default function(context) { | |
const fileSource = context.eslint.sourceCode.text | |
return { | |
ImportDeclaration(node) { | |
const idx = node.parent.body.indexOf(node) | |
const line = node.loc.start.line | |
if (idx > 0) { | |
const prevNode = node.parent.body[idx - 1] | |
if (line - prevNode.loc.end.line < 2) { | |
if (prevNode.loc.end.column < node.loc.end.column) { | |
context.report(node, 'Imports should be sorted by length, with the longest first') | |
} | |
} | |
} | |
} | |
}; | |
}; | |
This file contains hidden or 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
// ok: | |
import chalk from 'chalk' | |
import { foo } from './bar' | |
import { | |
baz as quux | |
} from './utils' | |
// error: | |
import { | |
baz as quux | |
} from './utils' | |
import { foo } from './bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment