class RubbyCop::Cop::Style::TrailingCommaInLiteral

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
]

Public Instance Methods

on_array(node) click to toggle source
# File lib/rubbycop/cop/style/trailing_comma_in_literal.rb, line 33
def on_array(node)
  check_literal(node, 'item of %s array') if node.square_brackets?
end
on_hash(node) click to toggle source
# File lib/rubbycop/cop/style/trailing_comma_in_literal.rb, line 37
def on_hash(node)
  check_literal(node, 'item of %s hash')
end

Private Instance Methods

check_literal(node, kind) click to toggle source
# File lib/rubbycop/cop/style/trailing_comma_in_literal.rb, line 43
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