Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Parser incorrectly handles nested sclass definitions #22

Closed
pithyless opened this Issue Apr 7, 2013 · 1 comment

Comments

Projects
None yet
2 participants

Came across an issue while using flog to parse sinatra's code base. Specifically, the culprit is: https://github.com/sinatra/sinatra/blob/62b6e1f871319089a4b09e172a8add2dbae44bd8/lib/sinatra/base.rb#L1326-1332

Here's an example to reproduce the issue.

Flog correctly finds Foo's instance method and class method with the same name. On the other hand, flog interprets everything in Bar after define_singleton to be instance methods and ends up merging the two methods into one score.

class Foo
  def foo
    42
  end

  class << self
    def foo
      42
    end
  end
end

class Bar
  def bar
    42
  end

  class << self
    def define_singleton
      (class << self; self; end).class_eval do
        "waaaaa?!?!"
      end
    end

    def bar
      42
    end
  end
end
› flog -a test.rb
    29.4: flog total
     4.9: flog/method average

    17.1: Bar::define_singleton            test.rb:19
     5.5: Foo#none
     5.5: Bar#none
     0.7: Bar#bar                          test.rb:25
     0.4: Foo::foo                         test.rb:7
     0.3: Foo#foo                          test.rb:2

If I understand correctly, the issue is that flog tracks the sclass scope as a boolean in @sclass.

Maybe it should be switched to an array that could keep track of a stack of self scopes? I can try to come up with a patch if you agree this is the right direction.

@zenspider zenspider was assigned Apr 8, 2013

Owner

zenspider commented Apr 10, 2013

Thanks!

@zenspider zenspider closed this Apr 10, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment