Sha256: 94168d6f6867da03d99ef35663fb17d0eb1939c95228a1d16060df1eddd01ab7

Contents?: true

Size: 938 Bytes

Versions: 73

Compression:

Stored size: 938 Bytes

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 < Base
        include MethodPreference
        extend AutoCorrector

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

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

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

          add_offense(node.loc.selector, message: message) do |corrector|
            corrector.replace(node.loc.selector, preferred_method(node.method_name))
          end
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_methods.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_methods.rb
rubocop-1.29.1 lib/rubocop/cop/style/string_methods.rb
rubocop-1.29.0 lib/rubocop/cop/style/string_methods.rb
rubocop-1.28.2 lib/rubocop/cop/style/string_methods.rb
rubocop-1.28.1 lib/rubocop/cop/style/string_methods.rb
rubocop-1.28.0 lib/rubocop/cop/style/string_methods.rb
rubocop-1.27.0 lib/rubocop/cop/style/string_methods.rb
rubocop-1.26.1 lib/rubocop/cop/style/string_methods.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_methods.rb
rubocop-1.26.0 lib/rubocop/cop/style/string_methods.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/string_methods.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/string_methods.rb
rubocop-1.25.1 lib/rubocop/cop/style/string_methods.rb
rubocop-1.25.0 lib/rubocop/cop/style/string_methods.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/string_methods.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/string_methods.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/string_methods.rb
rubocop-1.24.1 lib/rubocop/cop/style/string_methods.rb
rubocop-1.24.0 lib/rubocop/cop/style/string_methods.rb