rubocop-magic_numbers 0.2.0 → 0.4.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 +88 -7
- data/lib/rubocop/cop/magic_numbers/base.rb +5 -3
- data/lib/rubocop/cop/magic_numbers/no_argument.rb +10 -5
- data/lib/rubocop/cop/magic_numbers/no_assignment.rb +4 -4
- data/lib/rubocop/cop/magic_numbers/no_default.rb +2 -3
- data/lib/rubocop/cop/magic_numbers/no_return.rb +2 -2
- data/lib/rubocop/magic_numbers/version.rb +1 -1
- data/lib/rubocop-magic_numbers.rb +3 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5510596fa8384cbcc426de2aed19efdd8f4546b59b71f491b97d8627402a0ddf
|
4
|
+
data.tar.gz: 5da082fc1d7238202e13afcfefc5238c3c9ec289024c557716d5c2a8cbe5bde4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b213bcb1d47aa8beae76f257adcd2e1d143155058551cd6b3ddf29117485cadbd2dc6dacb82be15cc77e517d2e7772d5187c2a8999185d7b97c7a654312a35a9
|
7
|
+
data.tar.gz: 6c2e696179b59f5c63aacb1150306210c94e6022f835355cdcdc607667189890d7ab510a75af8580aea93d3a2e2b3917664adb3363d50bac8ae5163bc3f47104
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# rubocop-magic_numbers
|
2
2
|
|
3
|
-

|
4
|
+

|
5
5
|
|
6
6
|
|
7
7
|
`rubocop-magic_numbers` is a gem that detects the use of magic numbers within Ruby code and raises them as offenses.
|
@@ -40,7 +40,74 @@ $ bundle install
|
|
40
40
|
|
41
41
|
## Usage
|
42
42
|
|
43
|
-
After installing the gem, `rubocop` should automatically detect and raise offenses for magic numbers within your code.
|
43
|
+
After installing the gem, `rubocop` should automatically detect and raise offenses for magic numbers within your code.
|
44
|
+
|
45
|
+
The gem will detect offenses of the following sorts:
|
46
|
+
|
47
|
+
### MagicNumbers/NoArgument
|
48
|
+
|
49
|
+
Detects when magic numbers are used as method arguments.
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
# BAD
|
53
|
+
@user.too_many_widgets?(20) # what does 20 mean?!
|
54
|
+
|
55
|
+
# GOOD
|
56
|
+
@user.too_many_widgets?(FREE_SUBSCRIPTION_WIDGET_MAX)
|
57
|
+
|
58
|
+
# BAD
|
59
|
+
monthly_average = total / 28 # why 28?
|
60
|
+
|
61
|
+
# GOOD
|
62
|
+
monthly_average = total / FOUR_WEEK_MONTH_IN_DAYS
|
63
|
+
```
|
64
|
+
|
65
|
+
### MagicNumbers/NoAssignment
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
# BAD
|
69
|
+
total_widget_limit = 20 # why 20?
|
70
|
+
|
71
|
+
# GOOD
|
72
|
+
total_widget_limit = FREE_SUBSCRIPTION_WIDGET_MAX
|
73
|
+
```
|
74
|
+
|
75
|
+
### MagicNumbers/NoDefault
|
76
|
+
|
77
|
+
|
78
|
+
``` ruby
|
79
|
+
# BAD
|
80
|
+
def over_widget_limit?(20)
|
81
|
+
# ...
|
82
|
+
end
|
83
|
+
|
84
|
+
# GOOD
|
85
|
+
def over_widget_limit?(FREE_SUBSCRIPTION_WIDGET_MAX)
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
### MagicNumbers/NoReturn
|
92
|
+
|
93
|
+
|
94
|
+
``` ruby
|
95
|
+
# BAD
|
96
|
+
def widget_limit_for_user(user)
|
97
|
+
return 20 if user.subscription_free?
|
98
|
+
|
99
|
+
return 40
|
100
|
+
end
|
101
|
+
|
102
|
+
# GOOD
|
103
|
+
def widget_limit_for_user(user)
|
104
|
+
return FREE_SUBSCRIPTION_WIDGET_MAX if user.subscription_free?
|
105
|
+
|
106
|
+
PAID_SUBSCRIPTION_WIDGET_MAX
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
You can customize the behavior of the gem by adding configurations to a `.rubocop.yml` file in your project's root directory.
|
44
111
|
|
45
112
|
Here are some examples of configurations you can use:
|
46
113
|
|
@@ -48,16 +115,30 @@ Here are some examples of configurations you can use:
|
|
48
115
|
require:
|
49
116
|
- rubocop-magic_numbers
|
50
117
|
|
51
|
-
|
52
|
-
#
|
118
|
+
MagicNumbers/NoArgument:
|
119
|
+
ForbiddenNumerics: All/Float/Integer # default All
|
120
|
+
IgnoredMethods:
|
121
|
+
- '[]' # defaults to just the #[] method
|
122
|
+
PermittedValues: # defaults to []
|
123
|
+
- -1
|
124
|
+
- 1
|
125
|
+
MagicNumbers/NoAssignment:
|
126
|
+
ForbiddenNumerics: All/Float/Integer # default All
|
127
|
+
|
128
|
+
MagicNumbers/Default:
|
129
|
+
ForbiddenNumerics: All/Float/Integer # default All
|
130
|
+
|
131
|
+
MagicNumbers/NoReturn:
|
132
|
+
AllowedReturns: Implicit/Explicit/None # default None
|
133
|
+
ForbiddenNumerics: All/Float/Integer # default All
|
53
134
|
```
|
54
135
|
|
55
136
|
For more information on configuring `rubocop`, please refer to the [official documentation](https://docs.rubocop.org/rubocop/configuration.html).
|
56
137
|
|
57
138
|
## Contributing
|
58
139
|
|
59
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
140
|
+
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.
|
60
141
|
|
61
142
|
## License
|
62
143
|
|
63
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
144
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -2,16 +2,17 @@
|
|
2
2
|
|
3
3
|
require 'rubocop'
|
4
4
|
require 'rubocop/cop/cop'
|
5
|
+
# rubocop:disable Lint/SuppressedException
|
6
|
+
# This is only available in newer versions of the RuboCop gem
|
5
7
|
begin
|
6
8
|
require 'rubocop/cop/base'
|
7
9
|
rescue LoadError
|
8
10
|
end
|
11
|
+
# rubocop:enable Lint/SuppressedException
|
9
12
|
|
10
13
|
module RuboCop
|
11
14
|
module Cop
|
12
|
-
module MagicNumbers
|
13
|
-
# Base class for all shared behaviour between these cops
|
14
|
-
|
15
|
+
module MagicNumbers # rubocop:disable Style/Documentation
|
15
16
|
def self.best_base_class
|
16
17
|
if ::RuboCop::Cop.const_defined?('Base')
|
17
18
|
::RuboCop::Cop::Base
|
@@ -20,6 +21,7 @@ module RuboCop
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
24
|
+
# Base class for all shared behaviour between these cops
|
23
25
|
class Base < best_base_class
|
24
26
|
CONFIG_ALL = 'All'
|
25
27
|
CONFIG_FLOAT = 'Float'
|
@@ -18,9 +18,9 @@ module RuboCop
|
|
18
18
|
{
|
19
19
|
_
|
20
20
|
_
|
21
|
-
(%<illegal_scalar_pattern>s _)
|
21
|
+
(%<illegal_scalar_pattern>s $_)
|
22
22
|
| # This is a union of lhs and rhs literal
|
23
|
-
(%<illegal_scalar_pattern>s _)
|
23
|
+
(%<illegal_scalar_pattern>s $_)
|
24
24
|
_
|
25
25
|
_
|
26
26
|
}
|
@@ -62,13 +62,18 @@ module RuboCop
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def illegal_argument?(node)
|
65
|
-
node_matches_pattern?(
|
66
|
-
node
|
65
|
+
captured_value = node_matches_pattern?(
|
66
|
+
node: node,
|
67
67
|
pattern: format(
|
68
68
|
MAGIC_NUMBER_ARGUMENT_PATTERN,
|
69
|
-
illegal_scalar_pattern:
|
69
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
70
70
|
)
|
71
71
|
)
|
72
|
+
captured_value && !permitted_values.include?(captured_value)
|
73
|
+
end
|
74
|
+
|
75
|
+
def permitted_values
|
76
|
+
Array(cop_config['PermittedValues'])
|
72
77
|
end
|
73
78
|
end
|
74
79
|
end
|
@@ -77,10 +77,10 @@ module RuboCop
|
|
77
77
|
|
78
78
|
def illegal_scalar_argument_to_setter?(node)
|
79
79
|
method = node_matches_pattern?(
|
80
|
-
node
|
80
|
+
node: node,
|
81
81
|
pattern: format(
|
82
82
|
MAGIC_NUMBER_ARGUMENT_TO_SETTER_PATTERN,
|
83
|
-
illegal_scalar_pattern:
|
83
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
84
84
|
)
|
85
85
|
)
|
86
86
|
|
@@ -89,10 +89,10 @@ module RuboCop
|
|
89
89
|
|
90
90
|
def illegal_multi_assign_right_hand_side?(node)
|
91
91
|
node_matches_pattern?(
|
92
|
-
node
|
92
|
+
node: node,
|
93
93
|
pattern: format(
|
94
94
|
MAGIC_NUMBER_MULTI_ASSIGN_PATTERN,
|
95
|
-
illegal_scalar_pattern:
|
95
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
96
96
|
)
|
97
97
|
)
|
98
98
|
end
|
@@ -35,7 +35,6 @@ module RuboCop
|
|
35
35
|
|
36
36
|
add_offense(
|
37
37
|
node,
|
38
|
-
|
39
38
|
message: DEFAULT_OPTIONAL_ARGUMENT_MSG
|
40
39
|
)
|
41
40
|
end
|
@@ -45,10 +44,10 @@ module RuboCop
|
|
45
44
|
|
46
45
|
def illegal_positional_default?(node)
|
47
46
|
node_matches_pattern?(
|
48
|
-
node
|
47
|
+
node: node,
|
49
48
|
pattern: format(
|
50
49
|
MAGIC_NUMBER_OPTIONAL_ARGUMENT_PATTERN,
|
51
|
-
illegal_scalar_pattern:
|
50
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
52
51
|
)
|
53
52
|
)
|
54
53
|
end
|
@@ -54,9 +54,9 @@ module RuboCop
|
|
54
54
|
return implicit_return?(node.children.last) if is_node_begin_type
|
55
55
|
|
56
56
|
pattern = format(MAGIC_NUMBER_RETURN_PATTERN, {
|
57
|
-
illegal_scalar_pattern:
|
57
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
58
58
|
})
|
59
|
-
node_matches_pattern?(node
|
59
|
+
node_matches_pattern?(node: node, pattern: pattern)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Morrice
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-
|
12
|
+
date: 2023-08-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|
@@ -70,7 +70,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
70
|
requirements:
|
71
71
|
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
|
-
version:
|
73
|
+
version: 2.7.0
|
74
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
75
|
requirements:
|
76
76
|
- - ">="
|