Skip to content

Fix Rails/TimeZone cop error on invalid string literal encoding #1475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1475](https://github.com/rubocop/rubocop-rails/pull/1475): Fix `Rails/TimeZone` cop error on invalid string literal encoding. ([@viralpraxis][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def check_time_node(klass, node)
end

def attach_timezone_specifier?(date)
date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s)
return false unless date.respond_to?(:value)

!date.value.to_s.valid_encoding? || TIMEZONE_SPECIFIER.match?(date.value.to_s)
end

def build_message(klass, method_name, node)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::TimeZone, :config do
shared_examples 'with invalid timezone literal encoding' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
Time.new("l\366, 26 maj 2001 00:15:58")
RUBY
end
end

context 'when EnforcedStyle is "strict"' do
let(:cop_config) { { 'EnforcedStyle' => 'strict' } }

include_context 'with invalid timezone literal encoding'

it 'registers an offense for Time.now' do
expect_offense(<<~RUBY)
Time.now
Expand Down Expand Up @@ -334,6 +344,8 @@
context 'when EnforcedStyle is "flexible"' do
let(:cop_config) { { 'EnforcedStyle' => 'flexible' } }

include_context 'with invalid timezone literal encoding'

it 'registers an offense for Time.now' do
expect_offense(<<~RUBY)
Time.now
Expand Down
Loading