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/RelativeDateConstant errors on constants defined with conditional assignment operator #4240

Closed
timrogers opened this issue Apr 4, 2017 · 2 comments

Comments

@timrogers
Copy link

Constants defined with a conditional assignment operator (we do this to prevent specs from trying to redefine constants if a file is required more than once) caused the Rails/RelativeDateConstant cop to error.

Expected behavior

The cop should allow code like the following, because it doesn't contain a relative date:

DEFAULT_SUBJECT ||= "My Default Subject"

Actual behavior

Rubocop errors like this:

ndefined method `lambda_or_proc?' for nil:NilClass
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/rails/relative_date_constant.rb:30:in `on_casgn'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:43:in `block (2 levels) in on_casgn'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:106:in `with_cop_error_handling'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:42:in `block in on_casgn'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:41:in `each'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:41:in `on_casgn'
(eval):2:in `block in on_or_asgn'
(eval):2:in `each'
(eval):2:in `on_or_asgn'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:47:in `on_or_asgn'
(eval):2:in `block in on_begin'
(eval):2:in `each'
(eval):2:in `on_begin'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:47:in `on_begin'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/ast/traversal.rb:88:in `on_class'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:47:in `on_class'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/ast/traversal.rb:141:in `on_while'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:47:in `on_module'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/ast/traversal.rb:12:in `walk'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/commissioner.rb:60:in `investigate'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/team.rb:124:in `investigate'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/team.rb:112:in `offenses'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cop/team.rb:54:in `inspect_file'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:248:in `inspect_file'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:195:in `block in do_inspection_loop'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:227:in `block in iterate_until_no_changes'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:220:in `loop'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:220:in `iterate_until_no_changes'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:191:in `do_inspection_loop'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:101:in `block in file_offenses'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:111:in `file_offense_cache'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:99:in `file_offenses'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:90:in `process_file'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:65:in `each'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:65:in `reduce'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:65:in `each_inspected_file'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:57:in `inspect_files'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/runner.rb:36:in `run'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cli.rb:72:in `execute_runner'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/lib/rubocop/cli.rb:27:in `run'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/bin/rubocop:13:in `block in <top (required)>'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rubocop-0.48.1/bin/rubocop:12:in `<top (required)>'
/Users/timrogers/.rbenv/versions/2.3.3/bin/rubocop:22:in `load'
/Users/timrogers/.rbenv/versions/2.3.3/bin/rubocop:22:in `<top (required)>'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `load'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `kernel_load'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in `run'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in `exec'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in `dispatch'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in `start'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/exe/bundle:32:in `block in <top (required)>'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/Users/timrogers/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/exe/bundle:24:in `<top (required)>'
/Users/timrogers/.rbenv/versions/2.3.3/bin/bundle:22:in `load'
/Users/timrogers/.rbenv/versions/2.3.3/bin/bundle:22:in `<main>'

Steps to reproduce the problem

Use the conditional assignment operator (||=) to define a constant

RuboCop version

$ bundle exec rubocop -V
0.48.1 (using Parser 2.4.0.0, running on ruby 2.3.3 x86_64-darwin16)
@bbatsov bbatsov closed this as completed in 75b8eb4 Apr 5, 2017
@timrogers
Copy link
Author

@bbatsov Thanks ❤️ (I wasn't planning a drive-by issue and was going to look at fixing this myself, but am very thankful that you got a chance before me!)

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 5, 2017

Well, it turned out this cop wasn't even supporting ||=. :-) Thought the problem was a missing nil check, turned out it it was more complex, but it was fun to fix it.

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

2 participants