Skip to content

Commit

Permalink
Require a space before first argument of a method call (#4265)
Browse files Browse the repository at this point in the history
Ruby permits a method name to be followed immediately by a string
literal, with no space between them. This change updates the
SpaceBeforeFirstArg cop to register a violation for source code that
does this.
  • Loading branch information
cjlarose authored and bbatsov committed Apr 12, 2017
1 parent 52e5d4e commit fde6b20
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Bug fixes

* [#4265](https://github.com/bbatsov/rubocop/pull/4265): Require a space before first argument of a method call in `Style/SpaceBeforeFirstArg` cop. ([@cjlarose][])
* [#4237](https://github.com/bbatsov/rubocop/pull/4237): Fix false positive in `Lint/AmbiguousBlockAssociation` cop for lambdas. ([@smakagon][])
* [#4242](https://github.com/bbatsov/rubocop/issues/4242): Add `Capfile` to the list of known Ruby filenames. ([@bbatsov][])
* [#4240](https://github.com/bbatsov/rubocop/issues/4240): Handle `||=` in `Rails/RelativeDateConstant`. ([@bbatsov][])
Expand Down Expand Up @@ -2745,3 +2746,4 @@
[@dpostorivo]: https://github.com/dpostorivo
[@konto-andrzeja]: https://github.com/konto-andrzeja
[@sadovnik]: https://github.com/sadovnik
[@cjlarose]: https://github.com/cjlarose
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/space_before_first_arg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ module Style
# @bad
# something x
# something y, z
# something'hello'
#
# @good
# something x
# something y, z
# something 'hello'
#
class SpaceBeforeFirstArg < Cop
include PrecedingFollowingAlignment
Expand All @@ -34,7 +36,7 @@ def on_send(node)
space = range_between(first_arg_with_space.begin_pos,
first_arg.begin_pos)

add_offense(space, space) if space.length > 1
add_offense(space, space) if space.length != 1
end

def autocorrect(range)
Expand Down
2 changes: 2 additions & 0 deletions manual/cops_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -5182,10 +5182,12 @@ config parameter is true.
# bad
something x
something y, z
something'hello'

# good
something x
something y, z
something 'hello'
```

### Important attributes
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/space_before_first_arg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@
END
end

it 'registers an offense for method call with no spaces before the '\
'first arg' do
inspect_source(cop, <<-END.strip_indent)
something'hello'
a.something'hello world'
END
expect(cop.messages)
.to eq(['Put one space between the method name and the first ' \
'argument.'] * 2)
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(cop, <<-END.strip_indent)
something'hello'
a.something'hello world'
END
expect(new_source).to eq(<<-END.strip_indent)
something 'hello'
a.something 'hello world'
END
end

it 'accepts a method call with one space before the first arg' do
inspect_source(cop, <<-END.strip_indent)
something x
Expand Down

0 comments on commit fde6b20

Please sign in to comment.