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

[WIP] New cop Rails/SafeNavigationOperator #2545

Closed
wants to merge 2 commits into from

Conversation

syndbg
Copy link
Contributor

@syndbg syndbg commented Dec 28, 2015

Addresses

#2428

Changes

Checks for the use of #try and instead encourages
changing it to Ruby 2.3's safe navigation operator - *..

The cop is enabled by default.

E.g

# bad
obj.try! :id
# good
obj&.id

Features (TODOs):

  • Lint #try!.
  • Autocorrect #try! methods to &..
  • Add option to lint #try.
  • Add autocorrection for #try.

Checks for the use of `#try` and instead encourages
changing it to Ruby 2.3's safe navigation operator - `*.`.

The cop is enabled by default.

E.g

```ruby
obj.try! :id
obj&.id
```
context "with method #{method}" do
context 'and with a receiver' do
it 'registers an offense for an invocation with args' do
inspect_source(cop, "obj.#{method}! :id")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ! should not be here.

# Checks for use of `#try` or `#try!`.
class SafeNavigationOperator < Cop
MSG = 'Prefer `&.` over `ActiveSupport#try!`.'
INVALID_METHODS = [:try!]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this TARGET_METHODS and freeze the array.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 3, 2016

@syndbg ping

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 3, 2016

One more thing - the new cop should make use of the functionality @alexdowad just added about target Ruby versions.

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 10, 2016

I'll close this for now. Feel free to reopen it if you make any progress with it.

@gy
Copy link

gy commented Aug 3, 2016

@syndbg any plans to work on this soon or should I pick it up?

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 3, 2016

@gy take it. @syndbg has been pretty busy lately (we know each other in real life).

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.

6 participants