Skip to content

Commit

Permalink
Disallow nested shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Oct 11, 2023
1 parent 224d487 commit e168f8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/rubocop/cop/sorbet/forbid_type_aliased_shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,22 @@ class ForbidTypeAliasedShapes < RuboCop::Cop::Base
(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 ...)
)
)
PATTERN

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

alias_method :on_numblock, :on_block
Expand Down
13 changes: 12 additions & 1 deletion spec/rubocop/cop/sorbet/forbid_type_aliased_shapes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require "spec_helper"

RSpec.describe(RuboCop::Cop::Sorbet::ForbidTypeAliasedShapes, :config) do
def message
"Type aliases shouldn't contain shapes because of significant performance overhead"
end

it("allows defining type aliases that don't contain shapes") do
expect_no_offenses(<<~RUBY)
Foo = T.type_alias { Integer }
Expand All @@ -12,7 +16,14 @@
it("disallows defining type aliases that contain shapes") do
expect_offense(<<~RUBY)
Foo = T.type_alias { { foo: Integer } }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type aliases shouldn't contain shapes because of significant performance overhead
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
end

it("disallows defining type aliases that contain nested shapes") do
expect_offense(<<~RUBY)
Foo = T.type_alias { [{ foo: Integer }] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
end
end

0 comments on commit e168f8c

Please sign in to comment.