rubocop-magic_numbers 0.5.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e50541096764d84680fd5253e6233a4ed8ef80250a6cb416a0431c3bd194ce66
4
- data.tar.gz: 0e85a4951ca5c75ba6d097950e14bc42591eb27167a04f2d723f2093b7ffc066
3
+ metadata.gz: 584000ca91b0dcec9c83f5b87d98945db33ed96ca4a0eeb7075a5c990cf0d51f
4
+ data.tar.gz: af634bd0ffea2acc603bce2891aaa04e82f7027f96e4b26f6aebb92d26c3d578
5
5
  SHA512:
6
- metadata.gz: 6cbbc1083814d6ec1ebdcb632e6fd34d283fbea606509640382fb79e5fbdaad27a8c90daf8db7588781be54251378a05489aebdbc26c6862f82686e27ebd8f31
7
- data.tar.gz: 2d707528dbe8d168ddd09838b6ca5906232a70951921744c76ae6f30fd432b3f7d7c8870ebefce5b8f8becb4e506a977cb837260d84ad4dd89fadf28270e8996
6
+ metadata.gz: 99906d35cbbb8d6d6d75a23f00fe2c101fad6ca8b3f357499f3d897119cdec8b42f5cd4cb79cadbb0d9cef46578d2fa278210f747664838f9ee47847bf84bd66
7
+ data.tar.gz: ec6a8554d7b5ffa8f885889da690e3ae67c7eec738227098d890f0e3be824eb47101ed6b6cb055944a14e0726d4f3b843a090ced454ae82fb670532ba62b3a20
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  ![Ruby tests](https://github.com/meetcleo/rubocop-magic_numbers/actions/workflows/ruby.yml/badge.svg)
4
4
  ![RuboCop Lint](https://github.com/meetcleo/rubocop-magic_numbers/actions/workflows/rubocop.yml/badge.svg)
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
- $ bundle install
37
+ bundle install
39
38
  ```
40
39
 
41
40
  ## Usage
@@ -74,7 +73,6 @@ total_widget_limit = FREE_SUBSCRIPTION_WIDGET_MAX
74
73
 
75
74
  ### MagicNumbers/NoDefault
76
75
 
77
-
78
76
  ``` ruby
79
77
  # BAD
80
78
  def over_widget_limit?(limit = 20)
@@ -89,7 +87,6 @@ end
89
87
 
90
88
  ### MagicNumbers/NoReturn
91
89
 
92
-
93
90
  ``` ruby
94
91
  # BAD
95
92
  def widget_limit_for_user(user)
@@ -116,6 +113,9 @@ require:
116
113
 
117
114
  MagicNumbers/NoArgument:
118
115
  ForbiddenNumerics: All/Float/Integer # default All
116
+ CheckArrays: # default true
117
+ CheckRanges: # default true
118
+ CheckHashes: # default false
119
119
  IgnoredMethods:
120
120
  - '[]' # defaults to just the #[] method
121
121
  PermittedValues: # defaults to []
@@ -123,8 +123,12 @@ MagicNumbers/NoArgument:
123
123
  - 1
124
124
  MagicNumbers/NoAssignment:
125
125
  ForbiddenNumerics: All/Float/Integer # default All
126
+ PermittedValues: # defaults to []
127
+ - 0
128
+ - 1
129
+ - 3.14
126
130
 
127
- MagicNumbers/Default:
131
+ MagicNumbers/NoDefault:
128
132
  ForbiddenNumerics: All/Float/Integer # default All
129
133
 
130
134
  MagicNumbers/NoReturn:
@@ -132,6 +136,25 @@ MagicNumbers/NoReturn:
132
136
  ForbiddenNumerics: All/Float/Integer # default All
133
137
  ```
134
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
+
135
158
  For more information on configuring `rubocop`, please refer to the [official documentation](https://docs.rubocop.org/rubocop/configuration.html).
136
159
 
137
160
  ## Rails usage
@@ -161,7 +184,7 @@ end
161
184
 
162
185
  ## Contributing
163
186
 
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.
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.
165
188
 
166
189
  ## License
167
190
 
@@ -7,11 +7,34 @@ 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
- # object.bottles_on_the_wall(DEFAULT_BOTTLE_COUNT)
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
39
  MAGIC_NUMBER_ARGUMENT_PATTERN = <<-PATTERN
17
40
  (send
@@ -30,11 +53,25 @@ module RuboCop
30
53
  ARGUMENT_MSG = 'Do not use magic number arguments to methods'
31
54
 
32
55
  CONFIG_IGNORED_METHODS_NAME = 'IgnoredMethods'
56
+ CONFIG_CHECK_RANGES_NAME = 'CheckRanges'
57
+ CONFIG_CHECK_HASHES_NAME = 'CheckHashes'
58
+ CONFIG_CHECK_ARRAYS_NAME = 'CheckArrays'
59
+
60
+ # The config option enabling checks inside each literal node type
61
+ NESTED_LITERAL_CONFIG_NAMES = {
62
+ irange: CONFIG_CHECK_RANGES_NAME,
63
+ erange: CONFIG_CHECK_RANGES_NAME,
64
+ hash: CONFIG_CHECK_HASHES_NAME,
65
+ array: CONFIG_CHECK_ARRAYS_NAME
66
+ }.freeze
33
67
 
34
68
  # By default, don't raise an offense for magic numbers arguments
35
- # for these methods
69
+ # for these methods, and only look inside range literal arguments
36
70
  DEFAULT_CONFIG = {
37
- CONFIG_IGNORED_METHODS_NAME => ['[]']
71
+ CONFIG_IGNORED_METHODS_NAME => ['[]'],
72
+ CONFIG_CHECK_ARRAYS_NAME => true,
73
+ CONFIG_CHECK_RANGES_NAME => true,
74
+ CONFIG_CHECK_HASHES_NAME => false
38
75
  }.freeze
39
76
 
40
77
  def cop_config
@@ -42,7 +79,7 @@ module RuboCop
42
79
  end
43
80
 
44
81
  def on_message_send(node)
45
- return unless illegal_argument?(node)
82
+ return unless illegal_argument?(node) || illegal_nested_argument?(node)
46
83
  return if ignored_method?(node)
47
84
 
48
85
  add_offense(node, message: ARGUMENT_MSG)
@@ -72,6 +109,38 @@ module RuboCop
72
109
  captured_value && !permitted_values.include?(captured_value)
73
110
  end
74
111
 
112
+ # Check whether any argument is a range, hash, or array literal (as
113
+ # enabled by config) containing a magic number, at any depth
114
+ def illegal_nested_argument?(node)
115
+ node.arguments.any? { |argument| contains_illegal_number?(argument) }
116
+ end
117
+
118
+ def contains_illegal_number?(node)
119
+ nested_values(node).any? do |value|
120
+ illegal_number?(value) || contains_illegal_number?(value)
121
+ end
122
+ end
123
+
124
+ # The child nodes of a literal that should be checked for magic numbers,
125
+ # or none if that literal type isn't enabled by config. For hashes only
126
+ # the values are checked, since numeric keys are usually the meaning of
127
+ # the mapping (e.g. { 404 => :not_found }).
128
+ def nested_values(node)
129
+ return [] unless node.is_a?(RuboCop::AST::Node)
130
+
131
+ config_name = NESTED_LITERAL_CONFIG_NAMES[node.type]
132
+ return [] unless config_name && cop_config[config_name]
133
+
134
+ node.hash_type? ? node.pairs.map(&:value) : node.children.compact
135
+ end
136
+
137
+ def illegal_number?(node)
138
+ return false unless node.is_a?(RuboCop::AST::Node)
139
+ return false unless forbidden_numerics.include?(node.type)
140
+
141
+ !permitted_values.include?(node.value)
142
+ end
143
+
75
144
  def permitted_values
76
145
  Array(cop_config['PermittedValues'])
77
146
  end
@@ -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
- (array <(%<illegal_scalar_pattern>s _) ...>)
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
- forbidden_numerics.include?(node.expression&.type)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MagicNumbers
5
- VERSION = '0.5.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  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.5.0
4
+ version: 0.7.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: 2024-03-05 00:00:00.000000000 Z
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.4.6
81
- signing_key:
78
+ rubygems_version: 4.0.15
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