Sha256: 4946c7c33cbcd6838fa4feb58d996a2dca79f8650d64dce49af29b596b1cfa53

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for redundant uses of double splat hash braces.
      #
      # @example
      #
      #   # bad
      #   do_something(**{foo: bar, baz: qux})
      #
      #   # good
      #   do_something(foo: bar, baz: qux)
      #
      class RedundantDoubleSplatHashBraces < Base
        extend AutoCorrector

        MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'

        def on_hash(node)
          return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
          return unless (parent = node.parent)
          return unless parent.kwsplat_type?

          add_offense(parent) do |corrector|
            corrector.remove(parent.loc.operator)
            corrector.remove(opening_brace(node))
            corrector.remove(closing_brace(node))
          end
        end

        private

        def opening_brace(node)
          node.loc.begin.join(node.children.first.loc.expression.begin)
        end

        def closing_brace(node)
          node.children.last.loc.expression.end.join(node.loc.end)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
rubocop-1.46.0 lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
rubocop-1.45.1 lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
rubocop-1.45.0 lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
rubocop-1.44.1 lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
rubocop-1.44.0 lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb