Skip to content

Instantly share code, notes, and snippets.

@j-f1
Created August 9, 2017 22:29
Show Gist options
  • Save j-f1/d4beb130f6f84e868e6bdb6c3afbd73a to your computer and use it in GitHub Desktop.
Save j-f1/d4beb130f6f84e868e6bdb6c3afbd73a to your computer and use it in GitHub Desktop.
Require sorting imports by length
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')
}
}
}
}
};
};
// 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