Sha256: 60b4adb39d841fe38f146a05b2820041be206c88edeab630ef8c48542c7872c2

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop ensures that each key in a multi-line hash
      # starts on a separate line.
      #
      # @example
      #
      #   # bad
      #   {
      #     a: 1, b: 2,
      #     c: 3
      #   }
      #
      #   # good
      #   {
      #     a: 1,
      #     b: 2,
      #     c: 3
      #   }
      class MultilineHashKeyLineBreaks < Cop
        include MultilineElementLineBreaks

        MSG = 'Each key in a multi-line hash must start on a ' \
          'separate line.'.freeze

        def on_hash(node)
          # This cop only deals with hashes wrapped by a set of curly
          # braces like {foo: 1}. That is, not a kwargs hashes.
          # Style/MultilineMethodArgumentLineBreaks handles those.
          return unless starts_with_curly_brace?(node)

          check_line_breaks(node, node.children) if node.loc.begin
        end

        def autocorrect(node)
          EmptyLineCorrector.insert_before(node)
        end

        private

        def starts_with_curly_brace?(node)
          node.loc.begin
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.68.1 lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb
rubocop-0.68.0 lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb
rubocop-0.67.2 lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb
rubocop-0.67.1 lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb
rubocop-0.67.0 lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb