-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add autofixer for order rule #908
Conversation
3 similar comments
2 similar comments
Hi, i dont have ideas how to fix appveyor build. Could you give me a hint? |
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.
What happens when there's non-require non-import statements between the mis-ordered items? I wouldn't expect this rule to enforce "all imports/requires are grouped together/at the top"
@ljharb, currently autofixer just fix errors reported by original rule. Code
transforms to
due to rule requires 'fs' to occurs before 'async'. |
What about: var b = require('b');
require('a')(b); ? In other words, I think this rule is only safely fixable in a subset of the circumstances that the rule warns on - eslint core's policy (which I think everyone should follow) is that autofixes must always be "safe", and not change the meaning of the code, even if that means not autofixing some things. |
🙈 Currently autofixer brokes the code above. Im going to update autofixer to avoid unsafe fixes. |
Specifically: I think that to start, autofixing can only be said to be safe only within a chunk of requires and/or imports, where the requires are purely assignments - and any code that's not a variable-assigned-require or an import-with-a-binding has to be considered a boundary across which autofixing can't cross. For example, side-effecting requires/imports (like just |
Hi. Currently reorder fixes is very safe and only strict var-assignement-require and imports can be reordered and autofixes would not applied to the following code
Im not sure about this. Maybe such reorderings should be allowed |
1 similar comment
@tihonove using |
Ok, are there anything i can do to complete pr? |
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.
Thanks, this is very thorough.
We'll need to get the tests passing before it can be merged tho.
tests/src/rules/order.js
Outdated
test({ | ||
code: ` | ||
var async = require('async'); | ||
var fs = require('fs');` + ' ' + ` |
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.
you could do:
var fs = require('fs');${' '}
without having to use string concatenation
tests/src/rules/order.js
Outdated
message: '`fs` import should occur before import of `async`', | ||
}], | ||
})), | ||
// reorder cant cross variable assignemet |
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's not really the variable assignment that's an issue here; it's what's on the RHS - in other words, anything that's depending on a global, or on built-in prototypes, or on any imported value.
Probably safest tho to stick with "variable assignment"
I dont have ideas how to fix appveyor build. Could you give me a hint? Seems it fails after all test successfully passed |
I believe the appveyor build might be broken on master; @benmosher, can you confirm? |
Would be great to see this merged.. |
@benmosher any chance of merging and releasing this? |
@tihonove - master build is now passing again |
It sure would be great to have this merged 🙏 |
@ljharb I see you have merge rights now. Given you approved this and Ben does not seem to be about, would you consider merging it? Thanks. |
@lukeapage i've had them this whole time; I don't think an autofixer - especially one as risky as an ordering autofixer - should be merged with only one collaborator having reviewed it. |
@benmosher anything we can do to make this review easy? It could be really nice to get this in a release :) |
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 just tried this on a pretty big codebase, worked like a charm without any issues!
CHANGELOG.md
Outdated
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel | |||
## [2.8.0] - 2017-10-18 | |||
### Added | |||
- [`exports-last`] rule ([#620] + [#632], thanks [@k15a]) | |||
- Autofixer for [`order`] rule ([#711], thanks [@tihonove]) |
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.
Might need to be adjusted
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.
not super familiar with fixers or this rule in particular, but tests look good and @MoOx's anecdote about running it on a big project is a big deal. thanks for this!
will merge once |
macOS build seems to be struggling. Are those really necessary btw (as this lib doesn't involve any "macos" specific code?) |
@MoOx I agree that it shouldn't matter but the tests are there for the times when it mysteriously does. 😅 LGTM now, though 👍 |
See #1086 - this PR caused a performance regression. It’d be great if anyone interested in keeping this autofixer in the plugin could look into that issue. |
You're my hero, @tihonove! I just needed to apply an import/order rule to 395 files; this saved me a full day of hair pulling. |
I really want this feature :-)
Fixes #711. Fixes #547.