Fixes bug in Metrics/MethodLength
cop. The cop does not count block comments even though count comments setting is turned off.
#12658
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replace this text with a summary of the changes in your PR.
The more detailed you are, the better.
The other day I was trying to reduce the number of lines in the methods of my project. I am not a big fan of block comments but I usually use them to toggle code snippets I want to test later on. I discovered my rubocop LSP instance was complaining that the method had a lot of lines, that was because I commented with
=begin ... =end
a lot of lines.The code changes on this PR attempts to fix the issue, so it will not count block comments. The main problem is this "bad API" method was used by CodeLengthCalculator#irrelevant_line?. So, block comments were not considered by the
CodeLengthCalculator
(because the regex in the "bad API" method does not catch multiline comments (ie, block comments)).My first approach to solve the issue consisted in using @processed_source to analyze its tokens array. But I found out that the tokens array value is set using tokenise(create_parser(ruby_version)).
create_parser
must use a specific ruby version class depending on the ruby version used. After realizing that, I thought "Mmmhh... maybe thiscreate_parser
method is very expensive in terms of computation and maybe I should not use it..."Then, I decided to follow a simpler approach: find all the places irrelevant_line? method is called, and add a filter stage just before the method is called. The filter stage consists in calling a recursive method that will remove all lines between a
"=begin"
tag, and"=end"
tag.For example, by using this CodeLengthCalculator#clean_block_comments recursive method, you will transform this array:
and convert that array into this one:
My solution seems to work, I can add more tests to it if you want more. But I'd love to make a more efficient solution to this bug. Any feedback is welcome.
Notes on the checklist item:
Metrics/PerceivedComplexity
andMetrics/AbcSize
. I don't know how to solve these two issues.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.