rubocop-magic_numbers 0.7.0 → 0.8.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 +19 -2
- data/config/default.yml +32 -0
- data/lib/rubocop/cop/magic_numbers/base.rb +2 -18
- data/lib/rubocop/cop/magic_numbers/no_argument.rb +21 -28
- data/lib/rubocop/magic_numbers/plugin.rb +33 -0
- data/lib/rubocop/magic_numbers/version.rb +1 -1
- data/lib/rubocop/magic_numbers.rb +1 -0
- metadata +20 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 33ad5620bc3e0d0bcdc88a4242ed8999f2bd4299ebf82fc30a20c43fff9d1de0
|
|
4
|
+
data.tar.gz: 5515602e5eba421afb2c103751bb4aac2d8b1131c036a16df17f015840e0f6cf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 80b5d7d6d78b076f149e6f5cdbf5c146fdfa882b3ef072e2d0d8fd1c31178b9e1c8858671020b4182d9b8978c1dcbfb2528020c7d62c8775d46a9dee9107b550
|
|
7
|
+
data.tar.gz: 541941e49caca325bcecce5bf0d15e3bc401a9804d3c371f062d4051688391140206da1bc3c660219c107ad020281701f3e6a617483cad201de52eba1035bd0e
|
data/README.md
CHANGED
|
@@ -39,7 +39,17 @@ bundle install
|
|
|
39
39
|
|
|
40
40
|
## Usage
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
Enable the plugin in your `.rubocop.yml`:
|
|
43
|
+
|
|
44
|
+
```yaml
|
|
45
|
+
plugins:
|
|
46
|
+
- rubocop-magic_numbers
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`rubocop` will then detect and raise offenses for magic numbers within your code.
|
|
50
|
+
|
|
51
|
+
> [!NOTE]
|
|
52
|
+
> The plugin system requires RuboCop 1.72 or later. If you previously loaded this gem with `require: rubocop-magic_numbers`, move it to `plugins:` instead. See RuboCop's [plugin documentation](https://docs.rubocop.org/rubocop/latest/plugins.html) for details.
|
|
43
53
|
|
|
44
54
|
The gem will detect offenses of the following sorts:
|
|
45
55
|
|
|
@@ -61,6 +71,13 @@ monthly_average = total / 28 # why 28?
|
|
|
61
71
|
monthly_average = total / FOUR_WEEK_MONTH_IN_DAYS
|
|
62
72
|
```
|
|
63
73
|
|
|
74
|
+
Magic numbers are detected in any argument position:
|
|
75
|
+
|
|
76
|
+
``` ruby
|
|
77
|
+
# BAD
|
|
78
|
+
@user.widgets_between(start_date, 20)
|
|
79
|
+
```
|
|
80
|
+
|
|
64
81
|
### MagicNumbers/NoAssignment
|
|
65
82
|
|
|
66
83
|
``` ruby
|
|
@@ -108,7 +125,7 @@ You can customize the behavior of the gem by adding configurations to a `.ruboco
|
|
|
108
125
|
Here are some examples of configurations you can use:
|
|
109
126
|
|
|
110
127
|
```yaml
|
|
111
|
-
|
|
128
|
+
plugins:
|
|
112
129
|
- rubocop-magic_numbers
|
|
113
130
|
|
|
114
131
|
MagicNumbers/NoArgument:
|
data/config/default.yml
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
MagicNumbers/NoArgument:
|
|
2
|
+
Description: 'Detects magic numbers used as method arguments.'
|
|
3
|
+
Enabled: true
|
|
4
|
+
ForbiddenNumerics: All
|
|
5
|
+
IgnoredMethods:
|
|
6
|
+
- '[]'
|
|
7
|
+
CheckArrays: true
|
|
8
|
+
CheckRanges: true
|
|
9
|
+
CheckHashes: false
|
|
10
|
+
PermittedValues: []
|
|
11
|
+
|
|
12
|
+
MagicNumbers/NoAssignment:
|
|
13
|
+
Description: 'Detects magic numbers assigned to variables or properties.'
|
|
14
|
+
Enabled: true
|
|
15
|
+
ForbiddenNumerics: All
|
|
16
|
+
AllowedAssignments:
|
|
17
|
+
- class_variables
|
|
18
|
+
- global_variables
|
|
19
|
+
PermittedValues: []
|
|
20
|
+
|
|
21
|
+
MagicNumbers/NoDefault:
|
|
22
|
+
Description: 'Detects magic numbers used as default argument values.'
|
|
23
|
+
Enabled: true
|
|
24
|
+
ForbiddenNumerics: All
|
|
25
|
+
|
|
26
|
+
MagicNumbers/NoReturn:
|
|
27
|
+
Description: 'Detects magic numbers returned from methods or procs.'
|
|
28
|
+
Enabled: true
|
|
29
|
+
ForbiddenNumerics: All
|
|
30
|
+
AllowedReturns:
|
|
31
|
+
- None
|
|
32
|
+
PermittedReturnValues: []
|
|
@@ -1,28 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'rubocop'
|
|
4
|
-
require 'rubocop/cop/cop'
|
|
5
|
-
# rubocop:disable Lint/SuppressedException
|
|
6
|
-
# This is only available in newer versions of the RuboCop gem
|
|
7
|
-
begin
|
|
8
|
-
require 'rubocop/cop/base'
|
|
9
|
-
rescue LoadError
|
|
10
|
-
end
|
|
11
|
-
# rubocop:enable Lint/SuppressedException
|
|
12
4
|
|
|
13
5
|
module RuboCop
|
|
14
6
|
module Cop
|
|
15
|
-
module MagicNumbers
|
|
16
|
-
def self.best_base_class
|
|
17
|
-
if ::RuboCop::Cop.const_defined?('Base')
|
|
18
|
-
::RuboCop::Cop::Base
|
|
19
|
-
else
|
|
20
|
-
::RuboCop::Cop::Cop
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
7
|
+
module MagicNumbers
|
|
24
8
|
# Base class for all shared behaviour between these cops
|
|
25
|
-
class Base <
|
|
9
|
+
class Base < ::RuboCop::Cop::Base
|
|
26
10
|
CONFIG_ALL = 'All'
|
|
27
11
|
CONFIG_FLOAT = 'Float'
|
|
28
12
|
CONFIG_INTEGER = 'Integer'
|
|
@@ -36,20 +36,6 @@ module RuboCop
|
|
|
36
36
|
#
|
|
37
37
|
# GOOD: Object.bottles_on_the_wall(count: DEFAULT_BOTTLE_COUNT)
|
|
38
38
|
class NoArgument < RuboCop::Cop::MagicNumbers::Base
|
|
39
|
-
MAGIC_NUMBER_ARGUMENT_PATTERN = <<-PATTERN
|
|
40
|
-
(send
|
|
41
|
-
{
|
|
42
|
-
_
|
|
43
|
-
_
|
|
44
|
-
(%<illegal_scalar_pattern>s $_)
|
|
45
|
-
| # This is a union of lhs and rhs literal
|
|
46
|
-
(%<illegal_scalar_pattern>s $_)
|
|
47
|
-
_
|
|
48
|
-
_
|
|
49
|
-
}
|
|
50
|
-
)
|
|
51
|
-
PATTERN
|
|
52
|
-
|
|
53
39
|
ARGUMENT_MSG = 'Do not use magic number arguments to methods'
|
|
54
40
|
|
|
55
41
|
CONFIG_IGNORED_METHODS_NAME = 'IgnoredMethods'
|
|
@@ -74,12 +60,25 @@ module RuboCop
|
|
|
74
60
|
CONFIG_CHECK_HASHES_NAME => false
|
|
75
61
|
}.freeze
|
|
76
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
|
+
|
|
77
76
|
def cop_config
|
|
78
77
|
DEFAULT_CONFIG.merge(super)
|
|
79
78
|
end
|
|
80
79
|
|
|
81
80
|
def on_message_send(node)
|
|
82
|
-
return unless illegal_argument?(node)
|
|
81
|
+
return unless illegal_argument?(node)
|
|
83
82
|
return if ignored_method?(node)
|
|
84
83
|
|
|
85
84
|
add_offense(node, message: ARGUMENT_MSG)
|
|
@@ -99,14 +98,7 @@ module RuboCop
|
|
|
99
98
|
end
|
|
100
99
|
|
|
101
100
|
def illegal_argument?(node)
|
|
102
|
-
|
|
103
|
-
node: node,
|
|
104
|
-
pattern: format(
|
|
105
|
-
MAGIC_NUMBER_ARGUMENT_PATTERN,
|
|
106
|
-
illegal_scalar_pattern: illegal_scalar_pattern
|
|
107
|
-
)
|
|
108
|
-
)
|
|
109
|
-
captured_value && !permitted_values.include?(captured_value)
|
|
101
|
+
magic_number_argument?(node) || illegal_nested_argument?(node)
|
|
110
102
|
end
|
|
111
103
|
|
|
112
104
|
# Check whether any argument is a range, hash, or array literal (as
|
|
@@ -117,7 +109,7 @@ module RuboCop
|
|
|
117
109
|
|
|
118
110
|
def contains_illegal_number?(node)
|
|
119
111
|
nested_values(node).any? do |value|
|
|
120
|
-
|
|
112
|
+
magic_number?(value) || contains_illegal_number?(value)
|
|
121
113
|
end
|
|
122
114
|
end
|
|
123
115
|
|
|
@@ -134,11 +126,12 @@ module RuboCop
|
|
|
134
126
|
node.hash_type? ? node.pairs.map(&:value) : node.children.compact
|
|
135
127
|
end
|
|
136
128
|
|
|
137
|
-
def
|
|
138
|
-
|
|
139
|
-
|
|
129
|
+
def forbidden_numeric?(type)
|
|
130
|
+
forbidden_numerics.include?(type)
|
|
131
|
+
end
|
|
140
132
|
|
|
141
|
-
|
|
133
|
+
def permitted_value?(value)
|
|
134
|
+
permitted_values.include?(value)
|
|
142
135
|
end
|
|
143
136
|
|
|
144
137
|
def permitted_values
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'lint_roller'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
|
|
6
|
+
module RuboCop
|
|
7
|
+
module MagicNumbers
|
|
8
|
+
# Integrates rubocop-magic_numbers with RuboCop's plugin system
|
|
9
|
+
# (https://docs.rubocop.org/rubocop/latest/plugins.html).
|
|
10
|
+
class Plugin < LintRoller::Plugin
|
|
11
|
+
def about
|
|
12
|
+
LintRoller::About.new(
|
|
13
|
+
name: 'rubocop-magic_numbers',
|
|
14
|
+
version: VERSION,
|
|
15
|
+
homepage: 'https://github.com/meetcleo/rubocop-magic_numbers',
|
|
16
|
+
description: 'A RuboCop plugin for detecting the use of magic numbers.'
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def supported?(context)
|
|
21
|
+
context.engine == :rubocop
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def rules(_context)
|
|
25
|
+
LintRoller::Rules.new(
|
|
26
|
+
type: :path,
|
|
27
|
+
config_format: :rubocop,
|
|
28
|
+
value: Pathname.new(__dir__).join('../../../config/default.yml')
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
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.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gavin Morrice
|
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
|
10
10
|
cert_chain: []
|
|
11
11
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: lint_roller
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.1'
|
|
13
27
|
- !ruby/object:Gem::Dependency
|
|
14
28
|
name: parser
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -30,14 +44,14 @@ dependencies:
|
|
|
30
44
|
requirements:
|
|
31
45
|
- - ">="
|
|
32
46
|
- !ruby/object:Gem::Version
|
|
33
|
-
version:
|
|
47
|
+
version: 1.72.1
|
|
34
48
|
type: :runtime
|
|
35
49
|
prerelease: false
|
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
51
|
requirements:
|
|
38
52
|
- - ">="
|
|
39
53
|
- !ruby/object:Gem::Version
|
|
40
|
-
version:
|
|
54
|
+
version: 1.72.1
|
|
41
55
|
description: rubocop/magic_numbers implements a rubocop cop for detecting the use
|
|
42
56
|
of bare numbers when linting
|
|
43
57
|
email:
|
|
@@ -48,6 +62,7 @@ extensions: []
|
|
|
48
62
|
extra_rdoc_files: []
|
|
49
63
|
files:
|
|
50
64
|
- README.md
|
|
65
|
+
- config/default.yml
|
|
51
66
|
- lib/rubocop-magic_numbers.rb
|
|
52
67
|
- lib/rubocop/cop/magic_numbers/base.rb
|
|
53
68
|
- lib/rubocop/cop/magic_numbers/no_argument.rb
|
|
@@ -55,12 +70,14 @@ files:
|
|
|
55
70
|
- lib/rubocop/cop/magic_numbers/no_default.rb
|
|
56
71
|
- lib/rubocop/cop/magic_numbers/no_return.rb
|
|
57
72
|
- lib/rubocop/magic_numbers.rb
|
|
73
|
+
- lib/rubocop/magic_numbers/plugin.rb
|
|
58
74
|
- lib/rubocop/magic_numbers/version.rb
|
|
59
75
|
homepage: https://github.com/Bodacious/rubocop-magic_numbers
|
|
60
76
|
licenses:
|
|
61
77
|
- MIT
|
|
62
78
|
metadata:
|
|
63
79
|
rubygems_mfa_required: 'true'
|
|
80
|
+
default_lint_roller_plugin: RuboCop::MagicNumbers::Plugin
|
|
64
81
|
rdoc_options: []
|
|
65
82
|
require_paths:
|
|
66
83
|
- lib
|