Skip to content

Commit

Permalink
[Fix rubocop#1770] Add utc/localtime support to Rails/TimeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 15, 2015
1 parent 30c34b6 commit b0245dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#1773](https://github.com/bbatsov/rubocop/pull/1773): Fix typo ('strptime' -> 'strftime') in `Rails/TimeZone`. ([@palkan][])
* [#1777](https://github.com/bbatsov/rubocop/pull/1777): Fix offense message from Rails/TimeZone. ([@mzp][])
* Fix handling of `while` and `until` with assignment in `IndentationWidth`. ([@lumeet][])
* [#1767](https://github.com/bbatsov/rubocop/pull/1767): Do not register offenses on Time.utc and Time.localtime(offset) by `Rails/TimeZone`. ([@palkan][])

## 0.30.0 (06/04/2015)

Expand Down
17 changes: 16 additions & 1 deletion lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class TimeZone < Cop

MSG = 'Do not use `%s` without zone. Use `%s` instead.'

MSG_LOCALTIME = 'Do not use `Time.localtime` without offset or zone.'

TIMECLASS = [:Time, :DateTime]

DANGER_METHODS = [:now, :local, :new, :strftime, :parse, :at]
Expand All @@ -50,6 +52,8 @@ def check_time_node(klass, node)
return if (chain & DANGER_METHODS).empty? ||
!(chain & good_methods).empty?

return check_localtime(node) if chain.include?(:localtime)

method_name = (chain & DANGER_METHODS).join('.')
safe_method_name = safe_method(method_name, node)

Expand Down Expand Up @@ -96,8 +100,19 @@ def safe_method(method_name, node)
end
end

def check_localtime(node)
p = node
while !p.nil? && p.send_type?
break if extract_method(p) == :localtime
p = p.parent
end
_receiver, _method, args = *p

add_offense(node, :selector, MSG_LOCALTIME) if args.nil?
end

def good_methods
style == :always ? [:zone] : [:zone, :in_time_zone]
style == :always ? [:zone, :utc] : [:zone, :utc, :in_time_zone]
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.first.message).to include('Time.zone.local')
end

it "accepts #{klass}.now.utc" do
inspect_source(cop, "#{klass}.now.utc")
expect(cop.offenses).to be_empty
end
end

it 'registers an offense for Time.parse' do
Expand Down Expand Up @@ -71,6 +76,11 @@
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.parse.localtime' do
inspect_source(cop, "Time.parse('12:00').localtime")
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.at.in_time_zone' do
inspect_source(cop, 'Time.at(ts).in_time_zone')
expect(cop.offenses.size).to eq(1)
Expand Down Expand Up @@ -113,6 +123,16 @@
)
expect(cop.offenses).to be_empty
end

it 'accepts Time.parse.localtime(offset)' do
inspect_source(cop, "Time.parse('12:00').localtime('+03:00')")
expect(cop.offenses).to be_empty
end

it 'accepts Time.zone.parse.localtime' do
inspect_source(cop, "Time.zone.parse('12:00').localtime")
expect(cop.offenses).to be_empty
end
end

context 'when EnforcedStyle is "ignore_acceptable"' do
Expand Down

0 comments on commit b0245dc

Please sign in to comment.