Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
Load scoped constants also in classes, not only modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Jun 25, 2016
1 parent 473b43c commit 5548418
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/ruby-lint/constant_loader.rb
Expand Up @@ -83,8 +83,18 @@ def on_module(node)
@module_nesting.push(cp.to_s)
end

def after_module(_node)
@module_nesting.pop
end

def on_class(node)
name, _parent, _body = *node
cp = ConstantPath.new(name)

@module_nesting.push(cp.to_s)
end

def after_module(node)
def after_class(_node)
@module_nesting.pop
end

Expand Down
20 changes: 19 additions & 1 deletion spec/ruby-lint/constant_loader_spec.rb
Expand Up @@ -85,7 +85,7 @@
end

context 'loading scoped constants' do
it 'loads a constant from a scope' do
before do
@registry.register('Foo') do |defs|
defs.define_constant('Foo') do |klass|
klass.inherits(defs.constant_proxy('Object', @registry))
Expand All @@ -99,7 +99,9 @@
klass.define_method('hello_bar')
end
end
end

it 'loads a constant from a module scope' do
code = <<-CODE
module Foo
class Qux
Expand All @@ -114,5 +116,21 @@ def hello_qux
@loader.run([@ast])
@loader.loaded?('Foo::Bar').should == true
end

it 'loads a constant from a class scope' do
code = <<-CODE
class Foo
class Qux
def hello_qux
Bar.hello_bar
end
end
end
CODE
@ast = parse(code)

@loader.run([@ast])
@loader.loaded?('Foo::Bar').should == true
end
end
end

0 comments on commit 5548418

Please sign in to comment.