rubocop-magic_numbers 0.7.0 → 0.7.1
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 +4 -4
- data/README.md +7 -0
- data/lib/rubocop/cop/magic_numbers/no_argument.rb +21 -28
- data/lib/rubocop/magic_numbers/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 63602a8a62b0911504ec48643cfa23d0b72c53b79d6003e84ef480a154ba9172
|
|
4
|
+
data.tar.gz: 5a7a342e4dce635c700f1670346128fedc15ecfb7fb0be189eba123b890bbb5f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 45909c1e736672257304a1e579b32409138a2cfb0b90b11ca904946aff491e48063610c1123ec5b73cf0c52d20ccdf4bff118e6ff3cfe4546236749a26b73eb9
|
|
7
|
+
data.tar.gz: 8988333f7e772d6ecc8e4a22bdd3dc72f7b93f0a3d3270ee3f1242aaee9c30d8974f3f8bb1c298af3344ead8557263aaf5a47a14d530425c76e40c2bcdcf938d
|
data/README.md
CHANGED
|
@@ -61,6 +61,13 @@ monthly_average = total / 28 # why 28?
|
|
|
61
61
|
monthly_average = total / FOUR_WEEK_MONTH_IN_DAYS
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
+
Magic numbers are detected in any argument position:
|
|
65
|
+
|
|
66
|
+
``` ruby
|
|
67
|
+
# BAD
|
|
68
|
+
@user.widgets_between(start_date, 20)
|
|
69
|
+
```
|
|
70
|
+
|
|
64
71
|
### MagicNumbers/NoAssignment
|
|
65
72
|
|
|
66
73
|
``` ruby
|
|
@@ -36,20 +36,6 @@ module RuboCop
|
|
|
36
36
|
#
|
|
37
37
|
# GOOD: Object.bottles_on_the_wall(count: DEFAULT_BOTTLE_COUNT)
|
|
38
38
|
class NoArgument < RuboCop::Cop::MagicNumbers::Base
|
|
39
|
-
MAGIC_NUMBER_ARGUMENT_PATTERN = <<-PATTERN
|
|
40
|
-
(send
|
|
41
|
-
{
|
|
42
|
-
_
|
|
43
|
-
_
|
|
44
|
-
(%<illegal_scalar_pattern>s $_)
|
|
45
|
-
| # This is a union of lhs and rhs literal
|
|
46
|
-
(%<illegal_scalar_pattern>s $_)
|
|
47
|
-
_
|
|
48
|
-
_
|
|
49
|
-
}
|
|
50
|
-
)
|
|
51
|
-
PATTERN
|
|
52
|
-
|
|
53
39
|
ARGUMENT_MSG = 'Do not use magic number arguments to methods'
|
|
54
40
|
|
|
55
41
|
CONFIG_IGNORED_METHODS_NAME = 'IgnoredMethods'
|
|
@@ -74,12 +60,25 @@ module RuboCop
|
|
|
74
60
|
CONFIG_CHECK_HASHES_NAME => false
|
|
75
61
|
}.freeze
|
|
76
62
|
|
|
63
|
+
# A forbidden numeric literal that isn't in PermittedValues
|
|
64
|
+
def_node_matcher :magic_number?, '(#forbidden_numeric? !#permitted_value?)'
|
|
65
|
+
|
|
66
|
+
# A magic number as any argument, or as the receiver of a call with
|
|
67
|
+
# exactly one argument. The receiver case catches numbers on the left of
|
|
68
|
+
# binary operators (`10 * foo`) without flagging calls like `10.times`.
|
|
69
|
+
def_node_matcher :magic_number_argument?, <<~PATTERN
|
|
70
|
+
{
|
|
71
|
+
(send _ _ <#magic_number? ...>)
|
|
72
|
+
(send #magic_number? _ _)
|
|
73
|
+
}
|
|
74
|
+
PATTERN
|
|
75
|
+
|
|
77
76
|
def cop_config
|
|
78
77
|
DEFAULT_CONFIG.merge(super)
|
|
79
78
|
end
|
|
80
79
|
|
|
81
80
|
def on_message_send(node)
|
|
82
|
-
return unless illegal_argument?(node)
|
|
81
|
+
return unless illegal_argument?(node)
|
|
83
82
|
return if ignored_method?(node)
|
|
84
83
|
|
|
85
84
|
add_offense(node, message: ARGUMENT_MSG)
|
|
@@ -99,14 +98,7 @@ module RuboCop
|
|
|
99
98
|
end
|
|
100
99
|
|
|
101
100
|
def illegal_argument?(node)
|
|
102
|
-
|
|
103
|
-
node: node,
|
|
104
|
-
pattern: format(
|
|
105
|
-
MAGIC_NUMBER_ARGUMENT_PATTERN,
|
|
106
|
-
illegal_scalar_pattern: illegal_scalar_pattern
|
|
107
|
-
)
|
|
108
|
-
)
|
|
109
|
-
captured_value && !permitted_values.include?(captured_value)
|
|
101
|
+
magic_number_argument?(node) || illegal_nested_argument?(node)
|
|
110
102
|
end
|
|
111
103
|
|
|
112
104
|
# Check whether any argument is a range, hash, or array literal (as
|
|
@@ -117,7 +109,7 @@ module RuboCop
|
|
|
117
109
|
|
|
118
110
|
def contains_illegal_number?(node)
|
|
119
111
|
nested_values(node).any? do |value|
|
|
120
|
-
|
|
112
|
+
magic_number?(value) || contains_illegal_number?(value)
|
|
121
113
|
end
|
|
122
114
|
end
|
|
123
115
|
|
|
@@ -134,11 +126,12 @@ module RuboCop
|
|
|
134
126
|
node.hash_type? ? node.pairs.map(&:value) : node.children.compact
|
|
135
127
|
end
|
|
136
128
|
|
|
137
|
-
def
|
|
138
|
-
|
|
139
|
-
|
|
129
|
+
def forbidden_numeric?(type)
|
|
130
|
+
forbidden_numerics.include?(type)
|
|
131
|
+
end
|
|
140
132
|
|
|
141
|
-
|
|
133
|
+
def permitted_value?(value)
|
|
134
|
+
permitted_values.include?(value)
|
|
142
135
|
end
|
|
143
136
|
|
|
144
137
|
def permitted_values
|