Sha256: 83ec04a47343024e51387d7c15714247b511a7ed1a02149563d72ab64d8b2c1e

Contents?: true

Size: 801 Bytes

Versions: 10

Compression:

Stored size: 801 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 < Base
        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 { |corrector| corrector.replace(node.loc.expression, "Time.zone.now") }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-discourse-3.10.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.9.3 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.9.2 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.9.1 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.9.0 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/no_time_new_without_args.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/no_time_new_without_args.rb