Skip to content

Commit

Permalink
Merge pull request #187 from Shopify/ko/shim-classes
Browse files Browse the repository at this point in the history
Skip child nodes if parent node is tagged as a shim
  • Loading branch information
KaanOzkan authored Nov 24, 2023
2 parents 7cc8771 + 05d91e9 commit 03c6e04
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
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

0 comments on commit 03c6e04

Please sign in to comment.