-
Notifications
You must be signed in to change notification settings - Fork 180
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
chore(js): add lint rules for import order: eslint-plugin-simple-import-sort #6028
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍾 This is some first class charlie work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From looking at these changes and reading through the README, I think I would choose the eslint-plugin-import/order rule over this plugin, especially considering the fact that eslint-plugin-import
is already in our build toolchain.
Unfortunately, neither eslint-plugin-import/order
nor eslint-plugin-simple-import-sort
allow configuring "put type imports last". For me personally, I'd rather have no import order checking than being forced to mix my type imports and code imports, which would harm how I like to read the code
@@ -1,4 +1,6 @@ | |||
// @flow | |||
import '../../../robot-api/__utils__/epic-test-mocks' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't say I'm a fan of this. "Import the mock before you import the code under test" is my preferred way to do this, which is how things were ordered before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean, isn't this importing the mocks before any other imports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, but we only need it because this plugin arbitrarily put them out of order in the first place. The "after" here seems arbitrary and (as noted in my other comment) not what the plugin is supposed to do
// before re-ordering
// import mocks
import { setupEpicTestMocks, runEpicTest } from '../../../robot-api/__utils__'
// import other stuff
import * as Fixtures from '../../__fixtures__'
import * as Actions from '../../actions'
// import code under test
import { calibrationEpic } from '..'
// after re-ordering
// import mocks but only for their `jest.mock` side effects
import '../../../robot-api/__utils__/epic-test-mocks'
// import code under test
import { calibrationEpic } from '..'
// import mocks and other stuff
import * as Fixtures from '../../__fixtures__'
import { runEpicTest, setupEpicTestMocks } from '../../../robot-api/__utils__'
import * as Actions from '../../actions'
import { setupEpicTestMocks, runEpicTest } from '../../../robot-api/__utils__' | ||
import '../../../robot-api/__utils__/epic-test-mocks' | ||
|
||
import { calibrationEpic } from '..' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the documentation of the plugin:
Since “.” sorts before “/”, relative imports of files higher up in the directory structure come before closer ones – "../../utils" comes before "../utils". Perhaps surprisingly though, ".." would come before "../../utils" (since shorter substrings sort before longer strings). For that reason there’s one addition to the alphabetical rule: "." and ".." are treated as "./" and "../".
This seems to be the exact opposite of what has happened in this file, which is confusing to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confuses me too!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
me three
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me personally, I'd rather have no import order checking than being forced to mix my type imports and code imports, which would harm how I like to read the code
I agree with this. Annoying that we can't have types go last :/
Overview
Closes #5624
Settled on
eslint-plugin-simple-import-sort
for this one. Eslint has https://eslint.org/docs/rules/sort-imports but I don't like it because it cares about the "style" of your import"memberSyntaxSortOrder": ["none", "all", "multiple", "single"]
whereas we care about alphabetical and../../../
nesting depth ordering but we don't wantimport something from '../something
to be sorted differently thanimport {something} from '../something
. (It also didn't seem to autofix properly but I didn't try too hard after finding out aboutmemberSyntaxSortOrder
)Changelog
Review requests
This PR is 2 commits, the first adds
eslint-plugin-simple-import-sort
and contains eslint autofix changes for all js projects, the second manually fixes failing jest tests inapp
for several epics -- these failed when imports were re-ordered, because they relied on importing files that set up jest mocks before importing other files. To fix this, I imported the mock-setup file at the top.eslint-plugin-simple-import-sort
and.eslint.js
?Risk assessment
medium. If there are any side-effecting imports in JS land, re-ordering the imports could break them. Hopefully we only rely on side-effecting imports in tests (for mocking), and we'll get ❌'s if the test fail.