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 a new Style/EvalWithLocation cop #5173

Merged
merged 2 commits into from
Dec 6, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Dec 2, 2017

eval can receive a file name and a line number to display actual backtrace.

For example:

eval <<-RUBY
  raise rescue p $!.backtrace
RUBY
# => ["(eval):1:in `<main>'", "test.rb:1:in `eval'", "test.rb:1:in `<main>'"]

eval <<-RUBY, binding, __FILE__, __LINE__
  raise rescue p $!.backtrace
RUBY
# => ["test.rb:1:in `<main>'", "test.rb:1:in `eval'", "test.rb:1:in `<main>'"]

If we pass __FILE__ and __LINE__ to an eval method, an exception that occurs in the eval method has a backtrace with actual file name and line number. But sometimes we forget writing __FILE__ and __LINE__.

This cop detects this problem. This cop adds an offense for an eval method call without a location.

Note: I think we can implement auto-correction for this cop in the next step.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

module RuboCop
module Cop
module Style
# TODO
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess you forgot about this. :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🙀 I'll update it!

# RUBY
class EvalWithLocation < Cop
MSG = 'Pass `__FILE__` and `__LINE__` to `eval` method ' \
'to display actual backtrace.'.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be reworded to something mentioning "source location metadata (that's used by backtraces)."

MSG = 'Pass `__FILE__` and `__LINE__` to `eval` method ' \
'to display actual backtrace.'.freeze
MSG_INCORRECT_LINE = 'Use `%<expected>s` instead of `%<actual>s` ' \
'to display actual backtrace.'.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

@pocke pocke force-pushed the Style/EvalWithLocation branch from ffc8e1a to 39b9cf4 Compare December 3, 2017 11:06
@pocke
Copy link
Collaborator Author

pocke commented Dec 3, 2017

I added the documentation and updated the messages.

I think this should be reworded to something mentioning "source location metadata (that's used by backtraces)."

Sorry, I'm not sure what you meant.

I updated the message to Pass `__FILE__` and `__LINE__` to `eval` method. That is used by backtraces. and Use `%<expected>s` instead of `%<actual>s`. That is used by backtraces.
I think the messages should have __FILE__ and __LINE__, because users can't understand how to fix this offense without these concrete metadata names. So I omit source location metadata from the messages.
What do you think?

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 3, 2017

I'd only replace the second sentence with , as they are used by backtraces. Generally we aim to have each message be a single sentence.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 4, 2017

This has to be rebased, btw.

@pocke
Copy link
Collaborator Author

pocke commented Dec 6, 2017

I'd only replace the second sentence with , as they are used by backtraces. Generally we aim to have each message be a single sentence.

I understand. Thanks!

I'll update the messages, and rebase.

@pocke pocke force-pushed the Style/EvalWithLocation branch from 39b9cf4 to 26f56cb Compare December 6, 2017 02:57
@bbatsov bbatsov merged commit 828d058 into rubocop:master Dec 6, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 6, 2017

👍

@brandonweiss
Copy link
Contributor

brandonweiss commented Mar 6, 2018

@pocke Why does the example in the PR description pass __LINE__ directly but the comment in the cop does __LINE__ + 1?

@pocke
Copy link
Collaborator Author

pocke commented Mar 6, 2018

@pocke Why does the example in the PR description pass LINE directly but the comment in the cop does LINE + 1?

It is my mistake, __LINE__ + 1 is correct if you use a haredoc.

@brandonweiss
Copy link
Contributor

🤔 I don't think I understand why the offset is necessary?

Perhaps the reasoning/instructions should be documented and/or included in part of the offense message? I imagine people will frequently look at the documentation and include the offset when they don't need to and/or not include the offset when they do need to.

@marcandre
Copy link
Contributor

Wow, this is the first time that RuboCop actually signals a genuine error for me, thanks to this cop 🎉
Thanks @pocke 🙇‍♂️!

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.

4 participants