Class: RuboCop::Cop::Betterment::HardcodedID
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Betterment::HardcodedID
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/betterment/hardcoded_id.rb
Constant Summary collapse
- MSG =
'Hardcoded IDs cause flaky tests. Use a sequence instead.'
Instance Method Summary collapse
- #hardcoded_id?(node) ⇒ Object
- #key(node) ⇒ Object
- #on_block(node) ⇒ Object (also: #on_numblock)
- #on_factory(node) ⇒ Object
- #on_let_id(node) ⇒ Object
- #on_send(node) ⇒ Object
- #pair(node, value_pattern) ⇒ Object
- #references_hardcoded_id?(node, method_name) ⇒ Object
- #value(node) ⇒ Object
Instance Method Details
#hardcoded_id?(node) ⇒ Object
22 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 22 def_node_matcher :hardcoded_id?, '#pair(#value)' |
#key(node) ⇒ Object
13 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 13 def_node_matcher :key, '/^id$|_id$/' |
#on_block(node) ⇒ Object Also known as: on_numblock
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 48 def on_block(node) on_let_id(node) do |name, value| node.parent&.each_descendant do |child| each_factory_attribute(child) do |attribute_node| next unless references_hardcoded_id?(attribute_node, name) add_offense(node) do |corrector| attribute = Utils::HardcodedAttribute.new(attribute_node) corrector.replace(attribute.node.value, value.value.to_s) if attribute.correctable? end end end end end |
#on_factory(node) ⇒ Object
28 29 30 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 28 def_node_matcher :on_factory, <<~PATTERN (send (const nil? :FactoryBot) {:create | :create_list} _factory ... (hash $...)) PATTERN |
#on_let_id(node) ⇒ Object
33 34 35 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 33 def_node_matcher :on_let_id, <<~PATTERN (block (send nil? {:let | :let!} (sym $#key)) _block_args $#value) PATTERN |
#on_send(node) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 37 def on_send(node) each_factory_attribute(node) do |attribute_node| next unless hardcoded_id?(attribute_node) add_offense(attribute_node) do |corrector| attribute = Utils::HardcodedAttribute.new(attribute_node) correct_factory_usage(corrector, attribute) if attribute.correctable? end end end |
#pair(node, value_pattern) ⇒ Object
19 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 19 def_node_matcher :pair, '(pair (sym #key) %1)' |
#references_hardcoded_id?(node, method_name) ⇒ Object
25 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 25 def_node_matcher :references_hardcoded_id?, '#pair((send nil? %1))' |
#value(node) ⇒ Object
16 |
# File 'lib/rubocop/cop/betterment/hardcoded_id.rb', line 16 def_node_matcher :value, '{int | (str /^\d+$/)}' |