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

[Fixes #9760] Make RangeHelp#range_with_surrounding_space params consistent #10727

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9760](https://github.com/rubocop/rubocop/issues/9760): (Breaking) Change RangeHelp#range_with_surrounding_space to accept range as a positional argument. ([@pirj][])
Expand Up @@ -41,7 +41,7 @@ def correct_same_line_brace(corrector)
end

def correct_next_line_brace(corrector)
corrector.remove(range_with_surrounding_space(range: node.loc.end, side: :left))
corrector.remove(range_with_surrounding_space(node.loc.end, side: :left))

corrector.insert_before(
last_element_range_with_trailing_comma(node).end,
Expand All @@ -51,7 +51,7 @@ def correct_next_line_brace(corrector)

def content_if_comment_present(corrector, node)
range = range_with_surrounding_space(
range: children(node).last.source_range,
children(node).last.source_range,
side: :right
).end.resize(1)
if range.source == '#'
Expand Down Expand Up @@ -86,7 +86,7 @@ def last_element_range_with_trailing_comma(node)

def last_element_trailing_comma_range(node)
range = range_with_surrounding_space(
range: children(node).last.source_range,
children(node).last.source_range,
side: :right
).end.resize(1)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/unused_arg_corrector.rb
Expand Up @@ -29,7 +29,7 @@ def correct(corrector, processed_source, node)
end

def correct_for_blockarg_type(corrector, node)
range = range_with_surrounding_space(range: node.source_range, side: :left)
range = range_with_surrounding_space(node.source_range, side: :left)
range = range_with_surrounding_comma(range, :left)

corrector.remove(range)
Expand Down
6 changes: 1 addition & 5 deletions lib/rubocop/cop/internal_affairs/node_matcher_directive.rb
Expand Up @@ -99,11 +99,7 @@ def insert_directive(corrector, node, actual_name)
# If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
arguments = pattern_arguments(node.arguments[1].source)

range = range_with_surrounding_space(
range: node.loc.expression,
side: :left,
newlines: false
)
range = range_with_surrounding_space(node.loc.expression, side: :left, newlines: false)
indentation = range.source.match(/^\s*/)[0]
directive = "#{indentation}# @!method #{actual_name}(#{arguments.join(', ')})\n"
directive = "\n#{directive}" if add_newline?(node)
Expand Down
Expand Up @@ -43,7 +43,7 @@ def on_send(node)
private

def offending_range(node)
with_space = range_with_surrounding_space(range: node.loc.expression)
with_space = range_with_surrounding_space(node.loc.expression)

range_with_surrounding_comma(with_space, :left)
end
Expand Down
Expand Up @@ -56,7 +56,7 @@ def on_send(node)
private

def offending_range(node)
with_space = range_with_surrounding_space(range: node.loc.expression)
with_space = range_with_surrounding_space(node.loc.expression)

range_with_surrounding_comma(with_space, :left)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/empty_comment.rb
Expand Up @@ -97,7 +97,7 @@ def investigate(comments)
def autocorrect(corrector, node)
previous_token = previous_token(node)
range = if previous_token && same_line?(node, previous_token)
range_with_surrounding_space(range: node.loc.expression, newlines: false)
range_with_surrounding_space(node.loc.expression, newlines: false)
else
range_by_whole_lines(node.loc.expression, include_final_newline: true)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/initial_indentation.rb
Expand Up @@ -41,7 +41,7 @@ def space_before(token)
return unless token
return if token.column.zero?

space_range = range_with_surrounding_space(range: token.pos, side: :left, newlines: false)
space_range = range_with_surrounding_space(token.pos, side: :left, newlines: false)
# If the file starts with a byte order mark (BOM), the column can be
# non-zero, but then we find out here if there's no space to the left
# of the first token.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/multiline_block_layout.rb
Expand Up @@ -117,7 +117,7 @@ def autocorrect(corrector, node)

def autocorrect_arguments(corrector, node)
end_pos = range_with_surrounding_space(
range: node.arguments.source_range,
node.arguments.source_range,
side: :right,
newlines: false
).end_pos
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_around_block_parameters.rb
Expand Up @@ -127,7 +127,7 @@ def check_arg(arg)

expr = arg.source_range
check_no_space(
range_with_surrounding_space(range: expr, side: :left).begin_pos,
range_with_surrounding_space(expr, side: :left).begin_pos,
expr.begin_pos - 1,
'Extra space before'
)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_around_operators.rb
Expand Up @@ -161,7 +161,7 @@ def operator_with_regular_syntax?(send_node)
end

def check_operator(type, operator, right_operand)
with_space = range_with_surrounding_space(range: operator)
with_space = range_with_surrounding_space(operator)
return if with_space.source.start_with?("\n")

offense(type, operator, with_space, right_operand) do |msg|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_before_block_braces.rb
Expand Up @@ -65,7 +65,7 @@ def on_block(node)
return if conflict_with_block_delimiters?(node)

left_brace = node.loc.begin
space_plus_brace = range_with_surrounding_space(range: left_brace)
space_plus_brace = range_with_surrounding_space(left_brace)
used_style =
space_plus_brace.source.start_with?('{') ? :no_space : :space

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_before_first_arg.rb
Expand Up @@ -36,7 +36,7 @@ def on_send(node)
return unless regular_method_call_with_arguments?(node)

first_arg = node.first_argument.source_range
first_arg_with_space = range_with_surrounding_space(range: first_arg, side: :left)
first_arg_with_space = range_with_surrounding_space(first_arg, side: :left)
space = range_between(first_arg_with_space.begin_pos, first_arg.begin_pos)
return if space.length == 1
return unless expect_params_after_method_name?(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_inside_block_braces.rb
Expand Up @@ -185,7 +185,7 @@ def space_inside_left_brace(left_brace, args_delimiter)
'Space between { and | detected.')
end
else
brace_with_space = range_with_surrounding_space(range: left_brace, side: :right)
brace_with_space = range_with_surrounding_space(left_brace, side: :right)
space(brace_with_space.begin_pos + 1, brace_with_space.end_pos,
'Space inside { detected.')
end
Expand All @@ -196,7 +196,7 @@ def pipe?(args_delimiter)
end

def space_inside_right_brace(right_brace)
brace_with_space = range_with_surrounding_space(range: right_brace, side: :left)
brace_with_space = range_with_surrounding_space(right_brace, side: :left)
space(brace_with_space.begin_pos, brace_with_space.end_pos - 1,
'Space inside } detected.')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/trailing_whitespace.rb
Expand Up @@ -89,7 +89,7 @@ def whitespace_is_indentation?(range, level)
end

def whitespace_only?(range)
source = range_with_surrounding_space(range: range).source
source = range_with_surrounding_space(range).source
source.start_with?("\n") && source.end_with?("\n")
end

Expand Down
10 changes: 5 additions & 5 deletions lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
Expand Up @@ -71,16 +71,16 @@ def comment_range_with_surrounding_space(directive_comment_range, line_comment_r
processed_source.comment_config.comment_only_line?(directive_comment_range.line) &&
directive_comment_range.begin_pos == line_comment_range.begin_pos
# When the previous line is blank, it should be retained
range_with_surrounding_space(range: directive_comment_range, side: :right)
range_with_surrounding_space(directive_comment_range, side: :right)
else
# Eat the entire comment, the preceding space, and the preceding
# newline if there is one.
original_begin = directive_comment_range.begin_pos
range = range_with_surrounding_space(
range: directive_comment_range, side: :left, newlines: true
directive_comment_range, side: :left, newlines: true
)

range_with_surrounding_space(range: range,
range_with_surrounding_space(range,
side: :right,
# Special for a comment that
# begins the file: remove
Expand All @@ -94,13 +94,13 @@ def directive_range_in_list(range, ranges)
# is NOT being removed?
if ends_its_line?(ranges.last) && trailing_range?(ranges, range)
# Eat the comma on the left.
range = range_with_surrounding_space(range: range, side: :left)
range = range_with_surrounding_space(range, side: :left)
range = range_with_surrounding_comma(range, :left)
end

range = range_with_surrounding_comma(range, :right)
# Eat following spaces up to EOL, but not the newline itself.
range_with_surrounding_space(range: range, side: :right, newlines: false)
range_with_surrounding_space(range, side: :right, newlines: false)
end

def each_redundant_disable(&block)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/redundant_cop_enable_directive.rb
Expand Up @@ -60,7 +60,7 @@ def register_offense(comment, cop_names)
message: format(MSG, cop: all_or_name(name))
) do |corrector|
if directive.match?(cop_names)
corrector.remove(range_with_surrounding_space(range: directive.range, side: :right))
corrector.remove(range_with_surrounding_space(directive.range, side: :right))
else
corrector.remove(range_with_comma(comment, name))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/redundant_require_statement.rb
Expand Up @@ -41,7 +41,7 @@ def on_send(node)
return unless unnecessary_require_statement?(node)

add_offense(node) do |corrector|
range = range_with_surrounding_space(range: node.loc.expression, side: :right)
range = range_with_surrounding_space(node.loc.expression, side: :right)

corrector.remove(range)
end
Expand Down
Expand Up @@ -45,7 +45,7 @@ def on_send(node)

def trailing_comma_range(node)
range_with_surrounding_space(
range: node.arguments[-2].source_range,
node.arguments[-2].source_range,
side: :right
).end.resize(1)
end
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/mixin/range_help.rb
Expand Up @@ -51,7 +51,9 @@ def range_with_surrounding_comma(range, side = :both)
Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

def range_with_surrounding_space(range:, side: :both,
NOT_GIVEN = Module.new
def range_with_surrounding_space(range = NOT_GIVEN,
side: :both,
newlines: true, whitespace: false,
continuations: false)
buffer = @processed_source.buffer
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/accessor_grouping.rb
Expand Up @@ -72,7 +72,7 @@ def autocorrect(corrector, node)
if (preferred_accessors = preferred_accessors(node))
corrector.replace(node, preferred_accessors)
else
range = range_with_surrounding_space(range: node.loc.expression, side: :left)
range = range_with_surrounding_space(node.loc.expression, side: :left)
corrector.remove(range)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/arguments_forwarding.rb
Expand Up @@ -133,7 +133,7 @@ def register_offense_to_forwarding_method_arguments(forwarding_method)
def register_offense_to_method_definition_arguments(method_definition)
add_offense(arguments_range(method_definition)) do |corrector|
arguments_range = range_with_surrounding_space(
range: method_definition.arguments.source_range, side: :left
method_definition.arguments.source_range, side: :left
)
corrector.replace(arguments_range, '(...)')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/block_delimiters.rb
Expand Up @@ -281,7 +281,7 @@ def whitespace_after?(range, length = 1)
def move_comment_before_block(corrector, comment, block_node, closing_brace)
range = block_node.chained? ? end_of_chain(block_node.parent).source_range : closing_brace
comment_range = range_between(range.end_pos, comment.loc.expression.end_pos)
corrector.remove(range_with_surrounding_space(range: comment_range, side: :right))
corrector.remove(range_with_surrounding_space(comment_range, side: :right))
corrector.insert_after(range, "\n")

corrector.insert_before(block_node, "#{comment.text}\n")
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/commented_keyword.rb
Expand Up @@ -66,7 +66,7 @@ def on_new_investigation

def register_offense(comment, matched_keyword)
add_offense(comment, message: format(MSG, keyword: matched_keyword)) do |corrector|
range = range_with_surrounding_space(range: comment.loc.expression, newlines: false)
range = range_with_surrounding_space(comment.loc.expression, newlines: false)
corrector.remove(range)

unless matched_keyword == 'end'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/encoding.rb
Expand Up @@ -51,7 +51,7 @@ def register_offense(line_number, comment)
text = comment.without(:encoding)

if text.blank?
corrector.remove(range_with_surrounding_space(range: range, side: :right))
corrector.remove(range_with_surrounding_space(range, side: :right))
else
corrector.replace(range, text)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/frozen_string_literal_comment.rb
Expand Up @@ -182,7 +182,7 @@ def disabled_offense(processed_source)
end

def remove_comment(corrector, node)
corrector.remove(range_with_surrounding_space(range: node.pos, side: :right))
corrector.remove(range_with_surrounding_space(node.pos, side: :right))
end

def enable_comment(corrector)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_as_last_array_item.rb
Expand Up @@ -87,7 +87,7 @@ def braces_style?

def remove_last_element_trailing_comma(corrector, node)
range = range_with_surrounding_space(
range: node.children.last.source_range,
node.children.last.source_range,
side: :right
).end.resize(1)

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/hash_syntax.rb
Expand Up @@ -223,7 +223,7 @@ def range_for_autocorrect_ruby19(pair_node)
operator = pair_node.loc.operator

range = key.join(operator)
range_with_surrounding_space(range: range, side: :right)
range_with_surrounding_space(range, side: :right)
end

def argument_without_space?(node)
Expand All @@ -235,7 +235,7 @@ def autocorrect_hash_rockets(corrector, pair_node)

key_with_hash_rocket = ":#{pair_node.key.source}#{pair_node.inverse_delimiter(true)}"
corrector.replace(pair_node.key, key_with_hash_rocket)
corrector.remove(range_with_surrounding_space(range: op))
corrector.remove(range_with_surrounding_space(op))
end

def autocorrect_no_mixed_keys(corrector, pair_node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/keyword_parameters_order.rb
Expand Up @@ -65,7 +65,7 @@ def append_newline_to_last_kwoptarg(arguments, corrector)

def remove_kwargs(kwarg_nodes, corrector)
kwarg_nodes.each do |kwarg|
with_space = range_with_surrounding_space(range: kwarg.source_range)
with_space = range_with_surrounding_space(kwarg.source_range)
corrector.remove(range_with_surrounding_comma(with_space, :left))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/line_end_concatenation.rb
Expand Up @@ -70,7 +70,7 @@ def check_token_set(index)

def autocorrect(corrector, operator_range)
# Include any trailing whitespace so we don't create a syntax error.
operator_range = range_with_surrounding_space(range: operator_range,
operator_range = range_with_surrounding_space(operator_range,
side: :right,
newlines: false)
one_more_char = operator_range.resize(operator_range.size + 1)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/map_to_hash.rb
Expand Up @@ -59,7 +59,7 @@ def on_send(node)
def autocorrect(corrector, to_h, map)
removal_range = range_between(to_h.loc.dot.begin_pos, to_h.loc.selector.end_pos)

corrector.remove(range_with_surrounding_space(range: removal_range, side: :left))
corrector.remove(range_with_surrounding_space(removal_range, side: :left))
corrector.replace(map.loc.selector, 'to_h')
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/multiline_if_then.rb
Expand Up @@ -29,7 +29,7 @@ def on_normal_if_unless(node)
return unless non_modifier_then?(node)

add_offense(node.loc.begin, message: format(MSG, keyword: node.keyword)) do |corrector|
corrector.remove(range_with_surrounding_space(range: node.loc.begin, side: :left))
corrector.remove(range_with_surrounding_space(node.loc.begin, side: :left))
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/style/multiline_in_pattern_then.rb
Expand Up @@ -41,9 +41,7 @@ def on_in_pattern(node)

range = node.loc.begin
add_offense(range) do |corrector|
corrector.remove(
range_with_surrounding_space(range: range, side: :left, newlines: false)
)
corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/multiline_method_signature.rb
Expand Up @@ -59,7 +59,7 @@ def arguments_range(node)
node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
)

range_with_surrounding_space(range: range, side: :left)
range_with_surrounding_space(range, side: :left)
end

def opening_line(node)
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/style/multiline_when_then.rb
Expand Up @@ -39,9 +39,7 @@ def on_when(node)

range = node.loc.begin
add_offense(range) do |corrector|
corrector.remove(
range_with_surrounding_space(range: range, side: :left, newlines: false)
)
corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/nested_parenthesized_calls.rb
Expand Up @@ -44,7 +44,7 @@ def autocorrect(corrector, nested)
last_arg = nested.last_argument.source_range

leading_space =
range_with_surrounding_space(range: first_arg.begin,
range_with_surrounding_space(first_arg.begin,
side: :left,
whitespace: true,
continuations: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/nil_lambda.rb
Expand Up @@ -57,7 +57,7 @@ def on_block(node)

def autocorrect(corrector, node)
range = if node.single_line?
range_with_surrounding_space(range: node.body.loc.expression)
range_with_surrounding_space(node.body.loc.expression)
else
range_by_whole_lines(node.body.loc.expression, include_final_newline: true)
end
Expand Down