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/WhereRange output the error if using ternary operator. #1281

Closed
r-plus opened this issue May 20, 2024 · 0 comments · Fixed by #1292
Closed

Rails/WhereRange output the error if using ternary operator. #1281

r-plus opened this issue May 20, 2024 · 0 comments · Fixed by #1292
Labels
bug Something isn't working

Comments

@r-plus
Copy link

r-plus commented May 20, 2024

Expected behavior

User.where('created_at <= ?' true ? Time.current : 1.day.ago)

This code expected to be autocorrect with parentheses parameter.

expected

User.where(created_at: ..(true ? Time.current : 1.day.ago))

Actual behavior

Actual is output the error. (w/o autocorrect option -a too)

$ cat test.rb
User.where('created_at <= ?', true ? Time.current : 1.day.ago)

$ bundle exec rubocop -a test.rb
Inspecting 1 file
.

0 files inspected, no offenses detected
undefined method `type' for nil
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:71:in `on_iflipflop'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:153:in `on_if'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:71:in `on_pair'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:71:in `on_hash'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:159:in `block in on_send'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:156:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:156:in `each_with_index'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:156:in `on_send'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:71:in `on_send'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cop/team.rb:91:in `investigate'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:349:in `block in inspect_file'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:348:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:348:in `flat_map'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:348:in `inspect_file'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:291:in `block in do_inspection_loop'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:325:in `block in iterate_until_no_changes'
<internr-plus`loop'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:318:in `iterate_until_no_changes'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:287:in `do_inspection_loop'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:168:in `block in file_offenses'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:193:in `file_offense_cache'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:167:in `file_offenses'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:158:in `process_file'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:139:in `block in each_inspected_file'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:138:in `each'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:138:in `reduce'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:138:in `each_inspected_file'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:124:in `inspect_files'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/runner.rb:77:in `run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/command.rb:11:in `run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli/environment.rb:18:in `run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli.rb:122:in `run_command'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli.rb:129:in `execute_runners'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli.rb:51:in `block in run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli.rb:81:in `profile_if_needed'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/lib/rubocop/cli.rb:43:in `run'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/exe/rubocop:19:in `block in <top (required)>'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/benchmark.rb:313:in `realtime'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/gems/rubocop-1.63.5/exe/rubocop:19:in `<top (required)>'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/bin/rubocop:25:in `load'
/Users/r-plus/gitclone/server/vendor/bundle/ruby/3.3.0/bin/rubocop:25:in `<top (required)>'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli/exec.rb:58:in `load'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli/exec.rb:58:in `kernel_load'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli/exec.rb:23:in `run'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli.rb:451:in `exec'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli.rb:34:in `dispatch'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/cli.rb:28:in `start'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/bundler-2.5.9/exe/bundle:28:in `block in <top (required)>'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/Users/r-plus/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/bundler-2.5.9/exe/bundle:20:in `<top (required)>'
/Users/r-plus/.rbenv/versions/3.3.1/bin/bundle:25:in `load'
/Users/r-plus/.rbenv/versions/3.3.1/bin/bundle:25:in `<main>'

$ cat test.rb
User.where(created_at: ..true ? Time.current : 1.day.ago)

Additionally, autocorrected code will not have parentheses for ternary operator.
Range operators (.., ...) have higher precedence than ternary operator (?, :), so this autocorrected code will not provide range object to where method.

irb(main):001:0> ..true ? 1 : 0
=> 0

Steps to reproduce the problem

Create ruby file that using ternary operator for where like above example then autocorrect it with Rails/WhereRange.

RuboCop version

$ bundle exec rubocop -V
1.63.5 (using Parser 3.3.1.0, rubocop-ast 1.31.3, running on ruby 3.3.1) [arm64-darwin23]
  - rubocop-capybara 2.20.0
  - rubocop-factory_bot 2.23.1
  - rubocop-graphql 1.5.1
  - rubocop-performance 1.21.0
  - rubocop-rails 2.25.0
  - rubocop-rspec 2.29.2
  - rubocop-rspec_rails 2.28.3```
@koic koic added the bug Something isn't working label May 27, 2024
fatkodima added a commit to fatkodima/rubocop-rails that referenced this issue Jun 7, 2024
koic added a commit that referenced this issue Jun 8, 2024
…ssions

[Fix #1281] Fix `WhereRange` autocorrect for complex expressions
@koic koic closed this as completed in #1292 Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants