Skip to content

Commit

Permalink
Extract shared T::Enum cop logic into a mixin
Browse files Browse the repository at this point in the history
Introduce a mixin containing logic that will be shared between all cops that operate on `T::Enum`s.
  • Loading branch information
egiurleo committed Apr 19, 2024
1 parent 9471d9b commit 9d093e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
35 changes: 35 additions & 0 deletions lib/rubocop/cop/sorbet/mixin/t_enum.rb
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
5 changes: 3 additions & 2 deletions lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require_relative "sorbet/mixin/target_sorbet_version.rb"
require_relative "sorbet/mixin/signature_help.rb"
require_relative "sorbet/mixin/target_sorbet_version"
require_relative "sorbet/mixin/t_enum"
require_relative "sorbet/mixin/signature_help"

require_relative "sorbet/binding_constant_without_type_alias"
require_relative "sorbet/constants_from_strings"
Expand Down

0 comments on commit 9d093e0

Please sign in to comment.