Skip to content

Commit d9e7b81

Browse files
committed
Adopt params and sig from other when merging a Method with no sig
1 parent 33e96d0 commit d9e7b81

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

lib/rbi/rewriters/merge_trees.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,12 @@ def merge_with(other)
479479
super
480480

481481
# If self is the all-anonymous side (since compatible_with? ensures
482-
# at most one side is), adopt other's non-anonymous param names and sigs.
483-
if params.all?(&:anonymous?)
482+
# at most one side is), or if self has no sigs but other does, adopt
483+
# other's non-anonymous param names and sigs.
484+
if params.all?(&:anonymous?) || (sigs.empty? && !other.sigs.empty?)
484485
@params = other.params.dup
485486
@sigs = other.sigs.dup unless other.sigs.empty?
487+
return
486488
end
487489

488490
other.sigs.each do |sig|

test/rbi/rewriters/merge_trees_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,5 +1333,30 @@ def m2(_a); end
13331333
RBI
13341334
refute_empty(res.conflicts)
13351335
end
1336+
1337+
def test_merge_methods_without_sig_adopts_params_and_sig_from_other
1338+
tree1 = parse_rbi(<<~RBI)
1339+
class Foo
1340+
def expand=(value); end
1341+
end
1342+
RBI
1343+
1344+
tree2 = parse_rbi(<<~RBI)
1345+
class Foo
1346+
sig { params(a_different_name: T.nilable(T::Array[String])).returns(T.nilable(T::Array[String])) }
1347+
def expand=(a_different_name); end
1348+
end
1349+
RBI
1350+
1351+
res = tree1.merge(tree2)
1352+
1353+
assert_equal(<<~RBI, res.string)
1354+
class Foo
1355+
sig { params(a_different_name: T.nilable(T::Array[String])).returns(T.nilable(T::Array[String])) }
1356+
def expand=(a_different_name); end
1357+
end
1358+
RBI
1359+
assert_empty(res.conflicts)
1360+
end
13361361
end
13371362
end

0 commit comments

Comments
 (0)