Sha256: 86e29a7be68becc65246bd0449635fd93d60351fe253f346b67ed6d8efcfa82d

Contents?: true

Size: 825 Bytes

Versions: 30

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      # Use `Time.zone.now` instead of `Time.new` without arguments.
      #
      # @example
      #   # bad
      #   now = Time.new
      #
      #   # good
      #   now = Time.zone.now
      class NoTimeNewWithoutArgs < Cop
        MSG = "Use `Time.zone.now` instead of `Time.new` without arguments."

        def_node_matcher :time_new_without_args?, <<-MATCHER
          (send (const nil? :Time) :new)
        MATCHER

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

          add_offense(node, message: MSG)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.loc.expression, "Time.zone.now")
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
rubocop-discourse-3.8.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.7.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.7.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.6.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.6.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.5.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.5.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.4.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.4.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.3.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.2.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.1.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.0.3 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.0.2 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.0.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-2.6.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-2.5.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-2.4.2 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-2.4.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb