rubocop / rubocop-ast

RuboCop's AST extensions and NodePattern functionality
https://docs.rubocop.org/rubocop-ast
MIT License
112 stars 53 forks source link

`?` repetition causes matched nodes to be returned as an array #322

Open Earlopain opened 1 month ago

Earlopain commented 1 month ago

https://nodepattern.herokuapp.com/?p=(%0A%20%20def%20%24_method_name%0A%20%20(args)%0A%20%20(or-asgn%20%24(ivasgn%20_%20...)%20...)%3F%0A)&ruby=def%20foo%0A%20%20%40bar%20%7C%7C%3D%20baz%0Aend

grafik

The pattern is reduced from what I'm trying to write. I'd like to use OR {} but because each branch will contain different amount of captures, I write multiple ? matchers after each other. * and + can return 0-n and 1-n matches so it totally makes sense to return an array for these. + on the other hand is ony 0-1 so I'd expect this to be either be the node or nil.

Can't change this now without a major version bump of course and the solution for me is to just call flatten somewhere in the code but I wanted to see what others think. Does my "complaint" make sense?

marcandre commented 1 month ago

Does my "complaint" make sense?

It does.

Can't change this now without a major version bump of course

Well, there's a major version in the works... :-)