From 07f414474d7169bd673ae5893e3e8a99e469b2dc Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 9 Apr 2020 19:29:05 -0400 Subject: [PATCH 1/2] Have correctors accept nodes in addition to ranges. Provide better errors in case of wrong argument Before: "undefined method `source_buffer' for 1..3:Range" After: "Expected a Parser::Source::Range or Rubocop::AST::Node, got Range" --- CHANGELOG.md | 1 + lib/rubocop/cop/corrector.rb | 68 ++++++++++++++++++------------ manual/development.md | 8 ++-- spec/rubocop/cop/corrector_spec.rb | 19 +++++++++ 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2617369153..04bfe6878f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### New features +* [#7863](https://github.com/rubocop-hq/rubocop/issues/7863): Corrector now accepts nodes in addition to ranges. ([@marcandre][]) * [#7862](https://github.com/rubocop-hq/rubocop/issues/7862): Corrector now has a `wrap` method. ([@marcandre][]) * [#7850](https://github.com/rubocop-hq/rubocop/issues/7850): Make it possible to enable/disable pending cops. ([@koic][]) * [#7861](https://github.com/rubocop-hq/rubocop/issues/7861): Make it to allow `Style/CaseEquality` when the receiver is a constant. ([@rafaelfranca][]) diff --git a/lib/rubocop/cop/corrector.rb b/lib/rubocop/cop/corrector.rb index 7ea0d79a5be..84243ec21d1 100644 --- a/lib/rubocop/cop/corrector.rb +++ b/lib/rubocop/cop/corrector.rb @@ -72,18 +72,18 @@ def rewrite # Removes the source range. # - # @param [Parser::Source::Range] range - def remove(range) - validate_range range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node + def remove(node_or_range) + range = to_range(node_or_range) @source_rewriter.remove(range) end # Inserts new code before the given source range. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [String] content - def insert_before(range, content) - validate_range range + def insert_before(node_or_range, content) + range = to_range(node_or_range) # TODO: Fix Cops using bad ranges instead if range.end_pos > @source_buffer.source.size range = range.with(end_pos: @source_buffer.source.size) @@ -94,38 +94,38 @@ def insert_before(range, content) # Inserts new code after the given source range. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [String] content - def insert_after(range, content) - validate_range range + def insert_after(node_or_range, content) + range = to_range(node_or_range) @source_rewriter.insert_after(range, content) end # Wraps the given source range with the given before and after texts # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [String] before # @param [String] after - def wrap(range, before, after) - validate_range range + def wrap(node_or_range, before, after) + range = to_range(node_or_range) @source_rewriter.wrap(range, before, after) end # Replaces the code of the source range `range` with `content`. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [String] content - def replace(range, content) - validate_range range + def replace(node_or_range, content) + range = to_range(node_or_range) @source_rewriter.replace(range, content) end # Removes `size` characters prior to the source range. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [Integer] size - def remove_preceding(range, size) - validate_range range + def remove_preceding(node_or_range, size) + range = to_range(node_or_range) to_remove = Parser::Source::Range.new(range.source_buffer, range.begin_pos - size, range.begin_pos) @@ -136,10 +136,10 @@ def remove_preceding(range, size) # If `size` is greater than the size of `range`, the removed region can # overrun the end of `range`. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [Integer] size - def remove_leading(range, size) - validate_range range + def remove_leading(node_or_range, size) + range = to_range(node_or_range) to_remove = Parser::Source::Range.new(range.source_buffer, range.begin_pos, range.begin_pos + size) @@ -150,10 +150,10 @@ def remove_leading(range, size) # If `size` is greater than the size of `range`, the removed region can # overrun the beginning of `range`. # - # @param [Parser::Source::Range] range + # @param [Parser::Source::Range, Rubocop::AST::Node] range or node # @param [Integer] size - def remove_trailing(range, size) - validate_range range + def remove_trailing(node_or_range, size) + range = to_range(node_or_range) to_remove = Parser::Source::Range.new(range.source_buffer, range.end_pos - size, range.end_pos) @@ -163,11 +163,25 @@ def remove_trailing(range, size) private # :nodoc: - def validate_range(range) - buffer = range.source_buffer + def to_range(node_or_range) + range = case node_or_range + when ::RuboCop::AST::Node, ::Parser::Source::Comment + node_or_range.loc.expression + when ::Parser::Source::Range + node_or_range + else + raise TypeError, + 'Expected a Parser::Source::Range, Comment or ' \ + "Rubocop::AST::Node, got #{node_or_range.class}" + end + validate_buffer(range.source_buffer) + range + end + + def validate_buffer(buffer) return if buffer == @source_buffer - unless buffer.is_a?(Parser::Source::Buffer) + unless buffer.is_a?(::Parser::Source::Buffer) # actually this should be enforced by parser gem raise 'Corrector expected range source buffer to be a ' \ "Parser::Source::Buffer, but got #{buffer.class}" diff --git a/manual/development.md b/manual/development.md index fae17ddbb22..8fb3b7ce86d 100644 --- a/manual/development.md +++ b/manual/development.md @@ -256,15 +256,15 @@ And then define the `autocorrect` method on the cop side: def autocorrect(node) lambda do |corrector| internal_expression = node.children[0].children[0].source - corrector.replace(node.loc.expression, "#{internal_expression}.any?") + corrector.replace(node, "#{internal_expression}.any?") end end ``` The corrector allows you to `insert_after` and `insert_before` or -`replace` in a specific range of the code. +`replace` a specific node or in any specific range of the code. -The range can be determined on `node.location` where it brings specific +Range can be determined on `node.location` where it brings specific ranges for expression or other internal information that the node holds. ### Configuration @@ -290,7 +290,7 @@ def autocorrect(node) internal_expression = node.children[0].children[0].source replacement = cop_config['ReplaceAnyWith'] || "any?" new_expression = "#{internal_expression}.#{replacement}" - corrector.replace(node.loc.expression, new_expression) + corrector.replace(node, new_expression) end end ``` diff --git a/spec/rubocop/cop/corrector_spec.rb b/spec/rubocop/cop/corrector_spec.rb index a0dfa889d60..fd4d2412194 100644 --- a/spec/rubocop/cop/corrector_spec.rb +++ b/spec/rubocop/cop/corrector_spec.rb @@ -78,6 +78,25 @@ def do_rewrite(corrections = nil, &block) end.to rewrite_to 'true a false' end + it 'accepts a node instead of a range' do + expect do |corrector| + corrector.replace(node.rhs, 'maybe') + end.to rewrite_to 'true and maybe' + end + + it 'raises a useful error if not given a node or a range' do + # rubocop:disable Style/MultilineBlockChain + expect do + do_rewrite { |corr| corr.replace(1..3, 'oops') } + end.to raise_error(RuboCop::ErrorWithAnalyzedFileLocation) do |e| + expect(e.cause.message).to eq( + 'Expected a Parser::Source::Range, Comment or Rubocop::AST::Node, ' \ + 'got Range' + ) + end + # rubocop:enable Style/MultilineBlockChain + end + context 'when range is from incorrect source' do let(:other_source) { parse_source(source) } let(:op_other) do From 8efed68245950ffe4c1b2dee83deda2598b79b05 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Fri, 10 Apr 2020 04:19:02 -0400 Subject: [PATCH 2/2] Simplify now that corrector accept node arguments and/or allow wrap --- .../cop/bundler/insecure_protocol_source.rb | 2 +- .../cop/correctors/condition_corrector.rb | 3 +-- .../cop/correctors/empty_line_corrector.rb | 2 +- .../lambda_literal_to_method_corrector.rb | 6 +++--- .../cop/correctors/line_break_corrector.rb | 4 ++-- .../cop/correctors/percent_literal_corrector.rb | 2 +- .../cop/correctors/string_literal_corrector.rb | 4 ++-- .../internal_affairs/offense_location_keyword.rb | 2 +- lib/rubocop/cop/layout/dot_position.rb | 2 +- .../heredoc_argument_closing_parenthesis.rb | 4 ++-- lib/rubocop/cop/layout/heredoc_indentation.rb | 4 ++-- lib/rubocop/cop/layout/multiline_block_layout.rb | 2 +- .../cop/layout/space_around_block_parameters.rb | 4 ++-- .../cop/layout/space_in_lambda_literal.rb | 2 +- .../cop/layout/space_inside_range_literal.rb | 2 +- lib/rubocop/cop/lint/boolean_symbol.rb | 2 +- lib/rubocop/cop/lint/inherit_exception.rb | 2 +- lib/rubocop/cop/lint/literal_in_interpolation.rb | 2 +- lib/rubocop/cop/lint/multiple_comparison.rb | 2 +- .../cop/lint/non_deterministic_require_order.rb | 4 ++-- lib/rubocop/cop/lint/number_conversion.rb | 2 +- .../cop/lint/redundant_string_coercion.rb | 2 +- lib/rubocop/cop/lint/uri_regexp.rb | 2 +- lib/rubocop/cop/mixin/hash_transform_method.rb | 2 +- .../naming/rescued_exceptions_variable_name.rb | 2 +- lib/rubocop/cop/style/alias.rb | 8 ++++---- lib/rubocop/cop/style/and_or.rb | 9 ++++----- lib/rubocop/cop/style/array_join.rb | 2 +- lib/rubocop/cop/style/character_literal.rb | 4 ++-- lib/rubocop/cop/style/conditional_assignment.rb | 16 ++++++++-------- lib/rubocop/cop/style/dir.rb | 2 +- .../disable_cops_within_source_code_directive.rb | 2 +- .../cop/style/double_cop_disable_directive.rb | 2 +- lib/rubocop/cop/style/each_for_simple_loop.rb | 2 +- lib/rubocop/cop/style/each_with_object.rb | 6 +++--- lib/rubocop/cop/style/empty_method.rb | 2 +- lib/rubocop/cop/style/even_odd.rb | 2 +- lib/rubocop/cop/style/expand_path_arguments.rb | 6 +++--- lib/rubocop/cop/style/format_string.rb | 4 ++-- lib/rubocop/cop/style/hash_each_methods.rb | 6 +++--- lib/rubocop/cop/style/hash_syntax.rb | 4 +--- lib/rubocop/cop/style/if_unless_modifier.rb | 2 +- lib/rubocop/cop/style/lambda.rb | 4 ++-- lib/rubocop/cop/style/lambda_call.rb | 4 ++-- lib/rubocop/cop/style/module_function.rb | 4 ++-- lib/rubocop/cop/style/multiline_if_modifier.rb | 2 +- lib/rubocop/cop/style/mutable_constant.rb | 6 ++---- lib/rubocop/cop/style/next.rb | 4 ++-- lib/rubocop/cop/style/nil_comparison.rb | 2 +- lib/rubocop/cop/style/non_nil_check.rb | 8 ++++---- lib/rubocop/cop/style/not.rb | 2 +- lib/rubocop/cop/style/numeric_literal_prefix.rb | 2 +- lib/rubocop/cop/style/numeric_literals.rb | 2 +- lib/rubocop/cop/style/numeric_predicate.rb | 2 +- lib/rubocop/cop/style/one_line_conditional.rb | 2 +- lib/rubocop/cop/style/or_assignment.rb | 2 +- lib/rubocop/cop/style/percent_q_literals.rb | 2 +- lib/rubocop/cop/style/perl_backrefs.rb | 4 ++-- lib/rubocop/cop/style/proc.rb | 2 +- lib/rubocop/cop/style/raise_args.rb | 2 +- lib/rubocop/cop/style/random_with_offset.rb | 6 +++--- lib/rubocop/cop/style/redundant_condition.rb | 7 +++---- lib/rubocop/cop/style/redundant_conditional.rb | 2 +- lib/rubocop/cop/style/redundant_exception.rb | 6 +++--- lib/rubocop/cop/style/redundant_interpolation.rb | 4 ++-- lib/rubocop/cop/style/redundant_return.rb | 12 +++++------- lib/rubocop/cop/style/redundant_self.rb | 2 +- lib/rubocop/cop/style/rescue_modifier.rb | 2 +- lib/rubocop/cop/style/return_nil.rb | 2 +- lib/rubocop/cop/style/safe_navigation.rb | 2 +- lib/rubocop/cop/style/self_assignment.rb | 2 +- lib/rubocop/cop/style/special_global_vars.rb | 2 +- .../cop/style/stabby_lambda_parentheses.rb | 5 +---- lib/rubocop/cop/style/string_hash_keys.rb | 2 +- lib/rubocop/cop/style/symbol_array.rb | 2 +- lib/rubocop/cop/style/symbol_literal.rb | 2 +- lib/rubocop/cop/style/ternary_parentheses.rb | 3 +-- lib/rubocop/cop/style/trivial_accessors.rb | 2 +- lib/rubocop/cop/style/variable_interpolation.rb | 2 +- lib/rubocop/cop/style/while_until_modifier.rb | 2 +- lib/rubocop/cop/style/word_array.rb | 2 +- lib/rubocop/cop/style/zero_length_predicate.rb | 2 +- 82 files changed, 131 insertions(+), 144 deletions(-) diff --git a/lib/rubocop/cop/bundler/insecure_protocol_source.rb b/lib/rubocop/cop/bundler/insecure_protocol_source.rb index a0fee381230..26006e2b567 100644 --- a/lib/rubocop/cop/bundler/insecure_protocol_source.rb +++ b/lib/rubocop/cop/bundler/insecure_protocol_source.rb @@ -53,7 +53,7 @@ def on_send(node) def autocorrect(node) lambda do |corrector| corrector.replace( - node.first_argument.loc.expression, "'https://rubygems.org'" + node.first_argument, "'https://rubygems.org'" ) end end diff --git a/lib/rubocop/cop/correctors/condition_corrector.rb b/lib/rubocop/cop/correctors/condition_corrector.rb index 7b35969e3d4..d0ade803f33 100644 --- a/lib/rubocop/cop/correctors/condition_corrector.rb +++ b/lib/rubocop/cop/correctors/condition_corrector.rb @@ -10,8 +10,7 @@ def correct_negative_condition(node) lambda do |corrector| corrector.replace(node.loc.keyword, node.inverse_keyword) - corrector.replace(condition.source_range, - condition.children.first.source) + corrector.replace(condition, condition.children.first.source) end end diff --git a/lib/rubocop/cop/correctors/empty_line_corrector.rb b/lib/rubocop/cop/correctors/empty_line_corrector.rb index 8f78e6c92e0..15446cd546b 100644 --- a/lib/rubocop/cop/correctors/empty_line_corrector.rb +++ b/lib/rubocop/cop/correctors/empty_line_corrector.rb @@ -18,7 +18,7 @@ def correct(node) end def insert_before(node) - ->(corrector) { corrector.insert_before(node.source_range, "\n") } + ->(corrector) { corrector.insert_before(node, "\n") } end end end diff --git a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb index 7d1696a40a9..b1db3cbb8dc 100644 --- a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +++ b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb @@ -44,13 +44,13 @@ def insert_separating_space(corrector) end def replace_selector(corrector) - corrector.replace(method.source_range, 'lambda') + corrector.replace(method, 'lambda') end def remove_arguments(corrector) return if arguments.empty_and_without_delimiters? - corrector.remove(arguments.source_range) + corrector.remove(arguments) end def insert_arguments(corrector) @@ -62,7 +62,7 @@ def insert_arguments(corrector) def remove_leading_whitespace(corrector) corrector.remove_preceding( - arguments.source_range, + arguments, arguments.source_range.begin_pos - block_node.send_node.source_range.end_pos ) diff --git a/lib/rubocop/cop/correctors/line_break_corrector.rb b/lib/rubocop/cop/correctors/line_break_corrector.rb index d0dfb1179f6..420e0b55607 100644 --- a/lib/rubocop/cop/correctors/line_break_corrector.rb +++ b/lib/rubocop/cop/correctors/line_break_corrector.rb @@ -38,9 +38,9 @@ def move_comment(eol_comment:, node:, corrector:) return unless eol_comment text = eol_comment.loc.expression.source - corrector.insert_before(node.source_range, + corrector.insert_before(node, text + "\n" + (' ' * node.loc.keyword.column)) - corrector.remove(eol_comment.loc.expression) + corrector.remove(eol_comment) end private diff --git a/lib/rubocop/cop/correctors/percent_literal_corrector.rb b/lib/rubocop/cop/correctors/percent_literal_corrector.rb index be53f026c6a..1731125557e 100644 --- a/lib/rubocop/cop/correctors/percent_literal_corrector.rb +++ b/lib/rubocop/cop/correctors/percent_literal_corrector.rb @@ -26,7 +26,7 @@ def correct(node, char) def wrap_contents(node, contents, char, delimiters) lambda do |corrector| corrector.replace( - node.source_range, + node, "%#{char}#{delimiters[0]}#{contents}#{delimiters[1]}" ) end diff --git a/lib/rubocop/cop/correctors/string_literal_corrector.rb b/lib/rubocop/cop/correctors/string_literal_corrector.rb index 9b54ac1d0b7..018fff80f82 100644 --- a/lib/rubocop/cop/correctors/string_literal_corrector.rb +++ b/lib/rubocop/cop/correctors/string_literal_corrector.rb @@ -13,9 +13,9 @@ def correct(node, style) lambda do |corrector| str = node.str_content if style == :single_quotes - corrector.replace(node.source_range, to_string_literal(str)) + corrector.replace(node, to_string_literal(str)) else - corrector.replace(node.source_range, str.inspect) + corrector.replace(node, str.inspect) end end end diff --git a/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb b/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb index 8fced0a88d9..0a3219d0736 100644 --- a/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +++ b/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb @@ -28,7 +28,7 @@ def on_send(node) def autocorrect(node) (*, keyword) = offending_location_argument(node.parent) - ->(corrector) { corrector.replace(node.source_range, ":#{keyword}") } + ->(corrector) { corrector.replace(node, ":#{keyword}") } end private diff --git a/lib/rubocop/cop/layout/dot_position.rb b/lib/rubocop/cop/layout/dot_position.rb index ae1f455bdb8..1cf74baa9d8 100644 --- a/lib/rubocop/cop/layout/dot_position.rb +++ b/lib/rubocop/cop/layout/dot_position.rb @@ -44,7 +44,7 @@ def autocorrect(node) when :leading corrector.insert_before(selector_range(node), dot) when :trailing - corrector.insert_after(node.receiver.source_range, dot) + corrector.insert_after(node.receiver, dot) end end end diff --git a/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb b/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb index 73beeb698e6..76d495cb57e 100644 --- a/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +++ b/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb @@ -169,7 +169,7 @@ def fix_closing_parenthesis(node, corrector) end def add_correct_closing_paren(node, corrector) - corrector.insert_after(node.arguments.last.source_range, ')') + corrector.insert_after(node.arguments.last, ')') end def remove_incorrect_closing_paren(node, corrector) @@ -249,7 +249,7 @@ def fix_external_trailing_comma(node, corrector) def add_correct_external_trailing_comma(node, corrector) return unless external_trailing_comma?(node) - corrector.insert_after(node.arguments.last.source_range, ',') + corrector.insert_after(node.arguments.last, ',') end def remove_incorrect_external_trailing_comma(node, corrector) diff --git a/lib/rubocop/cop/layout/heredoc_indentation.rb b/lib/rubocop/cop/layout/heredoc_indentation.rb index 9265ebc3437..b8387841102 100644 --- a/lib/rubocop/cop/layout/heredoc_indentation.rb +++ b/lib/rubocop/cop/layout/heredoc_indentation.rb @@ -184,14 +184,14 @@ def adjust_squiggly(corrector, node) def adjust_minus(corrector, node) heredoc_beginning = node.loc.expression.source corrected = heredoc_beginning.sub(/<<-?/, '<<~') - corrector.replace(node.loc.expression, corrected) + corrector.replace(node, corrected) end def correct_by_library(node) lambda do |corrector| corrector.replace(node.loc.heredoc_body, indented_body(node)) corrected = ".#{STRIP_METHODS[style]}" - corrector.insert_after(node.loc.expression, corrected) + corrector.insert_after(node, corrected) end end diff --git a/lib/rubocop/cop/layout/multiline_block_layout.rb b/lib/rubocop/cop/layout/multiline_block_layout.rb index 9ab1a0af316..2b591734c1b 100644 --- a/lib/rubocop/cop/layout/multiline_block_layout.rb +++ b/lib/rubocop/cop/layout/multiline_block_layout.rb @@ -129,7 +129,7 @@ def autocorrect_body(corrector, node, block_body) block_start_col = node.source_range.column - corrector.insert_before(first_node.source_range, + corrector.insert_before(first_node, "\n #{' ' * block_start_col}") end diff --git a/lib/rubocop/cop/layout/space_around_block_parameters.rb b/lib/rubocop/cop/layout/space_around_block_parameters.rb index 69e654f025c..d17276c7df8 100644 --- a/lib/rubocop/cop/layout/space_around_block_parameters.rb +++ b/lib/rubocop/cop/layout/space_around_block_parameters.rb @@ -43,9 +43,9 @@ def autocorrect(target) lambda do |corrector| if target.is_a?(RuboCop::AST::Node) if target.parent.children.first == target - corrector.insert_before(target.source_range, ' ') + corrector.insert_before(target, ' ') else - corrector.insert_after(target.source_range, ' ') + corrector.insert_after(target, ' ') end elsif target.source =~ /^\s+$/ corrector.remove(target) diff --git a/lib/rubocop/cop/layout/space_in_lambda_literal.rb b/lib/rubocop/cop/layout/space_in_lambda_literal.rb index bee7f944873..7f39424fcc1 100644 --- a/lib/rubocop/cop/layout/space_in_lambda_literal.rb +++ b/lib/rubocop/cop/layout/space_in_lambda_literal.rb @@ -46,7 +46,7 @@ def autocorrect(lambda_node) children = lambda_node.parent.children lambda do |corrector| if style == :require_space - corrector.insert_before(children[1].source_range, ' ') + corrector.insert_before(children[1], ' ') else corrector.remove(space_after_arrow(lambda_node)) end diff --git a/lib/rubocop/cop/layout/space_inside_range_literal.rb b/lib/rubocop/cop/layout/space_inside_range_literal.rb index f318d892374..22e97b46d78 100644 --- a/lib/rubocop/cop/layout/space_inside_range_literal.rb +++ b/lib/rubocop/cop/layout/space_inside_range_literal.rb @@ -35,7 +35,7 @@ def autocorrect(node) lambda do |corrector| corrector.replace( - node.source_range, + node, expression .sub(/\s+#{operator_escaped}/, operator) .sub(/#{operator_escaped}\s+/, operator) diff --git a/lib/rubocop/cop/lint/boolean_symbol.rb b/lib/rubocop/cop/lint/boolean_symbol.rb index 3ed1df85d5a..46c3ecdac35 100644 --- a/lib/rubocop/cop/lint/boolean_symbol.rb +++ b/lib/rubocop/cop/lint/boolean_symbol.rb @@ -41,7 +41,7 @@ def autocorrect(node) corrector.remove(parent.loc.operator) boolean_literal = "#{node.source} =>" end - corrector.replace(node.loc.expression, boolean_literal) + corrector.replace(node, boolean_literal) end end end diff --git a/lib/rubocop/cop/lint/inherit_exception.rb b/lib/rubocop/cop/lint/inherit_exception.rb index f0f0a846178..713b535f8ba 100644 --- a/lib/rubocop/cop/lint/inherit_exception.rb +++ b/lib/rubocop/cop/lint/inherit_exception.rb @@ -77,7 +77,7 @@ def on_send(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.loc.expression, preferred_base_class) + corrector.replace(node, preferred_base_class) end end diff --git a/lib/rubocop/cop/lint/literal_in_interpolation.rb b/lib/rubocop/cop/lint/literal_in_interpolation.rb index dfd6c523590..d704b222834 100644 --- a/lib/rubocop/cop/lint/literal_in_interpolation.rb +++ b/lib/rubocop/cop/lint/literal_in_interpolation.rb @@ -37,7 +37,7 @@ def autocorrect(node) return if node.dstr_type? # nested, fixed in next iteration value = autocorrected_value(node) - ->(corrector) { corrector.replace(node.parent.source_range, value) } + ->(corrector) { corrector.replace(node.parent, value) } end private diff --git a/lib/rubocop/cop/lint/multiple_comparison.rb b/lib/rubocop/cop/lint/multiple_comparison.rb index 7ce2efe3181..4d0302ab183 100644 --- a/lib/rubocop/cop/lint/multiple_comparison.rb +++ b/lib/rubocop/cop/lint/multiple_comparison.rb @@ -39,7 +39,7 @@ def autocorrect(node) new_center = "#{center.source} && #{center.source}" lambda do |corrector| - corrector.replace(center.source_range, new_center) + corrector.replace(center, new_center) end end end diff --git a/lib/rubocop/cop/lint/non_deterministic_require_order.rb b/lib/rubocop/cop/lint/non_deterministic_require_order.rb index 60a07eea672..387f3dfeb73 100644 --- a/lib/rubocop/cop/lint/non_deterministic_require_order.rb +++ b/lib/rubocop/cop/lint/non_deterministic_require_order.rb @@ -52,12 +52,12 @@ def on_block(node) def autocorrect(node) if unsorted_dir_block?(node) lambda do |corrector| - corrector.replace(node.loc.expression, "#{node.source}.sort.each") + corrector.replace(node, "#{node.source}.sort.each") end else lambda do |corrector| source = node.receiver.source - corrector.replace(node.loc.expression, "#{source}.sort.each") + corrector.replace(node, "#{source}.sort.each") end end end diff --git a/lib/rubocop/cop/lint/number_conversion.rb b/lib/rubocop/cop/lint/number_conversion.rb index 30583d17ff5..8b5d592ba43 100644 --- a/lib/rubocop/cop/lint/number_conversion.rb +++ b/lib/rubocop/cop/lint/number_conversion.rb @@ -55,7 +55,7 @@ def on_send(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.loc.expression, + corrector.replace(node, correct_method(node, node.receiver)) end end diff --git a/lib/rubocop/cop/lint/redundant_string_coercion.rb b/lib/rubocop/cop/lint/redundant_string_coercion.rb index 6e3a03ad3e6..fc9987fe0b5 100644 --- a/lib/rubocop/cop/lint/redundant_string_coercion.rb +++ b/lib/rubocop/cop/lint/redundant_string_coercion.rb @@ -38,7 +38,7 @@ def autocorrect(node) lambda do |corrector| receiver = node.receiver corrector.replace( - node.source_range, + node, if receiver receiver.source else diff --git a/lib/rubocop/cop/lint/uri_regexp.rb b/lib/rubocop/cop/lint/uri_regexp.rb index 2c17b7f869b..ff6d83ac2cf 100644 --- a/lib/rubocop/cop/lint/uri_regexp.rb +++ b/lib/rubocop/cop/lint/uri_regexp.rb @@ -54,7 +54,7 @@ def autocorrect(node) argument = arg ? "(#{arg.source})" : '' corrector.replace( - node.loc.expression, + node, "#{top_level}URI::DEFAULT_PARSER.make_regexp#{argument}" ) end diff --git a/lib/rubocop/cop/mixin/hash_transform_method.rb b/lib/rubocop/cop/mixin/hash_transform_method.rb index 600e2546657..0ad0c68b579 100644 --- a/lib/rubocop/cop/mixin/hash_transform_method.rb +++ b/lib/rubocop/cop/mixin/hash_transform_method.rb @@ -161,7 +161,7 @@ def set_new_arg_name(transformed_argname, corrector) def set_new_body_expression(transforming_body_expr, corrector) corrector.replace( - block_node.body.loc.expression, + block_node.body, transforming_body_expr.loc.expression.source ) end diff --git a/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb b/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb index 1eb6597df62..ed0a83a8117 100644 --- a/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +++ b/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb @@ -73,7 +73,7 @@ def autocorrect(node) node.body&.each_descendant(:lvar) do |var| next unless var.children.first == offending_name - corrector.replace(var.loc.expression, preferred_name) + corrector.replace(var, preferred_name) end end end diff --git a/lib/rubocop/cop/style/alias.rb b/lib/rubocop/cop/style/alias.rb index 2f9950c4775..a86818b7333 100644 --- a/lib/rubocop/cop/style/alias.rb +++ b/lib/rubocop/cop/style/alias.rb @@ -115,7 +115,7 @@ def correct_alias_method_to_alias(send_node) lambda do |corrector| new, old = *send_node.arguments replacement = "alias #{identifier(new)} #{identifier(old)}" - corrector.replace(send_node.source_range, replacement) + corrector.replace(send_node, replacement) end end @@ -125,15 +125,15 @@ def correct_alias_to_alias_method(node) 'alias_method ' \ ":#{identifier(node.new_identifier)}, " \ ":#{identifier(node.old_identifier)}" - corrector.replace(node.source_range, replacement) + corrector.replace(node, replacement) end end def correct_alias_with_symbol_args(node) lambda do |corrector| - corrector.replace(node.new_identifier.source_range, + corrector.replace(node.new_identifier, node.new_identifier.source[1..-1]) - corrector.replace(node.old_identifier.source_range, + corrector.replace(node.old_identifier, node.old_identifier.source[1..-1]) end end diff --git a/lib/rubocop/cop/style/and_or.rb b/lib/rubocop/cop/style/and_or.rb index 958b840b5e0..73463ed1df3 100644 --- a/lib/rubocop/cop/style/and_or.rb +++ b/lib/rubocop/cop/style/and_or.rb @@ -97,12 +97,12 @@ def correct_send(node, corrector) return unless correctable_send?(node) corrector.replace(whitespace_before_arg(node), '(') - corrector.insert_after(node.last_argument.source_range, ')') + corrector.insert_after(node.last_argument, ')') end def correct_setter(node, corrector) - corrector.insert_before(node.receiver.source_range, '(') - corrector.insert_after(node.last_argument.source_range, ')') + corrector.insert_before(node.receiver, '(') + corrector.insert_after(node.last_argument, ')') end # ! is a special case: @@ -124,8 +124,7 @@ def correct_not(node, receiver, corrector) def correct_other(node, corrector) return if node.source_range.begin.is?('(') - corrector.insert_before(node.source_range, '(') - corrector.insert_after(node.source_range, ')') + corrector.wrap(node, '(', ')') end def correctable_send?(node) diff --git a/lib/rubocop/cop/style/array_join.rb b/lib/rubocop/cop/style/array_join.rb index 59d53abceb9..209581ca15a 100644 --- a/lib/rubocop/cop/style/array_join.rb +++ b/lib/rubocop/cop/style/array_join.rb @@ -30,7 +30,7 @@ def autocorrect(node) array, join_arg = join_candidate?(node).map(&:source) lambda do |corrector| - corrector.replace(node.source_range, "#{array}.join(#{join_arg})") + corrector.replace(node, "#{array}.join(#{join_arg})") end end end diff --git a/lib/rubocop/cop/style/character_literal.rb b/lib/rubocop/cop/style/character_literal.rb index 82ddf0f8481..6d314f99781 100644 --- a/lib/rubocop/cop/style/character_literal.rb +++ b/lib/rubocop/cop/style/character_literal.rb @@ -33,9 +33,9 @@ def autocorrect(node) # special character like \n # or ' which needs to use "" or be escaped. if string.length == 2 || string == "'" - corrector.replace(node.source_range, %("#{string}")) + corrector.replace(node, %("#{string}")) elsif string.length == 1 # normal character - corrector.replace(node.source_range, "'#{string}'") + corrector.replace(node, "'#{string}'") end end end diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb index 7eb4e488a0e..1ae6dcc3c1e 100644 --- a/lib/rubocop/cop/style/conditional_assignment.rb +++ b/lib/rubocop/cop/style/conditional_assignment.rb @@ -458,7 +458,7 @@ def assignment(node) def correct_if_branches(corrector, cop, node) if_branch, elsif_branches, else_branch = extract_tail_branches(node) - corrector.insert_before(node.source_range, lhs(if_branch)) + corrector.insert_before(node, lhs(if_branch)) replace_branch_assignment(corrector, if_branch) correct_branches(corrector, elsif_branches) replace_branch_assignment(corrector, else_branch) @@ -475,13 +475,13 @@ def replace_branch_assignment(corrector, branch) source end - corrector.replace(branch.source_range, replacement) + corrector.replace(branch, replacement) end def correct_branches(corrector, branches) branches.each do |branch| *_, assignment = *branch - corrector.replace(branch.source_range, assignment.source) + corrector.replace(branch, assignment.source) end end end @@ -494,7 +494,7 @@ class << self def correct(node) lambda do |corrector| - corrector.replace(node.source_range, correction(node)) + corrector.replace(node, correction(node)) end end @@ -547,7 +547,7 @@ def remove_parentheses(corrector, node) end def move_branch_inside_condition(corrector, branch, assignment) - corrector.insert_before(branch.loc.expression, assignment.source) + corrector.insert_before(branch, assignment.source) end end end @@ -589,7 +589,7 @@ def extract_tail_branches(node) def move_branch_inside_condition(corrector, branch, condition, assignment, column) branch_assignment = tail(branch) - corrector.insert_before(branch_assignment.loc.expression, + corrector.insert_before(branch_assignment, assignment.source) remove_whitespace_in_branches(corrector, branch, condition, column) @@ -611,7 +611,7 @@ def correct(cop, node) when_branches, else_branch = extract_tail_branches(node) lambda do |corrector| - corrector.insert_before(node.source_range, lhs(else_branch)) + corrector.insert_before(node, lhs(else_branch)) correct_branches(corrector, when_branches) replace_branch_assignment(corrector, else_branch) @@ -652,7 +652,7 @@ def extract_branches(case_node) def move_branch_inside_condition(corrector, branch, condition, assignment, column) branch_assignment = tail(branch) - corrector.insert_before(branch_assignment.loc.expression, + corrector.insert_before(branch_assignment, assignment.source) remove_whitespace_in_branches(corrector, branch, condition, column) diff --git a/lib/rubocop/cop/style/dir.rb b/lib/rubocop/cop/style/dir.rb index 0b1161c91e0..43cf739ff91 100644 --- a/lib/rubocop/cop/style/dir.rb +++ b/lib/rubocop/cop/style/dir.rb @@ -33,7 +33,7 @@ def on_send(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, '__dir__') + corrector.replace(node, '__dir__') end end diff --git a/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb b/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb index 55fb434c0a1..59e813c9643 100644 --- a/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +++ b/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb @@ -34,7 +34,7 @@ def investigate(processed_source) def autocorrect(comment) lambda do |corrector| - corrector.replace(comment.loc.expression, '') + corrector.replace(comment, '') end end diff --git a/lib/rubocop/cop/style/double_cop_disable_directive.rb b/lib/rubocop/cop/style/double_cop_disable_directive.rb index de6fd93628f..7dc7f93e377 100644 --- a/lib/rubocop/cop/style/double_cop_disable_directive.rb +++ b/lib/rubocop/cop/style/double_cop_disable_directive.rb @@ -45,7 +45,7 @@ def autocorrect(comment) end lambda do |corrector| - corrector.replace(comment.loc.expression, + corrector.replace(comment, comment.text[/#{prefix} \S+/]) end end diff --git a/lib/rubocop/cop/style/each_for_simple_loop.rb b/lib/rubocop/cop/style/each_for_simple_loop.rb index 1c39c667323..304b13971bd 100644 --- a/lib/rubocop/cop/style/each_for_simple_loop.rb +++ b/lib/rubocop/cop/style/each_for_simple_loop.rb @@ -42,7 +42,7 @@ def autocorrect(node) max += 1 if range_type == :irange - corrector.replace(node.send_node.source_range, + corrector.replace(node.send_node, "#{max - min}.times") end end diff --git a/lib/rubocop/cop/style/each_with_object.rb b/lib/rubocop/cop/style/each_with_object.rb index a695283d332..25a85770838 100644 --- a/lib/rubocop/cop/style/each_with_object.rb +++ b/lib/rubocop/cop/style/each_with_object.rb @@ -48,15 +48,15 @@ def autocorrect(node) first_arg, second_arg = *node.arguments - corrector.replace(first_arg.loc.expression, second_arg.source) - corrector.replace(second_arg.loc.expression, first_arg.source) + corrector.replace(first_arg, second_arg.source) + corrector.replace(second_arg, first_arg.source) return_value = return_value(node.body) if return_value_occupies_whole_line?(return_value) corrector.remove(whole_line_expression(return_value)) else - corrector.remove(return_value.loc.expression) + corrector.remove(return_value) end end end diff --git a/lib/rubocop/cop/style/empty_method.rb b/lib/rubocop/cop/style/empty_method.rb index 3da2f3f8302..c752c66d4b3 100644 --- a/lib/rubocop/cop/style/empty_method.rb +++ b/lib/rubocop/cop/style/empty_method.rb @@ -57,7 +57,7 @@ def on_def(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, corrected(node)) + corrector.replace(node, corrected(node)) end end diff --git a/lib/rubocop/cop/style/even_odd.rb b/lib/rubocop/cop/style/even_odd.rb index 92c8b49b220..83425c1c80f 100644 --- a/lib/rubocop/cop/style/even_odd.rb +++ b/lib/rubocop/cop/style/even_odd.rb @@ -38,7 +38,7 @@ def autocorrect(node) replacement_method = replacement_method(arg, method) correction = "#{base_number.source}.#{replacement_method}?" - ->(corrector) { corrector.replace(node.source_range, correction) } + ->(corrector) { corrector.replace(node, correction) } end end diff --git a/lib/rubocop/cop/style/expand_path_arguments.rb b/lib/rubocop/cop/style/expand_path_arguments.rb index fc63efccbc4..c856eb50ecb 100644 --- a/lib/rubocop/cop/style/expand_path_arguments.rb +++ b/lib/rubocop/cop/style/expand_path_arguments.rb @@ -97,7 +97,7 @@ def autocorrect(node) autocorrect_expand_path(corrector, current_path, default_dir) elsif (default_dir = pathname_parent_expand_path(node)) || (default_dir = pathname_new_parent_expand_path(node)) - corrector.replace(default_dir.loc.expression, '__dir__') + corrector.replace(default_dir, '__dir__') remove_parent_method(corrector, default_dir) end end @@ -145,8 +145,8 @@ def autocorrect_expand_path(corrector, current_path, default_dir) else new_path = "'#{parent_path(stripped_current_path)}'" - corrector.replace(current_path.loc.expression, new_path) - corrector.replace(default_dir.loc.expression, '__dir__') + corrector.replace(current_path, new_path) + corrector.replace(default_dir, '__dir__') end end diff --git a/lib/rubocop/cop/style/format_string.rb b/lib/rubocop/cop/style/format_string.rb index 20150045cb8..9775e85fe42 100644 --- a/lib/rubocop/cop/style/format_string.rb +++ b/lib/rubocop/cop/style/format_string.rb @@ -104,7 +104,7 @@ def autocorrect_from_percent(corrector, node) corrected = "#{style}(#{node.receiver.source}, #{args})" - corrector.replace(node.loc.expression, corrected) + corrector.replace(node, corrected) end def autocorrect_to_percent(corrector, node) @@ -119,7 +119,7 @@ def autocorrect_to_percent(corrector, node) "[#{param_args.map(&:source).join(', ')}]" end - corrector.replace(node.loc.expression, "#{format} % #{args}") + corrector.replace(node, "#{format} % #{args}") end end end diff --git a/lib/rubocop/cop/style/hash_each_methods.rb b/lib/rubocop/cop/style/hash_each_methods.rb index 31e5e1e939f..d07e785063d 100644 --- a/lib/rubocop/cop/style/hash_each_methods.rb +++ b/lib/rubocop/cop/style/hash_each_methods.rb @@ -60,7 +60,7 @@ def used?(arg) end def correct_implicit(node, corrector, method_name) - corrector.replace(node.loc.expression, method_name) + corrector.replace(node, method_name) correct_args(node, corrector) end @@ -70,14 +70,14 @@ def correct_key_value_each(node, corrector) return correct_implicit(node, corrector, name) unless receiver new_source = receiver.source + ".#{name}" - corrector.replace(node.loc.expression, new_source) + corrector.replace(node, new_source) end def correct_args(node, corrector) args = node.parent.arguments name, = *args.children.find { |arg| used?(arg) } - corrector.replace(args.source_range, "|#{name}|") + corrector.replace(args, "|#{name}|") end def kv_range(outer_node) diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index 987d3fcdbb8..1cc57d63ec1 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -188,11 +188,9 @@ def argument_without_space?(node) end def autocorrect_hash_rockets(corrector, pair_node) - key = pair_node.key.source_range op = pair_node.loc.operator - corrector.insert_after(key, pair_node.inverse_delimiter(true)) - corrector.insert_before(key, ':') + corrector.wrap(pair_node.key, ':', pair_node.inverse_delimiter(true)) corrector.remove(range_with_surrounding_space(range: op)) end diff --git a/lib/rubocop/cop/style/if_unless_modifier.rb b/lib/rubocop/cop/style/if_unless_modifier.rb index 23825b58937..50966313329 100644 --- a/lib/rubocop/cop/style/if_unless_modifier.rb +++ b/lib/rubocop/cop/style/if_unless_modifier.rb @@ -63,7 +63,7 @@ def autocorrect(node) else to_modifier_form(node) end - ->(corrector) { corrector.replace(node.source_range, replacement) } + ->(corrector) { corrector.replace(node, replacement) } end private diff --git a/lib/rubocop/cop/style/lambda.rb b/lib/rubocop/cop/style/lambda.rb index c0598ddecb6..edcbb982dcf 100644 --- a/lib/rubocop/cop/style/lambda.rb +++ b/lib/rubocop/cop/style/lambda.rb @@ -109,13 +109,13 @@ def message_line_modifier(node) end def autocorrect_method_to_literal(corrector, node) - corrector.replace(node.send_node.source_range, '->') + corrector.replace(node.send_node, '->') return unless node.arguments? arg_str = "(#{lambda_arg_string(node.arguments)})" - corrector.insert_after(node.send_node.source_range, arg_str) + corrector.insert_after(node.send_node, arg_str) corrector.remove(arguments_with_whitespace(node)) end diff --git a/lib/rubocop/cop/style/lambda_call.rb b/lib/rubocop/cop/style/lambda_call.rb index 41a52a04ced..276203512e9 100644 --- a/lib/rubocop/cop/style/lambda_call.rb +++ b/lib/rubocop/cop/style/lambda_call.rb @@ -37,7 +37,7 @@ def autocorrect(node) receiver = node.receiver.source replacement = node.source.sub("#{receiver}.", "#{receiver}.call") - corrector.replace(node.source_range, replacement) + corrector.replace(node, replacement) else add_parentheses(node, corrector) unless node.parenthesized? corrector.remove(node.loc.selector) @@ -54,7 +54,7 @@ def offense?(node) def add_parentheses(node, corrector) if node.arguments.empty? - corrector.insert_after(node.source_range, '()') + corrector.insert_after(node, '()') else corrector.replace(args_begin(node), '(') corrector.insert_after(args_end(node), ')') diff --git a/lib/rubocop/cop/style/module_function.rb b/lib/rubocop/cop/style/module_function.rb index 401d1ab8532..91314fa1602 100644 --- a/lib/rubocop/cop/style/module_function.rb +++ b/lib/rubocop/cop/style/module_function.rb @@ -98,9 +98,9 @@ def autocorrect(node) lambda do |corrector| if extend_self_node?(node) - corrector.replace(node.source_range, 'module_function') + corrector.replace(node, 'module_function') else - corrector.replace(node.source_range, 'extend self') + corrector.replace(node, 'extend self') end end end diff --git a/lib/rubocop/cop/style/multiline_if_modifier.rb b/lib/rubocop/cop/style/multiline_if_modifier.rb index 311bfef3641..a23fee5980c 100644 --- a/lib/rubocop/cop/style/multiline_if_modifier.rb +++ b/lib/rubocop/cop/style/multiline_if_modifier.rb @@ -29,7 +29,7 @@ def on_if(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, to_normal_if(node)) + corrector.replace(node, to_normal_if(node)) end end diff --git a/lib/rubocop/cop/style/mutable_constant.rb b/lib/rubocop/cop/style/mutable_constant.rb index 6d9a681873f..e3e512859ee 100644 --- a/lib/rubocop/cop/style/mutable_constant.rb +++ b/lib/rubocop/cop/style/mutable_constant.rb @@ -77,11 +77,9 @@ def autocorrect(node) if splat_value correct_splat_expansion(corrector, expr, splat_value) elsif node.array_type? && !node.bracketed? - corrector.insert_before(expr, '[') - corrector.insert_after(expr, ']') + corrector.wrap(expr, '[', ']') elsif requires_parentheses?(node) - corrector.insert_before(expr, '(') - corrector.insert_after(expr, ')') + corrector.wrap(expr, '(', ')') end corrector.insert_after(expr, '.freeze') diff --git a/lib/rubocop/cop/style/next.rb b/lib/rubocop/cop/style/next.rb index e63bb27164b..d5047b87f3b 100644 --- a/lib/rubocop/cop/style/next.rb +++ b/lib/rubocop/cop/style/next.rb @@ -152,13 +152,13 @@ def autocorrect_modifier(corrector, node) "next #{node.inverse_keyword} #{node.condition.source}\n" \ "#{' ' * node.source_range.column}#{body.source}" - corrector.replace(node.source_range, replacement) + corrector.replace(node, replacement) end def autocorrect_block(corrector, node) next_code = "next #{node.inverse_keyword} #{node.condition.source}" - corrector.insert_before(node.source_range, next_code) + corrector.insert_before(node, next_code) corrector.remove(cond_range(node, node.condition)) corrector.remove(end_range(node)) diff --git a/lib/rubocop/cop/style/nil_comparison.rb b/lib/rubocop/cop/style/nil_comparison.rb index f3405c7f05e..63fa7a8b56d 100644 --- a/lib/rubocop/cop/style/nil_comparison.rb +++ b/lib/rubocop/cop/style/nil_comparison.rb @@ -49,7 +49,7 @@ def autocorrect(node) else node.source.sub(/\s*={2,3}\s*nil/, '.nil?') end - ->(corrector) { corrector.replace(node.source_range, new_code) } + ->(corrector) { corrector.replace(node, new_code) } end private diff --git a/lib/rubocop/cop/style/non_nil_check.rb b/lib/rubocop/cop/style/non_nil_check.rb index 8106ba4ef67..8afdf059471 100644 --- a/lib/rubocop/cop/style/non_nil_check.rb +++ b/lib/rubocop/cop/style/non_nil_check.rb @@ -111,15 +111,15 @@ def autocorrect_comparison(node) return if expr == new_code - ->(corrector) { corrector.replace(node.source_range, new_code) } + ->(corrector) { corrector.replace(node, new_code) } end def autocorrect_non_nil(node, inner_node) lambda do |corrector| if inner_node.receiver - corrector.replace(node.source_range, inner_node.receiver.source) + corrector.replace(node, inner_node.receiver.source) else - corrector.replace(node.source_range, 'self') + corrector.replace(node, 'self') end end end @@ -127,7 +127,7 @@ def autocorrect_non_nil(node, inner_node) def autocorrect_unless_nil(node, receiver) lambda do |corrector| corrector.replace(node.parent.loc.keyword, 'if') - corrector.replace(node.source_range, receiver.source) + corrector.replace(node, receiver.source) end end end diff --git a/lib/rubocop/cop/style/not.rb b/lib/rubocop/cop/style/not.rb index 988a2d8d87d..320d89b80ae 100644 --- a/lib/rubocop/cop/style/not.rb +++ b/lib/rubocop/cop/style/not.rb @@ -69,7 +69,7 @@ def correct_opposite_method(range, child) def correct_with_parens(range, node) lambda do |corrector| corrector.replace(range, '!(') - corrector.insert_after(node.source_range, ')') + corrector.insert_after(node, ')') end end diff --git a/lib/rubocop/cop/style/numeric_literal_prefix.rb b/lib/rubocop/cop/style/numeric_literal_prefix.rb index 4ae93b228f1..559f76317ba 100644 --- a/lib/rubocop/cop/style/numeric_literal_prefix.rb +++ b/lib/rubocop/cop/style/numeric_literal_prefix.rb @@ -59,7 +59,7 @@ def on_int(node) def autocorrect(node) lambda do |corrector| type = literal_type(node) - corrector.replace(node.source_range, + corrector.replace(node, send(:"format_#{type}", node.source)) end end diff --git a/lib/rubocop/cop/style/numeric_literals.rb b/lib/rubocop/cop/style/numeric_literals.rb index 7f3bdaac33f..ef216bd505d 100644 --- a/lib/rubocop/cop/style/numeric_literals.rb +++ b/lib/rubocop/cop/style/numeric_literals.rb @@ -48,7 +48,7 @@ def on_float(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, format_number(node)) + corrector.replace(node, format_number(node)) end end diff --git a/lib/rubocop/cop/style/numeric_predicate.rb b/lib/rubocop/cop/style/numeric_predicate.rb index c9d8dc1a384..b2c95fa5110 100644 --- a/lib/rubocop/cop/style/numeric_predicate.rb +++ b/lib/rubocop/cop/style/numeric_predicate.rb @@ -73,7 +73,7 @@ def autocorrect(node) _, replacement = check(node) lambda do |corrector| - corrector.replace(node.loc.expression, replacement) + corrector.replace(node, replacement) end end diff --git a/lib/rubocop/cop/style/one_line_conditional.rb b/lib/rubocop/cop/style/one_line_conditional.rb index 49f22c25065..0d5f967080b 100644 --- a/lib/rubocop/cop/style/one_line_conditional.rb +++ b/lib/rubocop/cop/style/one_line_conditional.rb @@ -36,7 +36,7 @@ def on_normal_if_unless(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, replacement(node)) + corrector.replace(node, replacement(node)) end end diff --git a/lib/rubocop/cop/style/or_assignment.rb b/lib/rubocop/cop/style/or_assignment.rb index fe3df4a73fe..75df3918220 100644 --- a/lib/rubocop/cop/style/or_assignment.rb +++ b/lib/rubocop/cop/style/or_assignment.rb @@ -69,7 +69,7 @@ def autocorrect(node) end lambda do |corrector| - corrector.replace(node.source_range, + corrector.replace(node, "#{variable} ||= #{default.source}") end end diff --git a/lib/rubocop/cop/style/percent_q_literals.rb b/lib/rubocop/cop/style/percent_q_literals.rb index 23df738481e..eeefa57d12e 100644 --- a/lib/rubocop/cop/style/percent_q_literals.rb +++ b/lib/rubocop/cop/style/percent_q_literals.rb @@ -39,7 +39,7 @@ def on_str(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, corrected(node.source)) + corrector.replace(node, corrected(node.source)) end end diff --git a/lib/rubocop/cop/style/perl_backrefs.rb b/lib/rubocop/cop/style/perl_backrefs.rb index 5f5367daf53..d675cd7a09f 100644 --- a/lib/rubocop/cop/style/perl_backrefs.rb +++ b/lib/rubocop/cop/style/perl_backrefs.rb @@ -24,10 +24,10 @@ def autocorrect(node) backref, = *node parent_type = node.parent ? node.parent.type : nil if %i[dstr xstr regexp].include?(parent_type) - corrector.replace(node.source_range, + corrector.replace(node, "{Regexp.last_match(#{backref})}") else - corrector.replace(node.source_range, + corrector.replace(node, "Regexp.last_match(#{backref})") end end diff --git a/lib/rubocop/cop/style/proc.rb b/lib/rubocop/cop/style/proc.rb index 65911e20400..422c20f1f1f 100644 --- a/lib/rubocop/cop/style/proc.rb +++ b/lib/rubocop/cop/style/proc.rb @@ -26,7 +26,7 @@ def on_block(node) end def autocorrect(node) - ->(corrector) { corrector.replace(node.source_range, 'proc') } + ->(corrector) { corrector.replace(node, 'proc') } end end end diff --git a/lib/rubocop/cop/style/raise_args.rb b/lib/rubocop/cop/style/raise_args.rb index 5a6388c3f91..b0bd7f3f801 100644 --- a/lib/rubocop/cop/style/raise_args.rb +++ b/lib/rubocop/cop/style/raise_args.rb @@ -58,7 +58,7 @@ def autocorrect(node) correction_compact_to_exploded(node) end - ->(corrector) { corrector.replace(node.source_range, replacement) } + ->(corrector) { corrector.replace(node, replacement) } end private diff --git a/lib/rubocop/cop/style/random_with_offset.rb b/lib/rubocop/cop/style/random_with_offset.rb index fcf81683ce4..b998ef1d96c 100644 --- a/lib/rubocop/cop/style/random_with_offset.rb +++ b/lib/rubocop/cop/style/random_with_offset.rb @@ -66,13 +66,13 @@ def on_send(node) def autocorrect(node) lambda do |corrector| if integer_op_rand?(node) - corrector.replace(node.source_range, + corrector.replace(node, corrected_integer_op_rand(node)) elsif rand_op_integer?(node) - corrector.replace(node.source_range, + corrector.replace(node, corrected_rand_op_integer(node)) elsif rand_modified?(node) - corrector.replace(node.source_range, + corrector.replace(node, corrected_rand_modified(node)) end end diff --git a/lib/rubocop/cop/style/redundant_condition.rb b/lib/rubocop/cop/style/redundant_condition.rb index a2ee5e71fa5..0fdbc6027aa 100644 --- a/lib/rubocop/cop/style/redundant_condition.rb +++ b/lib/rubocop/cop/style/redundant_condition.rb @@ -48,11 +48,11 @@ def autocorrect(node) if node.ternary? correct_ternary(corrector, node) elsif node.modifier_form? || !node.else_branch - corrector.replace(node.source_range, node.if_branch.source) + corrector.replace(node, node.if_branch.source) else corrected = make_ternary_form(node) - corrector.replace(node.source_range, corrected) + corrector.replace(node, corrected) end end end @@ -116,8 +116,7 @@ def correct_ternary(corrector, node) return unless node.else_branch.range_type? - corrector.insert_before(node.else_branch.loc.expression, '(') - corrector.insert_after(node.else_branch.loc.expression, ')') + corrector.wrap(node.else_branch, '(', ')') end end end diff --git a/lib/rubocop/cop/style/redundant_conditional.rb b/lib/rubocop/cop/style/redundant_conditional.rb index 2aaa06dfe33..14efbbbdb9e 100644 --- a/lib/rubocop/cop/style/redundant_conditional.rb +++ b/lib/rubocop/cop/style/redundant_conditional.rb @@ -40,7 +40,7 @@ def on_if(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.loc.expression, replacement_condition(node)) + corrector.replace(node, replacement_condition(node)) end end diff --git a/lib/rubocop/cop/style/redundant_exception.rb b/lib/rubocop/cop/style/redundant_exception.rb index 40c830911fa..805a8e4eff5 100644 --- a/lib/rubocop/cop/style/redundant_exception.rb +++ b/lib/rubocop/cop/style/redundant_exception.rb @@ -32,17 +32,17 @@ def autocorrect(node) # rubocop:disable Metrics/MethodLength exploded?(node) do |command, message| return lambda do |corrector| if node.parenthesized? - corrector.replace(node.source_range, + corrector.replace(node, "#{command}(#{message.source})") else - corrector.replace(node.source_range, + corrector.replace(node, "#{command} #{message.source}") end end end compact?(node) do |new_call, message| lambda do |corrector| - corrector.replace(new_call.source_range, message.source) + corrector.replace(new_call, message.source) end end end diff --git a/lib/rubocop/cop/style/redundant_interpolation.rb b/lib/rubocop/cop/style/redundant_interpolation.rb index b62c084f030..90a06e45d6d 100644 --- a/lib/rubocop/cop/style/redundant_interpolation.rb +++ b/lib/rubocop/cop/style/redundant_interpolation.rb @@ -72,13 +72,13 @@ def embedded_in_percent_array?(node) def autocorrect_variable_interpolation(embedded_node, node) replacement = "#{embedded_node.loc.expression.source}.to_s" - ->(corrector) { corrector.replace(node.loc.expression, replacement) } + ->(corrector) { corrector.replace(node, replacement) } end def autocorrect_single_variable_interpolation(embedded_node, node) variable_loc = embedded_node.children.first.loc replacement = "#{variable_loc.expression.source}.to_s" - ->(corrector) { corrector.replace(node.loc.expression, replacement) } + ->(corrector) { corrector.replace(node, replacement) } end def autocorrect_other(embedded_node, node) diff --git a/lib/rubocop/cop/style/redundant_return.rb b/lib/rubocop/cop/style/redundant_return.rb index 02584af0f27..63de3759f57 100644 --- a/lib/rubocop/cop/style/redundant_return.rb +++ b/lib/rubocop/cop/style/redundant_return.rb @@ -71,7 +71,7 @@ def autocorrect(node) private def correct_without_arguments(return_node, corrector) - corrector.replace(return_node.source_range, 'nil') + corrector.replace(return_node, 'nil') end def correct_with_arguments(return_node, corrector) @@ -91,15 +91,13 @@ def hash_without_braces?(node) end def add_brackets(corrector, node) - kids = node.children.map(&:source_range) - corrector.insert_before(kids.first, '[') - corrector.insert_after(kids.last, ']') + corrector.insert_before(node.children.first, '[') + corrector.insert_after(node.children.last, ']') end def add_braces(corrector, node) - kids = node.children.map(&:source_range) - corrector.insert_before(kids.first, '{') - corrector.insert_after(kids.last, '}') + corrector.insert_before(node.children.first, '{') + corrector.insert_after(node.children.last, '}') end # rubocop:disable Metrics/CyclomaticComplexity diff --git a/lib/rubocop/cop/style/redundant_self.rb b/lib/rubocop/cop/style/redundant_self.rb index 6bb6a4c1691..886c81d70b3 100644 --- a/lib/rubocop/cop/style/redundant_self.rb +++ b/lib/rubocop/cop/style/redundant_self.rb @@ -110,7 +110,7 @@ def on_block(node) def autocorrect(node) lambda do |corrector| - corrector.remove(node.receiver.source_range) + corrector.remove(node.receiver) corrector.remove(node.loc.dot) end end diff --git a/lib/rubocop/cop/style/rescue_modifier.rb b/lib/rubocop/cop/style/rescue_modifier.rb index 58229097fb3..a6683b9073a 100644 --- a/lib/rubocop/cop/style/rescue_modifier.rb +++ b/lib/rubocop/cop/style/rescue_modifier.rb @@ -64,7 +64,7 @@ def autocorrect(node) "\n#{offset(node)}end" lambda do |corrector| - corrector.replace(node.source_range, correction) + corrector.replace(node, correction) end end end diff --git a/lib/rubocop/cop/style/return_nil.rb b/lib/rubocop/cop/style/return_nil.rb index 927835ce89a..980805c1626 100644 --- a/lib/rubocop/cop/style/return_nil.rb +++ b/lib/rubocop/cop/style/return_nil.rb @@ -60,7 +60,7 @@ def on_return(node) def autocorrect(node) lambda do |corrector| corrected = style == :return ? 'return' : 'return nil' - corrector.replace(node.source_range, corrected) + corrector.replace(node, corrected) end end diff --git a/lib/rubocop/cop/style/safe_navigation.rb b/lib/rubocop/cop/style/safe_navigation.rb index dce2ea3d6f7..7941b1bd93c 100644 --- a/lib/rubocop/cop/style/safe_navigation.rb +++ b/lib/rubocop/cop/style/safe_navigation.rb @@ -131,7 +131,7 @@ def handle_comments(corrector, node, method_call) comments = comments(node) return if comments.empty? - corrector.insert_before(method_call.loc.expression, + corrector.insert_before(method_call, "#{comments.map(&:text).join("\n")}\n") end diff --git a/lib/rubocop/cop/style/self_assignment.rb b/lib/rubocop/cop/style/self_assignment.rb index 635dc1ea6a0..c0f4b63b481 100644 --- a/lib/rubocop/cop/style/self_assignment.rb +++ b/lib/rubocop/cop/style/self_assignment.rb @@ -88,7 +88,7 @@ def autocorrect_boolean_node(node, rhs) def apply_autocorrect(node, rhs, operator, new_rhs) lambda do |corrector| corrector.insert_before(node.loc.operator, operator) - corrector.replace(rhs.source_range, new_rhs.source) + corrector.replace(rhs, new_rhs.source) end end end diff --git a/lib/rubocop/cop/style/special_global_vars.rb b/lib/rubocop/cop/style/special_global_vars.rb index fb16820c616..638ec786f0e 100644 --- a/lib/rubocop/cop/style/special_global_vars.rb +++ b/lib/rubocop/cop/style/special_global_vars.rb @@ -145,7 +145,7 @@ def autocorrect(node) node = node.parent end - corrector.replace(node.source_range, replacement(node, global_var)) + corrector.replace(node, replacement(node, global_var)) end end diff --git a/lib/rubocop/cop/style/stabby_lambda_parentheses.rb b/lib/rubocop/cop/style/stabby_lambda_parentheses.rb index 522cba8ffd1..993decc58d8 100644 --- a/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +++ b/lib/rubocop/cop/style/stabby_lambda_parentheses.rb @@ -57,10 +57,7 @@ def message(_node) def missing_parentheses_corrector(node) lambda do |corrector| - args_loc = node.loc.expression - - corrector.insert_before(args_loc, '(') - corrector.insert_after(args_loc, ')') + corrector.wrap(node, '(', ')') end end diff --git a/lib/rubocop/cop/style/string_hash_keys.rb b/lib/rubocop/cop/style/string_hash_keys.rb index 47edfa9bd01..2edaf6ee175 100644 --- a/lib/rubocop/cop/style/string_hash_keys.rb +++ b/lib/rubocop/cop/style/string_hash_keys.rb @@ -41,7 +41,7 @@ def on_pair(node) def autocorrect(node) lambda do |corrector| symbol_content = node.str_content.to_sym.inspect - corrector.replace(node.source_range, symbol_content) + corrector.replace(node, symbol_content) end end end diff --git a/lib/rubocop/cop/style/symbol_array.rb b/lib/rubocop/cop/style/symbol_array.rb index 06f1c8cb9ec..e614c00c942 100644 --- a/lib/rubocop/cop/style/symbol_array.rb +++ b/lib/rubocop/cop/style/symbol_array.rb @@ -81,7 +81,7 @@ def correct_bracketed(node) end lambda do |corrector| - corrector.replace(node.source_range, "[#{syms.join(', ')}]") + corrector.replace(node, "[#{syms.join(', ')}]") end end diff --git a/lib/rubocop/cop/style/symbol_literal.rb b/lib/rubocop/cop/style/symbol_literal.rb index a1daa811bcc..40e0afe327a 100644 --- a/lib/rubocop/cop/style/symbol_literal.rb +++ b/lib/rubocop/cop/style/symbol_literal.rb @@ -23,7 +23,7 @@ def on_sym(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, node.source.delete(%q('"))) + corrector.replace(node, node.source.delete(%q('"))) end end end diff --git a/lib/rubocop/cop/style/ternary_parentheses.rb b/lib/rubocop/cop/style/ternary_parentheses.rb index 3d78db2c136..cfcf457fd64 100644 --- a/lib/rubocop/cop/style/ternary_parentheses.rb +++ b/lib/rubocop/cop/style/ternary_parentheses.rb @@ -207,8 +207,7 @@ def correct_parenthesized(condition) def correct_unparenthesized(condition) lambda do |corrector| - corrector.insert_before(condition.source_range, '(') - corrector.insert_after(condition.source_range, ')') + corrector.wrap(condition, '(', ')') end end diff --git a/lib/rubocop/cop/style/trivial_accessors.rb b/lib/rubocop/cop/style/trivial_accessors.rb index 6b663ff2af2..8e6f3e2862d 100644 --- a/lib/rubocop/cop/style/trivial_accessors.rb +++ b/lib/rubocop/cop/style/trivial_accessors.rb @@ -161,7 +161,7 @@ def autocorrect_instance(node) return unless names_match?(node) && !node.predicate_method? && kind lambda do |corrector| - corrector.replace(node.source_range, + corrector.replace(node, accessor(kind, node.method_name)) end end diff --git a/lib/rubocop/cop/style/variable_interpolation.rb b/lib/rubocop/cop/style/variable_interpolation.rb index ef5fe382b90..e75bbf43ffd 100644 --- a/lib/rubocop/cop/style/variable_interpolation.rb +++ b/lib/rubocop/cop/style/variable_interpolation.rb @@ -29,7 +29,7 @@ def on_node_with_interpolations(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.source_range, "{#{node.source}}") + corrector.replace(node, "{#{node.source}}") end end diff --git a/lib/rubocop/cop/style/while_until_modifier.rb b/lib/rubocop/cop/style/while_until_modifier.rb index a63e9d12dac..942d61d0b87 100644 --- a/lib/rubocop/cop/style/while_until_modifier.rb +++ b/lib/rubocop/cop/style/while_until_modifier.rb @@ -43,7 +43,7 @@ def autocorrect(node) "#{node.condition.source}" lambda do |corrector| - corrector.replace(node.source_range, oneline) + corrector.replace(node, oneline) end end diff --git a/lib/rubocop/cop/style/word_array.rb b/lib/rubocop/cop/style/word_array.rb index 6696f0a5fb3..b071656d994 100644 --- a/lib/rubocop/cop/style/word_array.rb +++ b/lib/rubocop/cop/style/word_array.rb @@ -93,7 +93,7 @@ def correct_bracketed(node) end lambda do |corrector| - corrector.replace(node.source_range, "[#{words.join(', ')}]") + corrector.replace(node, "[#{words.join(', ')}]") end end end diff --git a/lib/rubocop/cop/style/zero_length_predicate.rb b/lib/rubocop/cop/style/zero_length_predicate.rb index cb3590e5562..17e3276071d 100644 --- a/lib/rubocop/cop/style/zero_length_predicate.rb +++ b/lib/rubocop/cop/style/zero_length_predicate.rb @@ -37,7 +37,7 @@ def on_send(node) def autocorrect(node) lambda do |corrector| - corrector.replace(node.loc.expression, replacement(node)) + corrector.replace(node, replacement(node)) end end