Class: Rubocop::Cop::Semicolon

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/semicolon.rb

Constant Summary collapse

MSG =
'Do not use semicolons to terminate expressions.'

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name, #on_comment

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect(source, tokens, ast, comments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/semicolon.rb', line 8

def inspect(source, tokens, ast, comments)
  on_node(:begin, ast) do |node|
    exprs = node.children

    next if exprs.size < 2

    # create a map matching lines to the number of expressions on them
    exprs_lines = exprs.map { |e| e.loc.expression.line }
    lines = exprs_lines.group_by { |i| i }

    # every line with more than 1 expression on it is an offence
    lines.each do |line, expr_on_line|
      add_offence(:convention, line, MSG) if expr_on_line.size > 1
    end
  end

  # not pretty reliable, but the best we can do for now
  source.each_with_index do |line, index|
    add_offence(:convention, index, MSG) if line =~ /;\s*\z/
  end
end