-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
feat: Per rule autofix configuration #125
base: main
Are you sure you want to change the base?
feat: Per rule autofix configuration #125
Conversation
Hi @Samuel-Therrien-Beslogic!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
Hi @Samuel-Therrien-Beslogic!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
What about instead of disabling autofixes, it turns it into suggestions? |
That is what I would intend. I don't think suggestions need to be disabled as they're not "automated". I'll try to update my RFC to make this clearer. One of the open question is whether suggestions could also be disabled (my proposal says no, but I've left the door open). The only case I could see it being valid is if a plugin has really bad suggestions and the maintainers are not updating it. |
I think this RFC, where it only allows "demoting" autofixes to suggestions on a per-rule basis, is excellent and more than sufficient. |
…ed" to suggestions.
@Samuel-Therrien-Beslogic please be sure to sign the CLA. |
I sent the Corporate Contributor CLA to my employer (I know this is just a doc, and not code, but they preferred it given it was done under work hours). The person in charge told me he received the email. So hopefully that should be done soon :) |
To that end, if we're only using the autofixes portion of the config to turn off autofixes for specific rules, I'd propose having the config just be an array of rule names to turn autofix off, rather than it being a
|
I guess you might need more than just an array in order to re-enable autofixes in later configs. You have it described in the RFC with export default [
{
autofixes: {
// We don't want this to autofix, as a rule suddenly not failing should require human attention
'@eslint-community/eslint-comments/no-unused-disable': false,
},
rules: {
'@eslint-community/eslint-comments/no-unused-disable': 'error',
},
},
{
files: ['*.spec.js'],
autofixes: {
// Let's pretend we want this to be autofixed in tests, for the sake of the RFC
'@eslint-community/eslint-comments/no-unused-disable': true,
},
}
]; |
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 for getting this started. I left a note regarding providing implementation details, which is required for an RFC.
used. Be sure to define any new terms in this section. | ||
--> | ||
|
||
Similar to how Ruff (<https://docs.astral.sh/ruff/settings/#lint_unfixable>) does it, a top-level key to specify which rules to not autofix would be in my opinion the least disruptive and forward/backwards compatible. It should be overridable (in the `overrides` section), and picked up when extending a configuration. |
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.
The new config system doesn't support an overrides
key.
] | ||
``` | ||
|
||
The fix should still exist as a suggestion. Only autofixing (when running `eslint --fix` or editor action on save) should be disabled. |
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 assuming you mean in all situations when autofixes are applied? That would include using the ESLint.outputFixes()
or SourceCodeFixer
APIs.
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 am not familiar with ESLint's API or implementation, how would you rephrase this? I wrote this purely from an ESLint user's PoV because, well, that's what I am ^^
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.
Technically, Linter
(or report-translator
?) could just create a new object in the suggestions
array and move the fix
into it. We'd just have to figure out what to set as desc
(the text that appears in IDEs as an action to apply the suggestion).
https://eslint.org/docs/latest/integrate/nodejs-api#-lintmessage-type
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 surprised that anything with an an autofix available isn't also automatically available as an editor suggestion. Do plugins have to provide both separately? If not, what is the "desc" equivalent to here? Or maybe ESLint just autofills suggestions
from the autofix ?
(the example below is an autofixable rule that shows up as an editor suggestion, I just want to disable autofixes w/o removing the ability to manually trigger the fix from this menu)
If it's the case that they're provided separately, only the entry from LintMessage.fix
needs to be dropped.
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.
That is a good point :-)
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.
"Fix this autofix/prefer-template problem" is a text VS Code generates by itself when there's LintMessage#fix
property. On click, VS Code applies the code change described in LintMessage#fix
.
For LintMessage#suggestions
, VS Code shows suggestion.desc
as the text. On click, VS Code applies the code change described in suggestion.fix
.
So, IDEs use both LintMessage#fix
and LintMessage#suggestions
to generate editor suggestions. Rules don't provide the same fix in both LintMessage#fix
and LintMessage#suggestions
. Furthermore, it probably never makes sense for a rule to provide LintMessage#suggestions
when it provides LintMessage#fix
.
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 think it makes sense to provide both sometimes - an autofix can be partial, and suggestions can go farther.
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.
@mdjermanovic Is there no way to provide a non-automatic fix (ie: a suggestion) using a default message ?
Oh yeah, look at that, I can see the implication of your explanation where the editor hint for the fix is completely lost. Using the no-autofix
plugin I do loose suggestions (although maybe it's because of how that plugin highjack rules, I don't have a better example with more "vanilla" rules off the top of my head)
vs
That's a behaviour I'd like to improve as part of this RFC over how the no-autofix
plugin works. So I would prefer "promoting" the disabled autofix to a suggestion.
If there's no way to provide a "use default message", can we just manually pass
{
suggestion: {
fix: lintMessage.fix,
// Something like this, with translation support?
desc: `Fix this ${ruleName} problem`,
// or
desc: `Apply the disabled autofix for ${ruleName}`,
},
fix: undefined,
}
or maybe allowing an empty desc
to fallback to a default "fix this" message should be a requirement for this RFC.
Thoughts on how to handle this? I'm pretty convinced on "promoting" the fix to suggestion, but the description if still an open question.
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.
Maybe just desc: 'Apply autofix'
?
desc: 'Apply the disabled autofix'
also sounds good to me.
I think there's no need for the rule name in desc
as IDEs display the rule name with the suggestion.
or maybe allowing an empty
desc
to fallback to a default "fix this" message should be a requirement for this RFC.
Suggestions should generally provide a descriptive message, so I think it's better to not implement a fallback.
Explain the design with enough detail that someone familiar with ESLint | ||
can implement it by reading this document. Please get into specifics | ||
of your approach, corner cases, and examples of how the change will be | ||
used. Be sure to define any new terms in this section. |
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're missing details on how this will be implemented. Please take a look at the code and see how you expect this feature to be implemented.
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.
If I really need to learn about how ESLint is implemented I can, but idk when I'll get to it. I'd be more than happy if an existing contributor is willing to fill in this section for me as I am not personally concerned with implementation details.
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.
Looking at the code a bit, I would say this is probably a good place to start exploring: https://github.com/eslint/eslint/blob/main/lib/eslint/eslint.js#L386-L394
It's what's being used to determine if a rule should have a fix applied to it, based on a few different conditions. Based on the response from that, a "fixer" is passed back to the linter to use as part of its fix attempt loop.
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.
@Samuel-Therrien-Beslogic exploring the implementation is part of the RFC process. We can't really evaluate any proposal without it.
Thinking more on the name, maybe something like export default [
{
disableAutofixes: {
// We don't want this to autofix, as a rule suddenly not failing should require human attention
'@eslint-community/eslint-comments/no-unused-disable': true,
},
rules: {
'@eslint-community/eslint-comments/no-unused-disable': 'error',
},
},
{
files: ['*.spec.js'],
disableAutofixes: {
// Let's pretend we want this to be autofixed in tests, for the sake of the RFC
'@eslint-community/eslint-comments/no-unused-disable': false,
},
}
]; |
Thanks. I am not familiar with flat-configs yet (too many plugins haven't migrated to v9, and I will need to rewrite our entire configs)
Agreed. This is also closer to the inspiration cited with Ruff.
Indeed a record seems cleaner to be able to re-enable. One could allow an array to I've added an entry about this in the FAQ |
used. Be sure to define any new terms in this section. | ||
--> | ||
|
||
Similar to how Ruff (<https://docs.astral.sh/ruff/settings/#lint_unfixable>) does it, a top-level key to specify which rules to not autofix would be in my opinion the least disruptive and forward/backwards compatible. It should be overridable in per-file configurations, and picked up when extending a configuration. |
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.
An alternative could be to expand rule config values to allow objects in which this would be a property. For example:
export default [
{
rules: {
"@eslint-community/eslint-comments/no-unused-disable": {
severity: "error",
options: [/* ... */],
disableAutofixes: true
}
}
}
];
The advantages of this approach are that all the configurations for the rule can be in one place, and it would be easier to add more "meta" options if needed in the future.
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 like it! 👍
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.
We considered this a while back, even without trying to turn off autofixing. I'm not a fan of forcing rules to have to write out "severity" and "options", which is why we stuck with just an array.
This would also complicate rule configuration merging.
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.
Given a large number of config in the community are already in array format, I don't think we can drop it; instead, we can support both:
"@eslint-community/eslint-comments/no-unused-disable": ["error", {...}]
// is as the same as:
"@eslint-community/eslint-comments/no-unused-disable": {
severity: "error",
options: [/* ... */],
disableAutofixes: false, // the default
}
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 don't think anyone was suggesting dropping the current format. I just think an object format adds additional complexity when merging rule configurations that isn't necessary. Plus, to modify a rule to disable autofix, you'd first need to convert the array into an object vs. adding a new key elsewhere.
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.
An alternative could be to expand rule config values to allow objects in which this would be a property. For example:
export default [ { rules: { "@eslint-community/eslint-comments/no-unused-disable": { severity: "error", options: [/* ... */], disableAutofixes: true } } } ];The advantages of this approach are that all the configurations for the rule can be in one place, and it would be easier to add more "meta" options if needed in the future.
Another advantage is that autofixes could be turned off from the CLI (with --rule 'some-rule: { disableAutofixes: true, ...}'
) or inline with /* eslint */
config comments without introducing additional syntax.
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.
Changing the rule config format was a considered option (although not too much thoughts was put into what exactly that change would entail).
I like your suggestion too as it would also be backwards compatible and imo feels like a natural place for the config to live. But I went for the approach that reduces maintenance as much as possible.
At the end of the day it's really not my decision and both would satisfy my needs. So I'm leaving this one to the maintainers :)
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.
@fasttime this is additional syntax, though. Config comments and the command line don't already support this object notation.
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.
@fasttime this is additional syntax, though. Config comments and the command line don't already support this object notation.
Well, true. I should have written "with the same new syntax". My point is that rules can be configured in multiple ways, but the suggested approach only allows disabling autofixes inside a config.
I think while we are here, we should at least ask the question if it's sensible to disable autofixes from the CLI and in config comments. If the answer is no, we can go with the suggested approach and be assured that we won't have to rethink the design later.
How will this RFC be documented? Does it need a formal announcement | ||
on the ESLint blog to explain the motivation? | ||
--> | ||
I think that "Configuring autofixes" or "Disabling autofixes" could be documented as a subsection of [Configuring Rules](https://eslint.org/docs/latest/use/configure/rules). Or as a section on the same level (between "Configuring Rules" and "Configuring Plugins") |
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.
If a new top-level property like disableAutofixes
is added to configuration objects then it should be documented in the eslint
repo in docs/src/use/configure/configuration-files.md. Additionally we may want to add a note to docs/src/extend/custom-rules.md to mention that some autofixes will be converted automatically into suggestions when the new feature is used.
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.
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.
Any input on whether the main feature's documentation should be a subsection of
Configuring Rules
or on the same level ?
I think a new section inside the "Configure Rules" page is fine.
used. Be sure to define any new terms in this section. | ||
--> | ||
|
||
Similar to how Ruff (<https://docs.astral.sh/ruff/settings/#lint_unfixable>) does it, a top-level key to specify which rules to not autofix would be in my opinion the least disruptive and forward/backwards compatible. It should be overridable in per-file configurations, and picked up when extending a configuration. |
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.
An alternative could be to expand rule config values to allow objects in which this would be a property. For example:
export default [ { rules: { "@eslint-community/eslint-comments/no-unused-disable": { severity: "error", options: [/* ... */], disableAutofixes: true } } } ];The advantages of this approach are that all the configurations for the rule can be in one place, and it would be easier to add more "meta" options if needed in the future.
Another advantage is that autofixes could be turned off from the CLI (with --rule 'some-rule: { disableAutofixes: true, ...}'
) or inline with /* eslint */
config comments without introducing additional syntax.
Summary
This feature aims to make it possible to control autofixes through shareable configuration on a per-rule basis.
Related Issues
eslint/eslint#18696