Skip to content

Commit

Permalink
simplify the logic of autocorrect_time_new and safe_method to reduce …
Browse files Browse the repository at this point in the history
…redundancy
  • Loading branch information
mterada1228 committed Dec 25, 2024
1 parent d67944d commit c8b111c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,9 @@ def autocorrect(corrector, node)
end

def autocorrect_time_new(node, corrector)
if node.first_argument&.str_type?
corrector.replace(node.loc.selector, 'parse')
elsif node.arguments?
corrector.replace(node.loc.selector, 'local')
else
corrector.replace(node.loc.selector, 'now')
end
replacement = replacement(node)

corrector.replace(node.loc.selector, replacement)
end

# remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone`
Expand Down Expand Up @@ -185,9 +181,7 @@ def method_send?(node)

def safe_method(method_name, node)
if %w[new current].include?(method_name)
return 'parse' if node.first_argument&.str_type?

node.arguments? ? 'local' : 'now'
replacement(node)
else
method_name
end
Expand Down Expand Up @@ -263,6 +257,12 @@ def offset_option_provided?(node)
pair.key.sym_type? && pair.key.value == :in && !pair.value.nil_type?
end
end

def replacement(node)
return 'now' unless node.arguments?

node.first_argument.str_type? ? 'parse' : 'local'
end
end
end
end
Expand Down

0 comments on commit c8b111c

Please sign in to comment.