Skip to content

Commit

Permalink
[Fix #6931] Ignore non-bare access modifiers in `Layout/AccessModifie…
Browse files Browse the repository at this point in the history
…rIndentation` (#6935)
  • Loading branch information
deivid-rodriguez authored and bbatsov committed Apr 22, 2019
1 parent ff1621e commit 48e63dd
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@
* [#6778](https://github.com/rubocop-hq/rubocop/issues/6778): Fix a false positive in `Style/HashSyntax` cop when a hash key is an interpolated string and EnforcedStyle is ruby19_no_mixed_keys. ([@tatsuyafw][])
* [#6902](https://github.com/rubocop-hq/rubocop/issues/6902): Fix a bug where `Naming/RescuedExceptionsVariableName` would handle an only first rescue for multiple rescue groups. ([@tatsuyafw][])
* [#6860](https://github.com/rubocop-hq/rubocop/issues/6860): Prevent auto-correct conflict of `Style/InverseMethods` and `Style/Not`. ([@hoshinotsuyoshi][])
* [#6935](https://github.com/rubocop-hq/rubocop/issues/6935): `Layout/AccessModifierIndentation` should ignore access modifiers that apply to specific methods. ([@deivid-rodriguez][])

### Changes

Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/layout/access_modifier_indentation.rb
Expand Up @@ -3,8 +3,9 @@
module RuboCop
module Cop
module Layout
# Modifiers should be indented as deep as method definitions, or as deep
# as the class/module keyword, depending on configuration.
# Bare access modifiers (those not applying to specific methods) should be
# indented as deep as method definitions, or as deep as the class/module
# keyword, depending on configuration.
#
# @example EnforcedStyle: indent (default)
# # bad
Expand Down Expand Up @@ -67,7 +68,8 @@ def check_body(body, node)
return if body.nil? # Empty class etc.
return unless body.begin_type?

modifiers = body.each_child_node(:send).select(&:access_modifier?)
modifiers = body.each_child_node(:send)
.select(&:bare_access_modifier?)
end_range = node.loc.end

modifiers.each { |modifier| check_modifier(modifier, end_range) }
Expand Down
5 changes: 3 additions & 2 deletions manual/cops_layout.md
Expand Up @@ -6,8 +6,9 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.49 | -

Modifiers should be indented as deep as method definitions, or as deep
as the class/module keyword, depending on configuration.
Bare access modifiers (those not applying to specific methods) should be
indented as deep as method definitions, or as deep as the class/module
keyword, depending on configuration.

### Examples

Expand Down
122 changes: 121 additions & 1 deletion spec/rubocop/cop/layout/access_modifier_indentation_spec.rb
Expand Up @@ -306,6 +306,25 @@ def test; end
RUBY
end

it 'accepts private with argument indented to method depth in a class' do
expect_no_offenses(<<-RUBY.strip_indent)
class Test
def test; end
private :test
end
RUBY
end

it 'accepts private def indented to method depth in a class' do
expect_no_offenses(<<-RUBY.strip_indent)
class Test
private def test; end
end
RUBY
end

it 'registers offense for private indented to method depth in a module' do
expect_offense(<<-RUBY.strip_indent)
module Test
Expand All @@ -318,6 +337,25 @@ def test; end
RUBY
end

it 'accepts private with argument indented to method depth in a module' do
expect_no_offenses(<<-RUBY.strip_indent)
module Test
def test; end
private :test
end
RUBY
end

it 'accepts private def indented to method depth in a module' do
expect_no_offenses(<<-RUBY.strip_indent)
module Test
private def test; end
end
RUBY
end

it 'registers offense for module fn indented to method depth in a module' do
expect_offense(<<-RUBY.strip_indent)
module Test
Expand All @@ -330,7 +368,27 @@ def test; end
RUBY
end

it 'registers offense for private indented to method depth in singleton' \
it 'accepts module fn with argument indented to ' \
'method depth in a module' do
expect_no_offenses(<<-RUBY.strip_indent)
module Test
def test; end
module_function :test
end
RUBY
end

it 'accepts module fn def indented to method depth in a module' do
expect_no_offenses(<<-RUBY.strip_indent)
module Test
module_function def test; end
end
RUBY
end

it 'registers offense for private indented to method depth in singleton ' \
'class' do
expect_offense(<<-RUBY.strip_indent)
class << self
Expand All @@ -343,6 +401,26 @@ def test; end
RUBY
end

it 'accepts private with argument indented to ' \
'method depth in singleton class' do
expect_no_offenses(<<-RUBY.strip_indent)
class << self
def test; end
private :test
end
RUBY
end

it 'accepts private def indented to method depth in singleton class' do
expect_no_offenses(<<-RUBY.strip_indent)
class << self
private def test; end
end
RUBY
end

it 'registers offense for private indented to method depth in class ' \
'defined with Class.new' do
expect_offense(<<-RUBY.strip_indent)
Expand All @@ -356,6 +434,27 @@ def test; end
RUBY
end

it 'accepts private with argument indented to method depth in class ' \
'defined with Class.new' do
expect_no_offenses(<<-RUBY.strip_indent)
Test = Class.new do
def test; end
private :test
end
RUBY
end

it 'accepts private def indented to method depth in class defined with ' \
'Class.new' do
expect_no_offenses(<<-RUBY.strip_indent)
Test = Class.new do
private def test; end
end
RUBY
end

it 'registers offense for private indented to method depth in module ' \
'defined with Module.new' do
expect_offense(<<-RUBY.strip_indent)
Expand All @@ -369,6 +468,27 @@ def test; end
RUBY
end

it 'accepts private with argument indented to method depth in module ' \
'defined with Module.new' do
expect_no_offenses(<<-RUBY.strip_indent)
Test = Module.new do
def test; end
private :test
end
RUBY
end

it 'accepts private def indented to method depth in module defined with ' \
'Module.new' do
expect_no_offenses(<<-RUBY.strip_indent)
Test = Module.new do
private def test; end
end
RUBY
end

it 'accepts private indented to the containing class indent level' do
expect_no_offenses(<<-RUBY.strip_indent)
class Test
Expand Down

0 comments on commit 48e63dd

Please sign in to comment.