Skip to content

Commit

Permalink
Use descendant search
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Oct 16, 2023
1 parent e168f8c commit c23a8fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/rubocop/cop/sorbet/forbid_type_aliased_shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ class ForbidTypeAliasedShapes < RuboCop::Cop::Base
)
PATTERN

# @!method shape_type_alias?(node)
def_node_matcher(:shape_type_alias?, <<-PATTERN)
(block
(send (const {nil? cbase} :T) :type_alias)
(args)
`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 c23a8fc

Please sign in to comment.