Skip to content

Commit

Permalink
Merge pull request #6866 from koic/fix_false_positive_for_layout_inde…
Browse files Browse the repository at this point in the history
…ntation_width

[Fix #6861] Fix a false positive for `Layout/IndentationWidth`
  • Loading branch information
jonas054 committed Jul 27, 2019
2 parents 7af549b + 20d76ec commit 95dd998
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#7190](https://github.com/rubocop-hq/rubocop/issues/7190): Support lower case drive letters on Windows. ([@jonas054][])
* [#5628](https://github.com/rubocop-hq/rubocop/issues/5628): Fix an error of `Layout/SpaceInsideStringInterpolation` on interpolations with multiple statements. ([@buehmann][])
* [#7128](https://github.com/rubocop-hq/rubocop/issues/7128): Make `Metrics/LineLength` aware of shebang. ([@koic][])
* [#6861](https://github.com/rubocop-hq/rubocop/issues/6861): Fix a false positive for `Layout/IndentationWidth` when using `EnforcedStyle: outdent` of `Layout/AccessModifierIndentation`. ([@koic][])

### Changes

Expand Down
24 changes: 19 additions & 5 deletions lib/rubocop/cop/layout/indentation_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,16 @@ def check_members(base, members)
if indentation_consistency_style == 'indented_internal_methods'
check_members_for_indented_internal_methods_style(members)
else
members.first.children.each do |member|
next if member.send_type? && member.access_modifier?

check_indentation(base, member)
end
check_members_for_normal_style(base, members)
end
end

def select_check_member(member)
return unless member

if access_modifier?(member.children.first)
return if access_modifier_indentation_style == 'outdent'

member.children.first
else
member
Expand All @@ -183,6 +181,14 @@ def check_members_for_indented_internal_methods_style(members)
end
end

def check_members_for_normal_style(base, members)
members.first.children.each do |member|
next if member.send_type? && member.access_modifier?

check_indentation(base, member)
end
end

def each_member(members)
previous_modifier = nil
members.first.children.each do |member|
Expand All @@ -199,6 +205,14 @@ def indented_internal_methods_style?
indentation_consistency_style == 'indented_internal_methods'
end

def special_modifier?(node)
node.bare_access_modifier? && SPECIAL_MODIFIERS.include?(node.source)
end

def access_modifier_indentation_style
config.for_cop('Layout/AccessModifierIndentation')['EnforcedStyle']
end

def indentation_consistency_style
config.for_cop('Layout/IndentationConsistency')['EnforcedStyle']
end
Expand Down
43 changes: 43 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,49 @@ def method
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects IndentationWidth and IndentationConsistency offenses' \
'when using `EnforcedStyle: outdent` and ' \
'`EnforcedStyle: indented_internal_methods`' do
create_file('.rubocop.yml', <<~YAML)
Layout/AccessModifierIndentation:
EnforcedStyle: outdent
Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods
YAML

source = <<-'RUBY'.strip_indent
class Foo
private
def do_something
# something
end
end
RUBY
create_file('example.rb', source)

expect(cli.run([
'--auto-correct',
'--only',
[
'Layout/AccessModifierIndentation',
'Layout/IndentationConsistency',
'Layout/IndentationWidth'
].join(',')
])).to eq(0)

corrected = <<-'RUBY'.strip_indent
class Foo
private
def do_something
# something
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects SymbolProc and SpaceBeforeBlockBraces offenses' do
source = ['foo.map{ |a| a.nil? }']
create_file('example.rb', source)
Expand Down
27 changes: 23 additions & 4 deletions spec/rubocop/cop/layout/indentation_width_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
subject(:cop) { described_class.new(config) }

let(:config) do
RuboCop::Config.new('Layout/IndentationWidth' => cop_config,
'Layout/IndentationConsistency' => consistency_config,
'Layout/EndAlignment' => end_alignment_config,
'Layout/DefEndAlignment' => def_end_alignment_config)
RuboCop::Config.new(
'Layout/IndentationWidth' => cop_config,
'Layout/AccessModifierIndentation' => access_modifier_config,
'Layout/IndentationConsistency' => consistency_config,
'Layout/EndAlignment' => end_alignment_config,
'Layout/DefEndAlignment' => def_end_alignment_config
)
end
let(:access_modifier_config) { { 'EnforcedStyle' => 'indent' } }
let(:consistency_config) { { 'EnforcedStyle' => 'normal' } }
let(:end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' }
Expand Down Expand Up @@ -1153,6 +1157,21 @@ def g
end
end

context 'when consistency style is outdent' do
let(:access_modifier_config) { { 'EnforcedStyle' => 'outdent' } }

it 'accepts access modifier is outdent' do
expect_no_offenses(<<~RUBY)
class Test
private
def foo
end
end
RUBY
end
end

context 'when consistency style is indented_internal_methods' do
let(:consistency_config) do
{ 'EnforcedStyle' => 'indented_internal_methods' }
Expand Down

0 comments on commit 95dd998

Please sign in to comment.