Sha256: 4fbcedbb87f74618cac55b9caea42b068e63c49476848ef8457c5d37ff2f49b7

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Flags use of strftime('%Y-%m-%d') for formatting dates
      # the preferred method is using .iso8601
      #
      # @example
      #   # bad
      #   DateTime.now.strftime('%Y-%m-%d')
      #   Time.now.strftime("%Y-%m-%d")
      #   Date.today.strftime("%Y-%m-%d")
      #
      #   # good
      #   DateTime.now.to_date.iso8601
      #   Time.now.to_date.iso8601
      #   Date.today.iso8601
      #
      # See also:
      # - https://gitlab.com/gitlab-org/gitlab/-/issues/410638
      class Iso8601Date < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector
        include RangeHelp

        MSG = 'Use `iso8601` instead of `strftime("%Y-%m-%d")`.'
        RESTRICT_ON_SEND = [:strftime].freeze

        # @!method strftime_iso8601?(node)
        def_node_matcher :strftime_iso8601?, <<~PATTERN
        (send $(...) :strftime (str "%Y-%m-%d"))
        PATTERN

        def on_send(node)
          return unless strftime_iso8601?(node)

          range = range_between(node.loc.selector.begin_pos, node.loc.end.end_pos)

          add_offense(range) do |corrector|
            corrector.replace(range, 'to_date.iso8601')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/style/iso8601_date.rb
gitlab-styles-13.0.0 lib/rubocop/cop/style/iso8601_date.rb