Skip to content

Commit

Permalink
Fix ConstNode#ancestor? raising NoMethodError when the constant i…
Browse files Browse the repository at this point in the history
…s not namespaced.
  • Loading branch information
dvandersluis committed Sep 16, 2020
1 parent c457a21 commit d0383b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

## master (unreleased)

### Bug fixes
* [#115](https://github.com/rubocop-hq/rubocop-ast/pull/115): Fix `ConstNode#ancestor?` when the constant is not namespaced. ([@dvandersluis][])

## 0.4.0 (2020-09-11)

### New features
Expand Down Expand Up @@ -97,3 +100,4 @@
[@owst]: https://github.com/owst
[@fatkodima]: https://github.com/fatkodima
[@koic]: https://github.com/koic
[@dvandersluis]: https://github.com/dvandersluis
2 changes: 2 additions & 0 deletions lib/rubocop/ast/node/const_node.rb
Expand Up @@ -28,6 +28,8 @@ 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

Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/ast/const_node_spec.rb
Expand Up @@ -31,6 +31,20 @@

it { expect(const_node.absolute?).to eq false }
end

context 'with a non-namespaced constant' do
let(:source) { 'Foo' }

it { expect(const_node.absolute?).to eq false }
end
end

describe '#relative?' do
context 'with a non-namespaced constant' do
let(:source) { 'Foo' }

it { expect(const_node.relative?).to eq true }
end
end

describe '#each_path' do
Expand Down

0 comments on commit d0383b2

Please sign in to comment.