-
Notifications
You must be signed in to change notification settings - Fork 26
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 #225 from Shopify/emily/t-enum-comparable-cop
Introduce `ForbidComparableTEnum` cop
- Loading branch information
Showing
9 changed files
with
176 additions
and
18 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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Mixing for writing cops that deal with `T::Enum`s | ||
module TEnum | ||
extend RuboCop::NodePattern::Macros | ||
def initialize(*) | ||
@scopes = [] | ||
super | ||
end | ||
|
||
# @!method t_enum?(node) | ||
def_node_matcher :t_enum?, <<~PATTERN | ||
(class (const...) (const (const nil? :T) :Enum) ...) | ||
PATTERN | ||
|
||
def on_class(node) | ||
@scopes.push(node) | ||
end | ||
|
||
def after_class(node) | ||
@scopes.pop | ||
end | ||
|
||
private | ||
|
||
def in_t_enum_class? | ||
t_enum?(@scopes&.last) | ||
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
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
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
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 |
File renamed without changes.