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 Parser::SyntaxError error with endless method for object equality #795

Closed
bkuhlmann opened this issue Apr 20, 2021 · 1 comment · Fixed by #796
Closed

Fix Parser::SyntaxError error with endless method for object equality #795

bkuhlmann opened this issue Apr 20, 2021 · 1 comment · Fixed by #796
Labels

Comments

@bkuhlmann
Copy link

bkuhlmann commented Apr 20, 2021

Overview

Discovered when attempting to write an endless method for object equality that I'd get a syntax error. Here's a snippet for illustration:

class Example
  def ==(other) = hash == other.hash
end

In this case, I'm using Reek to illustrate the stack dump in case it's helpful:

Reek Stack Dump
Inspecting 1 file(s):
        !!!
        Source '/Users/bkuhlmann/Engineering/Misc/snippet.rb' cannot be processed by Reek due to a syntax error in the source file.

        This is a problem that is outside of Reek's scope and should be fixed by you, the
        user, in order for Reek being able to continue.

        Things you can try:
        - Check the syntax of the problematic file
        - If the file is not in fact a Ruby file, exclude it in your .reek.yml file

        Exception message:

        #<Parser::SyntaxError: setter method cannot be defined in an endless method definition>

        Original backtrace:

        /Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/diagnostic/engine.rb:72:in `process'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/base.rb:274:in `diagnostic'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/ruby30.rb:27:in `endless_method_name'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/ruby30.rb:5156:in `_reduce_249'
	(eval):3:in `_racc_do_parse_c'
	(eval):3:in `do_parse'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/base.rb:190:in `parse'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/parser-3.0.1.0/lib/parser/base.rb:207:in `parse_with_comments'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/source/source_code.rb:117:in `parse'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/source/source_code.rb:53:in `syntax_tree'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:116:in `syntax_tree'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:120:in `examine_tree'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:94:in `block in run'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:104:in `wrap_exceptions'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:93:in `run'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:61:in `smells'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/examiner.rb:77:in `smelly?'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/report/progress_formatter.rb:43:in `progress'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/report/text_report.rb:22:in `add_examiner'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/cli/command/report_command.rb:26:in `block in populate_reporter_with_smells'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/cli/command/report_command.rb:25:in `each'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/cli/command/report_command.rb:25:in `populate_reporter_with_smells'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/cli/command/report_command.rb:17:in `execute'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/lib/reek/cli/application.rb:32:in `execute'
	/Users/bkuhlmann/.gem/ruby/3.0.1/gems/reek-6.0.3/bin/reek:12:in `<top (required)>'
	/Users/bkuhlmann/.gem/ruby/3.0.1/bin/reek:23:in `load'
	/Users/bkuhlmann/.gem/ruby/3.0.1/bin/reek:23:in `<main>'

        !!!

Steps to Recreate

You can recreate this issue quickly by saving this Ruby snippet to snippet.rb:

class Example
  def ==(other) = hash == other.hash
end

...and then you can run the following to recreate the error:

reek snippet.rb

Desired Behavior

I would like to be able to use an endless method since it's not assignment but equality that the parser is getting confused by.

Environment

rubocop -V
1.12.1 (using Parser 3.0.1.0, rubocop-ast 1.4.1, running on ruby 3.0.1 arm64-darwin20)
  - rubocop-performance 1.10.2
  - rubocop-rake 0.5.1
  - rubocop-rspec 2.2.0
gem list reek
reek (6.0.3)
@koic koic added the bug label Apr 20, 2021
koic added a commit to koic/parser that referenced this issue Apr 20, 2021
Fixes: whitequark#795.

This PR fixes false positives that the comparison methods recognizes as
a setter method.
koic added a commit to koic/parser that referenced this issue Apr 20, 2021
Fixes: whitequark#795.

This PR fixes false positives that the comparison methods recognizes as
a setter method.
koic added a commit to koic/parser that referenced this issue Apr 20, 2021
Fixes: whitequark#795.

This PR fixes false positives that the comparison methods recognizes as
a setter method.
koic added a commit to koic/parser that referenced this issue Apr 20, 2021
Fixes: whitequark#795.

This PR fixes false positives that non setter method ending with `=` recognizes as
a setter method.
koic added a commit to koic/parser that referenced this issue Apr 20, 2021
Fixes: whitequark#795.

This PR fixes false positives that comparison methods recognizes as
a setter method.
@bkuhlmann
Copy link
Author

Thanks! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants