2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-26 19:49:21 +01:00
|
|
|
require "forwardable"
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Cask
|
|
|
|
# This cop checks that a cask's stanzas are ordered correctly.
|
2021-06-23 13:23:18 -07:00
|
|
|
# @see https://docs.brew.sh/Cask-Cookbook#stanza-order
|
2021-01-12 18:25:05 +11:00
|
|
|
class StanzaOrder < Base
|
2018-10-26 19:49:21 +01:00
|
|
|
extend Forwardable
|
2021-01-12 18:25:05 +11:00
|
|
|
extend AutoCorrector
|
2018-10-26 19:49:21 +01:00
|
|
|
include CaskHelp
|
|
|
|
|
2023-04-13 19:10:38 +01:00
|
|
|
ON_SYSTEM_METHODS = RuboCop::Cask::Constants::ON_SYSTEM_METHODS
|
2019-10-03 08:50:45 +02:00
|
|
|
MESSAGE = "`%<stanza>s` stanza out of order"
|
2018-10-26 19:49:21 +01:00
|
|
|
|
|
|
|
def on_cask(cask_block)
|
|
|
|
@cask_block = cask_block
|
2023-04-13 19:10:38 +01:00
|
|
|
add_offenses(toplevel_stanzas)
|
|
|
|
|
|
|
|
return unless (on_blocks = toplevel_stanzas.select { |s| ON_SYSTEM_METHODS.include?(s.stanza_name) }).any?
|
|
|
|
|
|
|
|
on_blocks.map(&:method_node).each do |on_block|
|
|
|
|
next unless on_block.block_type?
|
|
|
|
|
|
|
|
block_contents = on_block.child_nodes.select(&:begin_type?)
|
|
|
|
inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?)
|
|
|
|
inner_stanzas = inner_nodes.map { |node| RuboCop::Cask::AST::Stanza.new(node, processed_source.comments) }
|
|
|
|
add_offenses(inner_stanzas, inner: true)
|
|
|
|
end
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :cask_block
|
2020-05-12 08:32:27 +01:00
|
|
|
|
2018-10-26 19:49:21 +01:00
|
|
|
def_delegators :cask_block, :cask_node, :toplevel_stanzas,
|
2023-04-13 19:10:38 +01:00
|
|
|
:sorted_toplevel_stanzas, :sorted_inner_stanzas
|
2018-10-26 19:49:21 +01:00
|
|
|
|
2023-04-13 19:10:38 +01:00
|
|
|
def add_offenses(stanzas, inner: false)
|
|
|
|
sorted_stanzas = inner ? sorted_inner_stanzas(stanzas) : sorted_toplevel_stanzas
|
|
|
|
offending_stanzas(stanzas, inner: inner).each do |stanza|
|
2018-10-26 19:49:21 +01:00
|
|
|
message = format(MESSAGE, stanza: stanza.stanza_name)
|
2021-01-12 18:25:05 +11:00
|
|
|
add_offense(stanza.source_range_with_comments, message: message) do |corrector|
|
2023-04-13 19:10:38 +01:00
|
|
|
correct_stanza_index = stanzas.index(stanza)
|
|
|
|
correct_stanza = sorted_stanzas[correct_stanza_index]
|
2021-01-12 18:25:05 +11:00
|
|
|
corrector.replace(stanza.source_range_with_comments,
|
|
|
|
correct_stanza.source_with_comments)
|
|
|
|
end
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-13 19:10:38 +01:00
|
|
|
def offending_stanzas(stanzas, inner: false)
|
|
|
|
sorted_stanzas = inner ? sorted_inner_stanzas(stanzas) : sorted_toplevel_stanzas
|
|
|
|
stanza_pairs = stanzas.zip(sorted_stanzas)
|
2018-10-26 19:49:21 +01:00
|
|
|
stanza_pairs.each_with_object([]) do |stanza_pair, offending_stanzas|
|
|
|
|
stanza, sorted_stanza = *stanza_pair
|
2023-04-18 15:06:50 -07:00
|
|
|
offending_stanzas << stanza if stanza != sorted_stanza
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|