From 0621fd4802c983bb26f3af93378370c2b0fd6648 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 16 Sep 2020 12:50:33 -0400 Subject: [PATCH] Fix `ConstNode#ancestor?` raising `NoMethodError` when the constant is not namespaced. --- lib/rubocop/ast/node/const_node.rb | 1 + spec/rubocop/ast/const_node_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/rubocop/ast/node/const_node.rb b/lib/rubocop/ast/node/const_node.rb index 12f52e01d..6ef055b96 100644 --- a/lib/rubocop/ast/node/const_node.rb +++ b/lib/rubocop/ast/node/const_node.rb @@ -28,6 +28,7 @@ def module_name? # @return [Boolean] if the constant starts with `::` (aka s(:cbase)) def absolute? + return false unless namespace each_path.first.cbase_type? end diff --git a/spec/rubocop/ast/const_node_spec.rb b/spec/rubocop/ast/const_node_spec.rb index 8a53db0f9..c106f0a93 100644 --- a/spec/rubocop/ast/const_node_spec.rb +++ b/spec/rubocop/ast/const_node_spec.rb @@ -31,6 +31,20 @@ it { expect(const_node.absolute?).to eq false } end + + context 'non-namespaced constant' do + let(:source) { 'Foo' } + + it { expect(const_node.absolute?).to eq false } + end + end + + describe '#relative?' do + context 'non-namespaced constant' do + let(:source) { 'Foo' } + + it { expect(const_node.relative?).to eq true } + end end describe '#each_path' do