Module: RuboCop::Cop::TrailingComma

Includes:
ConfigurableEnforcedStyle
Included in:
Style::TrailingCommaInArguments, Style::TrailingCommaInLiteral
Defined in:
lib/rubocop/cop/mixin/trailing_comma.rb

Overview

Common methods shared by Style/TrailingCommaInArguments and Style/TrailingCommaInLiteral

Constant Summary collapse

MSG =
'%s comma after the last %s'.freeze

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_detected, #supported_styles, #unexpected_style_detected

Instance Method Details

#autocorrect(range) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 125

def autocorrect(range)
  lambda do |corrector|
    case range.source
    when ',' then corrector.remove(range)
    else          corrector.insert_after(range, ',')
    end
  end
end

#avoid_comma(kind, comma_begin_pos, sb, extra_info) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 103

def avoid_comma(kind, comma_begin_pos, sb, extra_info)
  range = Parser::Source::Range.new(sb, comma_begin_pos,
                                    comma_begin_pos + 1)
  article = kind =~ /array/ ? 'an' : 'a'
  add_offense(range, range,
              format(MSG, 'Avoid', format(kind, article)) +
              "#{extra_info}.")
end

#brackets?(node) ⇒ Boolean

Returns true if the node has round/square/curly brackets.

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 60

def brackets?(node)
  node.loc.end
end

#check(node, items, kind, begin_pos, end_pos) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 17

def check(node, items, kind, begin_pos, end_pos)
  sb = node.source_range.source_buffer
  after_last_item = Parser::Source::Range.new(sb, begin_pos, end_pos)

  return if heredoc?(after_last_item.source)

  comma_offset = after_last_item.source =~ /,/

  if comma_offset && !inside_comment?(after_last_item, comma_offset)
    unless should_have_comma?(style, node)
      extra_info = case style
                   when :comma
                     ', unless each item is on its own line'
                   when :consistent_comma
                     ', unless items are split onto multiple lines'
                   else
                     ''
                   end
      avoid_comma(kind, after_last_item.begin_pos + comma_offset, sb,
                  extra_info)
    end
  elsif should_have_comma?(style, node)
    put_comma(items, kind, sb)
  end
end

#heredoc?(source_after_last_item) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 55

def heredoc?(source_after_last_item)
  source_after_last_item =~ /\w/
end

#inside_comment?(range, comma_offset) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 48

def inside_comment?(range, comma_offset)
  processed_source.comments.any? do |comment|
    comment_offset = comment.loc.expression.begin_pos - range.begin_pos
    comment_offset >= 0 && comment_offset < comma_offset
  end
end

#multiline?(node) ⇒ Boolean

Returns true if the round/square/curly brackets of the given node are on different lines, and each item within is on its own line, and the closing bracket is on its own line.

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 67

def multiline?(node)
  elements = if node.type == :send
               _receiver, _method_name, *args = *node
               args.flat_map do |a|
                 # For each argument, if it is a multi-line hash,
                 # then promote the hash elements to method arguments
                 # for the purpose of determining multi-line-ness.
                 if a.hash_type? && a.loc.first_line != a.loc.last_line
                   a.children
                 else
                   a
                 end
               end
             else
               node.children
             end

  # No need to process anything if the whole node is not multiline
  # Without the 2nd check, Foo.new({}) is considered multiline, which
  # it should not be. Essentially, if there are no elements, the
  # expression can not be multiline.
  return if !node.multiline? || elements.empty?

  items = elements.map(&:source_range)
  if style == :consistent_comma
    items.one? || items.each_cons(2).any? { |a, b| !on_same_line?(a, b) }
  else
    items << node.loc.end
    items.each_cons(2).all? { |a, b| !on_same_line?(a, b) }
  end
end

#on_same_line?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 99

def on_same_line?(a, b)
  a.last_line == b.line
end

#parameter_nameObject



13
14
15
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 13

def parameter_name
  'EnforcedStyleForMultiline'
end

#put_comma(items, kind, sb) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 112

def put_comma(items, kind, sb)
  last_item = items.last
  return if last_item.type == :block_pass

  last_expr = last_item.source_range
  ix = last_expr.source.rindex("\n") || 0
  ix += last_expr.source[ix..-1] =~ /\S/
  range = Parser::Source::Range.new(sb, last_expr.begin_pos + ix,
                                    last_expr.end_pos)
  add_offense(range, range,
              format(MSG, 'Put a', format(kind, 'a multiline') + '.'))
end

#should_have_comma?(style, node) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 43

def should_have_comma?(style, node)
  [:comma, :consistent_comma].include?(style) &&
    multiline?(node)
end