Sha256: 4673bd9af57311a15fb202f1cf514d775ce4174e6139e0536812dd7821791644

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for trailing comma in argument lists.
      #
      # @example
      #   # always bad
      #   method(1, 2,)
      #
      #   # good if EnforcedStyleForMultiline is consistent_comma
      #   method(
      #     1, 2,
      #     3,
      #   )
      #
      #   # good if EnforcedStyleForMultiline is comma or consistent_comma
      #   method(
      #     1,
      #     2,
      #   )
      #
      #   # good if EnforcedStyleForMultiline is no_comma
      #   method(
      #     1,
      #     2
      #   )
      class TrailingCommaInArguments < Cop
        include TrailingComma

        def on_send(node)
          return unless node.arguments? && node.parenthesized?

          check(node, node.arguments, 'parameter of %s method call',
                node.last_argument.source_range.end_pos,
                node.source_range.end_pos)
        end

        private

        def avoid_autocorrect?(args)
          args.last.hash_type? && args.last.braces? &&
            braces_will_be_removed?(args)
        end

        # Returns true if running with --auto-correct would remove the braces
        # of the last argument.
        def braces_will_be_removed?(args)
          brace_config = config.for_cop('Style/BracesAroundHashParameters')
          return false unless brace_config.fetch('Enabled')
          return false if brace_config['AutoCorrect'] == false

          brace_style = brace_config['EnforcedStyle']
          return true if brace_style == 'no_braces'

          return false unless brace_style == 'context_dependent'

          args.one? || !args[-2].hash_type?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.51.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.50.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.49.1 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.49.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.48.1 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.48.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb