Sha256: 3a83751a4f4d10726fc32cbdf0f01422be19884f5770f60a96a1e5d264777c69

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks that ActiveSupport aliases to core ruby methods
      # are not used.
      #
      # @example
      #   # good
      #   'some_string'.start_with?('prefix')
      #   'some_string'.end_with?('suffix')
      #   [1, 2, 'a'] << 'b'
      #   [1, 2, 'a'].unshift('b')
      #
      #   # bad
      #   'some_string'.starts_with?('prefix')
      #   'some_string'.ends_with?('suffix')
      #   [1, 2, 'a'].append('b')
      #   [1, 2, 'a'].prepend('b')
      #
      class ActiveSupportAliases < Cop
        MSG = 'Use `%s` instead of `%s`.'.freeze

        ALIASES = {
          starts_with?: {
            original: :start_with?, matcher: '(send str :starts_with? _)'
          },
          ends_with?: {
            original: :end_with?, matcher: '(send str :ends_with? _)'
          },
          append: { original: :<<, matcher: '(send array :append _)' },
          prepend: { original: :unshift, matcher: '(send array :prepend _)' }
        }.freeze

        ALIASES.each do |aliased_method, options|
          def_node_matcher aliased_method, options[:matcher]
        end

        def on_send(node)
          ALIASES.keys.each do |aliased_method|
            register_offense(node, aliased_method) if
              public_send(aliased_method, node)
          end
        end

        private

        def autocorrect(node)
          return false if append(node)
          lambda do |corrector|
            method_name = node.loc.selector.source
            replacement = ALIASES[method_name.to_sym][:original]
            corrector.replace(node.loc.selector, replacement.to_s)
          end
        end

        def register_offense(node, method_name)
          add_offense(
            node,
            :expression,
            format(MSG, ALIASES[method_name][:original], method_name)
          )
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-0.49.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-0.48.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-0.48.0 lib/rubocop/cop/rails/active_support_aliases.rb