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 #7675] Fix a false negative for Layout/SpaceBeforeFirstArg #7678

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* [#7661](https://github.com/rubocop-hq/rubocop/pull/7661): Fix to return correct info from multi-line regexp. ([@Tietew][])
* [#7655](https://github.com/rubocop-hq/rubocop/issues/7655): Fix an error when processing a regexp with a line break at the start of capture parenthesis. ([@koic][])
* [#7647](https://github.com/rubocop-hq/rubocop/issues/7647): Fix an `undefined method on_numblock` error when using Ruby 2.7's numbered parameters. ([@hanachin][])
* [#7675](https://github.com/rubocop-hq/rubocop/issues/7675): Fix a false negative for `Layout/SpaceBeforeFirstArg` when a vertical argument positions are aligned. ([@koic][])

### Changes

Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/layout/space_before_first_arg.rb
Expand Up @@ -54,13 +54,21 @@ def regular_method_call_with_arguments?(node)

def expect_params_after_method_name?(node)
return false if node.parenthesized?
return true if no_space_between_method_name_and_first_argument?(node)

first_arg = node.first_argument

same_line?(first_arg, node) &&
!(allow_for_alignment? &&
aligned_with_something?(first_arg.source_range))
end

def no_space_between_method_name_and_first_argument?(node)
end_pos_of_method_name = node.loc.selector.end_pos
begin_pos_of_argument = node.first_argument.source_range.begin_pos

end_pos_of_method_name == begin_pos_of_argument
end
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/layout/space_before_first_arg_spec.rb
Expand Up @@ -59,6 +59,31 @@
RUBY
end

context 'when a vertical argument positions are aligned' do
it 'registers an offense' do
inspect_source(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized'asdf'
RUBY

expect(cop.messages).to eq(
['Put one space between the method name and the first argument.']
)
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized'asdf'
RUBY

expect(new_source).to eq(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized 'asdf'
RUBY
end
end

it 'accepts a method call with one space before the first arg' do
expect_no_offenses(<<~RUBY)
something x
Expand Down