-
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.
This cop adds an offense whenever `Comparable` is included in a `T::Enum` class because it isn't performant (default implementation is about 8x as slow as comparing between constants).
- Loading branch information
Showing
4 changed files
with
105 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 | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Disallow including the `Comparable` module in `T::Enum`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# class Priority < T::Enum | ||
# include Comparable | ||
# | ||
# enums do | ||
# High = new(3) | ||
# Medium = new(2) | ||
# Low = new(1) | ||
# end | ||
# | ||
# def <=>(other) | ||
# serialize <=> other.serialize | ||
# end | ||
# end | ||
class ForbidComparableTEnum < Base | ||
include TEnum | ||
|
||
MSG = "Do not use `T::Enum` as a comparable object because of significant performance overhead." | ||
|
||
RESTRICT_ON_SEND = [:include, :prepend].freeze | ||
|
||
# @!method mix_in_comparable?(node) | ||
def_node_matcher :mix_in_comparable?, <<~PATTERN | ||
(send nil? {:include | :prepend} (const nil? :Comparable)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless in_t_enum_class? && mix_in_comparable?(node) | ||
|
||
add_offense(node) | ||
end | ||
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
55 changes: 55 additions & 0 deletions
55
spec/rubocop/cop/sorbet/t_enum/forbid_comparable_t_enum_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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ForbidComparableTEnum, :config) do | ||
it "registers an offense when T::Enum includes Comparable" do | ||
expect_offense(<<~RUBY) | ||
class MyEnum < T::Enum | ||
include Comparable | ||
^^^^^^^^^^^^^^^^^^ Do not use `T::Enum` as a comparable object because of significant performance overhead. | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers an offense when T::Enum prepends Comparable" do | ||
expect_offense(<<~RUBY) | ||
class MyEnum < T::Enum | ||
prepend Comparable | ||
^^^^^^^^^^^^^^^^^^ Do not use `T::Enum` as a comparable object because of significant performance overhead. | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when T::Enum includes other modules" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyEnum < T::Enum | ||
include T::Sig | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when T::Enum includes no other modules" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyEnum < T::Enum; end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when Comparable is included in a nested, non T::Enum class" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyEnum < T::Enum | ||
class Foo | ||
include Comparable | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when Comparable is prepended in a nested, non T::Enum class" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyEnum < T::Enum | ||
class Foo | ||
prepend Comparable | ||
end | ||
end | ||
RUBY | ||
end | ||
end |