class RuboCop::Cop::Style::Semicolon

This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.

@example

# bad
foo = 1; bar = 2;
baz = 3;

# good
foo = 1
bar = 2
baz = 3

Constants

MSG

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 58
def autocorrect(range)
  return unless range

  ->(corrector) { corrector.remove(range) }
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 23
def investigate(processed_source)
  return if processed_source.blank?

  @processed_source = processed_source

  check_for_line_terminator_or_opener
end
on_begin(node) click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 31
def on_begin(node)
  return if cop_config['AllowAsExpressionSeparator']

  exprs = node.children

  return if exprs.size < 2

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

  lines.each do |line, expr_on_line|
    # Every line with more than one expression on it is a
    # potential offense
    next unless expr_on_line.size > 1

    # TODO: Find the correct position of the semicolon. We don't know
    # if the first semicolon on the line is a separator of
    # expressions. It's just a guess.
    column = @processed_source[line - 1].index(';')

    next unless column

    convention_on(line, column, false)
  end
end

Private Instance Methods

check_for_line_terminator_or_opener() click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 66
def check_for_line_terminator_or_opener
  each_semicolon { |line, column| convention_on(line, column, true) }
end
convention_on(line, column, autocorrect) click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 81
def convention_on(line, column, autocorrect)
  range = source_range(@processed_source.buffer, line, column)
  # Don't attempt to autocorrect if semicolon is separating statements
  # on the same line
  add_offense(autocorrect ? range : nil, location: range)
end
each_semicolon() { |line, last.column| ... } click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 70
def each_semicolon
  tokens_for_lines.each do |line, tokens|
    yield line, tokens.last.column if tokens.last.semicolon?
    yield line, tokens.first.column if tokens.first.semicolon?
  end
end
tokens_for_lines() click to toggle source
# File lib/rubocop/cop/style/semicolon.rb, line 77
def tokens_for_lines
  @processed_source.tokens.group_by(&:line)
end