Skip to content

Commit

Permalink
Suppress RuboCop's offense
Browse files Browse the repository at this point in the history
This commit suppresses the following offenses:

```console
$ bundle exec rubocop
(snip)

lib/rubocop/cop/mixin/index_method.rb:107:7: W: Lint/UselessConstantScoping: Useless private access modifier for constant scope.
      Captures = ::Struct.new( ...
      ^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/mixin/index_method.rb:117:7: W: Lint/UselessConstantScoping: Useless private access modifier for constant scope.
      Autocorrection = ::Struct.new(:match, :block_node, :leading, :trailing) do ...
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

302 files inspected, 2 offenses detected
```

Follow-up to rubocop/rubocop#13831.
  • Loading branch information
koic committed Feb 13, 2025
1 parent c9a5749 commit f9adec0
Showing 1 changed file with 65 additions and 65 deletions.
130 changes: 65 additions & 65 deletions lib/rubocop/cop/mixin/index_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,71 @@ module Cop
module IndexMethod # rubocop:disable Metrics/ModuleLength
RESTRICT_ON_SEND = %i[each_with_object to_h map collect []].freeze

# Internal helper class to hold match data
Captures = ::Struct.new(
:transformed_argname,
:transforming_body_expr
) do
def noop_transformation?
transforming_body_expr.lvar_type? && transforming_body_expr.children == [transformed_argname]
end
end

# Internal helper class to hold autocorrect data
Autocorrection = ::Struct.new(:match, :block_node, :leading, :trailing) do
def self.from_each_with_object(node, match)
new(match, node, 0, 0)
end

def self.from_to_h(node, match)
new(match, node, 0, 0)
end

def self.from_map_to_h(node, match)
if node.block_literal?
strip_trailing_chars = 0
else
map_range = node.children.first.source_range
node_range = node.source_range
strip_trailing_chars = node_range.end_pos - map_range.end_pos
end

new(match, node.children.first, 0, strip_trailing_chars)
end

def self.from_hash_brackets_map(node, match)
new(match, node.children.last, "#{node.receiver.source}[".length, ']'.length)
end

def strip_prefix_and_suffix(node, corrector)
expression = node.source_range
corrector.remove_leading(expression, leading)
corrector.remove_trailing(expression, trailing)
end

def set_new_method_name(new_method_name, corrector)
range = block_node.send_node.loc.selector
if (send_end = block_node.send_node.loc.end)
# If there are arguments (only true in the `each_with_object` case)
range = range.begin.join(send_end)
end
corrector.replace(range, new_method_name)
end

def set_new_arg_name(transformed_argname, corrector)
return if block_node.numblock_type?

corrector.replace(block_node.arguments, "|#{transformed_argname}|")
end

def set_new_body_expression(transforming_body_expr, corrector)
body_source = transforming_body_expr.source
body_source = "{ #{body_source} }" if transforming_body_expr.hash_type? && !transforming_body_expr.braces?

corrector.replace(block_node.body, body_source)
end
end

def on_block(node)
on_bad_each_with_object(node) do |*match|
handle_possible_offense(node, match, 'each_with_object')
Expand Down Expand Up @@ -102,71 +167,6 @@ def execute_correction(corrector, node, correction)
correction.set_new_arg_name(captures.transformed_argname, corrector)
correction.set_new_body_expression(captures.transforming_body_expr, corrector)
end

# Internal helper class to hold match data
Captures = ::Struct.new(
:transformed_argname,
:transforming_body_expr
) do
def noop_transformation?
transforming_body_expr.lvar_type? && transforming_body_expr.children == [transformed_argname]
end
end

# Internal helper class to hold autocorrect data
Autocorrection = ::Struct.new(:match, :block_node, :leading, :trailing) do
def self.from_each_with_object(node, match)
new(match, node, 0, 0)
end

def self.from_to_h(node, match)
new(match, node, 0, 0)
end

def self.from_map_to_h(node, match)
if node.block_literal?
strip_trailing_chars = 0
else
map_range = node.children.first.source_range
node_range = node.source_range
strip_trailing_chars = node_range.end_pos - map_range.end_pos
end

new(match, node.children.first, 0, strip_trailing_chars)
end

def self.from_hash_brackets_map(node, match)
new(match, node.children.last, "#{node.receiver.source}[".length, ']'.length)
end

def strip_prefix_and_suffix(node, corrector)
expression = node.source_range
corrector.remove_leading(expression, leading)
corrector.remove_trailing(expression, trailing)
end

def set_new_method_name(new_method_name, corrector)
range = block_node.send_node.loc.selector
if (send_end = block_node.send_node.loc.end)
# If there are arguments (only true in the `each_with_object` case)
range = range.begin.join(send_end)
end
corrector.replace(range, new_method_name)
end

def set_new_arg_name(transformed_argname, corrector)
return if block_node.numblock_type?

corrector.replace(block_node.arguments, "|#{transformed_argname}|")
end

def set_new_body_expression(transforming_body_expr, corrector)
body_source = transforming_body_expr.source
body_source = "{ #{body_source} }" if transforming_body_expr.hash_type? && !transforming_body_expr.braces?

corrector.replace(block_node.body, body_source)
end
end
end
end
end

0 comments on commit f9adec0

Please sign in to comment.