/
semicolon.rb
101 lines (81 loc) · 2.87 KB
/
semicolon.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
98
99
100
101
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for multiple expressions placed on the same line.
# It also checks for lines terminated with a semicolon.
#
# This cop has `AllowAsExpressionSeparator` configuration option.
# It allows `;` to separate several expressions on the same line.
#
# @example
# # bad
# foo = 1; bar = 2;
# baz = 3;
#
# # good
# foo = 1
# bar = 2
# baz = 3
#
# @example AllowAsExpressionSeparator: false (default)
# # bad
# foo = 1; bar = 2
#
# @example AllowAsExpressionSeparator: true
# # good
# foo = 1; bar = 2
class Semicolon < Cop
include RangeHelp
MSG = 'Do not use semicolons to terminate expressions.'
def investigate(processed_source)
return if processed_source.blank?
@processed_source = processed_source
check_for_line_terminator_or_opener
end
def on_begin(node) # rubocop:todo Metrics/CyclomaticComplexity
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(&:first_line)
lines = exprs_lines.group_by(&:itself)
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
def autocorrect(range)
return unless range
->(corrector) { corrector.remove(range) }
end
private
def check_for_line_terminator_or_opener
each_semicolon { |line, column| convention_on(line, column, true) }
end
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
def tokens_for_lines
@processed_source.tokens.group_by(&:line)
end
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
end
end
end
end