Sha256: 3a5cbc47235b9c4443a4d7bfc0933d64eaad2dbd3012f8e42ffe2a56fb1a9e0b

Contents?: true

Size: 1.02 KB

Versions: 26

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop enforces the use of consistent method names
      # from the String class.
      #
      # @example
      #   # bad
      #   'name'.intern
      #   'var'.unfavored_method
      #
      #   # good
      #   'name'.to_sym
      #   'var'.preferred_method
      class StringMethods < Cop
        include MethodPreference

        MSG = 'Prefer `%<prefer>s` over `%<current>s`.'.freeze

        def on_send(node)
          return unless preferred_method(node.method_name)

          add_offense(node, location: :selector)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.loc.selector,
                              preferred_method(node.method_name))
          end
        end

        private

        def message(node)
          format(MSG,
                 prefer: preferred_method(node.method_name),
                 current: node.method_name)
        end
      end
    end
  end
end

Version data entries

26 entries across 24 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.63.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.62.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.61.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.61.0 lib/rubocop/cop/style/string_methods.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/string_methods.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/string_methods.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/string_methods.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/string_methods.rb
rubocop-0.60.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.59.2 lib/rubocop/cop/style/string_methods.rb
rubocop-0.59.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.59.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.58.2 lib/rubocop/cop/style/string_methods.rb
rubocop-0.58.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.58.0 lib/rubocop/cop/style/string_methods.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/string_methods.rb
rubocop-0.57.2 lib/rubocop/cop/style/string_methods.rb
rubocop-0.57.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.57.0 lib/rubocop/cop/style/string_methods.rb