Class: RuboCop::Cop::Sorbet::SignatureBuildOrder
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Sorbet::SignatureBuildOrder
- Extended by:
- AutoCorrector
- Includes:
- SignatureHelp
- Defined in:
- lib/rubocop/cop/sorbet/signatures/signature_build_order.rb
Overview
Checks for the correct order of ‘sig` builder methods.
Options:
-
‘Order`: The order in which to enforce the builder methods are called.
Instance Method Summary collapse
Methods included from SignatureHelp
#on_block, #signature?, #with_runtime?, #without_runtime?
Instance Method Details
#on_signature(node) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/sorbet/signatures/signature_build_order.rb', line 33 def on_signature(node) body = node.body actual_calls_and_indexes = call_chain(body).map.with_index do |send_node, actual_index| # The index this method call appears at in the configured Order. expected_index = builder_method_indexes[send_node.method_name] [send_node, actual_index, expected_index] end # Temporarily extract unknown method calls expected_calls_and_indexes, unknown_calls_and_indexes = actual_calls_and_indexes .partition { |_, _, expected_index| expected_index } # Sort known method calls by expected index expected_calls_and_indexes.sort_by! { |_, _, expected_index| expected_index } # Re-insert unknown method calls in their positions unknown_calls_and_indexes.each do |entry| _, original_index, _ = entry expected_calls_and_indexes.insert(original_index, entry) end # Compare expected and actual ordering expected_method_names = expected_calls_and_indexes.map { |send_node, _, _| send_node.method_name } actual_method_names = actual_calls_and_indexes.map { |send_node, _, _| send_node.method_name } return if expected_method_names == actual_method_names add_offense( body, message: "Sig builders must be invoked in the following order: #{expected_method_names.join(", ")}.", ) { |corrector| corrector.replace(body, expected_source(expected_calls_and_indexes)) } end |
#root_call(node) ⇒ Object
29 30 31 |
# File 'lib/rubocop/cop/sorbet/signatures/signature_build_order.rb', line 29 def_node_search(:root_call, <<~PATTERN) (send nil? #builder? ...) PATTERN |