rubocop-magic_numbers 0.6.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: 8c524463c1bd7ab5a180f5063ee27deceac29fdf0bfe9d682b03ce1f6f8a992d
4
- data.tar.gz: ca98482dc543d684dad741da511de7f873ae83dd2ae714fb5115b112855b412c
3
+ metadata.gz: 584000ca91b0dcec9c83f5b87d98945db33ed96ca4a0eeb7075a5c990cf0d51f
4
+ data.tar.gz: af634bd0ffea2acc603bce2891aaa04e82f7027f96e4b26f6aebb92d26c3d578
5
5
  SHA512:
6
- metadata.gz: 421a00bdfe1b50f2ac56d4759577dd8f77967a3a24a2111bb901563ec09fee149dc2e64cd45661ea8761ac0c8f9594ecd2e7f82aaddd78f3136651601dee7632
7
- data.tar.gz: 177fca60d9af08b2d409bc1e5018f9a9d4f7a3995fc4d04fe2e9efeafa0a9ccd2863ece0b90ce8a9ddcf078f1cb8bd1a251f8ff99ff870377f21d1903ee1a7b5
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 []
@@ -184,7 +184,7 @@ end
184
184
 
185
185
  ## Contributing
186
186
 
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.
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.
188
188
 
189
189
  ## License
190
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MagicNumbers
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  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.6.0
4
+ version: 0.7.0
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: 3.6.9
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