Skip to content

Commit

Permalink
Merge pull request #2297 from jonas054/2280_infinite_correction
Browse files Browse the repository at this point in the history
[Fix #2280] Avoid everything between key, value in ExtraSpacing
  • Loading branch information
bbatsov committed Oct 4, 2015
2 parents e24b96a + 5f9b0f5 commit 63741be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#2261](https://github.com/bbatsov/rubocop/issues/2261): Make relative `Exclude` paths in `$HOME/.rubocop_todo.yml` be relative to current directory. ([@jonas054][])
* [#2246](https://github.com/bbatsov/rubocop/pull/2246): Do not register an offense for `Style/TrailingUnderscoreVariable` when the underscore variable is preceeded by a splat variable. ([@rrosenblum][])
* [#2292](https://github.com/bbatsov/rubocop/pull/2292): Results should not be stored in the cache if affected by errors (crashes). ([@jonas054][])
* [#2280](https://github.com/bbatsov/rubocop/issues/2280): Avoid reporting space between hash literal keys and values in `Style/ExtraSpacing`. ([@jonas054][])

## 0.34.2 (21/09/2015)

Expand Down
27 changes: 15 additions & 12 deletions lib/rubocop/cop/style/extra_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ class ExtraSpacing < Cop

def investigate(processed_source)
ast = processed_source.ast
ignored_ranges = ast ? ignored_ranges(ast) : []

processed_source.tokens.each_cons(2) do |t1, t2|
next if t2.type == :tNL
next if t1.pos.line != t2.pos.line
next if t2.pos.begin_pos - 1 <= t1.pos.end_pos
next if allow_for_alignment? && aligned_with_something?(t2)
start_pos = t1.pos.end_pos
next if ignored_ranges(ast).find { |r| r.include?(start_pos) }

end_pos = t2.pos.begin_pos - 1
range = Parser::Source::Range.new(processed_source.buffer,
start_pos, end_pos)
add_offense(range, range, MSG) unless ignored_ranges.include?(range)
add_offense(range, range, MSG)
end
end

Expand All @@ -44,18 +45,20 @@ def autocorrect(range)
private

# Returns an array of ranges that should not be reported. It's the
# extra spaces between the separators (: or =>) and values in a hash,
# since those are handled by the Style/AlignHash cop.
# extra spaces between the keys and values in a hash, since those are
# handled by the Style/AlignHash cop.
def ignored_ranges(ast)
ranges = []
on_node(:pair, ast) do |pair|
_, value = *pair
ranges <<
Parser::Source::Range.new(processed_source.buffer,
pair.loc.operator.end_pos,
value.loc.expression.begin_pos - 1)
return [] unless ast

@ignored_ranges ||= begin
ranges = []
on_node(:pair, ast) do |pair|
key, value = *pair
r = key.loc.expression.end_pos...value.loc.expression.begin_pos
ranges << r
end
ranges
end
ranges
end

def allow_for_alignment?
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/extra_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@
expect(cop.offenses).to be_empty
end

it 'accepts space between key and value in a hash with hash rockets' do
source = [
'ospf_h = {',
" 'ospfTest' => {",
" 'foo' => {",
" area: '0.0.0.0', cost: 10, hello: 30, pass: true },",
" 'longname' => {",
" area: '1.1.1.38', pass: false },",
" 'vlan101' => {",
" area: '2.2.2.101', cost: 5, hello: 20, pass: true }",
' },',
" 'TestOspfInt' => {",
" 'x' => {",
" area: '0.0.0.19' },",
" 'vlan290' => {",
" area: '2.2.2.29', cost: 200, hello: 30, pass: true },",
" 'port-channel100' => {",
" area: '3.2.2.29', cost: 25, hello: 50, pass: false }",
' }',
'}']
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end

it 'can handle extra space before a float' do
source = ['{:a => "a",',
' :b => [nil, 2.5]}']
Expand Down

0 comments on commit 63741be

Please sign in to comment.