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

Allow braced numeric block calls in Style/MethodCallWithArgsParentheses #9854

Merged

Conversation

gsamokovarov
Copy link
Contributor

This is yet another fix for the omit_parentheses style of the
Style/MethodCallWithArgsParentheses cop. We're issuing offenses for
braced numblock calls:

updated_ids = Set.new(items_to_update) { _1.remote_id.to_s }
                     ^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.

Removing the parens leads to code that doesn't compile so this is not good.

Numeric block have their own AST node type:

-> ruby-parse -e "Set.new(items_to_update) { _1.remote_id }"
(numblock
  (send
    (const nil :Set) :new
    (send nil :items_to_update)) 1
  (send
    (lvar :_1) :remote_id))

vs

-> ruby-parse -e "Set.new(items_to_update) { |it| it.remote_id }"
(block
  (send
    (const nil :Set) :new
    (send nil :items_to_update))
  (args
    (procarg0
      (arg :it)))
  (send
    (lvar :it) :remote_id))

Send nodes can be queried for their block if given with #block_node,
however, the numblocks don't get recognized as such. Our check for braced
block calls used to rely on it:

https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node/mixin/method_dispatch_node.rb#L35-L37
https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node/mixin/method_dispatch_node.rb#L156-L158

I'm changing the implementation to work around this issue and recognize
numblock braced calls, but we may consider moving the fix to
rubocop-ast as well.

@gsamokovarov gsamokovarov force-pushed the omit-parentheses-braced-numblocks branch from acfbeef to ed9f5d2 Compare June 5, 2021 13:56
@gsamokovarov gsamokovarov force-pushed the omit-parentheses-braced-numblocks branch from bd5c6b2 to 336ce11 Compare June 5, 2021 18:46
This is yet another fix for the `omit_parentheses` style of the
`Style/MethodCallWithArgsParentheses` cop. We're issuing offenses for
braced `numblock` calls:

```ruby
updated_ids = Set.new(items_to_update) { _1.remote_id.to_s }
                     ^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
```

Numeric block have their own AST node type:

```
-> ruby-parse -e "Set.new(items_to_update) { _1.remote_id }"
(numblock
  (send
    (const nil :Set) :new
    (send nil :items_to_update)) 1
  (send
    (lvar :_1) :remote_id))
```

vs

```
-> ruby-parse -e "Set.new(items_to_update) { |it| it.remote_id }"
(block
  (send
    (const nil :Set) :new
    (send nil :items_to_update))
  (args
    (procarg0
      (arg :it)))
  (send
    (lvar :it) :remote_id))
```

Send nodes can be queried for their block if given with `#block_node`,
however, the `numblock`s don't get recognized as such:

https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node/mixin/method_dispatch_node.rb#L35-L37
https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node/mixin/method_dispatch_node.rb#L156-L158

I'm changing the implementation to work around this issue and recognize
`numblock` braced calls, but we may consider moving the fix to
`rubocop-ast` as well.
@gsamokovarov gsamokovarov force-pushed the omit-parentheses-braced-numblocks branch from 336ce11 to 3de2a7b Compare June 5, 2021 18:46
@koic koic merged commit 721cbf0 into rubocop:master Jun 5, 2021
@koic
Copy link
Member

koic commented Jun 5, 2021

Thank you, Genadi!

@gsamokovarov gsamokovarov deleted the omit-parentheses-braced-numblocks branch April 20, 2023 09:58
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 this pull request may close these issues.

None yet

3 participants