diff --git a/CHANGELOG.md b/CHANGELOG.md index a4d05ff11bf..ef20d04856e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/layout/access_modifier_indentation.rb b/lib/rubocop/cop/layout/access_modifier_indentation.rb index 61807bd6a92..07375f910d9 100644 --- a/lib/rubocop/cop/layout/access_modifier_indentation.rb +++ b/lib/rubocop/cop/layout/access_modifier_indentation.rb @@ -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 @@ -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) } diff --git a/manual/cops_layout.md b/manual/cops_layout.md index 1cdfef01235..a0e6c975700 100644 --- a/manual/cops_layout.md +++ b/manual/cops_layout.md @@ -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 diff --git a/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb b/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb index 3445bd292d1..1a9d06210aa 100644 --- a/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb +++ b/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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