Skip to content
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

[Fix #6914] Fix an error for Rails/RedundantAllowNil when with interpolations #6916

Merged
merged 1 commit into from Apr 10, 2019

Conversation

Blue-Pix
Copy link
Contributor

@Blue-Pix Blue-Pix commented Apr 9, 2019

Fixes #6914.

This PR fixes the following error for Rails/RedundantAllowNil when with interpolations.

class Foo < ActiveRecord::Base
  BAR = "sample"
  validates :name, "#{self::BAR}": true
end
class SampleValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if value.size > 3
      record.errors.add(attribute, 'too long')
    end
  end
end

This is stacktrace.

$ rubocop app/models/foo.rb --only Rails/RedundantAllowNil -d
An error occurred while Rails/RedundantAllowNil cop was inspecting /myapp/app/models/foo.rb:3:2.
undefined method `value' for "s(:dsym,\n  s(:begin,\n    s(:const,\n      s(:self), :BAR)))":RuboCop::AST::Node

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@@ -79,7 +79,7 @@ def find_allow_nil_and_allow_blank(node)
node.each_descendant do |descendant|
next unless descendant.pair_type?

key = descendant.children.first.value
key = node_key(descendant.children.first)
Copy link
Member

@koic koic Apr 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A solution is to use Node#source.

diff --git a/lib/rubocop/cop/rails/redundant_allow_nil.rb b/lib/rubocop/cop/rails/redundant_allow_nil.rb
index be75c1592..2f5934b56 100644
--- a/lib/rubocop/cop/rails/redundant_allow_nil.rb
+++ b/lib/rubocop/cop/rails/redundant_allow_nil.rb
@@ -79,10 +79,10 @@ module RuboCop
           node.each_descendant do |descendant|
             next unless descendant.pair_type?

-            key = descendant.children.first.value
+            key = descendant.children.first.source

-            allow_nil = descendant if key == :allow_nil
-            allow_blank = descendant if key == :allow_blank
+            allow_nil = descendant if key == 'allow_nil'
+            allow_blank = descendant if key == 'allow_blank'

             break if allow_nil && allow_blank
           end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for a review!

@koic
Copy link
Member

koic commented Apr 10, 2019

Please add a test case to reproduce.

diff --git a/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb b/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb
index 0ce9ffd5a..760661d4a 100644
--- a/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb
+++ b/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb
@@ -73,4 +73,12 @@ RSpec.describe RuboCop::Cop::Rails::RedundantAllowNil do
       RUBY
     end
   end
+
+  context 'when using string interpolation' do
+    it 'registers no offense' do
+      expect_no_offenses(<<-'RUBY'.strip_indent)
+        validates :details, "path_to_dynamic_validation/#{with_interpolation}": true
+      RUBY
+    end
+  end
 end

@koic
Copy link
Member

koic commented Apr 10, 2019

Looks good to me.
Can you squash your commits into one?

@bbatsov bbatsov merged commit e2b81c9 into rubocop:master Apr 10, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 10, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails/RedundantAllowNil crashes with interpolations
3 participants