lib/rubocop/cop/rails/date.rb in rubocop-0.52.0 vs lib/rubocop/cop/rails/date.rb in rubocop-0.52.1

- old
+ new

@@ -18,31 +18,41 @@ # and 'to_time_in_current_zone' is reported as warning. # # When EnforcedStyle is 'flexible' then only 'Date.today' is prohibited # and only 'to_time' is reported as warning. # - # @example - # # no offense - # Time.zone.today - # Time.zone.today - 1.day - # - # # flexible + # @example EnforcedStyle: strict + # # bad # Date.current # Date.yesterday + # Date.today + # date.to_time + # date.to_time_in_current_zone # - # # always reports offense + # # good + # Time.zone.today + # Time.zone.today - 1.day + # + # @example EnforcedStyle: flexible (default) + # # bad # Date.today # date.to_time # - # # reports offense only when style is 'strict' + # # good + # Time.zone.today + # Time.zone.today - 1.day + # Date.current + # Date.yesterday # date.to_time_in_current_zone + # class Date < Cop include ConfigurableEnforcedStyle - MSG = 'Do not use `%s` without zone. Use `%s` instead.'.freeze + MSG = 'Do not use `Date.%<day>s` without zone. Use ' \ + '`Time.zone.%<day>s` instead.'.freeze - MSG_SEND = 'Do not use `%s` on Date objects, because they ' \ + MSG_SEND = 'Do not use `%<method>s` on Date objects, because they ' \ 'know nothing about the time zone in use.'.freeze BAD_DAYS = %i[today current yesterday tomorrow].freeze def on_const(node) @@ -57,11 +67,11 @@ return unless node.receiver && bad_methods.include?(node.method_name) return if safe_chain?(node) || safe_to_time?(node) add_offense(node, location: :selector, - message: format(MSG_SEND, node.method_name)) + message: format(MSG_SEND, method: node.method_name)) end private def check_date_node(node) @@ -70,12 +80,10 @@ return if (chain & bad_days).empty? method_name = (chain & bad_days).join('.') add_offense(node, location: :selector, - message: format(MSG, - "Date.#{method_name}", - "Time.zone.#{method_name}")) + message: format(MSG, day: method_name.to_s)) end def extract_method_chain(node) [node, *node.each_ancestor(:send)].map(&:method_name) end