Skip to content

Commit

Permalink
Merge pull request rubocop#1198 from jonas054/1197_space_before_pipe
Browse files Browse the repository at this point in the history
[Fix rubocop#1197] Deal with new lambda syntax in SpaceInsideBlockBraces
  • Loading branch information
bbatsov committed Jul 7, 2014
2 parents f8e0329 + fa0fd47 commit 3f7fec3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bugs fixed

* [#1197](https://github.com/bbatsov/rubocop/issues/1197): Fix false positive for new lambda syntax in `SpaceInsideBlockBraces`. ([@jonas054][])

## 0.24.1 (03/07/2014)

### Bugs fixed
Expand Down
24 changes: 14 additions & 10 deletions lib/rubocop/cop/style/space_inside_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def check_inside(node, left_brace, right_brace)
def braces_with_contents_inside(node, inner, sb)
_method, args, _body = *node
left_brace, right_brace = node.loc.begin, node.loc.end
pipe = args.loc.begin
args_delimiter = args.loc.begin # Can be ( | or nil.

if inner =~ /^\S/
no_space_inside_left_brace(left_brace, pipe, sb)
no_space_inside_left_brace(left_brace, args_delimiter, sb)
else
space_inside_left_brace(left_brace, pipe, sb)
space_inside_left_brace(left_brace, args_delimiter, sb)
end

if inner =~ /\S$/
Expand All @@ -72,11 +72,11 @@ def braces_with_contents_inside(node, inner, sb)
end
end

def no_space_inside_left_brace(left_brace, pipe, sb)
if pipe
if left_brace.end_pos == pipe.begin_pos &&
def no_space_inside_left_brace(left_brace, args_delimiter, sb)
if pipe?(args_delimiter)
if left_brace.end_pos == args_delimiter.begin_pos &&
cop_config['SpaceBeforeBlockParameters']
offense(sb, left_brace.begin_pos, pipe.end_pos,
offense(sb, left_brace.begin_pos, args_delimiter.end_pos,
'Space between { and | missing.')
end
else
Expand All @@ -88,10 +88,10 @@ def no_space_inside_left_brace(left_brace, pipe, sb)
end
end

def space_inside_left_brace(left_brace, pipe, sb)
if pipe
def space_inside_left_brace(left_brace, args_delimiter, sb)
if pipe?(args_delimiter)
unless cop_config['SpaceBeforeBlockParameters']
offense(sb, left_brace.end_pos, pipe.begin_pos,
offense(sb, left_brace.end_pos, args_delimiter.begin_pos,
'Space between { and | detected.')
end
else
Expand All @@ -101,6 +101,10 @@ def space_inside_left_brace(left_brace, pipe, sb)
end
end

def pipe?(args_delimiter)
args_delimiter && args_delimiter.is?('|')
end

def space_inside_right_brace(right_brace, sb)
brace_with_space = range_with_surrounding_space(right_brace, :left)
space(sb, brace_with_space.begin_pos, brace_with_space.end_pos - 1,
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/style/space_inside_block_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
expect(cop.highlights).to eq(['{|'])
end

it 'accepts new lambda syntax' do
inspect_source(cop, ['->(x) { x }'])
expect(cop.messages).to be_empty
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(cop, 'each {|x| puts }')
expect(new_source).to eq('each { |x| puts }')
Expand Down Expand Up @@ -182,6 +187,11 @@
expect(cop.highlights).to eq([' '])
end

it 'accepts new lambda syntax' do
inspect_source(cop, ['->(x) { x }'])
expect(cop.messages).to be_empty
end

it 'auto-corrects unwanted space' do
new_source = autocorrect_source(cop, 'each { |x| puts }')
expect(new_source).to eq('each {|x| puts }')
Expand Down Expand Up @@ -256,6 +266,11 @@
expect(cop.highlights).to eq(['{|'])
end

it 'accepts new lambda syntax' do
inspect_source(cop, ['->(x) {x}'])
expect(cop.messages).to be_empty
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(cop, 'each {|x| puts}')
expect(new_source).to eq('each { |x| puts}')
Expand All @@ -277,6 +292,11 @@
expect(cop.highlights).to eq([' '])
end

it 'accepts new lambda syntax' do
inspect_source(cop, ['->(x) {x}'])
expect(cop.messages).to be_empty
end

it 'auto-corrects unwanted space' do
new_source = autocorrect_source(cop, 'each { |x| puts}')
expect(new_source).to eq('each {|x| puts}')
Expand Down

0 comments on commit 3f7fec3

Please sign in to comment.