Sha256: b248d35329c6b114035843be4c37358c6c1a7137928a510c505e4ebe8ccbb22b
Contents?: true
Size: 1.38 KB
Versions: 5
Compression:
Stored size: 1.38 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 end end end end
Version data entries
5 entries across 5 versions & 1 rubygems