2024-07-14 00:24:16 +00:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-02-20 10:22:39 -08:00
|
|
|
require "rubocops/extend/formula_cop"
|
2017-04-08 15:10:44 +05:30
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2018-04-25 07:58:01 +10:00
|
|
|
module FormulaAudit
|
2020-08-26 02:24:22 +02:00
|
|
|
# This cop checks if redundant components are present and for other component errors.
|
2017-04-08 15:10:44 +05:30
|
|
|
#
|
2023-12-30 12:30:40 -05:00
|
|
|
# - `url|checksum|mirror|version` should be inside `stable` block
|
2017-04-08 15:10:44 +05:30
|
|
|
# - `head` and `head do` should not be simultaneously present
|
2018-10-18 21:42:43 -04:00
|
|
|
# - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present
|
2020-09-03 10:34:22 +01:00
|
|
|
# - `stable do` should not be present without a `head` spec
|
2023-12-30 12:30:40 -05:00
|
|
|
# - `stable do` should not be present with only `url|checksum|mirror|version`
|
|
|
|
# - `head do` should not be present with only `url`
|
2023-02-20 18:10:59 -08:00
|
|
|
class ComponentsRedundancy < FormulaCop
|
2019-04-19 15:38:03 +09:00
|
|
|
HEAD_MSG = "`head` and `head do` should not be simultaneously present"
|
|
|
|
BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present"
|
2020-09-03 10:34:22 +01:00
|
|
|
STABLE_MSG = "`stable do` should not be present without a `head` spec"
|
2023-12-30 12:30:40 -05:00
|
|
|
STABLE_BLOCK_METHODS = [:url, :sha256, :mirror, :version].freeze
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
return if (body_node = formula_nodes.body_node).nil?
|
2022-11-05 04:17:50 +00:00
|
|
|
|
2020-08-24 07:39:07 -04:00
|
|
|
urls = find_method_calls_by_name(body_node, :url)
|
|
|
|
|
|
|
|
urls.each do |url|
|
|
|
|
url.arguments.each do |arg|
|
|
|
|
next if arg.class != RuboCop::AST::HashNode
|
|
|
|
|
|
|
|
url_args = arg.keys.each.map(&:value)
|
|
|
|
if method_called?(body_node, :sha256) && url_args.include?(:tag) && url_args.include?(:revision)
|
2025-05-30 16:42:32 -04:00
|
|
|
problem "Do not use both `sha256` and `tag:`/`revision:`."
|
2020-08-24 07:39:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-19 00:36:18 -04:00
|
|
|
stable_block = find_block(body_node, :stable)
|
2017-04-08 15:10:44 +05:30
|
|
|
if stable_block
|
2023-12-30 12:30:40 -05:00
|
|
|
STABLE_BLOCK_METHODS.each do |method_name|
|
2017-06-19 00:36:18 -04:00
|
|
|
problem "`#{method_name}` should be put inside `stable` block" if method_called?(body_node, method_name)
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
2023-12-30 12:30:40 -05:00
|
|
|
|
|
|
|
unless stable_block.body.nil?
|
|
|
|
child_nodes = stable_block.body.begin_type? ? stable_block.body.child_nodes : [stable_block.body]
|
|
|
|
if child_nodes.all? { |n| n.send_type? && STABLE_BLOCK_METHODS.include?(n.method_name) }
|
|
|
|
problem "`stable do` should not be present with only #{STABLE_BLOCK_METHODS.join("/")}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
head_block = find_block(body_node, :head)
|
|
|
|
if head_block && !head_block.body.nil?
|
|
|
|
child_nodes = head_block.body.begin_type? ? head_block.body.child_nodes : [head_block.body]
|
|
|
|
if child_nodes.all? { |n| n.send_type? && n.method_name == :url }
|
|
|
|
problem "`head do` should not be present with only `url`"
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
|
|
|
|
2017-06-19 00:36:18 -04:00
|
|
|
problem HEAD_MSG if method_called?(body_node, :head) &&
|
|
|
|
find_block(body_node, :head)
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-06-19 00:36:18 -04:00
|
|
|
problem BOTTLE_MSG if method_called?(body_node, :bottle) &&
|
|
|
|
find_block(body_node, :bottle)
|
2018-07-03 11:46:39 +10:00
|
|
|
|
|
|
|
return if method_called?(body_node, :head) ||
|
2020-09-03 10:34:22 +01:00
|
|
|
find_block(body_node, :head)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-07-03 11:46:39 +10:00
|
|
|
problem STABLE_MSG if stable_block
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|