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

Style/RescueModifier doesn't detect offence in single-line method #2052

Merged

Conversation

urbanautomaton
Copy link
Contributor

Currently the cop Style/RescueModifier does not detect an offence for the following, which I would expect to fail:

def a_method
  test rescue nil
end

Here is output from an example file.

The issue seems to be that the method body is detected as a node of type :rescue, causing it to be ignored by this line. It's not clear to me how to fix this without breaking other test cases, though, as parser returns an identical AST for the following code, which should not register an offence:

def another_method
  test
rescue
  nil
end

In the absence of any better ideas, I've added a failing test case. Any suggestions on how to make it pass would be gratefully received. 😄

@urbanautomaton urbanautomaton changed the title Modifier rescue cop doesn't detect offence in single-line method or ensure Style/RescueModifier doesn't detect offence in single-line method Jul 19, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 19, 2015

If this AST is the same, this is likely a parser bug. //cc @whitequark

@whitequark
Copy link

Nope, not a bug. Parser ASTs are semantic, and these constructs are semantically equivalent.

@whitequark
Copy link

One thing you can do is look at the token stream, where block rescue would be :kRESCUE, but modifier rescue would be :kRESCUE_MOD.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 19, 2015

Yep, perhaps this would be best.

@urbanautomaton urbanautomaton force-pushed the hotfix/modifier-rescue-in-method branch from 4443763 to baa7770 Compare July 19, 2015 19:26
@urbanautomaton
Copy link
Contributor Author

@whitequark: Thanks, that did the trick nicely.

@bbatsov: I've implemented the fix and all the tests now pass. I had to update the HTML formatter output fixture because of a minor order change, I presume because the offence is now reported on the rescue token rather than the whole expression. If there's a way to preserve the existing order, let me know and I'll update as necessary.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 20, 2015

@urbanautomaton Looks good. Just add some test about the position highlighting and squash the commits together.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 20, 2015

Add a changelog entry as well.

@urbanautomaton urbanautomaton force-pushed the hotfix/modifier-rescue-in-method branch 3 times, most recently from 3e5e6bf to 6ee269e Compare July 20, 2015 09:21
@urbanautomaton
Copy link
Contributor Author

Done, done and done.

I assume since I've not introduced any new uncovered lines of code that the coverage decrease is either a) noise or b) because I've slightly reduced the number of covered lines.

inspect_source(cop,
'method rescue handle')
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.first.location.source).to eq('rescue')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Checkout cop.highlights. It's the preferred way to check for offence highlights.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, changed.

Because they are semantically identical, it's not possible to use the AST to
distinguish the following methods:

    def a_method
      test rescue nil
    end

    def another_method
      test
    rescue
      nil
    end

Instead, scan the token stream for `:kRESCUE_MOD` to detect rescue modifier
use.
@urbanautomaton urbanautomaton force-pushed the hotfix/modifier-rescue-in-method branch from 6ee269e to e2275ae Compare July 20, 2015 14:31
bbatsov added a commit that referenced this pull request Jul 20, 2015
…n-method

Style/RescueModifier doesn't detect offence in single-line method
@bbatsov bbatsov merged commit 90900eb into rubocop:master Jul 20, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 20, 2015

👍 Thanks!

@urbanautomaton urbanautomaton deleted the hotfix/modifier-rescue-in-method branch March 21, 2019 09:22
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.

3 participants