-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ForbidTypedAliasedShapes cop
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
spec/rubocop/cop/sorbet/forbid_type_aliased_shapes_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |