diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-26 00:10:31 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-26 00:10:31 +0000 |
commit | a1d73247427b451ff85ccaa6b93dccf10e04283a (patch) | |
tree | 8a88d1a90fe26d24ef6ba78a267fd4c8727bd85f /rubocop | |
parent | 97cdd36a0b70b11e2592f3f63f7aca83342c8dfa (diff) | |
download | gitlab-ce-a1d73247427b451ff85ccaa6b93dccf10e04283a.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/rspec/timecop_freeze.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/timecop_freeze.rb b/rubocop/cop/rspec/timecop_freeze.rb new file mode 100644 index 00000000000..508b5df7c7f --- /dev/null +++ b/rubocop/cop/rspec/timecop_freeze.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module RSpec + # This cop checks for `Timecop.freeze` usage in specs. + # + # @example + # + # # bad + # Timecop.freeze(Time.current) { example.run } + # + # # good + # freeze_time(Time.current) { example.run } + # + class TimecopFreeze < RuboCop::Cop::Cop + include MatchRange + MESSAGE = 'Do not use `Timecop.freeze`, use `freeze_time` instead. ' \ + 'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.' + + def_node_matcher :timecop_freeze?, <<~PATTERN + (send (const nil? :Timecop) :freeze ?_) + PATTERN + + def on_send(node) + return unless timecop_freeze?(node) + + add_offense(node, location: :expression, message: MESSAGE) + end + + def autocorrect(node) + -> (corrector) do + each_match_range(node.source_range, /^(Timecop\.freeze)/) do |match_range| + corrector.replace(match_range, 'freeze_time') + end + end + end + end + end + end +end |