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

Fix bug in RangeHelp where range_by_whole_lines goes past end of buffer #5908

Merged
merged 3 commits into from
May 26, 2018

Conversation

EiNSTeiN-
Copy link
Contributor

@EiNSTeiN- EiNSTeiN- commented May 22, 2018

This fixes a bug where RangeHelp#range_by_whole_lines may return a range that goes past the end of the buffer when include_final_newline argument is true. This is because of the line end_offset += 1 if include_final_newline.

I added 2 tests that fail without this fix, the original bug I encountered was from BracesAroundHashParameters when the hash being removed occurs at the very end of the ruby code.

@EiNSTeiN- EiNSTeiN- force-pushed the fix-range-bug branch 2 times, most recently from 45c0766 to 3dd476d Compare May 22, 2018 22:16
@EiNSTeiN-
Copy link
Contributor Author

To add some more context, without this fix, an exception is raised when BracesAroundHashParameters attempts to auto-correct a problematic file.

     IndexError:
       The range file.rb:3:1 is outside the bounds of the source
     # /Users/einstein/.gem/ruby/2.3.3/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:276:in `check_range_validity'
     # /Users/einstein/.gem/ruby/2.3.3/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:268:in `combine'
     # /Users/einstein/.gem/ruby/2.3.3/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:129:in `replace'
     # /Users/einstein/.gem/ruby/2.3.3/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:153:in `remove'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/corrector.rb:75:in `remove'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/style/braces_around_hash_parameters.rb:146:in `remove_braces_with_range'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/style/braces_around_hash_parameters.rb:133:in `remove_braces_with_whitespace'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/style/braces_around_hash_parameters.rb:66:in `block in autocorrect'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/corrector.rb:61:in `block (2 levels) in rewrite'
     # /Users/einstein/.gem/ruby/2.3.3/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:220:in `transaction'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/corrector.rb:60:in `block in rewrite'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/corrector.rb:58:in `each'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/corrector.rb:58:in `rewrite'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:126:in `autocorrect_all_cops'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:70:in `autocorrect'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:98:in `block in offenses'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:115:in `investigate'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:94:in `offenses'
     # /Users/einstein/src/github.com/bbatsov/rubocop/lib/rubocop/cop/team.rb:44:in `inspect_file'

it 'removes hash braces' do
src = "render 'foo', {\n foo: bar\n}"
corrected = autocorrect_source(src)
expect(corrected).to eq("render 'foo', \n foo: bar\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this wasn't working before the change. It seems like this is what should be in the CHANGELOG. We normally only add CHANGELOG references for changes that could impact users. The average user doesn't know what RangeHelp or range_by_whole_lines is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, do you have a preferred wording for the changelog?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with

Fix `Style/BracesAroundHashParameters` auto-correct sometimes going past the end of the file.

Does that work better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds fine to me. Thanks for adding some context. Do you know what in particular about this line of code caused it go past the end of the file? Maybe something about the spacing or new lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the example broken source is on line 267

src = "render 'foo', {\n  foo: bar\n}"

the problem is that this cop wants to remove the braces around hash argument, and to do that it will try to select the entire line where } is located including a possible trailing newline, to do so range_by_whole_lines will add 1 character to the end of the line here:
https://github.com/bbatsov/rubocop/blob/a66faa88ce004f8717aff136230d196e8be3ee2c/lib/rubocop/cop/mixin/range_help.rb#L68
and this makes the range go past the end of the buffer because } in this example is the last character in the buffer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like the issue comes from a closing curly bracket, } living on a separate line than the contents of the hash. If that is the case, it would be useful to add to the CHANGELOG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some clarification to changelog 👍

…that goes past the end of the given buffer
@bbatsov bbatsov merged commit 8d59b65 into rubocop:master May 26, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented May 26, 2018

Thanks!

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.

3 participants