-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Autocorrect BracesAroundHashParameters leaves extra space #1527
Comments
The relevant code is here. Right now the braces are removed, but nothing else, which leaves two spaces. A fix could be checking if the character after the first brace is a hash and removing it if so. I'd love to get involved with Rubocop, so if no one objects, I can submit a pull request. |
Also I replicated the issue - here's the rubocop -v output.
|
@mattjmcnaughton Please go ahead with a pull request. Removing the space together with the brace sounds like a good plan. Do it for both braces. There's a method available in all cops called |
Ok so I made the changes you recommended @jonas054, and the changes fix @aripollak issue. But, it causes a lot of the other tests to fail. I'll include an output from a test in braces_around_hash_parameter_spec.rb that highlights the issue.
The output of the rspec test is: The same thing occurs for a lot of the other tests of the To me, removing all the extra white space seems like a good idea. But I'm also new to this project, so I may totally be missing something. Let me know what you think is best and I'll make a pull request accordingly! |
The central auto-correction logic has changed a lot over time, and so have the principles for how to do automatic corrections in the cops. At one point it was really important that corrections were made as small as possible in order to minimize the risk for interference ("clobbering") between cops. This risk is pretty much eliminated now since auto-correction is done one cop at a time, with writing to file and re-parsing the file after each cop has finished. So the behavior we had was the best choice when it was implemented, but now it's better to include whitespace clean-up. Go ahead and update the expected code in the examples by removing the extra whitespace. |
Ok awesome - thanks @jonas054! There is still one test that doesn't pass that I'd love some help with. It is this one in cli_spec. I'm thinking it make but something to do with the way newlines are handled but I'm not sure. Here's the diff between expected and actual.
Also, does the expected output for this test look right? Frankly I'm not sure what is being tested and what the expected behavior should be. Right now it passes, but I don't know if the output is what we actually want. Let me know if it's easier for me to make a pull request now - thought I'd wait until all the tests were all passing and I had a chance to update the change logs but I can do it sooner if that's easier. |
I hadn't examined The other spec example that you link to looks as expected. |
Ok, so as per my understanding of the original def range_with_surrounding_space(range, side = :both)
src = @processed_source.buffer.source
go_left = side == :left || side == :both
go_right = side == :right || side == :both
begin_pos = range.begin_pos
begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
end_pos = range.end_pos
end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/
end_pos += 1 if go_right && src[end_pos] == "\n"
Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos)
end I think there are two options. FirstAdding code to remove newlines within begin_pos = range.begin_pos
begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
begin_pos -= 1 if go_left && src[begin_pos - 1] == "\n" This option makes the most sense to me because it gives a uniform behavior to SecondRemove the To me, option 1 makes the most sense, but its up to you! |
I agree. Option 1 looks better. |
…roundhashparameters-leaves-extra-space [Fix #1527] Fix autocorrect bracesaroundhashparameters leaves extra space
When running rubocop with autocorrect, it changes a file like this:
There should only be one space after the comma instead of two.
The text was updated successfully, but these errors were encountered: