rubocop-performance 1.20.0 → 1.20.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dad3fdab16d4dbcb3047e3319ab415b6e9ae3e2cbf14a73a1874831bf473701
|
4
|
+
data.tar.gz: ca98aec2aac71f768e63c5b5ed3c73c2a5ef8242433fc8fe078e9930f47a4d38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16536c625e9f66d73368d70153a139f72da96dee225807c1154c07e7a2df999be9f06278b1c6db95836baa6832524da39715f8240151236c5b29ec47ce99e91b
|
7
|
+
data.tar.gz: bfd72acdc38b7337c4b783fc8e5e14516aa8926980cfbf2051487350eb939648ac9af302e1f5c0abc889f3564efb98ff5a7551d107d8d28b2912c4c9a0128b79
|
@@ -16,13 +16,19 @@ module RuboCop
|
|
16
16
|
# send('do_something')
|
17
17
|
# attr_accessor 'do_something'
|
18
18
|
# instance_variable_get('@ivar')
|
19
|
-
#
|
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
|
-
#
|
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,40 +40,68 @@ module RuboCop
|
|
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
|
+
|
45
|
+
TWO_ARGUMENTS_METHOD = :alias_method
|
46
|
+
MULTIPLE_ARGUMENTS_METHODS = %i[
|
47
|
+
attr_accessor attr_reader attr_writer private private_constant
|
48
|
+
protected public public_constant module_function
|
49
|
+
].freeze
|
50
|
+
|
37
51
|
# NOTE: `attr` method is not included in this list as it can cause false positives in Nokogiri API.
|
38
52
|
# And `attr` may not be used because `Style/Attr` registers an offense.
|
39
53
|
# https://github.com/rubocop/rubocop-performance/issues/278
|
40
54
|
RESTRICT_ON_SEND = (%i[
|
41
|
-
class_variable_defined?
|
55
|
+
class_variable_defined? const_set
|
42
56
|
define_method instance_method method_defined? private_class_method? private_method_defined?
|
43
57
|
protected_method_defined? public_class_method public_instance_method public_method_defined?
|
44
58
|
remove_class_variable remove_method undef_method class_variable_get class_variable_set
|
45
59
|
deprecate_constant remove_const ruby2_keywords define_singleton_method instance_variable_defined?
|
46
60
|
instance_variable_get instance_variable_set method public_method public_send remove_instance_variable
|
47
61
|
respond_to? send singleton_method __send__
|
48
|
-
] + COMMAND_METHODS).freeze
|
62
|
+
] + COMMAND_METHODS + INTERPOLATION_IGNORE_METHODS).freeze
|
49
63
|
|
50
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
51
64
|
def on_send(node)
|
52
65
|
return if COMMAND_METHODS.include?(node.method_name) && node.receiver
|
53
|
-
return unless (first_argument = node.first_argument)
|
54
|
-
return unless first_argument.str_type? || first_argument.dstr_type?
|
55
|
-
|
56
|
-
first_argument_value = first_argument.value
|
57
|
-
return if first_argument_value.include?(' ') || first_argument_value.include?('::')
|
58
|
-
|
59
|
-
replacement = argument_replacement(first_argument, first_argument_value)
|
60
66
|
|
61
|
-
|
67
|
+
string_arguments(node).each do |string_argument|
|
68
|
+
string_argument_value = string_argument.value
|
69
|
+
next if string_argument_value.include?(' ') || string_argument_value.include?('::')
|
62
70
|
|
63
|
-
|
64
|
-
corrector.replace(first_argument, replacement)
|
71
|
+
register_offense(string_argument, string_argument_value)
|
65
72
|
end
|
66
73
|
end
|
67
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
68
74
|
|
69
75
|
private
|
70
76
|
|
77
|
+
def string_arguments(node)
|
78
|
+
arguments = if node.method?(TWO_ARGUMENTS_METHOD)
|
79
|
+
[node.first_argument, node.arguments[1]]
|
80
|
+
elsif MULTIPLE_ARGUMENTS_METHODS.include?(node.method_name)
|
81
|
+
node.arguments
|
82
|
+
else
|
83
|
+
[node.first_argument]
|
84
|
+
end
|
85
|
+
|
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) }
|
93
|
+
end
|
94
|
+
|
95
|
+
def register_offense(argument, argument_value)
|
96
|
+
replacement = argument_replacement(argument, argument_value)
|
97
|
+
|
98
|
+
message = format(MSG, symbol_arg: replacement, string_arg: argument.source)
|
99
|
+
|
100
|
+
add_offense(argument, message: message) do |corrector|
|
101
|
+
corrector.replace(argument, replacement)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
71
105
|
def argument_replacement(node, value)
|
72
106
|
if node.str_type?
|
73
107
|
value.to_sym.inspect
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-performance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.20.
|
4
|
+
version: 1.20.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2024-01-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|