Sha256: af31a8c0babe2198d2dff30e642ee1006b503b870c4878c14072cd208a0d4a6a

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for trailing comma in array and hash literals.
      #
      # @example
      #   # always bad
      #   a = [1, 2,]
      #
      #   # good if EnforcedStyleForMultiline is consistent_comma
      #   a = [
      #     1, 2,
      #     3,
      #   ]
      #
      #   # good if EnforcedStyleForMultiline is comma or consistent_comma
      #   a = [
      #     1,
      #     2,
      #   ]
      #
      #   # good if EnforcedStyleForMultiline is no_comma
      #   a = [
      #     1,
      #     2
      #   ]
      class TrailingCommaInLiteral < Cop
        include ArraySyntax
        include TrailingComma

        def on_array(node)
          check_literal(node, 'item of %s array') if square_brackets?(node)
        end

        def on_hash(node)
          check_literal(node, 'item of %s hash')
        end

        private

        def check_literal(node, kind)
          return if node.children.empty?
          # A braceless hash is the last parameter of a method call and will be
          # checked as such.
          return unless brackets?(node)

          check(node, node.children, kind,
                node.children.last.source_range.end_pos,
                node.loc.end.begin_pos)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 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_literal.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_literal.rb
rubocop-0.43.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.42.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.41.2 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.41.1 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.41.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.40.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.39.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.38.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.37.2 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.37.1 lib/rubocop/cop/style/trailing_comma_in_literal.rb
rubocop-0.37.0 lib/rubocop/cop/style/trailing_comma_in_literal.rb