-
Notifications
You must be signed in to change notification settings - Fork 30
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 #284 from Shopify/at-forbid-t-enum
Add `ForbidTEnum` cop
- Loading branch information
Showing
6 changed files
with
133 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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocop" | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Disallow using `T::Enum`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# class MyEnum < T::Struct | ||
# enums do | ||
# A = new | ||
# B = new | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class MyEnum | ||
# A = "a' | ||
# B = "b" | ||
# C = "c" | ||
# end | ||
class ForbidTEnum < RuboCop::Cop::Base | ||
MSG = "Using `T::Enum` is deprecated." | ||
|
||
# @!method t_enum?(node) | ||
def_node_matcher(:t_enum?, <<~PATTERN) | ||
(const (const {nil? cbase} :T) :Enum) | ||
PATTERN | ||
|
||
def on_class(node) | ||
add_offense(node) if t_enum?(node.parent_class) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ForbidTEnum, :config) do | ||
describe("Offenses") do | ||
it "adds offense when inheriting T::Enum on a multiline class" do | ||
expect_offense(<<~RUBY) | ||
class Foo < T::Enum | ||
^^^^^^^^^^^^^^^^^^^ Using `T::Enum` is deprecated. | ||
end | ||
RUBY | ||
end | ||
|
||
it "adds offense when inheriting T::Enum on a singleline class" do | ||
expect_offense(<<~RUBY) | ||
class Foo < T::Enum; end | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Using `T::Enum` is deprecated. | ||
RUBY | ||
end | ||
|
||
it "adds offense when inheriting ::T::Enum" do | ||
expect_offense(<<~RUBY) | ||
class Foo < ::T::Enum; end | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Using `T::Enum` is deprecated. | ||
RUBY | ||
end | ||
|
||
it "adds offense for nested enums" do | ||
expect_offense(<<~RUBY) | ||
class Foo < T::Enum | ||
^^^^^^^^^^^^^^^^^^^ Using `T::Enum` is deprecated. | ||
class Bar < T::Enum | ||
^^^^^^^^^^^^^^^^^^^ Using `T::Enum` is deprecated. | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
describe("No offenses") do | ||
it "does not add offense when not using T::Enum" do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
end | ||
class Bar < Baz; end | ||
class Baz | ||
extend T::Enum | ||
end | ||
class T::Enum; end | ||
RUBY | ||
end | ||
end | ||
end |