From a02819faaf2b48837b2a7a9cac375215be9d4b17 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:01:14 +0100 Subject: [PATCH] Fix a false positive for `Rails/RelativeDateConstant` when assigning a lambda/proc with numblock --- .../fix_false_positive_relative_date_const_numblock.md | 1 + lib/rubocop/cop/rails/relative_date_constant.rb | 2 +- spec/rubocop/cop/rails/relative_date_constant_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_relative_date_const_numblock.md diff --git a/changelog/fix_false_positive_relative_date_const_numblock.md b/changelog/fix_false_positive_relative_date_const_numblock.md new file mode 100644 index 0000000000..0345003e0f --- /dev/null +++ b/changelog/fix_false_positive_relative_date_const_numblock.md @@ -0,0 +1 @@ +* [#1458](https://github.com/rubocop/rubocop-rails/pull/1458): Fix a false positive for `Rails/RelativeDateConstant` when assigning a lambda/proc with numblock. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/relative_date_constant.rb b/lib/rubocop/cop/rails/relative_date_constant.rb index a2fed38e8e..64e9700056 100644 --- a/lib/rubocop/cop/rails/relative_date_constant.rb +++ b/lib/rubocop/cop/rails/relative_date_constant.rb @@ -90,7 +90,7 @@ def offense_range(name, value) end def nested_relative_date(node, &callback) - return if node.nil? || node.block_type? + return if node.nil? || node.any_block_type? node.each_child_node do |child| nested_relative_date(child, &callback) diff --git a/spec/rubocop/cop/rails/relative_date_constant_spec.rb b/spec/rubocop/cop/rails/relative_date_constant_spec.rb index 148c5663c8..e2a6113f6e 100644 --- a/spec/rubocop/cop/rails/relative_date_constant_spec.rb +++ b/spec/rubocop/cop/rails/relative_date_constant_spec.rb @@ -18,6 +18,14 @@ class SomeClass RUBY end + it 'accepts a lambda with numblock' do + expect_no_offenses(<<~RUBY) + class SomeClass + EXPIRED_AT = -> { _1.year.ago } + end + RUBY + end + it 'accepts a proc' do expect_no_offenses(<<~RUBY) class SomeClass