rubocop-magic_numbers 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -7
- data/lib/rubocop/cop/magic_numbers/no_argument.rb +2 -2
- data/lib/rubocop/cop/magic_numbers/no_assignment.rb +4 -4
- data/lib/rubocop/cop/magic_numbers/no_default.rb +2 -2
- data/lib/rubocop/cop/magic_numbers/no_return.rb +15 -7
- data/lib/rubocop/magic_numbers/version.rb +1 -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: e50541096764d84680fd5253e6233a4ed8ef80250a6cb416a0431c3bd194ce66
|
4
|
+
data.tar.gz: 0e85a4951ca5c75ba6d097950e14bc42591eb27167a04f2d723f2093b7ffc066
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cbbc1083814d6ec1ebdcb632e6fd34d283fbea606509640382fb79e5fbdaad27a8c90daf8db7588781be54251378a05489aebdbc26c6862f82686e27ebd8f31
|
7
|
+
data.tar.gz: 2d707528dbe8d168ddd09838b6ca5906232a70951921744c76ae6f30fd432b3f7d7c8870ebefce5b8f8becb4e506a977cb837260d84ad4dd89fadf28270e8996
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# rubocop-magic_numbers
|
2
2
|
|
3
|
-
![Ruby tests](https://github.com/
|
4
|
-
![RuboCop Lint](https://github.com/
|
3
|
+
![Ruby tests](https://github.com/meetcleo/rubocop-magic_numbers/actions/workflows/ruby.yml/badge.svg)
|
4
|
+
![RuboCop Lint](https://github.com/meetcleo/rubocop-magic_numbers/actions/workflows/rubocop.yml/badge.svg)
|
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.
|
@@ -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
|
|
@@ -135,10 +134,35 @@ MagicNumbers/NoReturn:
|
|
135
134
|
|
136
135
|
For more information on configuring `rubocop`, please refer to the [official documentation](https://docs.rubocop.org/rubocop/configuration.html).
|
137
136
|
|
137
|
+
## Rails usage
|
138
|
+
|
139
|
+
If using as part of a Ruby on Rails project, you may want to add the following to your RuboCop configuration:
|
140
|
+
|
141
|
+
``` YAML
|
142
|
+
MagicNumbers/NoArgument:
|
143
|
+
Exclude:
|
144
|
+
- config/application.rb
|
145
|
+
- db/migrate/*.rb
|
146
|
+
```
|
147
|
+
|
148
|
+
This will prevent RuboCop from complainig about Rails version numbers in your migration files and application config.
|
149
|
+
|
150
|
+
``` ruby
|
151
|
+
module Cleo
|
152
|
+
class Application < Rails::Application
|
153
|
+
config.load_defaults 7.1 # <= here
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# If you remove `[]` from ignored methods, you might want to add this
|
158
|
+
class AddBankCardIdToUsers < ActiveRecord::Migration[7.1]
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
138
162
|
## Contributing
|
139
163
|
|
140
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
164
|
+
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.
|
141
165
|
|
142
166
|
## License
|
143
167
|
|
144
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
168
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -63,10 +63,10 @@ module RuboCop
|
|
63
63
|
|
64
64
|
def illegal_argument?(node)
|
65
65
|
captured_value = node_matches_pattern?(
|
66
|
-
node
|
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
72
|
captured_value && !permitted_values.include?(captured_value)
|
@@ -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
|
@@ -44,10 +44,10 @@ module RuboCop
|
|
44
44
|
|
45
45
|
def illegal_positional_default?(node)
|
46
46
|
node_matches_pattern?(
|
47
|
-
node
|
47
|
+
node: node,
|
48
48
|
pattern: format(
|
49
49
|
MAGIC_NUMBER_OPTIONAL_ARGUMENT_PATTERN,
|
50
|
-
illegal_scalar_pattern:
|
50
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
51
51
|
)
|
52
52
|
)
|
53
53
|
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
|
@@ -54,9 +58,13 @@ module RuboCop
|
|
54
58
|
return implicit_return?(node.children.last) if is_node_begin_type
|
55
59
|
|
56
60
|
pattern = format(MAGIC_NUMBER_RETURN_PATTERN, {
|
57
|
-
illegal_scalar_pattern:
|
61
|
+
illegal_scalar_pattern: illegal_scalar_pattern
|
58
62
|
})
|
59
|
-
node_matches_pattern?(node
|
63
|
+
node_matches_pattern?(node: node, pattern: pattern)
|
64
|
+
end
|
65
|
+
|
66
|
+
def permitted_return_values
|
67
|
+
Array(cop_config['PermittedReturnValues'])
|
60
68
|
end
|
61
69
|
end
|
62
70
|
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.5.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:
|
12
|
+
date: 2024-03-05 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
|
- - ">="
|