rubocop-magic_numbers 0.6.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 +12 -5
- data/lib/rubocop/cop/magic_numbers/no_argument.rb +90 -28
- data/lib/rubocop/magic_numbers/version.rb +1 -1
- metadata +2 -2
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
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|

|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
`rubocop-magic_numbers` is a gem that detects the use of magic numbers within Ruby code and raises them as offenses.
|
|
8
7
|
|
|
9
8
|
Magic numbers are typically integers or floats that are used within code with no descriptive context to help other developers understand what they represent. To write cleaner and more maintainable code, it is recommended to assign the numbers to constants with helpful names.
|
|
@@ -35,7 +34,7 @@ gem 'rubocop-magic_numbers', require: false, group: :development
|
|
|
35
34
|
And then execute:
|
|
36
35
|
|
|
37
36
|
```bash
|
|
38
|
-
|
|
37
|
+
bundle install
|
|
39
38
|
```
|
|
40
39
|
|
|
41
40
|
## Usage
|
|
@@ -62,6 +61,13 @@ monthly_average = total / 28 # why 28?
|
|
|
62
61
|
monthly_average = total / FOUR_WEEK_MONTH_IN_DAYS
|
|
63
62
|
```
|
|
64
63
|
|
|
64
|
+
Magic numbers are detected in any argument position:
|
|
65
|
+
|
|
66
|
+
``` ruby
|
|
67
|
+
# BAD
|
|
68
|
+
@user.widgets_between(start_date, 20)
|
|
69
|
+
```
|
|
70
|
+
|
|
65
71
|
### MagicNumbers/NoAssignment
|
|
66
72
|
|
|
67
73
|
``` ruby
|
|
@@ -74,7 +80,6 @@ total_widget_limit = FREE_SUBSCRIPTION_WIDGET_MAX
|
|
|
74
80
|
|
|
75
81
|
### MagicNumbers/NoDefault
|
|
76
82
|
|
|
77
|
-
|
|
78
83
|
``` ruby
|
|
79
84
|
# BAD
|
|
80
85
|
def over_widget_limit?(limit = 20)
|
|
@@ -89,7 +94,6 @@ end
|
|
|
89
94
|
|
|
90
95
|
### MagicNumbers/NoReturn
|
|
91
96
|
|
|
92
|
-
|
|
93
97
|
``` ruby
|
|
94
98
|
# BAD
|
|
95
99
|
def widget_limit_for_user(user)
|
|
@@ -116,6 +120,9 @@ require:
|
|
|
116
120
|
|
|
117
121
|
MagicNumbers/NoArgument:
|
|
118
122
|
ForbiddenNumerics: All/Float/Integer # default All
|
|
123
|
+
CheckArrays: # default true
|
|
124
|
+
CheckRanges: # default true
|
|
125
|
+
CheckHashes: # default false
|
|
119
126
|
IgnoredMethods:
|
|
120
127
|
- '[]' # defaults to just the #[] method
|
|
121
128
|
PermittedValues: # defaults to []
|
|
@@ -184,7 +191,7 @@ end
|
|
|
184
191
|
|
|
185
192
|
## Contributing
|
|
186
193
|
|
|
187
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/meetcleo/rubocop-magic_numbers
|
|
194
|
+
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.
|
|
188
195
|
|
|
189
196
|
## License
|
|
190
197
|
|
|
@@ -7,36 +7,72 @@ module RuboCop
|
|
|
7
7
|
module MagicNumbers
|
|
8
8
|
# Adds violations for magic numbers when used as the argument to a method
|
|
9
9
|
#
|
|
10
|
-
# BAD:
|
|
11
|
-
# object.bottles_on_the_wall(100)
|
|
10
|
+
# BAD: object.bottles_on_the_wall(100)
|
|
12
11
|
#
|
|
13
|
-
# GOOD:
|
|
14
|
-
#
|
|
12
|
+
# GOOD: object.bottles_on_the_wall(DEFAULT_BOTTLE_COUNT)
|
|
13
|
+
#
|
|
14
|
+
# Numbers nested inside range, hash (including keyword argument), and
|
|
15
|
+
# array literal arguments can also be detected, each controlled by its own
|
|
16
|
+
# config option.
|
|
17
|
+
#
|
|
18
|
+
# CheckRanges (default true)
|
|
19
|
+
#
|
|
20
|
+
# BAD: object.bottles_up_to(..100)
|
|
21
|
+
#
|
|
22
|
+
# GOOD: Object.bottles_up_to(..DEFAULT_BOTTLE_COUNT)
|
|
23
|
+
#
|
|
24
|
+
# CheckArrays (default false)
|
|
25
|
+
#
|
|
26
|
+
# BAD: object.bottles_on_the_wall([95, 97, 100])
|
|
27
|
+
#
|
|
28
|
+
# GOOD: Object.bottles_on_the_wall(verse_numbers)
|
|
29
|
+
#
|
|
30
|
+
# CheckHashes (default false)
|
|
31
|
+
#
|
|
32
|
+
# Defaults to false as a keyword argument
|
|
33
|
+
# parameter may describe the function of a magic number.
|
|
34
|
+
#
|
|
35
|
+
# BAD: object.bottles_on_the_wall(count: 100)
|
|
36
|
+
#
|
|
37
|
+
# GOOD: Object.bottles_on_the_wall(count: DEFAULT_BOTTLE_COUNT)
|
|
15
38
|
class NoArgument < RuboCop::Cop::MagicNumbers::Base
|
|
16
|
-
MAGIC_NUMBER_ARGUMENT_PATTERN = <<-PATTERN
|
|
17
|
-
(send
|
|
18
|
-
{
|
|
19
|
-
_
|
|
20
|
-
_
|
|
21
|
-
(%<illegal_scalar_pattern>s $_)
|
|
22
|
-
| # This is a union of lhs and rhs literal
|
|
23
|
-
(%<illegal_scalar_pattern>s $_)
|
|
24
|
-
_
|
|
25
|
-
_
|
|
26
|
-
}
|
|
27
|
-
)
|
|
28
|
-
PATTERN
|
|
29
|
-
|
|
30
39
|
ARGUMENT_MSG = 'Do not use magic number arguments to methods'
|
|
31
40
|
|
|
32
41
|
CONFIG_IGNORED_METHODS_NAME = 'IgnoredMethods'
|
|
42
|
+
CONFIG_CHECK_RANGES_NAME = 'CheckRanges'
|
|
43
|
+
CONFIG_CHECK_HASHES_NAME = 'CheckHashes'
|
|
44
|
+
CONFIG_CHECK_ARRAYS_NAME = 'CheckArrays'
|
|
45
|
+
|
|
46
|
+
# The config option enabling checks inside each literal node type
|
|
47
|
+
NESTED_LITERAL_CONFIG_NAMES = {
|
|
48
|
+
irange: CONFIG_CHECK_RANGES_NAME,
|
|
49
|
+
erange: CONFIG_CHECK_RANGES_NAME,
|
|
50
|
+
hash: CONFIG_CHECK_HASHES_NAME,
|
|
51
|
+
array: CONFIG_CHECK_ARRAYS_NAME
|
|
52
|
+
}.freeze
|
|
33
53
|
|
|
34
54
|
# By default, don't raise an offense for magic numbers arguments
|
|
35
|
-
# for these methods
|
|
55
|
+
# for these methods, and only look inside range literal arguments
|
|
36
56
|
DEFAULT_CONFIG = {
|
|
37
|
-
CONFIG_IGNORED_METHODS_NAME => ['[]']
|
|
57
|
+
CONFIG_IGNORED_METHODS_NAME => ['[]'],
|
|
58
|
+
CONFIG_CHECK_ARRAYS_NAME => true,
|
|
59
|
+
CONFIG_CHECK_RANGES_NAME => true,
|
|
60
|
+
CONFIG_CHECK_HASHES_NAME => false
|
|
38
61
|
}.freeze
|
|
39
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
|
+
|
|
40
76
|
def cop_config
|
|
41
77
|
DEFAULT_CONFIG.merge(super)
|
|
42
78
|
end
|
|
@@ -62,14 +98,40 @@ module RuboCop
|
|
|
62
98
|
end
|
|
63
99
|
|
|
64
100
|
def illegal_argument?(node)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
101
|
+
magic_number_argument?(node) || illegal_nested_argument?(node)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Check whether any argument is a range, hash, or array literal (as
|
|
105
|
+
# enabled by config) containing a magic number, at any depth
|
|
106
|
+
def illegal_nested_argument?(node)
|
|
107
|
+
node.arguments.any? { |argument| contains_illegal_number?(argument) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def contains_illegal_number?(node)
|
|
111
|
+
nested_values(node).any? do |value|
|
|
112
|
+
magic_number?(value) || contains_illegal_number?(value)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# The child nodes of a literal that should be checked for magic numbers,
|
|
117
|
+
# or none if that literal type isn't enabled by config. For hashes only
|
|
118
|
+
# the values are checked, since numeric keys are usually the meaning of
|
|
119
|
+
# the mapping (e.g. { 404 => :not_found }).
|
|
120
|
+
def nested_values(node)
|
|
121
|
+
return [] unless node.is_a?(RuboCop::AST::Node)
|
|
122
|
+
|
|
123
|
+
config_name = NESTED_LITERAL_CONFIG_NAMES[node.type]
|
|
124
|
+
return [] unless config_name && cop_config[config_name]
|
|
125
|
+
|
|
126
|
+
node.hash_type? ? node.pairs.map(&:value) : node.children.compact
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def forbidden_numeric?(type)
|
|
130
|
+
forbidden_numerics.include?(type)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def permitted_value?(value)
|
|
134
|
+
permitted_values.include?(value)
|
|
73
135
|
end
|
|
74
136
|
|
|
75
137
|
def permitted_values
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.7.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gavin Morrice
|
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
75
|
- !ruby/object:Gem::Version
|
|
76
76
|
version: '0'
|
|
77
77
|
requirements: []
|
|
78
|
-
rubygems_version:
|
|
78
|
+
rubygems_version: 4.0.15
|
|
79
79
|
specification_version: 4
|
|
80
80
|
summary: rubocop/magic_numbers implements a rubocop cop for detecting the use of bare
|
|
81
81
|
numbers when linting
|