Sha256: ee4b82e1a2a47d9bb1e51e10bcf4e9684627f1909fd60b3c91a86b87b37af7e4

Contents?: true

Size: 1.31 KB

Versions: 166

Compression:

Stored size: 1.31 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, ignore_last: false)
        return if children.empty?

        return unless method_uses_parens?(node, children.first)

        check_children_line_break(node, children, ignore_last: ignore_last)
      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, ignore_last: false)
        return if children.empty?

        line = start.first_line

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

        max_line = last_line(children, ignore_last: ignore_last)
        return if line == max_line

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

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

      def last_line(nodes, ignore_last:)
        if ignore_last
          nodes.map(&:first_line)
        else
          nodes.map(&:last_line)
        end.max
      end
    end
  end
end

Version data entries

166 entries across 165 versions & 17 rubygems

Version Path
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/first_element_line_break.rb