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

Support more complex argument patterns on Rails/Validation auto-correction #6689

Merged
merged 1 commit into from Jan 22, 2019

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Jan 21, 2019

About

I changed #autocorrect implementation to support more different arguments pattern by checking node.arguments.last type.

Background

I tried to auto-correct the following code with Rails/Validation cop with rubocop 0.63.0.

# app/models/example_model.rb
class ExampleModel < ActiveRecord::Base
  ATTRIBUTES = %i(b c)

  validates_numericality_of :a, *ATTRIBUTES, only_integer: true
end

Expected behavior

I expected that it would be auto-corrected like this:

$ rubocop --auto-correct --only Rails/Validation app/models/example_model.rb
Inspecting 1 file
C

Offenses:

app/models/example_model.rb:4:3: C: [Corrected] Rails/Validation: Prefer the new style validations validates :column, numericality: value over validates_numericality_of.
  validates_numericality_of :a, *ATTRIBUTES, only_integer: true
  ^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
class ExampleModel < ActiveRecord::Base
  ATTRIBUTES = %i(b c)

  validates :a, *ATTRIBUTES, numericality: { only_integer: true }
end

Actual behavior

Unfortunately it failed because Rails/Validation's #autocorrect doesn't support splat node in arguments.

$ rubocop --auto-correct --only Rails/Validation app/models/example_model.rb
Inspecting 1 file


0 files inspected, no offenses detected
undefined method `braces?' for "s(:splat,\n  s(:const, nil, :ATTRIBUTES))":RuboCop::AST::Node
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/rails/validation.rb:91:in `braced_options'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/rails/validation.rb:83:in `correct_validate_type'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/rails/validation.rb:62:in `block in autocorrect'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/corrector.rb:64:in `block (2 levels) in rewrite'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/parser-2.5.3.0/lib/parser/source/tree_rewriter.rb:220:in `transaction'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/corrector.rb:63:in `block in rewrite'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/corrector.rb:61:in `each'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/corrector.rb:61:in `rewrite'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:128:in `autocorrect_all_cops'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:72:in `autocorrect'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:100:in `block in offenses'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:117:in `investigate'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:96:in `offenses'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:280:in `inspect_file'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:227:in `block in do_inspection_loop'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:259:in `block in iterate_until_no_changes'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:252:in `loop'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:252:in `iterate_until_no_changes'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:223:in `do_inspection_loop'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:126:in `block in file_offenses'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:144:in `file_offense_cache'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:124:in `file_offenses'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:112:in `process_file'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:89:in `block in each_inspected_file'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:86:in `each'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:86:in `reduce'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:86:in `each_inspected_file'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:76:in `inspect_files'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/runner.rb:48:in `run'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cli.rb:174:in `execute_runner'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cli.rb:75:in `execute_runners'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/lib/rubocop/cli.rb:47:in `run'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/exe/rubocop:13:in `block in <top (required)>'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
/Users/r7kamura/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rubocop-0.63.0/exe/rubocop:12:in `<top (required)>'
/Users/r7kamura/.rbenv/versions/2.4.1/bin/rubocop:22:in `load'
/Users/r7kamura/.rbenv/versions/2.4.1/bin/rubocop:22:in `<main>'

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.

Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! 🙇

🚀

CHANGELOG.md Outdated Show resolved Hide resolved
@r7kamura r7kamura changed the title Fix auto-correct issues on Rails/Validation cop Support more complex argument patterns on Rails/Validation cop Jan 22, 2019
@r7kamura r7kamura changed the title Support more complex argument patterns on Rails/Validation cop Support more complex argument patterns on Rails/Validation auto-correction Jan 22, 2019
@koic
Copy link
Member

koic commented Jan 22, 2019

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

@r7kamura
Copy link
Contributor Author

OK, I just squashed and pushed it 😃

CHANGELOG.md Outdated Show resolved Hide resolved
@koic koic merged commit d6e7a16 into rubocop:master Jan 22, 2019
@r7kamura r7kamura deleted the feature/rails-validation branch January 22, 2019 15:51
This was referenced Feb 7, 2019
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.

None yet

3 participants