-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ambiguous_regexp_literal.rb
55 lines (47 loc) · 1.58 KB
/
ambiguous_regexp_literal.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for ambiguous regexp literals in the first argument of
# a method invocation without parentheses.
#
# @example
#
# # bad
#
# # This is interpreted as a method invocation with a regexp literal,
# # but it could possibly be `/` method invocations.
# # (i.e. `do_something./(pattern)./(i)`)
# do_something /pattern/i
#
# @example
#
# # good
#
# # With parentheses, there's no ambiguity.
# do_something(/pattern/i)
class AmbiguousRegexpLiteral < Base
extend AutoCorrector
MSG = 'Ambiguous regexp literal. Parenthesize the method arguments ' \
"if it's surely a regexp literal, or add a whitespace to the " \
'right of the `/` if it should be a division.'
def on_new_investigation
processed_source.diagnostics.each do |diagnostic|
next unless diagnostic.reason == :ambiguous_literal
offense_node = find_offense_node_by(diagnostic)
add_offense(diagnostic.location, severity: diagnostic.level) do |corrector|
add_parentheses(offense_node, corrector)
end
end
end
private
def find_offense_node_by(diagnostic)
node = processed_source.ast.each_node(:regexp).find do |regexp_node|
regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
end
node.parent
end
end
end
end
end