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 new Lint/RequireRangeParentheses
cop
#10792
Add new Lint/RequireRangeParentheses
cop
#10792
Conversation
a263284
to
9911536
Compare
# Checks that the range is enclosed in parentheses when the end of the range is line break. | ||
# | ||
# NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`. | ||
# So, this cop does not provide autocollection because it is left to user. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autocollection -> autocorrection
# | ||
# @example | ||
# | ||
# # bad |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also add comments for the bad/good cases so it's clearer what they mean.
module RuboCop | ||
module Cop | ||
module Lint | ||
# Checks that the range is enclosed in parentheses when the end of the range is line break. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that a range literal
is at a line break
# 42 | ||
# | ||
class RequireRangeParentheses < Base | ||
MSG = 'Use parentheses in the endless range to avoid confusion about precedence.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap the endless range literal %s to avoid precedence ambiguity.
9911536
to
8aed359
Compare
I've updated this PR. Thank you! |
## Context It emulates the following Ruby warning. ```console % cat example.rb 1... 2 ``` ```consle % ruby example.rb example.rb:1: warning: ... at EOL, should be parenthesized? ``` So, this cop will detect case like rubocop#10789. ## Summary It checks that a range literal is enclosed in parentheses when the end of the range is at a line break. ### example ```ruby # bad - Represents `(1..42)`, not endless range. 1.. 42 # good - It's incompatible, but your intentions when using endless range may be: (1..) 42 # good 1..42 # good (1..42) # good (1.. 42) ``` NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`. So, this cop does not provide autocorrection because it is left to user. ```ruby case condition when 42.. do_something end ```
8aed359
to
3c96b2a
Compare
Follow up #10792. This commit suppresses the following `Lint/RequireRangeParentheses` offense. ```console % bundle exec rubocop (snip) Offenses: lib/rubocop/cop/mixin/percent_array.rb:101:13: W: Lint/RequireRangeParentheses: Wrap the endless range literal node.children[0].location.expression.end_pos... to avoid precedence ambiguity. node.children[0].location.expression.end_pos... ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected ```
Context
It emulates the following Ruby warning.
So, this cop will detect case like #10789.
Summary
It checks that a range literal is enclosed in parentheses when the end of the range is at a line break.
example
NOTE: The following is maybe intended for
(42..)
. But, compatible is42..do_something
. So, this cop does not provide autocorrection because it is left to user.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.