Skip to content
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

Attempt to fix offense.highlighted_area returning an incorrectly constructed range #12748

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Earlopain
Copy link
Contributor

Attempt at #12527. This is not correct yet because tests are not passing.

To be honest, I'm not sure what's wrong here. Sometimes it starts one character to the left, adding 1 all the time is not the fix since it then starts one to the right in some cases. There's some offset fuckery going on that I'm not getting behind.

If someone can give insight into why that might be, that would be appreciated. I will add changleog/tests when I've got this figured out.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@Earlopain
Copy link
Contributor Author

Sample test case bundle exec rspec ./spec/rubocop/cli/autocorrect_spec.rb:1714. I have added a bit of debugging output in the method:

def highlighted_area
  correct_range = Parser::Source::Range.new(source_line, column, column + column_length)
  range = location.source_buffer.line_range(line)
  offset = location.begin_pos - column
  range = range.with(begin_pos: column, end_pos: column + column_length)

  pp "loc.begin_pos: #{location.begin_pos}, loc.end_pos #{location.end_pos}, loc.column: #{location.column}, loc.column_length: #{column_length}"
  pp correct_range.begin_pos - range.begin_pos
  pp correct_range.end_pos - range.end_pos
  pp correct_range.source, range.source

  range
end
example.rb:1:12: C: [Corrected] Style/MethodDefParentheses: Use def with parentheses when there are parameters.
def primes limit
+"loc.begin_pos: 11, loc.end_pos 16, loc.column: 11, loc.column_length: 5"
+0
+0
+"limit"
+"limit"
           ^^^^^
example.rb:2:24: C: [Corrected] Style/SymbolProc: Pass &:even? as an argument to select instead of a block.
  1.upto(limit).select { |i| i.even? }
-                       ^^^^^^^^^^^^^^^
+"loc.begin_pos: 40, loc.end_pos 55, loc.column: 23, loc.column_length: 15"
+0
+0
+"{ |i| i.even? }"
+"to(limit).selec"
+                      ^^^^^^^^^^^^^^^

I can tweak the positions that the source is correct but then the positions are all wrong. Haven't figured out how to get both to line up yet.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 15, 2024

@koic Can you take a look here?

@bbatsov bbatsov requested a review from koic March 15, 2024 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants