Sha256: 2eb9bd6afcf061e6847b9ca574d9bb2917734f040bc885e8ff09c4161bb4956d

Contents?: true

Size: 1.04 KB

Versions: 62

Compression:

Stored size: 1.04 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`.'

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

          add_offense(node, location: :selector)
        end
        alias on_csend on_send

        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

62 entries across 43 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/string_methods.rb
rubocop-0.89.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.89.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.88.0 lib/rubocop/cop/style/string_methods.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.87.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.87.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.86.0 lib/rubocop/cop/style/string_methods.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/string_methods.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/string_methods.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.85.1 lib/rubocop/cop/style/string_methods.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.85.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.84.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.83.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.82.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.81.0 lib/rubocop/cop/style/string_methods.rb
rubocop-0.80.1 lib/rubocop/cop/style/string_methods.rb
rubocop-0.80.0 lib/rubocop/cop/style/string_methods.rb