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 error in Rails/Presence when ternary operators are used in multiple lines #931

Merged
merged 1 commit into from
Feb 10, 2023

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Feb 8, 2023

When I run rubocop-rails against the following code, I get an error from Rails/Presence cop.

a.present? ?
  a :
  b

After investigation, I have identified the problem and would like to fix it.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@r7kamura r7kamura force-pushed the feature/presense-multiline-ternary branch from 8f7a032 to 3cce241 Compare February 8, 2023 04:28
@r7kamura
Copy link
Contributor Author

r7kamura commented Feb 8, 2023

The actual error I encountered was as follows:

$ bundle exec rubocop --debug example.rb
For /app: configuration from /app/.rubocop.yml
configuration from /bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/config/default.yml
configuration from /bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/config/default.yml
Default configuration from /bundle/ruby/2.7.0/gems/rubocop-1.37.1/config/default.yml
Inheriting configuration from /app/.rubocop_todo.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /app/example.rb
An error occurred while Rails/Presence cop was inspecting /app/example.rb:3:0.
undefined method `keyword' for #<Parser::Source::Map::Ternary:0x000055aaee81c4c0>
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:116:in `current'
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:110:in `message'
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:95:in `register_offense'
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:82:in `block in on_if'
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:64:in `redundant_receiver_and_other'
/bundle/ruby/2.7.0/gems/rubocop-rails-2.17.4/lib/rubocop/cop/rails/presence.rb:79:in `on_if'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:98:in `each'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:69:in `on_if'
/bundle/ruby/2.7.0/gems/rubocop-ast-1.23.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cop/team.rb:83:in `investigate'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:315:in `inspect_file'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:259:in `block in do_inspection_loop'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:293:in `block in iterate_until_no_changes'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:286:in `loop'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:286:in `iterate_until_no_changes'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:255:in `do_inspection_loop'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:138:in `block in file_offenses'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:163:in `file_offense_cache'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:137:in `file_offenses'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:128:in `process_file'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:109:in `block in each_inspected_file'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:108:in `each'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:108:in `reduce'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:108:in `each_inspected_file'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:94:in `inspect_files'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/runner.rb:47:in `run'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/command.rb:11:in `run'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli/environment.rb:18:in `run'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli.rb:72:in `run_command'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli.rb:79:in `execute_runners'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/lib/rubocop/cli.rb:48:in `run'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/exe/rubocop:19:in `block in <top (required)>'
/usr/local/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/bundle/ruby/2.7.0/gems/rubocop-1.37.1/exe/rubocop:19:in `<top (required)>'
/bundle/ruby/2.7.0/bin/rubocop:23:in `load'
/bundle/ruby/2.7.0/bin/rubocop:23:in `<top (required)>'
/bundle/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:58:in `load'
/bundle/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:58:in `kernel_load'
/bundle/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:23:in `run'
/bundle/gems/bundler-2.2.32/lib/bundler/cli.rb:478:in `exec'
/bundle/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/bundle/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/bundle/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/bundle/gems/bundler-2.2.32/lib/bundler/cli.rb:31:in `dispatch'
/bundle/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/bundle/gems/bundler-2.2.32/lib/bundler/cli.rb:25:in `start'
/bundle/gems/bundler-2.2.32/exe/bundle:49:in `block in <top (required)>'
/bundle/gems/bundler-2.2.32/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors'
/bundle/gems/bundler-2.2.32/exe/bundle:37:in `<top (required)>'
/bundle/bin/bundle:113:in `load'
/bundle/bin/bundle:113:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/Presence cop was inspecting /app/example.rb:3:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 2.7.5) [x86_64-linux]
Finished in 0.37355209999805083 seconds

@r7kamura r7kamura force-pushed the feature/presense-multiline-ternary branch from 3cce241 to 1bb961d Compare February 8, 2023 04:34
@koic
Copy link
Member

koic commented Feb 10, 2023

This looks good. Can you squash commits into one?

@r7kamura
Copy link
Contributor Author

OK, I'll squash them 👌

@r7kamura r7kamura force-pushed the feature/presense-multiline-ternary branch from 2ec4f7c to fc38085 Compare February 10, 2023 19:08
@koic koic merged commit 38a2ad0 into rubocop:master Feb 10, 2023
@koic
Copy link
Member

koic commented Feb 10, 2023

Thanks!

@r7kamura r7kamura deleted the feature/presense-multiline-ternary branch February 10, 2023 19:33
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

2 participants