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
Allow more control over fixes in CLIEngine #8039
Allow more control over fixes in CLIEngine #8039
Comments
While I'm not against this proposal, in fact, I proposed exactly the same thing here: #8018 (comment) if you are writing a stand-alone tool for this, you can do this by not using CLIEngine, but instead using |
@ilyavolodin unfortunately it's not quite that easy. |
What if |
I like that idea, @nzakas. It would also prevent one of the issues I was thinking about, which is that my approach would not be terribly efficient. |
Can I move forward with a PR on this issue? Are there any objections to allowing the |
Sorry, I might be slightly slow, but I'm not 100% sure I understand the proposed change for |
I'm talking about expanding what can be provided as the |
Here's one of the places this is used: https://github.com/eslint/eslint/blob/master/lib/cli-engine.js#L201. |
I see. In that case, I would support this enhancement. We should also make this backwards compatible, not to break existing integrations. |
I'm interested in implementing this, since this could be leveraged to fix #8675. If anyone has any objections or concerns, let me know here, otherwise I'll probably work on this in the next few days. |
@platinumazure if you haven't started yet, I have a branch locally with a good start at this, I just never took the time to finish it up. I'm not sure if I can take it across the finish line in the next few days, but I can at least push up the branch today or tomorrow if you want to check it out. |
@IanVS Thanks, I'd love to see your branch. |
@platinumazure I've opened a quick PR, as it seems closer than I remembered. |
* New: Allow passing a function as `fix` option (fixes #8039) * Pass fix in options, instead of separate arg * Simplify conditional logic * Return source code if shouldFix is false This way, the code that uses this doesn’t need to necessarily check the value of `fixed`. * Clarify that fixesWereApplied is always true here If we’ve gotten to this point, at least one fix will have been applied. * Add a test to ensure that fix functions are pure Meaning, that they cannot access the `this` value of SourceCodeFixer. * Add test with conditional shouldFix This is to verify that the problem can be used to return true or false conditionally, and that eslint will only apply fixes when true is returned. * Account for options not being provided This is to account for #8809
I'll talk a little about the use case I'm attempting to solve, and then my proposed solution:
Problem
Currently, it is not a simple matter to tell ESLint to apply fixes from only one rule, or to disable particular rules from fixes. Per-rule autofixing is being discussed in #8018, and @ljharb mentioned his use case of fixing one single rule at a time. In a subsequent chat discussion, I learned that Airbnb also uses inline config to change some rules to
warn
severity, and he would like to avoid applying fixes to those lines. Currently, there is no built-in way that I know of for ESLint to support this use case.Potential Solution
I would like to write a CLI tool (possibly an enhancement to eslint-nibble which would allow users to autofix one single rule at a time, while still respecting the rest of the eslint configuration in their
.eslintrc
file. Furthermore, I would like to allow an option to not fix messages with aseverity
of1
.Unfortunately, this is not currently possible. To apply fixes with
CLIEngine
, it's necessary to configure it withfix: true
. This adds anoutput
field to the results, whichCLIEngine.outputFixes()
applies to the filesystem. However, the creation of the error messages and output occur together, when callingCLIEngine.executeOnFiles
orCLIEngine.executeOnText
.Proposal
My proposal is to allow users to modify the messages within
report.results
(for example, to strip out any messages withseverity: 1
or all except a particular rule), before fixes are applied.I have experimented with adding an
applyFixesToReport
method toCLIEngine
, which would accept a report object, do a little bit of work to get sourceFile objects using our existing utilities, and then callSourceCodeFixer.applyFixes()
, returning a newreport
including anoutput
string if fixes were applied from the messages in the originally supplied report.The changes to ESLint are pretty minimal using this approach, and would allow much more flexibility in the fixes that ESLint performs. I'd be happy to submit a PR if this suggestion is approved.
The text was updated successfully, but these errors were encountered: