-
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.
Merge pull request #181 from Shopify/ko/aliased-shapes
Forbid usage of type aliased shapes
- Loading branch information
Showing
6 changed files
with
111 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,44 @@ | ||
# 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 shouldn't contain shapes because of significant performance overhead" | ||
|
||
# @!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 shape_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
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
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 } | ||
RUBY | ||
end | ||
|
||
it("disallows defining type aliases that contain shapes") do | ||
expect_offense(<<~RUBY) | ||
Foo = T.type_alias { { foo: Integer } } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
RUBY | ||
end | ||
|
||
it("disallows defining type aliases that contain nested shapes") do | ||
expect_offense(<<~RUBY) | ||
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 |