-
Notifications
You must be signed in to change notification settings - Fork 423
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 autocorrect functionality #944
Comments
First, thank you very much for putting this together. As I wrote on Twitter, it would be amazing to have this! 🎉 I hope neither you nor anyone else reading this is discouraged by the rest of this response. Take this wall-of-text as a compliment for taking on a great endeavour. Why has nobody done this, yet?The biggest question from a maintainer side: Why hasn't this been developed as a plugin? Credo has the necessary plugin APIs since version 1.1 (add an Several people, myself included, have since tried to come up with an experimental plugin to do this. I am guessing the others were as successful as I was in bringing it to fruition. Some people have pointed out that we weren't able to implement something like Autofix before Elixir 1.13 because we did not have So what do we need?Here's a quick rundown of the things we need to consider (off the top of my head):
(This list might very well be incomplete as it is a bit late and I have an early day tomorrow. Please add your thoughts in the comments below!) Where do we go from here?My biggest concern as a steward of this project is that this feature is a very shiny, high profile thing to work on for a weekend or a month, but if we do not nail the APIs, corner cases, upwards compatibility support etc. it will be a pain in the ?%$ to support for the coming years. I would suggest that we work on this as a proof-of-concept plugin. If there are any features we need from Credo for this to work, we can probably add them without compromising Credo's core (e.g. if the Just like Let me close by saying: I love this community and I would be delighted if we could work on this together. ✌️ |
I have started to playing around with the package doorgan/sourceror to reformat code that would raise issues in There is also an approach for a refactoring task to rename functions. But this is much more experimental as the other stuff. |
Thanks for the really good rundown of what we'll need for this. I had thought of a few of these before as well, but not all of them, so thanks for bringing this up. Here's what I have so far:
|
I still have difficulties wrapping my head around the question "What is the next actionable step?" here. Here's a Gist with standard checks that could be auto-fixable (including those fixed by Elixir's formatter): https://gist.github.com/rrrene/487a4362873db1fe93fe55b8df27fb28 |
This issue is to discuss how we can add autocorrect functionality to Credo.
What is it?
Autocorrect would enable Credo to programatically "correct" issues found in a given file
instead of only telling the user what can be improved and how to improve it. One example
would be that it would automatically change
foo |> bar(baz)
tobar(foo, baz)
withoutthe user having to manually make that change if the single pipe check is enabled.
How it might work
Autocorrect would be disabled on all checks by default, so
mix credo
would run exactlyas it does today. Autocorrect would be implemented on a per-check basis, since some
checks (like cyclomatic complexity) cannot be autocorrected.
If a user wanted to run autocorrect on all checks that support it, they can run
mix credo --autocorrect
and that will run Credo and autocorrect everything that canbe autocorrected.
A user could also enable autocorrect for certain checks by passing an argument to
that flag like
mix credo --autocorrect Credo.Check.Refactor.AppendSingleItem
, orby adding
autocorrect: true
to their config file like so:{Credo.Check.Consistency.ParameterPatternMatching, autocorrect: true}
.Implementation ideas
My thinking for a general sketch of an implementation would be something along
the lines of this:
autocorrect/1
callback to theCredo.Check
behavior. Thiscallback would either accept an AST and return an AST, or accept a String and
return a String (depending on the check) which corresponds to the AST node
or fragment of the file which triggered the issue. The
autocorrect/1
functionwill return the "corrected" string or AST for the given check. The default for this
callback will be to simply return what it is given (a
noop
which does noautocorrecting).
these functions are used as replacements for the original values in the file
or AST which triggered it.
file 😄
Workflow ideas
To validate this approach, I'd think we would maybe want to pick 2-3 checks as a
starting point (ideally ones that can happen multiple times per file) and implement
just these 2-3. Ideally we'd pick one that is fairly straightforward, like
Credo.Check.Readability.SinglePipe
,Credo.Check.Readability.StringSigils
, orCredo.Check.Readability.TrailingBlankLine
, and ideally one that uses an ASTfor the autocorrect and one that uses a string as the autocorrect, so we can try
both of those.
The text was updated successfully, but these errors were encountered: