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

Lint/NestedMethodDefinition does not know about instance_exec and friends #4185

Closed
buehmann opened this issue Mar 27, 2017 · 3 comments
Closed
Labels

Comments

@buehmann
Copy link
Contributor

buehmann commented Mar 27, 2017

Lint/NestedMethodDefinition allows methods to be nested in cases like these:

def works
  instance_eval do
    def bar; end
  end
  class_eval do
    def bar; end
  end
  module_eval do
    def bar; end
  end
end

On the other hand it complains about the following similar cases where the _exec variant of the _eval methods is used or a singleton class is opened via class <<:

def missing
  instance_exec do
    def bar; end
  end
  class_exec do
    def bar; end
  end
  module_exec do
    def bar; end
  end
  class << self
    def bar; end
  end
end
Inspecting 1 file
W

Offenses:

lint_nested_method_definition.rb:1:1: C: Method has too many lines. [12/10]
def missing ...
^^^^^^^^^^^
lint_nested_method_definition.rb:3:5: W: Method definitions must not be nested. Use lambda instead.
    def bar; end
    ^^^^^^^^^^^^
lint_nested_method_definition.rb:6:5: W: Method definitions must not be nested. Use lambda instead.
    def bar; end
    ^^^^^^^^^^^^
lint_nested_method_definition.rb:9:5: W: Method definitions must not be nested. Use lambda instead.
    def bar; end
    ^^^^^^^^^^^^
lint_nested_method_definition.rb:12:5: W: Method definitions must not be nested. Use lambda instead.
    def bar; end
    ^^^^^^^^^^^^

1 file inspected, 5 offenses detected

Expected behavior

I expect Lint/NestedMethodDefinition not to complain about these cases.

RuboCop version

$ rubocop -V
0.48.0 (using Parser 2.4.0.0, running on ruby 2.3.1 x86_64-darwin15)
@bbatsov bbatsov added the bug label Mar 28, 2017
Drenmi added a commit to Drenmi/rubocop that referenced this issue Mar 30, 2017
…ethods

This cop would incorrectly register an offense encountering a nested
method definition within a `#*_exec` method, which scopes the definition
to the instance, class, or module.

This change fixes that.
@buehmann
Copy link
Contributor Author

@Drenmi @bbatsov Thanks a lot for the fix. One case is not covered though (I take the blame for not including it explicitly in the title of this issue but only in the description):

class Foo
  def x(obj)
    class << obj
      def y
      end
    end
  end
end
bug.rb:4:7: W: Method definitions must not be nested. Use lambda instead.
      def y ...
      ^^^^^

Shall I open another issue for this?

@Drenmi
Copy link
Collaborator

Drenmi commented Mar 30, 2017

@buehmann: This is a bit of a cross cutting concern. I'm working on a node extension for def nodes in which I attempt to move many of the scoping checks into a single place.

Please feel free to open an issue though. The more traceability, the better. 🙂

@buehmann
Copy link
Contributor Author

Okay, thanks. Done.

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

No branches or pull requests

3 participants