Sha256: 1348d6aa98a52e9abb7a21dc151fb4709064204f72bf0726e34823902edd5fec

Contents?: true

Size: 1.13 KB

Versions: 64

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking for a line break before the first
    # element in a multi-line collection.
    module FirstElementLineBreak
      private

      def check_method_line_break(node, children)
        return if children.empty?

        return unless method_uses_parens?(node, children.first)

        check_children_line_break(node, children)
      end

      def method_uses_parens?(node, limit)
        source = node.source_range.source_line[0...limit.loc.column]
        /\s*\(\s*$/.match?(source)
      end

      def check_children_line_break(node, children, start = node)
        return if children.empty?

        line = start.first_line

        min = first_by_line(children)
        return if line != min.first_line

        max = last_by_line(children)
        return if line == max.last_line

        add_offense(min) { |corrector| EmptyLineCorrector.insert_before(corrector, min) }
      end

      def first_by_line(nodes)
        nodes.min_by(&:first_line)
      end

      def last_by_line(nodes)
        nodes.max_by(&:last_line)
      end
    end
  end
end

Version data entries

64 entries across 58 versions & 7 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/first_element_line_break.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/first_element_line_break.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/first_element_line_break.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.40.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.39.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.38.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.37.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.37.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.36.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.35.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.35.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-1.34.1 lib/rubocop/cop/mixin/first_element_line_break.rb