Skip to content

Commit

Permalink
Fix autocorrect for Sorbet signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedalbert committed Nov 2, 2024
1 parent 9c6dd62 commit b7b1f4b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/rubocop/cop/obsession/method_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,28 @@ def autocorrect(corrector, method, previous_method)
previous_method_range = previous_method_range.adjust(end_pos: 1)
end

method_range = source_range_with_comment(method)
method_range = source_range_with_signature(method)
if buffer.source[method_range.begin_pos - 1] == "\n"
method_range = method_range.adjust(end_pos: 1)
end

corrector.insert_after(previous_method_range, method_range.source)
corrector.remove(method_range)
end

def source_range_with_signature(method)
previous_node = method.left_sibling
begin_node = sorbet_signature?(previous_node) ? previous_node : method

begin_pos = begin_pos_with_comment(begin_node)
end_pos = end_position_for(method)

Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

def sorbet_signature?(node)
node && node.method_name == :sig && node.type == :block
end
end
end
end
Expand Down

0 comments on commit b7b1f4b

Please sign in to comment.