|
| 1 | +module RuboCop |
| 2 | + module Cop |
| 3 | + module Migration |
| 4 | + class TooManyMigrationRuns < Base |
| 5 | + def on_new_investigation |
| 6 | + return unless processed_source.ast |
| 7 | + |
| 8 | + definitions = extract_migrator_definitions |
| 9 | + call_count = count_migrator_calls(definitions) |
| 10 | + |
| 11 | + return unless call_count > 4 |
| 12 | + |
| 13 | + add_offense(processed_source.ast, |
| 14 | + message: "Too many migration runs (#{call_count}). Combine tests to reduce migrations. See spec/migrations/README.md for further guidance.") |
| 15 | + end |
| 16 | + |
| 17 | + private |
| 18 | + |
| 19 | + def extract_migrator_definitions |
| 20 | + definitions = { |
| 21 | + subject_names: [], |
| 22 | + method_names: [], |
| 23 | + let_names: [], |
| 24 | + before_after_blocks: Set.new |
| 25 | + } |
| 26 | + |
| 27 | + # Single pass through the AST to collect all definitions |
| 28 | + processed_source.ast.each_descendant(:def, :block) do |node| |
| 29 | + case node.type |
| 30 | + when :def |
| 31 | + extract_migrator_method(node, definitions[:method_names]) |
| 32 | + when :block |
| 33 | + extract_block_definitions(node, definitions) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + definitions |
| 38 | + end |
| 39 | + |
| 40 | + def extract_migrator_method(node, method_names) |
| 41 | + return unless contains_migrator_run?(node) |
| 42 | + |
| 43 | + method_names << node.method_name |
| 44 | + end |
| 45 | + |
| 46 | + def extract_block_definitions(node, definitions) |
| 47 | + return unless node.send_node |
| 48 | + |
| 49 | + method_name = node.send_node.method_name |
| 50 | + |
| 51 | + case method_name |
| 52 | + when :subject |
| 53 | + extract_named_migrator(node, definitions[:subject_names]) |
| 54 | + when :let, :let! |
| 55 | + extract_named_migrator(node, definitions[:let_names]) |
| 56 | + when :before, :after, :around |
| 57 | + definitions[:before_after_blocks].add(node.object_id) if contains_migrator_run?(node) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def extract_named_migrator(node, names) |
| 62 | + return unless contains_migrator_run?(node) |
| 63 | + |
| 64 | + first_arg = node.send_node.first_argument |
| 65 | + names << first_arg.value if first_arg&.sym_type? |
| 66 | + end |
| 67 | + |
| 68 | + def count_migrator_calls(definitions) |
| 69 | + call_count = 0 |
| 70 | + |
| 71 | + # Single pass through send nodes to count all migration calls |
| 72 | + processed_source.ast.each_descendant(:send) do |node| |
| 73 | + next unless migration_call?(node, definitions) |
| 74 | + |
| 75 | + call_count += 1 |
| 76 | + end |
| 77 | + |
| 78 | + call_count |
| 79 | + end |
| 80 | + |
| 81 | + def migration_call?(node, definitions) |
| 82 | + in_before_after_block = node.each_ancestor(:block).any? do |ancestor| |
| 83 | + definitions[:before_after_blocks].include?(ancestor.object_id) |
| 84 | + end |
| 85 | + |
| 86 | + if in_before_after_block |
| 87 | + # Count direct Migrator.run calls inside before/after/around blocks |
| 88 | + migrator_run_call?(node) |
| 89 | + else |
| 90 | + # Count direct calls (not in definitions) or helper invocations |
| 91 | + direct_migrator_call?(node) || helper_migration_call?(node, definitions) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def direct_migrator_call?(node) |
| 96 | + return false unless migrator_run_call?(node) |
| 97 | + |
| 98 | + !inside_definition?(node) |
| 99 | + end |
| 100 | + |
| 101 | + def migrator_run_call?(node) |
| 102 | + return false unless node.method_name == :run |
| 103 | + |
| 104 | + receiver = node.receiver |
| 105 | + return false unless receiver |
| 106 | + |
| 107 | + # Check for Sequel::Migrator.run or just Migrator.run |
| 108 | + if receiver.const_type? |
| 109 | + receiver_name = receiver.const_name |
| 110 | + return ['Migrator', 'Sequel::Migrator'].include?(receiver_name) |
| 111 | + end |
| 112 | + |
| 113 | + false |
| 114 | + end |
| 115 | + |
| 116 | + def helper_migration_call?(node, definitions) |
| 117 | + method = node.method_name |
| 118 | + definitions[:subject_names].include?(method) || |
| 119 | + definitions[:let_names].include?(method) || |
| 120 | + definitions[:method_names].include?(method) |
| 121 | + end |
| 122 | + |
| 123 | + def contains_migrator_run?(node) |
| 124 | + node.each_descendant(:send).any? { |send_node| migrator_run_call?(send_node) } |
| 125 | + end |
| 126 | + |
| 127 | + def inside_definition?(node) |
| 128 | + node.each_ancestor(:def, :block).any? do |ancestor| |
| 129 | + case ancestor.type |
| 130 | + when :def |
| 131 | + contains_migrator_run?(ancestor) |
| 132 | + when :block |
| 133 | + next false unless ancestor.send_node |
| 134 | + |
| 135 | + method = ancestor.send_node.method_name |
| 136 | + if %i[subject let let!].include?(method) |
| 137 | + contains_migrator_run?(ancestor) |
| 138 | + else |
| 139 | + %i[before after around].include?(method) |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | +end |
0 commit comments