whitesmith / whitesmith/rubycritic

Parsing of class fails if class name is defined based on condition.

Open
#260 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
3.5k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

RubyCritic 3.3.0
Ruby 2.3.1

When running rubycritic on such a module, it fails with with the following exception 'Reek::Errors::IncomprehensibleSourceError'.

module ExceptionHandler
    # => Class (inheritance dependent on whether db option is available)
    self::Exception = Class.new(
      (ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object
    ) do
      #Some code
    end
end

It seems that the ternary operator in the class name definition is causing this. Because if we change the code to:

module ExceptionHandler
    # => Class (inheritance dependent on whether db option is available)
    foo =(ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object
    self::Exception = Class.new(foo) do
      #Some code
    end
  end

there is no exception.

The exception happens on examiner.rb:107:in `rescue in run': !!! (Reek::Errors::IncomprehensibleSourceError)

Exception message:
undefined method `name' for #<#<Class:0x007f82aa196e58>:0x007f82aa1957d8>

I stumbled upon this exception when trying to run rubycritic on a project with the exception_handler GEM. The code in the above example is generated automatically by their GEM.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at examiner.rb:107 and reproduce the IncomprehensibleSourceError with the conditional class definition shown in the issue. Trace how the parser handles the ternary expression in the class name; done means RubyCritic processes that example without raising the undefined-method name exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.