Sha256: 8ac09db03027417a53f3c4f1ee150d93e859269cfba16227d84eacf67f1a52ee

Contents?: true

Size: 1.99 KB

Versions: 147

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Ensures that each argument in a multi-line method call
      # starts on a separate line.
      #
      # NOTE: This cop does not move the first argument, if you want that to
      # be on a separate line, see `Layout/FirstMethodArgumentLineBreak`.
      #
      # @example
      #
      #   # bad
      #   foo(a, b,
      #     c
      #   )
      #
      #   # bad
      #   foo(a, b, {
      #     foo: "bar",
      #   })
      #
      #   # good
      #   foo(
      #     a,
      #     b,
      #     c
      #   )
      #
      #   # good
      #   foo(a, b, c)
      #
      # @example AllowMultilineFinalElement: false (default)
      #
      #   # good
      #   foo(
      #     a,
      #     b,
      #     {
      #       foo: "bar",
      #     }
      #   )
      #
      # @example AllowMultilineFinalElement: true
      #
      #   # good
      #   foo(
      #     a,
      #     b,
      #     {
      #       foo: "bar",
      #     }
      #   )
      #
      class MultilineMethodArgumentLineBreaks < Base
        include MultilineElementLineBreaks
        extend AutoCorrector

        MSG = 'Each argument in a multi-line method call must start on a separate line.'

        def on_send(node)
          return if node.method?(:[]=)

          args = node.arguments

          # If there is a trailing hash arg without explicit braces, like this:
          #
          #    method(1, 'key1' => value1, 'key2' => value2)
          #
          # ...then each key/value pair is treated as a method 'argument'
          # when determining where line breaks should appear.
          last_arg = args.last
          args = args[0...-1] + last_arg.children if last_arg&.hash_type? && !last_arg&.braces?

          check_line_breaks(node, args, ignore_last: ignore_last_element?)
        end

        private

        def ignore_last_element?
          !!cop_config['AllowMultilineFinalElement']
        end
      end
    end
  end
end

Version data entries

147 entries across 146 versions & 15 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.69.2 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.69.1 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.69.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.68.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.67.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.66.1 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.66.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.65.1 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.65.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.64.1 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.63.4 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.63.3 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.63.2 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.63.1 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
rubocop-1.63.0 lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb