/
string_methods.rb
46 lines (39 loc) · 1.05 KB
/
string_methods.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
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop enforces the use of consistent method names
# from the String class.
#
# @example
# # bad
# 'name'.intern
# 'var'.unfavored_method
#
# # good
# 'name'.to_sym
# 'var'.preferred_method
class StringMethods < Cop
include MethodPreference
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'.freeze
def on_send(node)
return unless preferred_method(node.method_name)
add_offense(node, location: :selector)
end
alias on_csend on_send
def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.selector,
preferred_method(node.method_name))
end
end
private
def message(node)
format(MSG,
prefer: preferred_method(node.method_name),
current: node.method_name)
end
end
end
end
end