Skip to content

Commit

Permalink
Use descendant search
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Nov 24, 2023
1 parent e168f8c commit 139ca55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
23 changes: 5 additions & 18 deletions lib/rubocop/cop/sorbet/forbid_type_aliased_shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,17 @@ module Sorbet
class ForbidTypeAliasedShapes < RuboCop::Cop::Base
MSG = "Type aliases shouldn't contain shapes because of significant performance overhead"

# @!method type_alias?(node)
def_node_matcher(:type_alias?, <<-PATTERN)
# @!method shape_type_alias?(node)
def_node_matcher(:shape_type_alias?, <<-PATTERN)
(block
(send
(const nil? :T) :type_alias)
(send (const {nil? cbase} :T) :type_alias)
(args)
(hash ...)
)
PATTERN

# @!method nested_type_alias?(node)
def_node_matcher(:nested_type_alias?, <<-PATTERN)
(block
(send
(const nil? :T) :type_alias)
(args)
(array
(hash ...)
)
`hash
)
PATTERN

def on_block(node)
add_offense(node) if type_alias?(node) || nested_type_alias?(node)
add_offense(node) if shape_type_alias?(node)
end

alias_method :on_numblock, :on_block
Expand Down
10 changes: 8 additions & 2 deletions spec/rubocop/cop/sorbet/forbid_type_aliased_shapes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ def message

it("disallows defining type aliases that contain nested shapes") do
expect_offense(<<~RUBY)
Foo = T.type_alias { [{ foo: Integer }] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
A = T.type_alias { [{ foo: Integer }] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
B = T.type_alias { T.nilable({ foo: Integer }) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
C = T.type_alias { T::Hash[Symbol, { foo: Integer }] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
D = T.type_alias { T::Hash[Symbol, T::Array[T.any(String, { foo: Integer })]] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
end
end

0 comments on commit 139ca55

Please sign in to comment.