Additional option for --fix
See original GitHub issueThe problem you want to solve.
Currently, the --fix
command forces ESLint to apply fixes for all rules that are currently enabled. While this is helpful some of the time, there is a distinct difference between stylistic rules and non-stylistic rules that this command doesn’t take into account right now.
Developers tend to like autofixing style rules so they don’t have to think about these little details. They tend to get annoyed when ESLint says to insert a space here or there. Non-stylistic rules, on the other hand, might be used as an educational tool. For example, prefer-const
is arguably a good educational tool and something is lost when prefer-const
is autofixed. Similarly, rules that enforce best practices are often more useful when not autofixed so developers can see the warning and better understand what will happen to their code when autofixed.
Your take on the correct solution to problem.
I’d like to add an option to the --fix
command, such that you can do:
--fix problems
- fixes only non-stylistic rules--fix styles
- fixes only stylistic rules--fix all
- fixes all rules
In each case, every rule is executed but only the specifies rule type will apply fixes, so --fix styles
will also run the non-stylistic rules but will not apply fixes for non-stylistic rules.
Backwards Compatibility: The all
option should be the default so --fix
continues to function as it does right now.
I’d like to add a --fix-type
flag that is used to specify which types of fixes to apply for both --fix
and --fix-dry-run
. The possible values are:
--fix-type problem
only applies fixes for rules that are specified as problems--fix-type suggestion
only applies fixes for rules that are specified as suggestions--fix-type style
only applies fixes for rules that are specified as stylistic
You can specify multiple fix types using either commas (--fix-type problem,suggestion
) or multiple flags (--fix-type problem --fix-type suggestion
).
If you don’t use --fix-type
then all possible fixes are applied (same as current behavior).
Rough Implementation Details
- Add a
meta.type
property to each rule (both core and plugin) that can be one of these values:- problem - the rule is flagging a pattern that might cause errors or confusion
- suggestion - the rule is flagging a potentially better way of doing something
- style - the rule is flagging a stylistic error that, when changed, does not change the meaning of the code
- Use the
fix
option ofCLIEngine
to specify a function that uses the--fix-type
information to filter out fixes that should not be applied. - When
--fix-type
is specified, all configured rules are executed but only rules matching the--fix-type
will apply fixes.
Concrete Use Case
The primary use case I’m thinking of here is when people want to autofix styles in their code editor but do not want to autofix other problems in order to use ESLint as an educational tool.
Are you willing to implement this?
Yes! I’d love to dive into this as a way to start getting involved a bit more going forward. 😃 It may take me a while to implement based on my energy levels, but gotta start somewhere!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:19 (19 by maintainers)
Top GitHub Comments
FWIW, if anyone does need per-rule fixing, I threw together https://github.com/IanVS/eslint-filtered-fix a while back, which does just that.
Is there a possibility to use the
meta.fixable
? it can be “whitespace” or “code”, but seems we are only checking its existence. if not exist, ESLint does not apply fixes even if the rule implements fix functions. could someone explain a bit its design purpose?https://github.com/eslint/eslint/blob/a6ebfd3089fecd4fd13594ff7def3caeaf410fb5/lib/linter.js#L832-L834
plz let me know if I’m missing something! 😄