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

Rails/HttpPositionalArguments autofixing errors if given a variable #3652

Closed
dgmstuart opened this issue Oct 19, 2016 · 2 comments
Closed

Rails/HttpPositionalArguments autofixing errors if given a variable #3652

dgmstuart opened this issue Oct 19, 2016 · 2 comments

Comments

@dgmstuart
Copy link

The Rails/HttpPositionalArguments throws an error when trying to autofix the following:

attrs = { foo: bar }
post :create, attrs

Expected behavior

Autofix the above to this:

attrs = { foo: bar }
post :create, params: attrs

Actual behavior

An error occurred while Rails/HttpPositionalArguments cop was inspecting <<a spec file>>

segment of output of rubocop -a -d:

An error occurred while Rails/HttpPositionalArguments cop was inspecting /path/to/project/spec/controllers/a_controller_spec.rb.
undefined method `source' for :subscription_attrs:Symbol
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:59:in `map'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:59:in `convert_hash_data'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:77:in `autocorrect'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/cop.rb:195:in `correct'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/cop.rb:176:in `add_offense'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:37:in `on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:41:in `block (2 levels) in on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:96:in `with_cop_error_handling'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:40:in `block in on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `each'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:101:in `block in on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:99:in `each'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:99:in `each_with_index'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:99:in `on_send'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_send'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_begin'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:12:in `walk'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:58:in `investigate'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:120:in `investigate'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:100:in `offenses'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:51:in `inspect_file'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:243:in `inspect_file'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:190:in `block in do_inspection_loop'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:222:in `block in iterate_until_no_changes'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `loop'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `iterate_until_no_changes'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:186:in `do_inspection_loop'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:101:in `block in file_offenses'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:111:in `file_offense_cache'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:99:in `file_offenses'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:90:in `process_file'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `reduce'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each_inspected_file'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:57:in `inspect_files'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:36:in `run'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:71:in `execute_runner'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:27:in `run'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/bin/rubocop:13:in `block in <top (required)>'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/bin/rubocop:12:in `<top (required)>'
/usr/local/var/rbenv/versions/2.3.1/bin/rubocop:23:in `load'
/usr/local/var/rbenv/versions/2.3.1/bin/rubocop:23:in `<main>'

Steps to reproduce the problem

Run rubocop -a on a file containing the following:

RSpec.describe 'foo', type: :controller do 
  it "can't be autofixed" do
    attrs = { foo: bar }
    post :create, attrs
  end
end

RuboCop version

$ rubocop -V
0.44.1 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-darwin15)
@tejasbubane
Copy link
Contributor

tejasbubane commented Oct 19, 2016

@dgmstuart Cannot reproduce this on 0.44.1 and master.

⇒  cat rubocop_bug_test.rb
# frozen_string_literal: true

RSpec.describe 'foo', type: :controller do
  it "can't be autofixed" do
    attrs = { foo: bar }
    post :create, attrs
  end
end

⇒  rubocop -V
0.44.1 (using Parser 2.3.1.2, running on ruby 2.3.1 x86_64-darwin15)

⇒  rubocop -a rubocop_bug_test.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Please double check with your code since the error undefined method 'source' for :subscription_attrs:Symbol seems out of context to me. Can you post your rubocop config if the error still exists?

@pocke
Copy link
Collaborator

pocke commented Oct 20, 2016

I've encountered the error too in my project.

Reproduce

RSpec.describe 'foo', type: :controller do 
  it "can't be autofixed" do
    attrs = { foo: bar }
    post :create, attrs
  end
end

Save the above file as spec/test_spec.rb, and run rubocop -a --rails -d

An error occurred while Rails/HttpPositionalArguments cop was inspecting /tmp/tmp.0HgrrYDApd/spec/test_spec.rb.

1 error occurred:
An error occurred while Rails/HttpPositionalArguments cop was inspecting /tmp/tmp.0HgrrYDApd/spec/test_spec.rb.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
Mention the following information in the issue report:
0.44.1 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-linux)
For /tmp/tmp.0HgrrYDApd: configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/default.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/enabled.yml
Inheriting configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/disabled.yml
Inspecting 1 file
Scanning /tmp/tmp.0HgrrYDApd/spec/test_spec.rb
undefined method `source' for :attrs:Symbol
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:59:in `map'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:59:in `convert_hash_data'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:77:in `autocorrect'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/cop.rb:195:in `correct'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/cop.rb:176:in `add_offense'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/rails/http_positional_arguments.rb:37:in `on_send'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:41:in `block (2 levels) in on_send'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:96:in `with_cop_error_handling'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:40:in `block in on_send'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `each'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `on_send'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_begin'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:154:in `on_block'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:45:in `on_block'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:12:in `walk'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:58:in `investigate'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:120:in `investigate'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:100:in `offenses'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:51:in `inspect_file'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:243:in `inspect_file'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:190:in `block in do_inspection_loop'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:222:in `block in iterate_until_no_changes'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `loop'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `iterate_until_no_changes'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:186:in `do_inspection_loop'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:101:in `block in file_offenses'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:111:in `file_offense_cache'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:99:in `file_offenses'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:90:in `process_file'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `reduce'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each_inspected_file'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:57:in `inspect_files'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:36:in `run'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:71:in `execute_runner'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:27:in `run'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:13:in `block in <top (required)>'
/usr/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:12:in `<top (required)>'
/home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `load'
/home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `<main>'
.

1 file inspected, no offenses detected
Finished in 0.06136375598725863 seconds

I've written a patch for the problem, so I'll send a PR.

pocke added a commit to pocke/rubocop that referenced this issue Oct 20, 2016
bbatsov added a commit that referenced this issue Oct 20, 2016
…rguments-cop

[Fix #3652] Avoid crash Rails/HttpPositionalArguments for lvar params when auto-correct
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

No branches or pull requests

3 participants