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
Rubocop Issue: Omit parentheses for ternary conditions #4118
Comments
I actually believe that this is already happening in your code based on the example provided. The reason that RuboCop is registering an offense here is that the Ruby code is being parsed as the ternary statement being the argument to [1] pry(#<RuboCop::Cop::Style::TernaryParentheses>)> node
=> s(:if,
s(:begin,
s(:send, nil, :error_zip_file)),
s(:send, nil, :path,
s(:send, nil, :error_zip_file)),
s(:nil))
[2] pry(#<RuboCop::Cop::Style::TernaryParentheses>)> node.source
=> "(error_zip_file) ? path(error_zip_file) : nil"
[3] pry(#<RuboCop::Cop::Style::TernaryParentheses>)> node.parent
=> s(:send,
s(:const, nil, :File), :exist?,
s(:if,
s(:begin,
s(:send, nil, :error_zip_file)),
s(:send, nil, :path,
s(:send, nil, :error_zip_file)),
s(:nil)))
[4] pry(#<RuboCop::Cop::Style::TernaryParentheses>)> node.parent.source
=> "File.exist? (error_zip_file) ? path(error_zip_file) : nil"
This is kind of already happening from rrosenblum@C02NV0KSG3QN:~/work/rubocop (master u=)$ cat test.rb
# frozen_string_literal: true
error_zip_file = 'testfile/path'
File.exist? (error_zip_file) ? path(error_zip_file) : nil
rrosenblum@C02NV0KSG3QN:~/work/rubocop (master u=)$ b rubocop -D test.rb
Inspecting 1 file
W
Offenses:
test.rb:4:12: W: Lint/ParenthesesAsGroupedExpression: (...) interpreted as grouped expression.
File.exist? (error_zip_file) ? path(error_zip_file) : nil
^
test.rb:4:13: C: Style/RedundantParentheses: Don't use parentheses around a variable.
File.exist? (error_zip_file) ? path(error_zip_file) : nil
^^^^^^^^^^^^^^^^
test.rb:4:13: C: Style/TernaryParentheses: Omit parentheses for ternary conditions.
File.exist? (error_zip_file) ? path(error_zip_file) : nil
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 file inspected, 3 offenses detected |
Hm. It's very hard for RuboCop to know the programmer made a mistake here, because they may just as well have meant: File.exist? (error_zip_file) ? path(error_zip_file) : nil to do what it does. One thing that can be done is enable the Possibly we could add a |
@rrosenblum Whatever you mentioned in your comment is fine. But what about the second part of my question where |
@irfu572 in Hopefully this simplification is informative: def file_exist?(arg)
"arg = #{arg.inspect}"
end
file_exist? 'error_zip_file' ? 'path_to_error_zip_file' : nil
#=> arg = "path_to_error_zip_file"
file_exist? ('error_zip_file') ? 'path_to_error_zip_file' : nil
#=> arg = "path_to_error_zip_file"
file_exist?('error_zip_file') ? 'path_to_error_zip_file' : nil
#=> arg = "error_zip_file" Best practice in this case is to use parentheses for the I hope that helps. Let me know if I am unclear. |
@mikegee Thats exactly what I am saying. Rubocop should notify us right when we are unknowingly making this mistake. Ideal Solution for this would be, as you said, |
RuboCop has not added a cop that will check for parentheses because the rules are too vague to handle a generic solution. Checkout the style guide recommendations for parenthesis.
There are too many DSLs to be able to know what methods should or shouldn't have parentheses. Unfortunately, there isn't much that we can do here. For your purpose |
There is a wrong validation in rubocop where you have a function in ternary condition which accepts single param.
Ex:-
error_zip_file = 'testfile/path'
File.exist? (error_zip_file) ? path(error_zip_file) : nil
Expected behavior
It should not show errors for this statement.
Or
It should ask the coder to remove the space between 'exist?' and '(error_zipfile)' with the braces ON.
Actual behavior
It is showing invalid errors for this statement.
Errors:
1). Don't use parentheses around a variable.
2). Omit parentheses for ternary conditions.
Steps to reproduce the problem
error_zip_file = 'testfile/path'
File.exist? (error_zip_file) ? path(error_zip_file) : nil
Run rubocop for the above code.
Expected Output: it should return the path of error_zip_file if its present
If we change the code as per errors then the result will be wrong.
Fixed Code as per Rubocop:
File.exist? error_zip_file ? path(error_zip_file) : nil
with this code, ruby will actually send the rest of the code to File.exist? as a single param.
File.exist? (error_zip_file ? path(error_zip_file) : nil)
--- WrongNow the result will be true or false --- Wrong.
RuboCop version
The text was updated successfully, but these errors were encountered: