Skip to content

Commit

Permalink
Do not report duplicate for shim or annotations classes redefining th…
Browse files Browse the repository at this point in the history
…e parent class

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed May 30, 2024
1 parent 73327a3 commit 607640d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/tapioca/helpers/rbi_files_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,21 @@ def shims_or_todos_have_duplicates?(nodes, shim_rbi_dir:, todo_rbi_file:)
shims_or_todos = extract_shims_and_todos(nodes, shim_rbi_dir: shim_rbi_dir, todo_rbi_file: todo_rbi_file)
return false if shims_or_todos.empty?

not_shims_or_todos = nodes - shims_or_todos
shims_or_todos_empty_scopes = extract_empty_scopes(shims_or_todos)

# We need to discard classes that are redefining the parent of a class
shims_or_todos_empty_scopes.select! do |scope|
# Empty modules are always duplicates
next true unless scope.is_a?(RBI::Class)

# Empty classes without parents are also duplicates
parent_name = scope.superclass_name
next true unless parent_name

# Empty classes that are not redefining the parent are also duplicates
not_shims_or_todos.any? { |node| node.is_a?(RBI::Class) && node.superclass_name == parent_name }
end
return true unless shims_or_todos_empty_scopes.empty?

mixins = extract_mixins(shims_or_todos)
Expand Down
52 changes: 52 additions & 0 deletions spec/tapioca/cli/check_shims_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,58 @@ class Qux
refute_success_status(result)
end

it "ignores duplicates that have a parent class" do
@project.write!("sorbet/rbi/gems/foo@1.0.0.rbi", <<~RBI)
class Foo
end
class Bar < Foo
end
RBI

@project.write!("sorbet/rbi/shims/foo.rbi", <<~RBI)
class Foo < Baz
end
class Bar < Baz
end
RBI

result = @project.tapioca("check-shims --no-payload")
assert_success_status(result)
end

it "detects duplicates that have a parent class" do
@project.write!("sorbet/rbi/gems/foo@1.0.0.rbi", <<~RBI)
class Foo
end
class Bar < Foo
end
RBI

@project.write!("sorbet/rbi/shims/foo.rbi", <<~RBI)
class Foo
end
class Bar < Foo
end
RBI

result = @project.tapioca("check-shims --no-payload")

assert_stderr_equals(<<~ERR, result)
Duplicated RBI for ::Foo:
* sorbet/rbi/shims/foo.rbi:1:0-2:3
* sorbet/rbi/gems/foo@1.0.0.rbi:1:0-2:3
Duplicated RBI for ::Bar:
* sorbet/rbi/shims/foo.rbi:3:0-4:3
* sorbet/rbi/gems/foo@1.0.0.rbi:3:0-4:3
Please remove the duplicated definitions from sorbet/rbi/shims and sorbet/rbi/todo.rbi
ERR

refute_success_status(result)
end

it "detects duplicates from same shim file" do
@project.write!("sorbet/rbi/gems/foo@1.0.0.rbi", <<~RBI)
class Foo
Expand Down

0 comments on commit 607640d

Please sign in to comment.