From 7bfa1f6596f4096f53d38ed2da0f161278379251 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 10 Dec 2020 17:42:23 +0900 Subject: [PATCH] [Fix #401] Fix an error for `Rails/WhereEquals` Fixes #401. This PR fixes an error for `Rails/WhereEquals` when using only named placeholder template without replacement argument. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/rails/where_equals.rb | 2 +- spec/rubocop/cop/rails/where_equals_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13d779fe58..6ebe901afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#401](https://github.com/rubocop-hq/rubocop-rails/issues/401): Fix an error for `Rails/WhereEquals` using only named placeholder template without replacement argument. ([@koic][]) + ## 2.9.0 (2020-12-09) ### New features diff --git a/lib/rubocop/cop/rails/where_equals.rb b/lib/rubocop/cop/rails/where_equals.rb index 9a6a921f18..c7ddb653e2 100644 --- a/lib/rubocop/cop/rails/where_equals.rb +++ b/lib/rubocop/cop/rails/where_equals.rb @@ -68,7 +68,7 @@ def extract_column_and_value(template_node, value_node) when EQ_ANONYMOUS_RE, IN_ANONYMOUS_RE value_node.source when EQ_NAMED_RE, IN_NAMED_RE - return unless value_node.hash_type? + return unless value_node&.hash_type? pair = value_node.pairs.find { |p| p.key.value.to_sym == Regexp.last_match(2).to_sym } pair.value.source diff --git a/spec/rubocop/cop/rails/where_equals_spec.rb b/spec/rubocop/cop/rails/where_equals_spec.rb index 938fdeb4dd..59b03a4a09 100644 --- a/spec/rubocop/cop/rails/where_equals_spec.rb +++ b/spec/rubocop/cop/rails/where_equals_spec.rb @@ -160,4 +160,12 @@ User.where('name = ? AND age = ?', 'john', 19) RUBY end + + it 'does not register an offense when using only named placeholder template without replacement argument' do + expect_no_offenses(<<~'RUBY') + sql = User.where('name = :name').select(:id).to_sql + + User.where("id IN (#{sql})", name: 'Lastname').first + RUBY + end end