Sha256: b574f13f7083777d9b04843928da74ca125c6147d8f556b1ea4c4b01b6be60cc

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8
# 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)
          _receiver, _method_name, *args = *node
          return if args.empty?
          # It's impossible for a method call without parentheses to have
          # a trailing comma.
          return unless brackets?(node)

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

        private

        def avoid_autocorrect?(args)
          hash_with_braces?(args.last) && braces_will_be_removed?(args)
        end

        def hash_with_braces?(node)
          node.hash_type? && node.loc.begin
        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['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.size == 1 || !args[-2].hash_type?
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/trailing_comma_in_arguments.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.43.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.42.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.41.2 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.41.1 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.41.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb
rubocop-0.40.0 lib/rubocop/cop/style/trailing_comma_in_arguments.rb