-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
string_include.rb
65 lines (55 loc) · 2.06 KB
/
string_include.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
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# Identifies unnecessary use of a regex where `String#include?` would suffice.
#
# @safety
# This cop's offenses are not safe to autocorrect if a receiver is nil or a Symbol.
#
# @example
# # bad
# str.match?(/ab/)
# /ab/.match?(str)
# str =~ /ab/
# /ab/ =~ str
# str.match(/ab/)
# /ab/.match(str)
# /ab/ === str
#
# # good
# str.include?('ab')
class StringInclude < Base
extend AutoCorrector
MSG = 'Use `%<negation>sString#include?` instead of a regex match with literal-only pattern.'
RESTRICT_ON_SEND = %i[match =~ !~ match? ===].freeze
def_node_matcher :redundant_regex?, <<~PATTERN
{(call $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
(send (regexp (str $#literal?) (regopt)) {:match :match? :===} $_)
(match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)
(send (regexp (str $#literal?) (regopt)) :=~ $_)}
PATTERN
# rubocop:disable Metrics/AbcSize
def on_send(node)
return unless (receiver, regex_str = redundant_regex?(node))
negation = node.send_type? && node.method?(:!~)
message = format(MSG, negation: ('!' if negation))
add_offense(node, message: message) do |corrector|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
regex_str = interpret_string_escapes(regex_str)
dot = node.loc.dot ? node.loc.dot.source : '.'
new_source = "#{'!' if negation}#{receiver.source}#{dot}include?(#{to_string_literal(regex_str)})"
corrector.replace(node, new_source)
end
end
# rubocop:enable Metrics/AbcSize
alias on_csend on_send
alias on_match_with_lvasgn on_send
private
def literal?(regex_str)
regex_str.match?(/\A#{Util::LITERAL_REGEX}+\z/o)
end
end
end
end
end