Skip to content

Commit

Permalink
[Fix rubocop#1773] Fix typo ('strptime' to 'strftime')
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 9, 2015
1 parent 27f714c commit 64f3065
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

### Bugs fixed

* [#1773](https://github.com/bbatsov/rubocop/pull/1773): Fix type ('strptime' -> 'strftime') in `Rails/TimeZone`. ([@palkan][])
* [#1705](https://github.com/bbatsov/rubocop/issues/1705): Fix crash when reporting offenses of `MissingElse` cop. ([@gerry3][])
* [#1659](https://github.com/bbatsov/rubocop/pull/1659): Fix stack overflow with JRuby and Windows 8, during initial config validation. ([@pimterry][])
* [#1694](https://github.com/bbatsov/rubocop/issues/1694): Ignore methods with a `blockarg` in `TrivialAccessors`. ([@bbatsov][])
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def check_date_node(node)
chain = extract_method_chain(node)
return if (chain & bad_days).empty?

method_name = *(chain & bad_days)
method_name = (chain & bad_days).join('.')

add_offense(node, :selector,
format(MSG,
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TimeZone < Cop

TIMECLASS = [:Time, :DateTime]

DANGER_METHODS = [:now, :local, :new, :strptime, :parse, :at]
DANGER_METHODS = [:now, :local, :new, :strftime, :parse, :at]

def on_const(node)
_module, klass = *node
Expand All @@ -50,7 +50,7 @@ def check_time_node(klass, node)
return if (chain & DANGER_METHODS).empty? ||
!(chain & good_methods).empty?

method_name = *(chain & DANGER_METHODS)
method_name = (chain & DANGER_METHODS).join('.')

add_offense(node, :selector,
format(MSG,
Expand Down
29 changes: 17 additions & 12 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.strptime' do
inspect_source(cop, 'Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z")')
it 'registers an offense for Time.strftime' do
inspect_source(cop, 'Time.strftime(time_string, "%Y-%m-%dT%H:%M:%S%z")')
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.strptime.in_time_zone' do
it 'registers an offense for Time.strftime.in_time_zone' do
inspect_source(
cop,
'Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone'
'Time.strftime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone'
)
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.strptime with nested Time.zone' do
it 'registers an offense for Time.strftime with nested Time.zone' do
inspect_source(
cop,
'Time.strptime(Time.zone.now.to_s, "%Y-%m-%dT%H:%M:%S%z")'
'Time.strftime(Time.zone.now.to_s, "%Y-%m-%dT%H:%M:%S%z")'
)
expect(cop.offenses.size).to eq(1)
end

it 'registers an offense for Time.zone.strptime with nested Time.now' do
it 'registers an offense for Time.zone.strftime with nested Time.now' do
inspect_source(
cop,
'Time.zone.strptime(Time.now.to_s, "%Y-%m-%dT%H:%M:%S%z")'
'Time.zone.strftime(Time.now.to_s, "%Y-%m-%dT%H:%M:%S%z")'
)
expect(cop.offenses.size).to eq(1)
end
Expand Down Expand Up @@ -94,10 +94,15 @@
expect(cop.offenses).to be_empty
end

it 'accepts Time.zone.strptime' do
it 'accepts Time.strptime' do
inspect_source(cop, 'Time.strptime(datetime, format).in_time_zone')
expect(cop.offenses).to be_empty
end

it 'accepts Time.zone.strftime' do
inspect_source(
cop,
'Time.zone.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z")'
'Time.zone.strftime(time_string, "%Y-%m-%dT%H:%M:%S%z")'
)
expect(cop.offenses).to be_empty
end
Expand All @@ -113,10 +118,10 @@
end
end

it 'accepts Time.strptime.in_time_zone' do
it 'accepts Time.strftime.in_time_zone' do
inspect_source(
cop,
'Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone'
'Time.strftime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone'
)
expect(cop.offenses).to be_empty
end
Expand Down

0 comments on commit 64f3065

Please sign in to comment.