-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Improve documentation for Style/EvalWithLocation cop #9405
Improve documentation for Style/EvalWithLocation cop #9405
Conversation
Add description and example code for the case that a string variable is given as a code string.
Hmm, I wonder why we'd treat this case differently. Perhaps an oversight on our part and we should treat a variable param just a string literal. @dvandersluis what do you think? |
I think we cannot know where the given string variable is defined so the cop does not check this case. |
This is true about a string that came externally, but for any other string parameter it's pretty much the same as for an explicit string literal IMO. E.g. in your example you can supply the location metadata. |
Ah, I understand. You're thinking the cop should check as much as possible. Examples which the cop should check: def eval_string
code = <<~CODE
def do_something
end
CODE
eval(code) # report offense
end
def eval_code_from_file(file)
code = File.read(file)
eval(code, file, 1)
end Examples which the cop does not check: def eval_string(s)
eval(s)
end
def eval_code
eval(@code)
end |
@taichi-ishitani I think what @bbatsov is getting at is that your examples are still calls to In other words, your def eval_string
code = <<~CODE
def do_something
end
CODE
eval(code, binding, __FILE__, __LINE__) # this line is changed
end We don't need to know what the code is doing for this cop, just that eval is called without the proper arguments. However, I'm not sure if we should autocorrect when not given a string, because the cop sets a value relative to |
Yeah, that's exactly what I was referring to. Thanks for explaining it much better than me! :D |
def eval_string
code = <<~S
foo
S
instance_eval(code, __FILE__, __LINE__)
end
eval_string We will get following back trace when we invoke the above code:
The 1st line of the back trace shows incorrect location. |
Fair enough. I think updating the docs is the simplest thing we can do right now. Thanks! |
This PR is to improve documentation for
Style/EvalWithLocation
cop.There are no description and example for the case that a string variable is given as a code string like below.
I added description and example to make this case clear.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.