Skip to content

Commit

Permalink
Introduce ForbidTypedAliasedShapes cop
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Oct 10, 2023
1 parent c2bb447 commit d7e999f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ Sorbet/ForbidIncludeConstLiteral:
VersionAdded: 0.2.0
VersionChanged: 0.5.0

Sorbet/ForbidTypeAliasedShapes:
Description: 'Forbids defining type aliases that contain shapes'
Enabled: false
VersionAdded: 0.7.5

Sorbet/ForbidSuperclassConstLiteral:
Description: 'Forbid superclasses which are non-literal constants.'
Enabled: false
Expand Down
46 changes: 46 additions & 0 deletions lib/rubocop/cop/sorbet/forbid_type_aliased_shapes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "rubocop"

module RuboCop
module Cop
module Sorbet
# Disallows defining type aliases that contain shapes
#
# @example
#
# # bad
# Foo = T.type_alias { { foo: Integer } }
#
# # good
# class Foo
# extend T::Sig
#
# sig { params(foo: Integer).void }
# def initialize(foo)
# @foo = foo
# end
# end
class ForbidTypeAliasedShapes < RuboCop::Cop::Base
MSG = "Type aliases cannot contain shapes"

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

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

alias_method :on_numblock, :on_block
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "sorbet/constants_from_strings"
require_relative "sorbet/forbid_superclass_const_literal"
require_relative "sorbet/forbid_include_const_literal"
require_relative "sorbet/forbid_type_aliased_shapes"
require_relative "sorbet/forbid_untyped_struct_props"
require_relative "sorbet/implicit_conversion_method"
require_relative "sorbet/one_ancestor_per_line"
Expand Down
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ In the following section you find all available cops:
* [Sorbet/ForbidTStruct](cops_sorbet.md#sorbetforbidtstruct)
* [Sorbet/ForbidTUnsafe](cops_sorbet.md#sorbetforbidtunsafe)
* [Sorbet/ForbidTUntyped](cops_sorbet.md#sorbetforbidtuntyped)
* [Sorbet/ForbidTypeAliasedShapes](cops_sorbet.md#sorbetforbidtypealiasedshapes)
* [Sorbet/ForbidUntypedStructProps](cops_sorbet.md#sorbetforbiduntypedstructprops)
* [Sorbet/HasSigil](cops_sorbet.md#sorbethassigil)
* [Sorbet/IgnoreSigil](cops_sorbet.md#sorbetignoresigil)
Expand Down
25 changes: 25 additions & 0 deletions manual/cops_sorbet.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,31 @@ sig { params(my_argument: String).void }
def foo(my_argument); end
```
## Sorbet/ForbidTypeAliasedShapes
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Disabled | Yes | No | 0.7.5 | -
Disallows defining type aliases that contain shapes
### Examples
```ruby
# bad
Foo = T.type_alias { { foo: Integer } }

# good
class Foo
extend T::Sig

sig { params(foo: Integer).void }
def initialize(foo)
@foo = foo
end
end
```
## Sorbet/ForbidUntypedStructProps
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_type_aliased_shapes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe(RuboCop::Cop::Sorbet::ForbidTypeAliasedShapes, :config) do
it("allows defining type aliases that don't contain shapes") do
expect_no_offenses(<<~RUBY)
Foo = T.type_alias { Integer }
RUBY
end

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

0 comments on commit d7e999f

Please sign in to comment.