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

Request: allow method calls on constants #144

Closed
marcandre opened this issue Oct 26, 2020 · 5 comments
Closed

Request: allow method calls on constants #144

marcandre opened this issue Oct 26, 2020 · 5 comments

Comments

@marcandre
Copy link
Contributor

Currently methods & predicates can be called on self:

(_ #predicate?(:arg, %1) ...)
(_ #method(:arg, %1) ...)

Request is to allow calling them on constants:

(_ #Foo.predicate?(:arg, %1) ...)
(_ #Foo::Bar.method(:arg, %1) ...)
(_ #::Global::X.method(:arg, %1) ...)
@pirj
Copy link
Member

pirj commented Oct 26, 2020

Just for the reference, the idea was to allow usage of #rspec.example_groups.regular call chains, for namespacing purposes. Since this would only create complication in NP parser, and since Ruby has namespacing capabilities built-in, I completely support RSpec::ExampleGroups.regular syntax instead.

Just to make sure, is it correct that given:

module A
  module B
    def self.a_method
       # ...
    end
  end
end

module RuboCop
  module Cop
    class SomeCop < Base
      include A
      def_node_matcher :a, '( #B.a_method ...)'
    end
  end
end

would be able to call A::B.a_method?

It would be a nice way to structure configurable DSL helpers.

@marcandre
Copy link
Contributor Author

Just for the reference, the idea was to allow usage of #rspec.example_groups.regular call chains, for namespacing purposes. Since this would only create complication in NP parser

I don't think it would be difficult tbh. I just wonder what is the best to have, the most useful in general. Maybe even both forms...

and since Ruby has namespacing capabilities built-in, I completely support RSpec::ExampleGroups.regular syntax instead.

Just to make sure, is it correct that given:

module A
  module B
    def self.a_method
       # ...
    end
  end
end

module RuboCop
  module Cop
    class SomeCop < Base
      include A
      def_node_matcher :a, '( #B.a_method ...)'
    end
  end
end

would be able to call A::B.a_method?

Yes.

So the question is: should I implement both, or only one (and which one) 😅

@pirj
Copy link
Member

pirj commented Oct 27, 2020

If the namespace is relative, i.e. you don't have to specify #A::B.a_method when A is included, I'd go with constants, no method call chaining.

marcandre added a commit to marcandre/rubocop-ast that referenced this issue Oct 27, 2020
marcandre added a commit to marcandre/rubocop-ast that referenced this issue Oct 27, 2020
@marcandre
Copy link
Contributor Author

Released as v1.1.0

@pirj
Copy link
Member

pirj commented Oct 27, 2020

@marcandre You're like a ⚡-man! 💯
This is just in time for the 2.0 RuboCop-RSpec release, thanks a lot!

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

No branches or pull requests

2 participants