Sha256: 599f9516fb9153b90a6ac1b5bbbc9e242781a14f8dac362320ba5afab86def11

Contents?: true

Size: 1.09 KB

Versions: 17

Compression:

Stored size: 1.09 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
      def autocorrect(node)
        ->(corrector) { corrector.insert_before(node.source_range, "\n") }
      end

      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]
        source =~ /\s*\(\s*$/
      end

      def check_children_line_break(node, children, start = node)
        return if children.size < 2

        line = start.loc.line
        min = children.min_by { |n| n.loc.first_line }
        return if line != min.loc.first_line

        max = children.max_by { |n| n.loc.last_line }
        return if line == max.loc.last_line

        add_offense(min, :expression, self.class::MSG)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.49.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.49.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.48.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.48.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.47.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.47.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.46.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.45.0 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.44.1 lib/rubocop/cop/mixin/first_element_line_break.rb
rubocop-0.44.0 lib/rubocop/cop/mixin/first_element_line_break.rb