Sha256: 6cb55cae04b3a27969e4bd26c23e59a3ac760f29ea09e951fdbb8a9a329b2fb6
Contents?: true
Size: 1.42 KB
Versions: 2
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true require "rubocop" module RuboCop module Cop module Sorbet # Disallows the calls that are used to get constants fom Strings # such as +constantize+, +const_get+, and +constants+. # # The goal of this cop is to make the code easier to statically analyze, # more IDE-friendly, and more predictable. It leads to code that clearly # expresses which values the constant can have. # # @example # # # bad # class_name.constantize # # # bad # constants.detect { |c| c.name == "User" } # # # bad # const_get(class_name) # # # good # case class_name # when "User" # User # else # raise ArgumentError # end # # # good # { "User" => User }.fetch(class_name) class ConstantsFromStrings < ::RuboCop::Cop::Base MSG = "Don't use `%<method_name>s`, it makes the code harder to understand, less editor-friendly, " \ "and impossible to analyze. Replace `%<method_name>s` with a case/when or a hash." RESTRICT_ON_SEND = [ :constantize, :constants, :const_get, ].freeze def on_send(node) add_offense(node.selector, message: format(MSG, method_name: node.method_name)) end alias_method :on_csend, :on_send end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubocop-sorbet-0.8.7 | lib/rubocop/cop/sorbet/constants_from_strings.rb |
rubocop-sorbet-0.8.6 | lib/rubocop/cop/sorbet/constants_from_strings.rb |