rubocop-magic_numbers 0.5.0 → 0.6.0
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 +24 -1
- data/lib/rubocop/cop/magic_numbers/no_assignment.rb +35 -7
- data/lib/rubocop/magic_numbers/version.rb +1 -1
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c524463c1bd7ab5a180f5063ee27deceac29fdf0bfe9d682b03ce1f6f8a992d
|
|
4
|
+
data.tar.gz: ca98482dc543d684dad741da511de7f873ae83dd2ae714fb5115b112855b412c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 421a00bdfe1b50f2ac56d4759577dd8f77967a3a24a2111bb901563ec09fee149dc2e64cd45661ea8761ac0c8f9594ecd2e7f82aaddd78f3136651601dee7632
|
|
7
|
+
data.tar.gz: 177fca60d9af08b2d409bc1e5018f9a9d4f7a3995fc4d04fe2e9efeafa0a9ccd2863ece0b90ce8a9ddcf078f1cb8bd1a251f8ff99ff870377f21d1903ee1a7b5
|
data/README.md
CHANGED
|
@@ -123,8 +123,12 @@ MagicNumbers/NoArgument:
|
|
|
123
123
|
- 1
|
|
124
124
|
MagicNumbers/NoAssignment:
|
|
125
125
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
126
|
+
PermittedValues: # defaults to []
|
|
127
|
+
- 0
|
|
128
|
+
- 1
|
|
129
|
+
- 3.14
|
|
126
130
|
|
|
127
|
-
MagicNumbers/
|
|
131
|
+
MagicNumbers/NoDefault:
|
|
128
132
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
129
133
|
|
|
130
134
|
MagicNumbers/NoReturn:
|
|
@@ -132,6 +136,25 @@ MagicNumbers/NoReturn:
|
|
|
132
136
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
133
137
|
```
|
|
134
138
|
|
|
139
|
+
`MagicNumbers/NoAssignment` can be configured with `PermittedValues` when some
|
|
140
|
+
assigned values do not need the same explanation as other numeric values. This
|
|
141
|
+
allows configured values in local variable, instance variable, property, and
|
|
142
|
+
multiple assignments. Unpermitted magic numbers are still detected:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
# Allowed when MagicNumbers/NoAssignment permits 0 and 1
|
|
146
|
+
count = 0
|
|
147
|
+
@total = 1
|
|
148
|
+
first, second = 0, 1
|
|
149
|
+
|
|
150
|
+
# Still detected as an offense
|
|
151
|
+
retry_limit = 3
|
|
152
|
+
first, second = 2
|
|
153
|
+
first, second = 0, 2
|
|
154
|
+
first, second = 2, 0
|
|
155
|
+
first, second = 2, 3
|
|
156
|
+
```
|
|
157
|
+
|
|
135
158
|
For more information on configuring `rubocop`, please refer to the [official documentation](https://docs.rubocop.org/rubocop/configuration.html).
|
|
136
159
|
|
|
137
160
|
## Rails usage
|
|
@@ -17,14 +17,17 @@ module RuboCop
|
|
|
17
17
|
(send
|
|
18
18
|
({send self} ...)
|
|
19
19
|
$_
|
|
20
|
-
(%<illegal_scalar_pattern>s _)
|
|
20
|
+
$(%<illegal_scalar_pattern>s _)
|
|
21
21
|
)
|
|
22
22
|
PATTERN
|
|
23
23
|
|
|
24
24
|
MAGIC_NUMBER_MULTI_ASSIGN_PATTERN = <<-PATTERN
|
|
25
25
|
(masgn
|
|
26
26
|
(mlhs ({lvasgn ivasgn send} ...)+)
|
|
27
|
-
|
|
27
|
+
{
|
|
28
|
+
$(%<illegal_scalar_pattern>s _)
|
|
29
|
+
$(array <(%<illegal_scalar_pattern>s _) ...>)
|
|
30
|
+
}
|
|
28
31
|
)
|
|
29
32
|
PATTERN
|
|
30
33
|
LOCAL_VARIABLE_ASSIGN_MSG = 'Do not use magic number local variables'
|
|
@@ -32,7 +35,8 @@ module RuboCop
|
|
|
32
35
|
MULTIPLE_ASSIGN_MSG = 'Do not use magic numbers in multiple assignments'
|
|
33
36
|
PROPERTY_MSG = 'Do not use magic numbers to set properties'
|
|
34
37
|
DEFAULT_CONFIG = {
|
|
35
|
-
'AllowedAssignments' => %w[class_variables global_variables]
|
|
38
|
+
'AllowedAssignments' => %w[class_variables global_variables],
|
|
39
|
+
'PermittedValues' => []
|
|
36
40
|
}.freeze
|
|
37
41
|
|
|
38
42
|
def cop_config
|
|
@@ -76,7 +80,7 @@ module RuboCop
|
|
|
76
80
|
private
|
|
77
81
|
|
|
78
82
|
def illegal_scalar_argument_to_setter?(node)
|
|
79
|
-
method = node_matches_pattern?(
|
|
83
|
+
method, value = node_matches_pattern?(
|
|
80
84
|
node: node,
|
|
81
85
|
pattern: format(
|
|
82
86
|
MAGIC_NUMBER_ARGUMENT_TO_SETTER_PATTERN,
|
|
@@ -84,17 +88,19 @@ module RuboCop
|
|
|
84
88
|
)
|
|
85
89
|
)
|
|
86
90
|
|
|
87
|
-
method&.end_with?('=')
|
|
91
|
+
method&.end_with?('=') && illegal_scalar_expression?(value)
|
|
88
92
|
end
|
|
89
93
|
|
|
90
94
|
def illegal_multi_assign_right_hand_side?(node)
|
|
91
|
-
node_matches_pattern?(
|
|
95
|
+
multiple_assignment_value = node_matches_pattern?(
|
|
92
96
|
node: node,
|
|
93
97
|
pattern: format(
|
|
94
98
|
MAGIC_NUMBER_MULTI_ASSIGN_PATTERN,
|
|
95
99
|
illegal_scalar_pattern: illegal_scalar_pattern
|
|
96
100
|
)
|
|
97
101
|
)
|
|
102
|
+
|
|
103
|
+
illegal_scalar_expressions(multiple_assignment_value).any?
|
|
98
104
|
end
|
|
99
105
|
|
|
100
106
|
def illegal_scalar_value?(node)
|
|
@@ -103,7 +109,29 @@ module RuboCop
|
|
|
103
109
|
# multiassignment nodes contain individual assignments in their AST
|
|
104
110
|
# representations, but they aren't aware of their values, so we need to
|
|
105
111
|
# allow for expressionless assignments
|
|
106
|
-
|
|
112
|
+
illegal_scalar_expression?(node.expression)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def illegal_scalar_expression?(expression)
|
|
116
|
+
return false unless expression
|
|
117
|
+
return false unless forbidden_numerics.include?(expression.type)
|
|
118
|
+
|
|
119
|
+
!permitted_value?(expression.value)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def illegal_scalar_expressions(expression)
|
|
123
|
+
return [] unless expression
|
|
124
|
+
|
|
125
|
+
expressions = expression.array_type? ? expression.children : [expression]
|
|
126
|
+
expressions.select { |child_expression| illegal_scalar_expression?(child_expression) }
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def permitted_value?(value)
|
|
130
|
+
permitted_values.include?(value)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def permitted_values
|
|
134
|
+
Array(cop_config['PermittedValues'])
|
|
107
135
|
end
|
|
108
136
|
end
|
|
109
137
|
end
|
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-magic_numbers
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gavin Morrice
|
|
8
8
|
- Fell Sunderland
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: parser
|
|
@@ -62,7 +61,6 @@ licenses:
|
|
|
62
61
|
- MIT
|
|
63
62
|
metadata:
|
|
64
63
|
rubygems_mfa_required: 'true'
|
|
65
|
-
post_install_message:
|
|
66
64
|
rdoc_options: []
|
|
67
65
|
require_paths:
|
|
68
66
|
- lib
|
|
@@ -77,8 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
77
75
|
- !ruby/object:Gem::Version
|
|
78
76
|
version: '0'
|
|
79
77
|
requirements: []
|
|
80
|
-
rubygems_version: 3.
|
|
81
|
-
signing_key:
|
|
78
|
+
rubygems_version: 3.6.9
|
|
82
79
|
specification_version: 4
|
|
83
80
|
summary: rubocop/magic_numbers implements a rubocop cop for detecting the use of bare
|
|
84
81
|
numbers when linting
|