Sha256: 95132a6b161cfc4a6fbc866c5d997eeb9a84cb5bfa46914547ac2218e373e89b

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of `DateTime` that should be replaced by
      # `Date` or `Time`.
      #
      # @example
      #
      #   # bad - uses `DateTime` for current time
      #   DateTime.now
      #
      #   # good - uses `Time` for current time
      #   Time.now
      #
      #   # bad - uses `DateTime` for modern date
      #   DateTime.iso8601('2016-06-29')
      #
      #   # good - uses `Date` for modern date
      #   Date.iso8601('2016-06-29')
      #
      #   # good - uses `DateTime` with start argument for historical date
      #   DateTime.iso8601('1751-04-23', Date::ENGLAND)
      class DateTime < Cop
        MSG = 'Prefer Date or Time over DateTime.'.freeze

        def_node_matcher :date_time?, <<-PATTERN
          (send (const {nil? (cbase)} :DateTime) ...)
        PATTERN

        def_node_matcher :historic_date?, <<-PATTERN
          (send _ _ _ (const (const nil? :Date) _))
        PATTERN

        def on_send(node)
          return unless date_time?(node)
          return if historic_date?(node)
          add_offense(node)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/cop/style/date_time.rb
rubocop-0.58.1 lib/rubocop/cop/style/date_time.rb
rubocop-0.58.0 lib/rubocop/cop/style/date_time.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/date_time.rb
rubocop-0.57.2 lib/rubocop/cop/style/date_time.rb
rubocop-0.57.1 lib/rubocop/cop/style/date_time.rb
rubocop-0.57.0 lib/rubocop/cop/style/date_time.rb
rubocop-0.56.0 lib/rubocop/cop/style/date_time.rb
rubocop-0.55.0 lib/rubocop/cop/style/date_time.rb
rubocop-0.54.0 lib/rubocop/cop/style/date_time.rb
rubocop-0.53.0 lib/rubocop/cop/style/date_time.rb
rubocop-0.52.1 lib/rubocop/cop/style/date_time.rb
rubocop-0.52.0 lib/rubocop/cop/style/date_time.rb