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

Add option to Style/SymbolProc to ignore methods with parameters #9525

Closed
stephannv opened this issue Feb 18, 2021 · 0 comments · Fixed by #9535
Closed

Add option to Style/SymbolProc to ignore methods with parameters #9525

stephannv opened this issue Feb 18, 2021 · 0 comments · Fixed by #9535

Comments

@stephannv
Copy link

stephannv commented Feb 18, 2021

Is your feature request related to a problem? Please describe.

No.

Describe the solution you'd like

My application has some code like this:

on_scheduled_jobs(worker_class_name, some_arg) do |job|
  job.delete
end

after_transition(after_commit: true, to: :deleted) do |project|
  project.clear_indexes
end

Rubocop detects offense and suggests change my code to:

on_scheduled_jobs(worker_class_name, first_arg_c, &:delete)

after_transition(after_commit: true, to: :deleted, &:clear_indexes)

But in some scenarios, using symbol as proc isn't the best option for code style because don't follow other block definitions style. I know I have some alternatives like disable Style/SymbolProc, add methods to IgnoredMethods option or use # rubocop:disable Style/SymbolProc for some cases, but I think a option to ignore methods with parameters would be nice.

Describe alternatives you've considered

Add option (maybe a EnforcedStyle??) to ignore Style/SymbolProc for methods with parameters, or option to only enable Style/SymbolProc for methods without parameters.

For example:

# rubocop.yml
Style/SymbolProc:
  IgnoreMethodsWithParameters: true
# code.rb
users.each { |u| u.send_email } # Detects offense

after_transition(to: :paid) do |payment| # Doesn`t detect offense
   payment.send_notification
end
  

Additional context

koic added a commit to koic/rubocop that referenced this issue Feb 25, 2021
…ymbolProc`

## Summary

Fixes rubocop#9525.

This PR adds `AllowMethodsWithArguments` option to `Style/SymbolProc`.

### `AllowMethodsWithArguments: false` (default)

```ruby
# bad
something.do_something(foo) { |o| o.bar }

# good
something.do_something(foo, &:bar)
```

### `AllowMethodsWithArguments: true`

```ruby
# good
something.do_something(foo) { |o| o.bar }
```

If user prefer a style that allows block for method with arguments,
the user can set `true` to `AllowMethodsWithArguments`.

## Other Information

It may be possible to consider defaulting to `AllowMethodsWithArguments: true`,
but at the time of this patch `Style/SymbolProc` cop respect keeping the existing behavior.
bbatsov pushed a commit that referenced this issue Feb 25, 2021
## Summary

Fixes #9525.

This PR adds `AllowMethodsWithArguments` option to `Style/SymbolProc`.

### `AllowMethodsWithArguments: false` (default)

```ruby
# bad
something.do_something(foo) { |o| o.bar }

# good
something.do_something(foo, &:bar)
```

### `AllowMethodsWithArguments: true`

```ruby
# good
something.do_something(foo) { |o| o.bar }
```

If user prefer a style that allows block for method with arguments,
the user can set `true` to `AllowMethodsWithArguments`.

## Other Information

It may be possible to consider defaulting to `AllowMethodsWithArguments: true`,
but at the time of this patch `Style/SymbolProc` cop respect keeping the existing behavior.
This was referenced Mar 6, 2021
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.

1 participant