Skip to content

Commit

Permalink
[Fix #1406] Allow a newline in SpaceInsideRangeLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Batsov committed Nov 8, 2014
1 parent 200bbd5 commit 85f2b8d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#1415](https://github.com/bbatsov/rubocop/issues/1415): String literals concatenated with backslashes are now handled correctly by `StringLiteralsInInterpolation`. ([@jonas054][])
* [#1416](https://github.com/bbatsov/rubocop/issues/1416): Fix handling of `begin/rescue/else/end` in `ElseAlignment`. ([@jonas054][])
* [#1413](https://github.com/bbatsov/rubocop/issues/1413): Support empty elsif branches in `MultilineIfThen`. ([@janraasch][], [@jonas054][])
* [#1406](https://github.com/bbatsov/rubocop/issues/1406): Allow a newline in `SpaceInsideRangeLiteral`. ([@bbatsov][])

## 0.27.0 (30/10/2014)

Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/cop/style/space_inside_range_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module RuboCop
module Cop
module Style
# Checks for spaces inside range literals.
#
# @example
# # bad
# 1 .. 3
Expand Down Expand Up @@ -31,9 +32,13 @@ def on_erange(node)

def check(node)
expression = node.loc.expression.source
operator = node.loc.operator.source.gsub(/\./, '\.')
op = node.loc.operator.source
escaped_op = op.gsub(/\./, '\.')

# account for multiline range literals
expression.sub!(/#{escaped_op}\n\s*/, op)

return unless expression =~ /(\s#{operator})|(#{operator}\s)/
return unless expression =~ /(\s#{escaped_op})|(#{escaped_op}\s)/

add_offense(node, :expression)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/space_inside_range_literal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
expect(cop.offenses).to be_empty
end

it 'accepts multiline range literal with no space in it' do
inspect_source(cop, ['x = 0..',
' 10'])
expect(cop.offenses).to be_empty
end

it 'registers an offense in multiline range literal with space in it' do
inspect_source(cop, ['x = 0 ..',
' 10'])
expect(cop.offenses.size).to eq(1)
end

it 'autocorrects space around .. literal' do
corrected = autocorrect_source(cop, ['1 .. 2'])
expect(corrected).to eq '1..2'
Expand Down

0 comments on commit 85f2b8d

Please sign in to comment.