Skip to content

Commit e97c93f

Browse files
authored
Merge pull request #1475 from viralpraxis/fix-rails-time-zone-cop-error-on-invalid-string-literal-encoding
Fix `Rails/TimeZone` cop error on invalid string literal encoding
2 parents 2a44746 + b17bfb7 commit e97c93f

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1475](https://github.com/rubocop/rubocop-rails/pull/1475): Fix `Rails/TimeZone` cop error on invalid string literal encoding. ([@viralpraxis][])

lib/rubocop/cop/rails/time_zone.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def check_time_node(klass, node)
135135
end
136136

137137
def attach_timezone_specifier?(date)
138-
date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s)
138+
return false unless date.respond_to?(:value)
139+
140+
!date.value.to_s.valid_encoding? || TIMEZONE_SPECIFIER.match?(date.value.to_s)
139141
end
140142

141143
def build_message(klass, method_name, node)

spec/rubocop/cop/rails/time_zone_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::TimeZone, :config do
4+
shared_examples 'with invalid timezone literal encoding' do
5+
it 'does not register an offense' do
6+
expect_no_offenses(<<~'RUBY')
7+
Time.new("l\366, 26 maj 2001 00:15:58")
8+
RUBY
9+
end
10+
end
11+
412
context 'when EnforcedStyle is "strict"' do
513
let(:cop_config) { { 'EnforcedStyle' => 'strict' } }
614

15+
include_context 'with invalid timezone literal encoding'
16+
717
it 'registers an offense for Time.now' do
818
expect_offense(<<~RUBY)
919
Time.now
@@ -334,6 +344,8 @@
334344
context 'when EnforcedStyle is "flexible"' do
335345
let(:cop_config) { { 'EnforcedStyle' => 'flexible' } }
336346

347+
include_context 'with invalid timezone literal encoding'
348+
337349
it 'registers an offense for Time.now' do
338350
expect_offense(<<~RUBY)
339351
Time.now

0 commit comments

Comments
 (0)