Skip to content

Commit

Permalink
[Fix rubocop#7675] Fix a false negative for Layout/SpaceBeforeFirstArg
Browse files Browse the repository at this point in the history
Fixes rubocop#7675.

This PR fixes a false negative for `Layout/SpaceBeforeFirstArg`
when a vertical argument positions are aligned.
  • Loading branch information
koic committed Jan 31, 2020
1 parent 58ab70f commit 03cc4f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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

0 comments on commit 03cc4f9

Please sign in to comment.