From f0260b675216982de1201d5626dad74b841dcd30 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 17 Mar 2021 01:44:50 +0900 Subject: [PATCH] [Fix #9606] Fix an error for `Layout/IndentationConsistency` Fixes #9606. This PR fixes the following error for `Layout/IndentationConsistency` when using access modifier at the top level. ```console % cat example.rb public def foo end % bundle exec rubocop --only Layout/IndentationConsistency example.rb -d (snip) Scanning /Users/koic/src/github.com/koic/rubocop-issues/9606/example.rb An error occurred while Layout/IndentationConsistency cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/9606/example.rb:1:0. undefined method `source_range' for nil:NilClass /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/indentation_consistency.rb:165:in `base_column_for_normal_style' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/indentation_consistency.rb:181:in `check_normal_style' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/indentation_consistency.rb:174:in `check' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/indentation_consistency.rb:129:in `on_begin' ``` --- ...rror_for_layout_indentation_consistency.md | 1 + .../cop/layout/indentation_consistency.rb | 4 ++- .../layout/indentation_consistency_spec.rb | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_for_layout_indentation_consistency.md diff --git a/changelog/fix_error_for_layout_indentation_consistency.md b/changelog/fix_error_for_layout_indentation_consistency.md new file mode 100644 index 00000000000..d069ee090a5 --- /dev/null +++ b/changelog/fix_error_for_layout_indentation_consistency.md @@ -0,0 +1 @@ +* [#9606](https://github.com/rubocop/rubocop/issues/9606): Fix an error for `Layout/IndentationConsistency` when using access modifier at the top level. ([@koic][]) diff --git a/lib/rubocop/cop/layout/indentation_consistency.rb b/lib/rubocop/cop/layout/indentation_consistency.rb index ab01a983d92..222575780b0 100644 --- a/lib/rubocop/cop/layout/indentation_consistency.rb +++ b/lib/rubocop/cop/layout/indentation_consistency.rb @@ -162,8 +162,10 @@ def base_column_for_normal_style(node) # to the level of the module (see `AccessModifierIndentation` cop) we # return nil so that `check_alignment` will derive the correct # indentation from the first child that is not an access modifier. - module_indent = display_column(node.parent.source_range) access_modifier_indent = display_column(first_child.source_range) + return access_modifier_indent unless node.parent + + module_indent = display_column(node.parent.source_range) access_modifier_indent if access_modifier_indent > module_indent end diff --git a/spec/rubocop/cop/layout/indentation_consistency_spec.rb b/spec/rubocop/cop/layout/indentation_consistency_spec.rb index a1d53384f16..87d59234320 100644 --- a/spec/rubocop/cop/layout/indentation_consistency_spec.rb +++ b/spec/rubocop/cop/layout/indentation_consistency_spec.rb @@ -9,6 +9,33 @@ "#{}" RUBY end + + it 'accepts when using access modifier at the top level' do + expect_no_offenses(<<~'RUBY') + public + + def foo + end + RUBY + end + + it 'registers and corrects an offense when using access modifier and dedented method definition ' \ + 'at the top level' do + expect_offense(<<~'RUBY') + public + + def foo + ^^^^^^^ Inconsistent indentation detected. + end + RUBY + + expect_correction(<<~'RUBY') + public + + def foo + end + RUBY + end end context 'with if statement' do