/
redundant_regexp_character_class.rb
89 lines (78 loc) · 2.37 KB
/
redundant_regexp_character_class.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
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for unnecessary single-element Regexp character classes.
#
# @example
#
# # bad
# r = /[x]/
#
# # good
# r = /x/
#
# # bad
# r = /[\s]/
#
# # good
# r = /\s/
#
# # good
# r = /[ab]/
class RedundantRegexpCharacterClass < Cop
include MatchRange
include RegexpLiteralHelp
MSG_REDUNDANT_CHARACTER_CLASS = 'Redundant single-element character class, ' \
'`%<char_class>s` can be replaced with `%<element>s`.'
PATTERN = /
(
(?<!\\) # No \-prefix (i.e. not escaped)
\[ # Literal [
(?!\#\{) # Not (the start of) an interpolation
(?: # Either...
\\[^b] | # Any escaped character except b (which would change behaviour)
[^.*+?{}()|$] | # or one that doesn't require escaping outside the character class
\\[upP]\{[^}]+\} # or a unicode code-point or property
)
\] # Literal ]
)
/x.freeze
def on_regexp(node)
each_redundant_character_class(node) do |loc|
next if whitespace_in_free_space_mode?(node, loc)
add_offense(
node,
location: loc,
message: format(
MSG_REDUNDANT_CHARACTER_CLASS,
char_class: loc.source,
element: without_character_class(loc)
)
)
end
end
def autocorrect(node)
lambda do |corrector|
each_redundant_character_class(node) do |loc|
corrector.replace(loc, without_character_class(loc))
end
end
end
def each_redundant_character_class(node)
pattern_source(node).scan(PATTERN) do
yield match_range(node.loc.begin.end, Regexp.last_match)
end
end
private
def without_character_class(loc)
loc.source[1..-2]
end
def whitespace_in_free_space_mode?(node, loc)
return false unless freespace_mode_regexp?(node)
/\[\s\]/.match?(loc.source)
end
end
end
end
end