rubocop-magic_numbers 0.1.0 → 0.2.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: 4db321fec029b7e924bda74c35c9f9dcfdd902173d31965fad3ca07507ef606b
4
- data.tar.gz: f477370605f885adfd7a1353a6065c637226ff4b637001546b4b2e4109787bde
3
+ metadata.gz: 12fab74132b62ca4cf123a33001e1eadc3f47b6a52c9599881b9b1cdf09861c0
4
+ data.tar.gz: b9e7dfba1c5fca384d64a82f77ea220393613bd594db4e60e734de29d50a4c7d
5
5
  SHA512:
6
- metadata.gz: 408cb1f1488a6e57063a57b7422dc74145334c3c296e7d3dd129e1e7447c5e309df13040b7aae1d258294b21026e81ff586e4c9754f7af2be31b916343690a10
7
- data.tar.gz: 4020f1a2891c85d1f0ed3c13acffb47e10a3213d073335d907a1d53f2faeaf912b631a80b332a443b3e2e6fc8da93f54a6958ca5e72590c34dd66d2ef00bcf50
6
+ metadata.gz: 01fa64c0680e19aa431490ccc976574c56f30f5a26b20776d292e2ba01f22c481c6072e2a465dae1fccdc64d7c23653308dd550295607102160e9f4973d806cc
7
+ data.tar.gz: 4f6875eb58bcfcde76128da2709d7f215e986ce8a9c0c6013dc10428e1eddec0b14060b7eb8207a8f3ff0cb5fb2423b07e4f51c32409e301ddef835830c92553
@@ -1,12 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rubocop'
3
4
  require 'rubocop/cop/cop'
5
+ begin
6
+ require 'rubocop/cop/base'
7
+ rescue LoadError
8
+ end
4
9
 
5
10
  module RuboCop
6
11
  module Cop
7
12
  module MagicNumbers
8
13
  # Base class for all shared behaviour between these cops
9
- class Base < ::RuboCop::Cop::Cop
14
+
15
+ def self.best_base_class
16
+ if ::RuboCop::Cop.const_defined?('Base')
17
+ ::RuboCop::Cop::Base
18
+ else
19
+ ::RuboCop::Cop::Cop
20
+ end
21
+ end
22
+
23
+ class Base < best_base_class
10
24
  CONFIG_ALL = 'All'
11
25
  CONFIG_FLOAT = 'Float'
12
26
  CONFIG_INTEGER = 'Integer'
@@ -12,7 +12,7 @@ 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
  {
@@ -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
 
@@ -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,7 @@ module RuboCop
35
35
 
36
36
  add_offense(
37
37
  node,
38
- location: :expression,
38
+
39
39
  message: DEFAULT_OPTIONAL_ARGUMENT_MSG
40
40
  )
41
41
  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.2.0'
6
6
  end
7
7
  end
@@ -0,0 +1 @@
1
+ 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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Morrice
@@ -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