-
Notifications
You must be signed in to change notification settings - Fork 38
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 #248
Add autocorrect #248
Conversation
Thank you for reviewing, @veelenga and @Sija. Now before I start working on the final todo, do you think it is necessary?
Or is what I have so far good enough, and this can be added in a follow-up pull request? For reference, here's the relevant documentation: https://github.com/whitequark/parser/blob/master/lib/parser/source/tree_rewriter.rb#L59-L77 |
@FnControlOption I am fine moving this out of scope of this PR. Current work is big enough. Thanks |
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 tested this locally and it is looking great. Thanks for you work @FnControlOption
I left two small suggestions/comments
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.
🚀
User-facing changes:
--fix
.Style/LargeNumbers
and partially forLayout/TrailingBlankLines
.block
parameter toReportable#add_issue
. Theblock
receives aSource::Corrector
(described below) to perform autocorrection.expect_correction
andexpect_no_corrections
spec helpers.Implementation details:
Source::Rewriter
andSource::Rewriter::Action
, which are ports ofParser::Source::TreeRewriter
andParser::Source::TreeRewriter::Action
from the Ruby gemparser
. These classes accept source positions as integer indexes (instead of line and column).Source::Corrector
, which wraps around aSource::Rewriter
. This class provides helper methods that accept source locations asCrystal::Location
or{Int32, Int32}
. (The length of each source line is stored in an array, which is used to convert AST locations toRewriter
positions.)Changes required for recursive autocorrect (f39a7a4):
Source#correct
, which runs autocorrection and updates the underlying@code
property.Issue.new
now requires an additionalcode
parameter (becauseSource#code
is no longer immutable).Formatter::Util#affected_code
now has an additional method overload.String
as the first parameter instead ofSource
.Issue
so that we can now doaffected_code(issue)
instead ofaffected_code(issue.code, issue.location, issue.end_location)
Todo:
src/ameba/formatter
to indicate whether any issues have been autocorrected.RuboCop::Cop::Corrector
:remove_preceding
,remove_leading
,remove_trailing
Config#autocorrect
Runner#autocorrect
Source#correct
Source::Corrector
and its methods:nodoc:
directive toSource::Rewriter::Action
since it is only used internally.Future PRs:
different_replacements
andswallowed_insertions
(and maybe alsocrossing_deletions
). I didn't realize thatRuboCop::Cop::Corrector
configuresdifferent_replacements
andswallowed_insertions
to:raise
.