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

NodePattern for numblock "count" element #156

Closed
dvandersluis opened this issue Nov 30, 2020 · 6 comments · Fixed by #157
Closed

NodePattern for numblock "count" element #156

dvandersluis opened this issue Nov 30, 2020 · 6 comments · Fixed by #157

Comments

@dvandersluis
Copy link
Member

Consider:

ruby-parse --27 -e "something { _1 }"
(numblock
  (send nil :something) 1
  (lvar :_1))

The count of numbered parameters is returned as a bare integer, which makes it extra resistant to being captured in a node pattern. It's not an int node so int or int_type? don't work, and if I try #is_a?(Integer) I get ArgumentError: wrong number of arguments (given 2, expected 1) for some reason.

Any suggestions?

@dvandersluis
Copy link
Member Author

dvandersluis commented Nov 30, 2020

#integer? works if only using numblocks, but will fail when mixed with regular blocks:

({block numblock} _recv {(args ...) #integer?})
#=> NoMethodError: undefined method `integer?' for ...

@marcandre
Copy link
Contributor

A precise example of what you are trying to achieve would help me.

You can use %Integer as a matcher, since Integer===(val) corresponds to val.is_a?(Integer)

@marcandre
Copy link
Contributor

You can also use is_a?(%Integer)

What is confusing is that Integer or int are both interpreted the same way currently, and are compiled to Integer_type? and int_type?. The former doesn't make much sense...

@dvandersluis
Copy link
Member Author

%Integer worked! Thanks!

This is the matcher, for rubocop#9127

def_node_matcher :symbol_proc?, <<~PATTERN
  ({block numblock}
    ${(send ...) (super ...) zsuper}
    ${(args (arg _)) %Integer}
    (send (lvar _var) $_))
PATTERN

@marcandre
Copy link
Contributor

Good. The question remains: should the % in front of the constant be necessary, and I'd be tempted to say no. OTOH the fact that Const or %Const would be the same is a bit ugly too...

@dvandersluis
Copy link
Member Author

I agree, allowing Const makes sense to me, even if it introduces the small ugliness.

marcandre added a commit to marcandre/rubocop-ast that referenced this issue Nov 30, 2020
marcandre added a commit to marcandre/rubocop-ast that referenced this issue Nov 30, 2020
marcandre added a commit to marcandre/rubocop-ast that referenced this issue Nov 30, 2020
…ad of predicate `#Example_type?`)

Stricter for what is an identifier:
- forbids leading `_` or capital letter
- forbids `-` anywhere

Thus are no longer accepted `_Example` or `#_foo` or `bar-baz?`

[Fixes rubocop#156]
marcandre added a commit to marcandre/rubocop-ast that referenced this issue Nov 30, 2020
…ad of predicate `#Example_type?`)

Stricter for what is an identifier:
- forbids leading `_` or capital letter
- forbids `-` anywhere

Thus are no longer accepted `_Example` or `#_foo` or `bar-baz?`

[Fixes rubocop#156]
marcandre added a commit that referenced this issue Dec 1, 2020
…ad of predicate `#Example_type?`)

Stricter for what is an identifier:
- forbids leading `_` or capital letter
- forbids `-` anywhere

Thus are no longer accepted `_Example` or `#_foo` or `bar-baz?`

[Fixes #156]
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

Successfully merging a pull request may close this issue.

2 participants