rubocop-magic_numbers 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4db321fec029b7e924bda74c35c9f9dcfdd902173d31965fad3ca07507ef606b
4
- data.tar.gz: f477370605f885adfd7a1353a6065c637226ff4b637001546b4b2e4109787bde
3
+ metadata.gz: ea6ef829ef5402bdcd314250492b87a6f734d7633c54531cfa779689b690b851
4
+ data.tar.gz: b59bc49fb9783017ae9aa7999984d474779b215df32ac20da1bcafdcb60ade76
5
5
  SHA512:
6
- metadata.gz: 408cb1f1488a6e57063a57b7422dc74145334c3c296e7d3dd129e1e7447c5e309df13040b7aae1d258294b21026e81ff586e4c9754f7af2be31b916343690a10
7
- data.tar.gz: 4020f1a2891c85d1f0ed3c13acffb47e10a3213d073335d907a1d53f2faeaf912b631a80b332a443b3e2e6fc8da93f54a6958ca5e72590c34dd66d2ef00bcf50
6
+ metadata.gz: 15ca87f92adc73951779988d2a75f06ed141fe15dbde2959cfb690a70dff4addc7e5839d68745f490d715fdd73ed581390c7121f599f8c22f00220aede4eecb3
7
+ data.tar.gz: 7559121922795e3c71a9db0b975b83267bbe16f2aac43d1d9cb79e112235fe4ef7ae3387dc5aea1690232efc17747e84ffe2a4be77d5feb058cb0fc546f5a5a2
data/README.md CHANGED
@@ -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. You can customize the behavior of the gem by adding configurations to a `.rubocop.yml` file in your project's root directory.
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,8 +115,22 @@ Here are some examples of configurations you can use:
48
115
  require:
49
116
  - rubocop-magic_numbers
50
117
 
51
- # TODO
52
- # define configs here
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).
@@ -1,12 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rubocop'
3
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
4
12
 
5
13
  module RuboCop
6
14
  module Cop
7
- module MagicNumbers
15
+ module MagicNumbers # rubocop:disable Style/Documentation
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
+
8
24
  # Base class for all shared behaviour between these cops
9
- class Base < ::RuboCop::Cop::Cop
25
+ class Base < best_base_class
10
26
  CONFIG_ALL = 'All'
11
27
  CONFIG_FLOAT = 'Float'
12
28
  CONFIG_INTEGER = 'Integer'
@@ -12,15 +12,15 @@ module RuboCop
12
12
  #
13
13
  # GOOD:
14
14
  # object.bottles_on_the_wall(DEFAULT_BOTTLE_COUNT)
15
- class NoArgument < Base
15
+ class NoArgument < RuboCop::Cop::MagicNumbers::Base
16
16
  MAGIC_NUMBER_ARGUMENT_PATTERN = <<-PATTERN
17
17
  (send
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
  }
@@ -38,14 +38,14 @@ module RuboCop
38
38
  }.freeze
39
39
 
40
40
  def cop_config
41
- super.merge(DEFAULT_CONFIG)
41
+ DEFAULT_CONFIG.merge(super)
42
42
  end
43
43
 
44
44
  def on_message_send(node)
45
45
  return unless illegal_argument?(node)
46
46
  return if ignored_method?(node)
47
47
 
48
- add_offense(node, location: :expression, message: ARGUMENT_MSG)
48
+ add_offense(node, message: ARGUMENT_MSG)
49
49
  end
50
50
  alias on_send on_message_send # rubocop API method name
51
51
 
@@ -62,13 +62,18 @@ module RuboCop
62
62
  end
63
63
 
64
64
  def illegal_argument?(node)
65
- node_matches_pattern?(
65
+ captured_value = node_matches_pattern?(
66
66
  node:,
67
67
  pattern: format(
68
68
  MAGIC_NUMBER_ARGUMENT_PATTERN,
69
69
  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
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # bad: hours = 24
13
13
  #
14
14
  # good: HOURS_IN_ONE_DAY = 24
15
- class NoAssignment < Base
15
+ class NoAssignment < RuboCop::Cop::MagicNumbers::Base
16
16
  MAGIC_NUMBER_ARGUMENT_TO_SETTER_PATTERN = <<-PATTERN
17
17
  (send
18
18
  ({send self} ...)
@@ -43,7 +43,7 @@ module RuboCop
43
43
  return unless illegal_scalar_value?(node)
44
44
  return unless node_within_method?(node)
45
45
 
46
- add_offense(node, location: :expression, message: LOCAL_VARIABLE_ASSIGN_MSG)
46
+ add_offense(node, message: LOCAL_VARIABLE_ASSIGN_MSG)
47
47
  end
48
48
  alias on_lvasgn on_local_variable_assignment # rubocop API method name
49
49
 
@@ -51,7 +51,7 @@ module RuboCop
51
51
  return unless illegal_scalar_value?(node)
52
52
  return unless node_within_method?(node)
53
53
 
54
- add_offense(node, location: :expression, message: INSTANCE_VARIABLE_ASSIGN_MSG)
54
+ add_offense(node, message: INSTANCE_VARIABLE_ASSIGN_MSG)
55
55
  end
56
56
  alias on_ivasgn on_instance_variable_assignment # rubocop API method name
57
57
 
@@ -59,7 +59,7 @@ module RuboCop
59
59
  return unless illegal_scalar_argument_to_setter?(node)
60
60
  return unless node_within_method?(node)
61
61
 
62
- add_offense(node, location: :expression, message: PROPERTY_MSG)
62
+ add_offense(node, message: PROPERTY_MSG)
63
63
  end
64
64
  alias on_send on_message_send # rubocop API method name
65
65
 
@@ -69,7 +69,7 @@ module RuboCop
69
69
  # numbers amongst their assignments
70
70
  return false unless illegal_multi_assign_right_hand_side?(node)
71
71
 
72
- add_offense(node, location: :expression, message: MULTIPLE_ASSIGN_MSG)
72
+ add_offense(node, message: MULTIPLE_ASSIGN_MSG)
73
73
  end
74
74
  alias on_masgn on_multiple_assign
75
75
 
@@ -13,7 +13,7 @@ module RuboCop
13
13
  #
14
14
  # GOOD
15
15
  # def on_the_wall(bottles = DEFAULT_BOTTLE_COUNT)
16
- class NoDefault < Base
16
+ class NoDefault < RuboCop::Cop::MagicNumbers::Base
17
17
  MAGIC_NUMBER_OPTIONAL_ARGUMENT_PATTERN = <<-PATTERN
18
18
  (def
19
19
  _
@@ -35,7 +35,6 @@ module RuboCop
35
35
 
36
36
  add_offense(
37
37
  node,
38
- location: :expression,
39
38
  message: DEFAULT_OPTIONAL_ARGUMENT_MSG
40
39
  )
41
40
  end
@@ -7,7 +7,7 @@ module RuboCop
7
7
  module MagicNumbers
8
8
  # Raises an offense if a method returns with a magic number
9
9
  # Catches both explicit and implicit returns
10
- class NoReturn < Base
10
+ class NoReturn < RuboCop::Cop::MagicNumbers::Base
11
11
  MAGIC_NUMBER_RETURN_PATTERN = <<~PATTERN.chomp
12
12
  (%<illegal_scalar_pattern>s _)
13
13
  PATTERN
@@ -32,7 +32,7 @@ module RuboCop
32
32
  return if allowed_returns.include?(RETURN_TYPE_IMPLICIT)
33
33
  return unless implicit_return?(node.children.last)
34
34
 
35
- add_offense(node.children.last, location: :expression, message: NO_EXPLICIT_RETURN_MSG)
35
+ add_offense(node.children.last, message: NO_EXPLICIT_RETURN_MSG)
36
36
  end
37
37
  alias on_def on_method_defined
38
38
 
@@ -40,7 +40,7 @@ module RuboCop
40
40
  return if allowed_returns.include?(RETURN_TYPE_EXPLICIT)
41
41
  return unless forbidden_numerics.include?(node.children.first&.type)
42
42
 
43
- add_offense(node.children.first, location: :expression, message: NO_EXPLICIT_RETURN_MSG)
43
+ add_offense(node.children.first, message: NO_EXPLICIT_RETURN_MSG)
44
44
  end
45
45
 
46
46
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MagicNumbers
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop/magic_numbers'
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.1.0
4
+ version: 0.3.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-06-19 00:00:00.000000000 Z
12
+ date: 2023-06-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -49,6 +49,7 @@ extensions: []
49
49
  extra_rdoc_files: []
50
50
  files:
51
51
  - README.md
52
+ - lib/rubocop-magic_numbers.rb
52
53
  - lib/rubocop/cop/magic_numbers/base.rb
53
54
  - lib/rubocop/cop/magic_numbers/no_argument.rb
54
55
  - lib/rubocop/cop/magic_numbers/no_assignment.rb