Sha256: fbde9935867b9fd129c7b458d64203348efd699a59b72ff400d75ed7fd590110

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Lint
      # This cop checks for uses of the deprecated class method usages.
      class DeprecatedClassMethods < Cop
        include AST::Sexp

        MSG = '`%s` is deprecated in favor of `%s`.'

        DEPRECATED_METHODS = [
          [:File, :exists?, :exist?],
          [:Dir, :exists?, :exist?]
        ]

        def on_send(node)
          receiver, method_name, *_args = *node

          DEPRECATED_METHODS.each do |data|
            next unless class_nodes(data).include?(receiver)
            next unless method_name == data[1]

            add_offense(node, :selector,
                        format(MSG,
                               deprecated_method(data),
                               replacement_method(data)))
          end
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            receiver, method_name, *_args = *node

            DEPRECATED_METHODS.each do |data|
              next unless class_nodes(data).include?(receiver)
              next unless method_name == data[1]

              corrector.replace(node.loc.selector,
                                data[2].to_s)
            end
          end
        end

        private

        def class_nodes(data)
          [s(:const, nil, data[0]),
           s(:const, s(:cbase), data[0])]
        end

        def deprecated_method(data)
          format('%s.%s', data[0], data[1])
        end

        def replacement_method(data)
          format('%s.%s', data[0], data[2])
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/deprecated_class_methods.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.30.1 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.30.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.29.1 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.29.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.28.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.27.1 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.27.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.26.1 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.26.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.25.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.24.1 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.24.0 lib/rubocop/cop/lint/deprecated_class_methods.rb
rubocop-0.23.0 lib/rubocop/cop/lint/deprecated_class_methods.rb