Skip to content

Commit

Permalink
Allow nodes to be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Dec 6, 2024
1 parent 2f4387b commit 68cc57f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/tapioca/rbi_ext/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def create_method(name, parameters: [], return_type: nil, class_method: false, v
self << method
end

sig { params(name: String).void }
def remove_method(name)
method = nodes.grep(RBI::Method).find { |node| node.name == name }
nodes.delete(method) if method
end

private

sig { returns(T::Hash[String, RBI::Node]) }
Expand Down
12 changes: 12 additions & 0 deletions spec/tapioca/rbi_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class BuilderSpec < Minitest::HooksSpec
rbi.create_type_variable("G", type: "type_member")
rbi.create_type_variable("H", type: "type_template", variance: :in, fixed: "Foo")
rbi.create_method("foo")
rbi.create_method("bar", return_type: "String")
rbi.remove_method("bar")
rbi.create_method("bar", return_type: "Integer")

assert_equal(<<~RBI, rbi.string)
class A; end
Expand All @@ -30,6 +33,9 @@ module B; end
sig { returns(T.untyped) }
def foo; end
sig { returns(Integer) }
def bar; end
RBI
end

Expand Down Expand Up @@ -120,6 +126,12 @@ module Bar; end
end
RBI
end

it "does not raise an nonexistent method is removed" do
rbi = RBI::Tree.new
rbi.remove_method("garbage")
assert_equal("", rbi.string)
end
end
end
end

0 comments on commit 68cc57f

Please sign in to comment.