Skip to content

Commit f9a6fbd

Browse files
committed
Merge pull request rubocop#2233 from jonas054/2121_infinite_autocorrect
[Fix rubocop#2121] Allow space before values in hash literals in ExtraSpacing
2 parents 2093a57 + ae56019 commit f9a6fbd

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## master (unreleased)
44

5-
### Bug fixes
5+
### Bug Fixes
66

77
* [#2232](https://github.com/bbatsov/rubocop/issues/2232): Fix false positive in `Lint/FormatParameterMismatch` for argument with splat operator. ([@dreyks][])
88
* [#2237](https://github.com/bbatsov/rubocop/pull/2237): Allow `Lint/FormatParameterMismatch` to be called using `Kernel.format` and `Kernel.sprintf`. ([@rrosenblum][])
99
* [#2234](https://github.com/bbatsov/rubocop/issues/2234): Do not register an offense for `Lint/FormatParameterMismatch` when the format string is a variable. ([@rrosenblum][])
1010
* [#2240](https://github.com/bbatsov/rubocop/pull/2240): `Lint/UnneededDisable` should not report non-`Lint` `rubocop:disable` comments when running `rubocop --lint`. ([@jonas054][])
11+
* [#2121](https://github.com/bbatsov/rubocop/issues/2121): Allow space before values in hash literals in `Style/ExtraSpacing` to avoid correction conflict. ([@jonas054][])
1112

1213
## 0.34.1 (09/09/2015)
1314

lib/rubocop/cop/style/extra_spacing.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class ExtraSpacing < Cop
2121
MSG = 'Unnecessary spacing detected.'
2222

2323
def investigate(processed_source)
24+
ast = processed_source.ast
25+
ignored_ranges = ast ? ignored_ranges(ast) : []
26+
2427
processed_source.tokens.each_cons(2) do |t1, t2|
2528
next if t2.type == :tNL
2629
next if t1.pos.line != t2.pos.line
@@ -30,7 +33,7 @@ def investigate(processed_source)
3033
end_pos = t2.pos.begin_pos - 1
3134
range = Parser::Source::Range.new(processed_source.buffer,
3235
start_pos, end_pos)
33-
add_offense(range, range, MSG)
36+
add_offense(range, range, MSG) unless ignored_ranges.include?(range)
3437
end
3538
end
3639

@@ -40,6 +43,21 @@ def autocorrect(range)
4043

4144
private
4245

46+
# Returns an array of ranges that should not be reported. It's the
47+
# extra spaces between the separators (: or =>) and values in a hash,
48+
# since those are handled by the Style/AlignHash cop.
49+
def ignored_ranges(ast)
50+
ranges = []
51+
on_node(:pair, ast) do |pair|
52+
_, value = *pair
53+
ranges <<
54+
Parser::Source::Range.new(processed_source.buffer,
55+
pair.loc.operator.end_pos,
56+
value.loc.expression.begin_pos - 1)
57+
end
58+
ranges
59+
end
60+
4361
def allow_for_alignment?
4462
cop_config['AllowForAlignment']
4563
end

lib/rubocop/options.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ def validate_exclude_limit_option(args)
200200
# This module contains help texts for command line options.
201201
module OptionsHelp
202202
MAX_EXCL = RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS.to_s
203-
# rubocop:disable Style/ExtraSpacing
204203
TEXT = {
205204
only: 'Run only the given cop(s).',
206205
only_guide_cops: ['Run only cops for rules that link to a',

spec/rubocop/cli_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ def abs(path)
9090
end
9191

9292
describe '--auto-correct' do
93+
it 'does not correct ExtraSpacing in a hash that would be changed back' do
94+
create_file('.rubocop.yml', ['Style/AlignHash:',
95+
' EnforcedColonStyle: table'])
96+
source = ['hash = {',
97+
' alice: {',
98+
' age: 23,',
99+
" role: 'Director'",
100+
' },',
101+
' bob: {',
102+
' age: 25,',
103+
" role: 'Consultant'",
104+
' }',
105+
'}']
106+
create_file('example.rb', source)
107+
expect(cli.run(['--auto-correct'])).to eq(1)
108+
expect(IO.read('example.rb')).to eq(source.join("\n") + "\n")
109+
end
110+
93111
it 'corrects IndentationWidth, RedundantBegin, and ' \
94112
'RescueEnsureAlignment offenses' do
95113
source = ['def verify_section',

spec/rubocop/cop/style/extra_spacing_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
expect(cop.offenses.size).to eq(1)
1515
end
1616

17+
it 'accepts aligned values of an implicit hash literal' do
18+
source = ["register(street1: '1 Market',",
19+
" street2: '#200',",
20+
" :city => 'Some Town',",
21+
" state: 'CA',",
22+
" postal_code:'99999-1111')"]
23+
inspect_source(cop, source)
24+
expect(cop.offenses).to be_empty
25+
end
26+
1727
it 'can handle extra space before a float' do
1828
source = ['{:a => "a",',
1929
' :b => [nil, 2.5]}']
@@ -137,14 +147,6 @@
137147
'carrier_attribute_name = FactoryGirl.create(:attribute,',
138148
" name: 'Carrier',",
139149
' values: %w{verizon})'
140-
],
141-
142-
'aligning values of an implicit hash literal' => [
143-
"register(street1: '1 Market',",
144-
" street2: '#200',",
145-
" city: 'Some Town',",
146-
" state: 'CA',",
147-
" postal_code: '99999-1111')"
148150
]
149151
}
150152

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy