rubocop-magic_numbers 0.4.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 +51 -4
- data/lib/rubocop/cop/magic_numbers/no_assignment.rb +35 -7
- data/lib/rubocop/cop/magic_numbers/no_return.rb +13 -5
- 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
|
@@ -77,17 +77,16 @@ total_widget_limit = FREE_SUBSCRIPTION_WIDGET_MAX
|
|
|
77
77
|
|
|
78
78
|
``` ruby
|
|
79
79
|
# BAD
|
|
80
|
-
def over_widget_limit?(20)
|
|
80
|
+
def over_widget_limit?(limit = 20)
|
|
81
81
|
# ...
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
# GOOD
|
|
85
|
-
def over_widget_limit?(FREE_SUBSCRIPTION_WIDGET_MAX)
|
|
85
|
+
def over_widget_limit?(limit = FREE_SUBSCRIPTION_WIDGET_MAX)
|
|
86
86
|
# ...
|
|
87
87
|
end
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
|
|
91
90
|
### MagicNumbers/NoReturn
|
|
92
91
|
|
|
93
92
|
|
|
@@ -124,8 +123,12 @@ MagicNumbers/NoArgument:
|
|
|
124
123
|
- 1
|
|
125
124
|
MagicNumbers/NoAssignment:
|
|
126
125
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
126
|
+
PermittedValues: # defaults to []
|
|
127
|
+
- 0
|
|
128
|
+
- 1
|
|
129
|
+
- 3.14
|
|
127
130
|
|
|
128
|
-
MagicNumbers/
|
|
131
|
+
MagicNumbers/NoDefault:
|
|
129
132
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
130
133
|
|
|
131
134
|
MagicNumbers/NoReturn:
|
|
@@ -133,8 +136,52 @@ MagicNumbers/NoReturn:
|
|
|
133
136
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
134
137
|
```
|
|
135
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
|
+
|
|
136
158
|
For more information on configuring `rubocop`, please refer to the [official documentation](https://docs.rubocop.org/rubocop/configuration.html).
|
|
137
159
|
|
|
160
|
+
## Rails usage
|
|
161
|
+
|
|
162
|
+
If using as part of a Ruby on Rails project, you may want to add the following to your RuboCop configuration:
|
|
163
|
+
|
|
164
|
+
``` YAML
|
|
165
|
+
MagicNumbers/NoArgument:
|
|
166
|
+
Exclude:
|
|
167
|
+
- config/application.rb
|
|
168
|
+
- db/migrate/*.rb
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This will prevent RuboCop from complainig about Rails version numbers in your migration files and application config.
|
|
172
|
+
|
|
173
|
+
``` ruby
|
|
174
|
+
module Cleo
|
|
175
|
+
class Application < Rails::Application
|
|
176
|
+
config.load_defaults 7.1 # <= here
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# If you remove `[]` from ignored methods, you might want to add this
|
|
181
|
+
class AddBankCardIdToUsers < ActiveRecord::Migration[7.1]
|
|
182
|
+
end
|
|
183
|
+
```
|
|
184
|
+
|
|
138
185
|
## Contributing
|
|
139
186
|
|
|
140
187
|
Bug reports and pull requests are welcome on GitHub at https://github.com/meetcleo/rubocop-magic_numbers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://www.contributor-covenant.org/) code of conduct.
|
|
@@ -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
|
|
@@ -9,28 +9,31 @@ module RuboCop
|
|
|
9
9
|
# Catches both explicit and implicit returns
|
|
10
10
|
class NoReturn < RuboCop::Cop::MagicNumbers::Base
|
|
11
11
|
MAGIC_NUMBER_RETURN_PATTERN = <<~PATTERN.chomp
|
|
12
|
-
(%<illegal_scalar_pattern>s _)
|
|
12
|
+
(%<illegal_scalar_pattern>s $_)
|
|
13
13
|
PATTERN
|
|
14
14
|
NO_EXPLICIT_RETURN_MSG = 'Do not return magic numbers from a method or proc'
|
|
15
15
|
|
|
16
16
|
CONFIG_NAME_ALLOWED_RETURNS = 'AllowedReturns'
|
|
17
|
+
CONFIG_NAME_PERMITTED_RETURN_VALUES = 'PermittedReturnValues'
|
|
17
18
|
|
|
18
19
|
RETURN_TYPE_IMPLICIT = 'Implicit'
|
|
19
20
|
RETURN_TYPE_EXPLICIT = 'Explicit'
|
|
20
21
|
RETURN_TYPE_NONE = 'None'
|
|
21
22
|
|
|
22
|
-
# Supported values are 'Explicit', 'Implicit', 'None'
|
|
23
23
|
DEFAULT_CONFIG = {
|
|
24
|
-
|
|
24
|
+
# Supported values are 'Explicit', 'Implicit', 'None'
|
|
25
|
+
CONFIG_NAME_ALLOWED_RETURNS => [RETURN_TYPE_NONE],
|
|
26
|
+
CONFIG_NAME_PERMITTED_RETURN_VALUES => []
|
|
25
27
|
}.freeze
|
|
26
28
|
|
|
27
29
|
def cop_config
|
|
28
|
-
|
|
30
|
+
DEFAULT_CONFIG.merge(super)
|
|
29
31
|
end
|
|
30
32
|
|
|
31
33
|
def on_method_defined(node)
|
|
32
34
|
return if allowed_returns.include?(RETURN_TYPE_IMPLICIT)
|
|
33
|
-
return unless implicit_return?(node.children.last)
|
|
35
|
+
return unless (captured_value = implicit_return?(node.children.last))
|
|
36
|
+
return if permitted_return_values.include?(captured_value)
|
|
34
37
|
|
|
35
38
|
add_offense(node.children.last, message: NO_EXPLICIT_RETURN_MSG)
|
|
36
39
|
end
|
|
@@ -39,6 +42,7 @@ module RuboCop
|
|
|
39
42
|
def on_return(node)
|
|
40
43
|
return if allowed_returns.include?(RETURN_TYPE_EXPLICIT)
|
|
41
44
|
return unless forbidden_numerics.include?(node.children.first&.type)
|
|
45
|
+
return if permitted_return_values.include?(node.children.first&.value)
|
|
42
46
|
|
|
43
47
|
add_offense(node.children.first, message: NO_EXPLICIT_RETURN_MSG)
|
|
44
48
|
end
|
|
@@ -58,6 +62,10 @@ module RuboCop
|
|
|
58
62
|
})
|
|
59
63
|
node_matches_pattern?(node: node, pattern: pattern)
|
|
60
64
|
end
|
|
65
|
+
|
|
66
|
+
def permitted_return_values
|
|
67
|
+
Array(cop_config['PermittedReturnValues'])
|
|
68
|
+
end
|
|
61
69
|
end
|
|
62
70
|
end
|
|
63
71
|
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
|