Skip to content

Commit

Permalink
Use CONST in relative date's node_matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jdelStrother committed May 11, 2021
1 parent c19d4b9 commit e318801
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rails/relative_date_constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RelativeDateConstant < Base

MSG = 'Do not assign `%<method_name>s` to constants as it ' \
'will be evaluated only once.'
RELATIVE_DATE_METHODS = %i[since from_now after ago until before yesterday tomorrow].freeze
RELATIVE_DATE_METHODS = %i[since from_now after ago until before yesterday tomorrow].to_set.freeze

def on_casgn(node)
nested_relative_date?(node) do |method_name|
Expand Down Expand Up @@ -89,10 +89,6 @@ def offense_range(name, value)
range_between(name.loc.expression.begin_pos, value.loc.expression.end_pos)
end

def relative_date_method?(method_name)
RELATIVE_DATE_METHODS.include?(method_name)
end

def nested_relative_date?(node, &callback)
return if node.block_type?

Expand All @@ -104,11 +100,11 @@ def nested_relative_date?(node, &callback)
end

def_node_matcher :relative_date_or_assignment?, <<~PATTERN
(:or_asgn (casgn _ _) (send _ $#relative_date_method?))
(:or_asgn (casgn _ _) (send _ $RELATIVE_DATE_METHODS))
PATTERN

def_node_matcher :relative_date?, <<~PATTERN
(send _ $#relative_date_method?)
(send _ $RELATIVE_DATE_METHODS)
PATTERN
end
end
Expand Down

0 comments on commit e318801

Please sign in to comment.