Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip child nodes if parent node is tagged as a shim #187

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions gem/lib/rbi-central/runtime/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ def initialize(context)
def visit(node)
return unless node

validate_node!(node)
annotations = validate_annotations!(node)
return if shim?(annotations)

validate_definition!(node, annotations)
visit_all(node.nodes) if node.is_a?(RBI::Tree)
end

sig { params(node: RBI::Node).void }
def validate_node!(node)
annotations = validate_annotations!(node)
private

# Do not test definitions tagged `@shim`
return if annotations.include?("shim")
sig { params(annotations: T::Array[String]).returns(T::Boolean) }
def shim?(annotations)
annotations.include?("shim")
end

sig { params(node: RBI::Node, annotations: T::Array[String]).void }
def validate_definition!(node, annotations)
loc = T.must(node.loc)

case node
Expand Down
30 changes: 30 additions & 0 deletions gem/test/rbi-central/runtime/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ def baz; end
], context.run!)
end

def test_shim_annotation
mock = MockGem.new(Dir.mktmpdir, "foo")
mock.gemspec!(mock.default_gemspec)
mock.write!("lib/foo.rb", <<~RB)
class Foo
def foo(*); end
end
RB
context = Context.new(mock.gem, "gem.rbi")
visitor = Runtime::Visitor.new(context)
rbi_tree = RBI::Parser.parse_string(<<~RBI)
class Foo
# @shim: some description
def foo; end
end

# @shim: some description
class Bar
def bar; end
end

class Baz
def baz; end
end
RBI
visitor.visit(rbi_tree)
assert_messages(["Missing runtime constant `::Baz` (defined at `-:11:0-13:3`)",
"Missing runtime constant `::Baz` (defined at `-:12:2-12:14`)",], context.run!)
end

def test_annotated_singleton_method_missing
mock = MockGem.new(Dir.mktmpdir, "foo")
mock.gemspec!(mock.default_gemspec)
Expand Down
Loading