Sha256: 67e4edd87313aef7aeb84f27180f896d6f8e70c8656ebbf945655b24244b4b0a

Contents?: true

Size: 1.94 KB

Versions: 22

Compression:

Stored size: 1.94 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 `%<prefer>s` instead of `%<current>s`.'

        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.each_key do |aliased_method|
            register_offense(node, aliased_method) if
              public_send(aliased_method, node)
          end
        end

        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

        private

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

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
rubocop-rails-2.8.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.8.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.7.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.6.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.5.2 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.5.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.5.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.4.2 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.4.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.4.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.3.2 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.3.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.3.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.2.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.2.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.1.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.0.1 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-0.71.0 lib/rubocop/cop/rails/active_support_aliases.rb
rubocop-rails-2.0.0 lib/rubocop/cop/rails/active_support_aliases.rb