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
|
2023-04-15 22:55:12 +01:00
|
|
|
# This cop checks that a cask's stanzas are ordered correctly, including nested within `on_*` blocks.
|
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
|
|
|
|
|
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
|
|
|
|
2023-04-21 00:30:45 +01:00
|
|
|
# Find all the stanzas that are direct children of the cask block or one of its `on_*` blocks.
|
|
|
|
puts "toplevel_stanzas: #{toplevel_stanzas.map(&:stanza_name).inspect}"
|
|
|
|
outer_and_inner_stanzas = toplevel_stanzas + toplevel_stanzas.map do |stanza|
|
|
|
|
return stanza unless stanza.method_node&.block_type?
|
|
|
|
|
|
|
|
inner_stanzas(stanza.method_node, stanza.comments)
|
2023-04-13 19:10:38 +01:00
|
|
|
end
|
2023-04-20 15:41:04 +00:00
|
|
|
|
2023-04-21 00:30:45 +01:00
|
|
|
puts "outer_and_inner_stanzas: #{outer_and_inner_stanzas.flatten.map(&:stanza_name).inspect}"
|
|
|
|
add_offenses(outer_and_inner_stanzas.flatten)
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :cask_block
|
2020-05-12 08:32:27 +01:00
|
|
|
|
2023-04-21 00:30:45 +01:00
|
|
|
def_delegators :cask_block, :cask_node, :toplevel_stanzas
|
2023-04-20 15:41:04 +00:00
|
|
|
|
|
|
|
def add_offenses(outer_and_inner_stanzas)
|
2023-04-21 00:30:45 +01:00
|
|
|
outer_and_inner_stanzas.each_cons(2) do |stanza1, stanza2|
|
|
|
|
next if stanza_order_index(stanza1.stanza_name) < stanza_order_index(stanza2.stanza_name)
|
|
|
|
|
|
|
|
puts "#{stanza2.stanza_name} should come before #{stanza1.stanza_name}"
|
|
|
|
add_offense(stanza1.method_node, message: format(MESSAGE, stanza: stanza1.stanza_name)) do |corrector|
|
|
|
|
# TODO: Move the stanza to the correct location.
|
2021-01-12 18:25:05 +11:00
|
|
|
end
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-21 00:30:45 +01:00
|
|
|
def stanza_order_index(stanza_name)
|
|
|
|
RuboCop::Cask::Constants::STANZA_ORDER.index(stanza_name)
|
2018-10-26 19:49:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|