Sha256: 2a52b6887c63733fe34efd4cd6d858ab58ba4eec6a39b29bbf207aeaaf0ef5c4

Contents?: true

Size: 1.12 KB

Versions: 189

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This autocorrects unused arguments.
    class UnusedArgCorrector
      extend RangeHelp

      class << self
        attr_reader :processed_source

        def correct(corrector, processed_source, node)
          return if %i[kwarg kwoptarg].include?(node.type)

          @processed_source = processed_source

          if node.blockarg_type?
            correct_for_blockarg_type(corrector, node)
          else
            variable_name = if node.optarg_type?
                              node.node_parts[0]
                            else
                              # Extract only a var name without splat (`*`)
                              node.source.gsub(/\A\*+/, '')
                            end

            corrector.replace(node.loc.name, "_#{variable_name}")
          end
        end

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

          corrector.remove(range)
        end
      end
    end
  end
end

Version data entries

189 entries across 182 versions & 18 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.69.2 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.69.1 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.69.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.68.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.67.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.66.1 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.66.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.65.1 lib/rubocop/cop/correctors/unused_arg_corrector.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.65.0 lib/rubocop/cop/correctors/unused_arg_corrector.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.64.1 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.63.4 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.63.3 lib/rubocop/cop/correctors/unused_arg_corrector.rb
rubocop-1.63.2 lib/rubocop/cop/correctors/unused_arg_corrector.rb