@@ -16,13 +16,19 @@ module Performance
16
16
# send('do_something')
17
17
# attr_accessor 'do_something'
18
18
# instance_variable_get('@ivar')
19
- # const_get ("string_#{interpolation}")
19
+ # respond_to? ("string_#{interpolation}")
20
20
#
21
21
# # good
22
22
# send(:do_something)
23
23
# attr_accessor :do_something
24
24
# instance_variable_get(:@ivar)
25
- # const_get(:"string_#{interpolation}")
25
+ # respond_to?(:"string_#{interpolation}")
26
+ #
27
+ # # good - these methods don't support namespaced symbols
28
+ # const_get("#{module_path}::Base")
29
+ # const_source_location("#{module_path}::Base")
30
+ # const_defined?("#{module_path}::Base")
31
+ #
26
32
#
27
33
class StringIdentifierArgument < Base
28
34
extend AutoCorrector
@@ -34,6 +40,8 @@ class StringIdentifierArgument < Base
34
40
protected public public_constant module_function
35
41
] . freeze
36
42
43
+ INTERPOLATION_IGNORE_METHODS = %i[ const_get const_source_location const_defined? ] . freeze
44
+
37
45
TWO_ARGUMENTS_METHOD = :alias_method
38
46
MULTIPLE_ARGUMENTS_METHODS = %i[
39
47
attr_accessor attr_reader attr_writer private private_constant
@@ -44,14 +52,14 @@ class StringIdentifierArgument < Base
44
52
# And `attr` may not be used because `Style/Attr` registers an offense.
45
53
# https://github.com/rubocop/rubocop-performance/issues/278
46
54
RESTRICT_ON_SEND = ( %i[
47
- class_variable_defined? const_defined? const_get const_set const_source_location
55
+ class_variable_defined? const_set
48
56
define_method instance_method method_defined? private_class_method? private_method_defined?
49
57
protected_method_defined? public_class_method public_instance_method public_method_defined?
50
58
remove_class_variable remove_method undef_method class_variable_get class_variable_set
51
59
deprecate_constant remove_const ruby2_keywords define_singleton_method instance_variable_defined?
52
60
instance_variable_get instance_variable_set method public_method public_send remove_instance_variable
53
61
respond_to? send singleton_method __send__
54
- ] + COMMAND_METHODS ) . freeze
62
+ ] + COMMAND_METHODS + INTERPOLATION_IGNORE_METHODS ) . freeze
55
63
56
64
def on_send ( node )
57
65
return if COMMAND_METHODS . include? ( node . method_name ) && node . receiver
@@ -75,9 +83,13 @@ def string_arguments(node)
75
83
[ node . first_argument ]
76
84
end
77
85
78
- arguments . compact . filter do |argument |
79
- argument . str_type? || argument . dstr_type?
80
- end
86
+ arguments . compact . filter { |argument | string_argument_compatible? ( argument , node ) }
87
+ end
88
+
89
+ def string_argument_compatible? ( argument , node )
90
+ return true if argument . str_type?
91
+
92
+ argument . dstr_type? && INTERPOLATION_IGNORE_METHODS . none? { |method | node . method? ( method ) }
81
93
end
82
94
83
95
def register_offense ( argument , argument_value )
0 commit comments