brew/Library/Homebrew/rubocops/dependency_order.rb

173 lines
6.6 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2023-02-20 10:22:39 -08:00
require "rubocops/extend/formula_cop"
module RuboCop
module Cop
module FormulaAudit
# This cop checks for correct order of `depends_on` in formulae.
#
# precedence order:
# build-time > test > normal > recommended > optional
2023-02-20 18:10:59 -08:00
class DependencyOrder < FormulaCop
extend AutoCorrector
sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
body_node = formula_nodes.body_node
check_dependency_nodes_order(body_node)
check_uses_from_macos_nodes_order(body_node)
([:head, :stable] + on_system_methods).each do |block_name|
block = find_block(body_node, block_name)
next unless block
2018-09-17 02:45:00 +02:00
check_dependency_nodes_order(block.body)
check_uses_from_macos_nodes_order(block.body)
end
end
def check_uses_from_macos_nodes_order(parent_node)
return if parent_node.nil?
dependency_nodes = parent_node.each_child_node.select { |x| uses_from_macos_node?(x) }
ensure_dependency_order(dependency_nodes)
end
def check_dependency_nodes_order(parent_node)
return if parent_node.nil?
2018-09-17 02:45:00 +02:00
dependency_nodes = parent_node.each_child_node.select { |x| depends_on_node?(x) }
ensure_dependency_order(dependency_nodes)
end
def ensure_dependency_order(nodes)
ordered = nodes.sort_by { |node| dependency_name(node).downcase }
ordered = sort_dependencies_by_type(ordered)
sort_conditional_dependencies!(ordered)
verify_order_in_source(ordered)
end
# Separate dependencies according to precedence order:
# build-time > test > normal > recommended > optional
def sort_dependencies_by_type(dependency_nodes)
unsorted_deps = dependency_nodes.to_a
ordered = []
ordered.concat(unsorted_deps.select { |dep| buildtime_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| test_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.reject { |dep| negate_normal_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| recommended_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| optional_dependency? dep })
end
# `depends_on :apple if build.with? "foo"` should always be defined
# after `depends_on :foo`.
# This method reorders the dependencies array according to the above rule.
2023-02-14 19:19:37 -08:00
sig { params(ordered: T::Array[RuboCop::AST::Node]).returns(T::Array[RuboCop::AST::Node]) }
def sort_conditional_dependencies!(ordered)
length = ordered.size
idx = 0
while idx < length
2023-02-14 19:19:37 -08:00
idx1 = T.let(nil, T.nilable(Integer))
idx2 = T.let(nil, T.nilable(Integer))
ordered.each_with_index do |dep, pos|
idx = pos+1
match_nodes = build_with_dependency_name(dep)
2024-01-26 13:47:59 -08:00
next if match_nodes.blank?
2018-09-17 02:45:00 +02:00
idx1 = pos
ordered.drop(idx1+1).each_with_index do |dep2, pos2|
next unless match_nodes.index(dependency_name(dep2))
2018-09-17 02:45:00 +02:00
idx2 = pos2 if idx2.nil? || pos2 > idx2
end
break if idx2
end
2023-02-14 19:19:37 -08:00
insert_after!(ordered, idx1, idx2 + T.must(idx1)) if idx2
end
ordered
end
# Verify actual order of sorted `depends_on` nodes in source code;
# raise RuboCop problem otherwise.
def verify_order_in_source(ordered)
ordered.each_with_index do |node_1, idx|
l1 = line_number(node_1)
2023-02-14 19:19:37 -08:00
l2 = T.let(nil, T.nilable(Integer))
node_2 = T.let(nil, T.nilable(RuboCop::AST::Node))
ordered.drop(idx + 1).each do |test_node|
l2 = line_number(test_node)
node_2 = test_node if l2 < l1
end
next unless node_2
offending_node(node_1)
2018-09-17 02:45:00 +02:00
problem "`dependency \"#{dependency_name(node_1)}\"` (line #{l1}) should be put before " \
"`dependency \"#{dependency_name(node_2)}\"` (line #{l2})" do |corrector|
indentation = " " * (start_column(node_2) - line_start_column(node_2))
line_breaks = "\n"
corrector.insert_before(node_2.source_range,
node_1.source + line_breaks + indentation)
corrector.remove(range_with_surrounding_space(range: node_1.source_range, side: :left))
end
end
end
# Node pattern method to match
# `depends_on` variants.
def_node_matcher :depends_on_node?, <<~EOS
{(if _ (send nil? :depends_on ...) nil?)
(send nil? :depends_on ...)}
EOS
def_node_matcher :uses_from_macos_node?, <<~EOS
{(if _ (send nil? :uses_from_macos ...) nil?)
(send nil? :uses_from_macos ...)}
EOS
def_node_search :buildtime_dependency?, "(sym :build)"
def_node_search :recommended_dependency?, "(sym :recommended)"
def_node_search :test_dependency?, "(sym :test)"
def_node_search :optional_dependency?, "(sym :optional)"
def_node_search :negate_normal_dependency?, "(sym {:build :recommended :test :optional})"
# Node pattern method to extract `name` in `depends_on :name` or `uses_from_macos :name`
def_node_search :dependency_name_node, <<~EOS
{(send nil? {:depends_on :uses_from_macos} {(hash (pair $_ _) ...) $({str sym} _) $(const nil? _)} ...)
(if _ (send nil? :depends_on {(hash (pair $_ _)) $({str sym} _) $(const nil? _)}) nil?)}
EOS
# Node pattern method to extract `name` in `build.with? :name`
def_node_search :build_with_dependency_node, <<~EOS
(send (send nil? :build) :with? $({str sym} _))
EOS
def insert_after!(arr, idx1, idx2)
arr.insert(idx2+1, arr.delete_at(idx1))
end
def build_with_dependency_name(node)
match_nodes = build_with_dependency_node(node)
match_nodes = match_nodes.to_a.compact
match_nodes.map { |n| string_content(n) } unless match_nodes.empty?
end
def dependency_name(dependency_node)
match_node = dependency_name_node(dependency_node).to_a.first
string_content(match_node) if match_node
end
end
end
end
end