From 300d60de6a7333eb584455b40d69c7fbfd14ea00 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 5 Nov 2021 01:02:48 +0900 Subject: [PATCH] Fix an error for `Style/Documentation` This PR fixes the following error for `Style/Documentation` when using a cbase class. ```ruby % cat example.rb class ::MyClass def method end end ``` ```console % bundle exec rubocop --only Style/Documentation -d (snip) An error occurred while Style/Documentation cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/bar/example.rb:1:0. undefined method `namespace' for s(:cbase):RuboCop::AST::Node /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/documentation.rb:181:in `qualify_const' ``` --- changelog/fix_error_for_style_documentation | 1 + lib/rubocop/cop/style/documentation.rb | 2 +- spec/rubocop/cop/style/documentation_spec.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_for_style_documentation diff --git a/changelog/fix_error_for_style_documentation b/changelog/fix_error_for_style_documentation new file mode 100644 index 00000000000..edd6c887add --- /dev/null +++ b/changelog/fix_error_for_style_documentation @@ -0,0 +1 @@ +* [#10234](https://github.com/rubocop/rubocop/pull/10234): Fix an error for `Style/Documentation` when using a cbase class. ([@koic][]) diff --git a/lib/rubocop/cop/style/documentation.rb b/lib/rubocop/cop/style/documentation.rb index 3dd1567d865..1dd2fc7405d 100644 --- a/lib/rubocop/cop/style/documentation.rb +++ b/lib/rubocop/cop/style/documentation.rb @@ -176,7 +176,7 @@ def identifier(node) end def qualify_const(node) - return if node.nil? + return if node.nil? || node.cbase_type? [qualify_const(node.namespace), node.short_name].compact end diff --git a/spec/rubocop/cop/style/documentation_spec.rb b/spec/rubocop/cop/style/documentation_spec.rb index 2d8bc509b08..a8336d49c46 100644 --- a/spec/rubocop/cop/style/documentation_spec.rb +++ b/spec/rubocop/cop/style/documentation_spec.rb @@ -17,6 +17,16 @@ def method RUBY end + it 'registers an offense for non-empty cbase class' do + expect_offense(<<~RUBY) + class ::MyClass + ^^^^^^^^^^^^^^^ Missing top-level documentation comment for `class MyClass`. + def method + end + end + RUBY + end + it 'does not consider comment followed by empty line to be class documentation' do expect_offense(<<~RUBY) # Copyright 2014