Sha256: 69a71c3cc0980aabd6a3ac2ee2bf0f8fad20d977633a339f0ef7fe3d18f9f28a

Contents?: true

Size: 1.95 KB

Versions: 6822

Compression:

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

6,822 entries across 6,816 versions & 25 rubygems

Version Path
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb
ory-keto-client-0.7.0.alpha0 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/active_support_aliases.rb