/
deprecated_class_methods.rb
150 lines (121 loc) · 4.24 KB
/
deprecated_class_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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for uses of the deprecated class method usages.
#
# @example
#
# # bad
#
# File.exists?(some_path)
# Dir.exists?(some_path)
# iterator?
# Socket.gethostbyname(host)
# Socket.gethostbyaddr(host)
#
# @example
#
# # good
#
# File.exist?(some_path)
# Dir.exist?(some_path)
# block_given?
# Addrinfo.getaddrinfo(nodename, service)
# Addrinfo.tcp(host, port).getnameinfo
class DeprecatedClassMethods < Base
extend AutoCorrector
# Inner class to DeprecatedClassMethods.
# This class exists to add abstraction and clean naming
# to the deprecated objects
class DeprecatedClassMethod
include RuboCop::AST::Sexp
attr_reader :method, :class_constant
def initialize(method, class_constant: nil, correctable: true)
@method = method
@class_constant = class_constant
@correctable = correctable
end
def class_nodes
@class_nodes ||=
if class_constant
[
s(:const, nil, class_constant),
s(:const, s(:cbase), class_constant)
]
else
[nil]
end
end
def correctable?
@correctable
end
def to_s
[class_constant, method].compact.join(delimeter)
end
private
def delimeter
CLASS_METHOD_DELIMETER
end
end
# Inner class to DeprecatedClassMethods.
# This class exists to add abstraction and clean naming
# to the replacements for deprecated objects
class Replacement
attr_reader :method, :class_constant
def initialize(method, class_constant: nil, instance_method: false)
@method = method
@class_constant = class_constant
@instance_method = instance_method
end
def to_s
[class_constant, method].compact.join(delimeter)
end
private
def delimeter
instance_method? ? INSTANCE_METHOD_DELIMETER : CLASS_METHOD_DELIMETER
end
def instance_method?
@instance_method
end
end
MSG = '`%<current>s` is deprecated in favor of `%<prefer>s`.'
DEPRECATED_METHODS_OBJECT = {
DeprecatedClassMethod.new(:exists?, class_constant: :File) =>
Replacement.new(:exist?, class_constant: :File),
DeprecatedClassMethod.new(:exists?, class_constant: :Dir) =>
Replacement.new(:exist?, class_constant: :Dir),
DeprecatedClassMethod.new(:iterator?) => Replacement.new(:block_given?),
DeprecatedClassMethod.new(:gethostbyaddr, class_constant: :Socket, correctable: false) =>
Replacement.new(:getnameinfo, class_constant: :Addrinfo, instance_method: true),
DeprecatedClassMethod.new(:gethostbyname, class_constant: :Socket, correctable: false) =>
Replacement.new(:getaddrinfo, class_constant: :Addrinfo, instance_method: true)
}.freeze
RESTRICT_ON_SEND = DEPRECATED_METHODS_OBJECT.keys.map(&:method).freeze
CLASS_METHOD_DELIMETER = '.'
INSTANCE_METHOD_DELIMETER = '#'
def on_send(node)
check(node) do |deprecated|
message = format(MSG, current: deprecated, prefer: replacement(deprecated))
add_offense(node.loc.selector, message: message) do |corrector|
if deprecated.correctable?
corrector.replace(node.loc.selector, replacement(deprecated).method)
end
end
end
end
private
def check(node)
DEPRECATED_METHODS_OBJECT.each_key do |deprecated|
next unless deprecated.class_nodes.include?(node.receiver)
next unless node.method?(deprecated.method)
yield deprecated
end
end
def replacement(deprecated)
DEPRECATED_METHODS_OBJECT[deprecated]
end
end
end
end
end