From 9aabe1ba910127e582bcbcbef82a3b977c6cf1a1 Mon Sep 17 00:00:00 2001 From: Razvan Azamfirei Date: Sat, 27 Jan 2024 16:19:16 -0500 Subject: [PATCH] rubocop: preserve comments when ordering uninstall methods --- .../rubocops/cask/uninstall_methods_order.rb | 34 ++++++++++++++----- Library/Homebrew/rubocops/rubocop-cask.rb | 2 +- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/uninstall_methods_order.rb b/Library/Homebrew/rubocops/cask/uninstall_methods_order.rb index 07c02166650fe..ae252485af95b 100644 --- a/Library/Homebrew/rubocops/cask/uninstall_methods_order.rb +++ b/Library/Homebrew/rubocops/cask/uninstall_methods_order.rb @@ -9,10 +9,9 @@ module Cask # This cop checks for the correct order of methods within the 'uninstall' and 'zap' stanzas. class UninstallMethodsOrder < Base extend AutoCorrector - include CaskHelp include HelperFunctions - MSG = T.let("`%s` method out of order".freeze, String) + MSG = T.let("`%s` method out of order", String) sig { params(node: AST::SendNode).void } def on_send(node) @@ -23,18 +22,35 @@ def on_send(node) method_nodes = hash_node.pairs.map(&:key) expected_order = method_nodes.sort_by { |method| method_order_index(method) } + comments = processed_source.comments method_nodes.each_with_index do |method, index| next if method == expected_order[index] - add_offense(method, message: format(MSG, method: method.children.first)) do |corrector| - indentation = " " * (start_column(method) - line_start_column(method)) - ordered_sources = expected_order.map do |expected_method| - hash_node.pairs.find { |pair| pair.key == expected_method }.source + report_and_correct_offense(method, hash_node, expected_order, comments) + end + end + + sig { + params(method: AST::Node, + hash_node: AST::HashNode, + expected_order: T::Array[AST::Node], + comments: T::Array[Parser::Source::Comment]).void + } + def report_and_correct_offense(method, hash_node, expected_order, comments) + add_offense(method, message: format(MSG, method: method.children.first)) do |corrector| + indentation = " " * (start_column(method) - line_start_column(method)) + new_code = expected_order.map do |expected_method| + method_pair = hash_node.pairs.find { |pair| pair.key == expected_method } + source = method_pair.source + + # Find and attach a comment on the same line as the method_pair, if any + inline_comment = comments.find do |comment| + comment.location.line == method_pair.loc.line && comment.location.column > method_pair.loc.column end - new_code = ordered_sources.join(",\n#{indentation}") - corrector.replace(hash_node.source_range, new_code) - end + inline_comment ? "#{source} #{inline_comment.text}" : source + end.join(",\n#{indentation}") + corrector.replace(hash_node.source_range, new_code) end end diff --git a/Library/Homebrew/rubocops/rubocop-cask.rb b/Library/Homebrew/rubocops/rubocop-cask.rb index dd498fb2615f8..572db73393352 100644 --- a/Library/Homebrew/rubocops/rubocop-cask.rb +++ b/Library/Homebrew/rubocops/rubocop-cask.rb @@ -18,9 +18,9 @@ require_relative "cask/homepage_url_trailing_slash" require_relative "cask/no_overrides" require_relative "cask/on_system_conditionals" -require_relative "cask/uninstall_methods_order" require_relative "cask/stanza_order" require_relative "cask/stanza_grouping" +require_relative "cask/uninstall_methods_order" require_relative "cask/url" require_relative "cask/url_legacy_comma_separators" require_relative "cask/variables"