Closed mmkal closed 3 weeks ago
Thanks for the feedback. It sounds like the use case you're describing is when you're applying autofixing on save in your IDE. This is something we don't recommend. ESLint needs completed code to make the best autofix decisions.
prefer-const
is actually one of our more popular rules, and when applied to completed code, works as expected.
I'd suggest turning off autofixing in your IDE.
@nzakas thanks. Unfortunately turning off autofixing in-IDE isn't a realistic suggestion, it's a useful part of my/many others' workflows.
I don't agree that ESLint needs completed code to make usefuul autofix decisions, it's extremely useful for as a code-writing aide, fixing things as it goes. There are a couple of minor annoyances like this, but definitely nothing bad enough to warrant the fairly extreme option of giving up on it as an IDE autofixer.
What about the second question - is there a way or could there be a way for an end-user to adjust a rule so it downgrades a fix to a suggestion? Something like:
export default [
{
rules: {
'prefer-const': 'warn:downgradeFixToSuggestion',
},
}
]
But maybe with better naming?
What rule do you want to change?
prefer-const
What change do you want to make?
Implement autofix
How do you think the change should be implemented?
A new option
Example code
What does the rule currently do for this code?
Changes it to
What will the rule do after it's changed?
Warn, and suggest the fix, but not apply it.
Participation
Additional comments
Reasoning for not wanting it to be auto-fixed:
// name = 'Bob'
line, to quickly test things out with a few different valuesconst
was introduced so long ago that I suspect it's not usually that helpful to have eslint remind them to useconst
. It's just a reflex (and some people don't like const at all, they can just disable the rule)It also is annoying when you set up a mutable variable and accidentally press "Save" before you've got round to mutating it.
The rule is still very useful as a reminder that you need to go back and const-ify anything that was temporary, but I don't think the autofix is. (
suggest
would be ideal).I think replacing the
fix
withsuggest
unilaterally would be good, but making it configurable would be a fine compromise to me.Alternatively, if eslint provided a way to downgrade a fix to a suggestion in an end-user's config, that would solve this problem and the dozens of other similar minor gripes I have with other people's rules.