2024-08-12 10:30:59 +01:00
|
|
|
# typed: true # rubocop:todo Sorbet/StrictSigil
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-09 02:15:28 +02:00
|
|
|
require "macos_version"
|
2023-02-20 10:22:39 -08:00
|
|
|
require "rubocops/extend/formula_cop"
|
2022-08-14 15:39:20 -04:00
|
|
|
require "rubocops/shared/on_system_conditionals_helper"
|
2017-07-29 16:36:32 +05:30
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module FormulaAudit
|
2018-10-18 21:42:43 -04:00
|
|
|
# This cop checks for various miscellaneous Homebrew coding styles.
|
2017-07-29 16:36:32 +05:30
|
|
|
class Lines < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(_formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(_formula_nodes)
|
2017-12-30 18:58:41 +00:00
|
|
|
[:automake, :ant, :autoconf, :emacs, :expat, :libtool, :mysql, :perl,
|
|
|
|
:postgresql, :python, :python3, :rbenv, :ruby].each do |dependency|
|
2017-07-29 16:36:32 +05:30
|
|
|
next unless depends_on?(dependency)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 03:12:50 +02:00
|
|
|
problem ":#{dependency} is deprecated. Usage should be \"#{dependency}\"."
|
2017-07-29 16:36:32 +05:30
|
|
|
end
|
|
|
|
|
2017-12-30 18:58:41 +00:00
|
|
|
{ apr: "apr-util", fortran: "gcc", gpg: "gnupg", hg: "mercurial",
|
|
|
|
mpi: "open-mpi", python2: "python" }.each do |requirement, dependency|
|
2017-12-23 16:38:24 +00:00
|
|
|
next unless depends_on?(requirement)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-12-23 16:38:24 +00:00
|
|
|
problem ":#{requirement} is deprecated. Usage should be \"#{dependency}\"."
|
|
|
|
end
|
|
|
|
|
2017-10-21 03:12:50 +02:00
|
|
|
problem ":tex is deprecated." if depends_on?(:tex)
|
2017-07-29 16:36:32 +05:30
|
|
|
end
|
|
|
|
end
|
2017-07-30 20:14:59 +05:30
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that a space is used for class inheritance.
|
2017-07-30 20:14:59 +05:30
|
|
|
class ClassInheritance < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
parent_class_node = formula_nodes.parent_class_node
|
2017-07-30 20:14:59 +05:30
|
|
|
begin_pos = start_column(parent_class_node)
|
2024-07-07 15:18:29 -04:00
|
|
|
end_pos = end_column(formula_nodes.class_node)
|
2023-04-18 15:06:50 -07:00
|
|
|
return if begin_pos-end_pos == 3
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-09-02 16:15:09 +01:00
|
|
|
problem "Use a space in class inheritance: " \
|
|
|
|
"class #{@formula_name.capitalize} < #{class_name(parent_class_node)}"
|
2017-07-30 20:14:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that template comments are removed.
|
2017-07-30 20:14:59 +05:30
|
|
|
class Comments < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(_formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(_formula_nodes)
|
2017-07-30 20:14:59 +05:30
|
|
|
audit_comments do |comment|
|
|
|
|
[
|
|
|
|
"# PLEASE REMOVE",
|
|
|
|
"# Documentation:",
|
|
|
|
"# if this fails, try separate make/make install steps",
|
|
|
|
"# The URL of the archive",
|
|
|
|
"## Naming --",
|
|
|
|
"# if your formula fails when building in parallel",
|
|
|
|
"# Remove unrecognized options if warned by configure",
|
2017-07-30 20:14:59 +05:30
|
|
|
'# system "cmake',
|
2017-07-30 20:14:59 +05:30
|
|
|
].each do |template_comment|
|
|
|
|
next unless comment.include?(template_comment)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-30 20:14:59 +05:30
|
|
|
problem "Please remove default template comments"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
audit_comments do |comment|
|
|
|
|
# Commented-out depends_on
|
|
|
|
next unless comment =~ /#\s*depends_on\s+(.+)\s*$/
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-30 20:14:59 +05:30
|
|
|
problem "Commented-out dependency #{Regexp.last_match(1)}"
|
2017-07-30 20:14:59 +05:30
|
|
|
end
|
2020-01-08 14:55:31 -05:00
|
|
|
|
|
|
|
return if formula_tap != "homebrew-core"
|
|
|
|
|
|
|
|
# Citation and tag comments from third-party taps
|
|
|
|
audit_comments do |comment|
|
|
|
|
next if comment !~ /#\s*(cite(?=\s*\w+:)|doi(?=\s*['"])|tag(?=\s*['"]))/
|
|
|
|
|
|
|
|
problem "Formulae in homebrew/core should not use `#{Regexp.last_match(1)}` comments"
|
|
|
|
end
|
2017-07-30 20:14:59 +05:30
|
|
|
end
|
|
|
|
end
|
2017-07-31 14:30:37 +05:30
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that idiomatic `assert_*` statements are used.
|
2017-10-21 12:50:49 +05:30
|
|
|
class AssertStatements < FormulaCop
|
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
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
find_every_method_call_by_name(body_node, :assert).each do |method|
|
|
|
|
if method_called_ever?(method, :include?) && !method_called_ever?(method, :!)
|
|
|
|
problem "Use `assert_match` instead of `assert ...include?`"
|
|
|
|
end
|
|
|
|
|
|
|
|
if method_called_ever?(method, :exist?) && !method_called_ever?(method, :!)
|
|
|
|
problem "Use `assert_predicate <path_to_file>, :exist?` instead of `#{method.source}`"
|
|
|
|
end
|
|
|
|
|
|
|
|
if method_called_ever?(method, :exist?) && method_called_ever?(method, :!)
|
|
|
|
problem "Use `refute_predicate <path_to_file>, :exist?` instead of `#{method.source}`"
|
|
|
|
end
|
|
|
|
|
|
|
|
if method_called_ever?(method, :executable?) && !method_called_ever?(method, :!)
|
|
|
|
problem "Use `assert_predicate <path_to_file>, :executable?` instead of `#{method.source}`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# This cop makes sure that `option`s are used idiomatically.
|
2017-10-21 12:50:49 +05:30
|
|
|
class OptionDeclarations < FormulaCop
|
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
|
|
|
|
2019-02-19 13:11:32 +00:00
|
|
|
problem "Use new-style option definitions" if find_method_def(body_node, :options)
|
2017-10-21 12:50:49 +05:30
|
|
|
|
2020-07-05 23:43:09 -04:00
|
|
|
if formula_tap == "homebrew-core"
|
|
|
|
# Use of build.with? implies options, which are forbidden in homebrew/core
|
|
|
|
find_instance_method_call(body_node, :build, :without?) do
|
|
|
|
problem "Formulae in homebrew/core should not use `build.without?`."
|
|
|
|
end
|
|
|
|
find_instance_method_call(body_node, :build, :with?) do
|
|
|
|
problem "Formulae in homebrew/core should not use `build.with?`."
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-07-11 17:41:58 -04:00
|
|
|
depends_on_build_with(body_node) do |build_with_node|
|
|
|
|
offending_node(build_with_node)
|
|
|
|
problem "Use `:optional` or `:recommended` instead of `if #{build_with_node.source}`"
|
2020-07-05 23:43:09 -04:00
|
|
|
end
|
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
find_instance_method_call(body_node, :build, :without?) do |method|
|
|
|
|
next unless unless_modifier?(method.parent)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
correct = method.source.gsub("out?", "?")
|
|
|
|
problem "Use if #{correct} instead of unless #{method.source}"
|
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, :build, :with?) do |method|
|
|
|
|
next unless unless_modifier?(method.parent)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
correct = method.source.gsub("?", "out?")
|
|
|
|
problem "Use if #{correct} instead of unless #{method.source}"
|
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, :build, :with?) do |method|
|
2017-10-25 16:05:29 +05:30
|
|
|
next unless expression_negated?(method)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
problem "Don't negate 'build.with?': use 'build.without?'"
|
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, :build, :without?) do |method|
|
2017-10-25 16:05:29 +05:30
|
|
|
next unless expression_negated?(method)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
problem "Don't negate 'build.without?': use 'build.with?'"
|
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, :build, :without?) do |method|
|
|
|
|
arg = parameters(method).first
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(arg, /^-?-?without-(.*)/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-09-02 16:15:09 +01:00
|
|
|
problem "Don't duplicate 'without': " \
|
|
|
|
"Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\""
|
2017-10-21 12:50:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, :build, :with?) do |method|
|
|
|
|
arg = parameters(method).first
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(arg, /^-?-?with-(.*)/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\""
|
|
|
|
end
|
|
|
|
|
2020-07-05 23:43:09 -04:00
|
|
|
find_instance_method_call(body_node, :build, :include?) do
|
|
|
|
problem "`build.include?` is deprecated"
|
2020-01-09 10:09:55 -05:00
|
|
|
end
|
2017-10-21 12:50:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def unless_modifier?(node)
|
|
|
|
return false unless node.if_type?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
node.modifier_form? && node.unless?
|
|
|
|
end
|
2020-07-11 17:41:58 -04:00
|
|
|
|
|
|
|
# Finds `depends_on "foo" if build.with?("bar")` or `depends_on "foo" if build.without?("bar")`
|
|
|
|
def_node_search :depends_on_build_with, <<~EOS
|
2020-07-12 14:20:05 -04:00
|
|
|
(if $(send (send nil? :build) {:with? :without?} str)
|
|
|
|
(send nil? :depends_on str) nil?)
|
2020-07-11 17:41:58 -04:00
|
|
|
EOS
|
2017-10-21 12:50:49 +05:30
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that formulae depend on `open-mpi` instead of `mpich`.
|
2019-06-06 11:31:07 -04:00
|
|
|
class MpiCheck < FormulaCop
|
2021-01-12 02:21:51 +11:00
|
|
|
extend AutoCorrector
|
|
|
|
|
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
|
|
|
|
2019-06-06 11:31:07 -04:00
|
|
|
# Enforce use of OpenMPI for MPI dependency in core
|
2023-04-18 15:06:50 -07:00
|
|
|
return if formula_tap != "homebrew-core"
|
2019-06-06 11:31:07 -04:00
|
|
|
|
|
|
|
find_method_with_args(body_node, :depends_on, "mpich") do
|
2020-01-08 15:38:48 -05:00
|
|
|
problem "Formulae in homebrew/core should use 'depends_on \"open-mpi\"' " \
|
2021-07-06 23:44:09 +05:30
|
|
|
"instead of '#{@offensive_node.source}'." do |corrector|
|
2021-01-12 02:21:51 +11:00
|
|
|
corrector.replace(@offensive_node.source_range, "depends_on \"open-mpi\"")
|
|
|
|
end
|
2019-06-06 11:31:07 -04:00
|
|
|
end
|
|
|
|
end
|
2020-06-04 21:05:38 -04:00
|
|
|
end
|
|
|
|
|
2024-07-25 21:00:10 -07:00
|
|
|
# This cop makes sure that formulae use `std_npm_args` instead of older
|
|
|
|
# `local_npm_install_args` and `std_npm_install_args`.
|
|
|
|
class StdNpmArgs < FormulaCop
|
|
|
|
extend AutoCorrector
|
|
|
|
|
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
return if (body_node = formula_nodes.body_node).nil?
|
|
|
|
|
|
|
|
find_method_with_args(body_node, :local_npm_install_args) do
|
|
|
|
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: false)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
find_method_with_args(body_node, :std_npm_install_args) do |method|
|
|
|
|
problem "Use 'std_npm_args' instead of '#{@offensive_node.method_name}'." do |corrector|
|
|
|
|
if (param = parameters(method).first.source) == "libexec"
|
|
|
|
corrector.replace(@offensive_node.source_range, "std_npm_args")
|
|
|
|
else
|
|
|
|
corrector.replace(@offensive_node.source_range, "std_npm_args(prefix: #{param})")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
find_every_method_call_by_name(body_node, :system).each do |method|
|
|
|
|
first_param, second_param = parameters(method)
|
|
|
|
next if !node_equals?(first_param, "npm") ||
|
|
|
|
!node_equals?(second_param, "install") ||
|
|
|
|
method.source.match(/(std_npm_args|local_npm_install_args|std_npm_install_args)/)
|
|
|
|
|
|
|
|
offending_node(method)
|
|
|
|
problem "Use `std_npm_args` for npm install"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-01 23:07:28 +08:00
|
|
|
# This cop makes sure that formulae depend on `openssl` instead of `quictls`.
|
|
|
|
class QuicTLSCheck < FormulaCop
|
|
|
|
extend AutoCorrector
|
|
|
|
|
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?
|
2023-07-01 23:07:28 +08:00
|
|
|
|
|
|
|
# Enforce use of OpenSSL for TLS dependency in core
|
|
|
|
return if formula_tap != "homebrew-core"
|
|
|
|
|
|
|
|
find_method_with_args(body_node, :depends_on, "quictls") do
|
|
|
|
problem "Formulae in homebrew/core should use 'depends_on \"openssl@3\"' " \
|
|
|
|
"instead of '#{@offensive_node.source}'." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, "depends_on \"openssl@3\"")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 17:31:39 +08:00
|
|
|
# This cop makes sure that formulae do not depend on `pyoxidizer` at build-time
|
|
|
|
# or run-time.
|
|
|
|
class PyoxidizerCheck < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
return if formula_nodes.body_node.nil?
|
2021-11-30 17:31:39 +08:00
|
|
|
# Disallow use of PyOxidizer as a dependency in core
|
2023-04-18 15:06:50 -07:00
|
|
|
return if formula_tap != "homebrew-core"
|
2023-07-14 12:57:23 +08:00
|
|
|
return unless depends_on?("pyoxidizer")
|
2021-11-30 17:31:39 +08:00
|
|
|
|
2023-07-14 12:57:23 +08:00
|
|
|
problem "Formulae in homebrew/core should not use '#{@offensive_node.source}'."
|
2021-11-30 17:31:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that the safe versions of `popen_*` calls are used.
|
2020-06-04 21:52:20 -04:00
|
|
|
class SafePopenCommands < FormulaCop
|
2021-01-12 02:21:51 +11:00
|
|
|
extend AutoCorrector
|
|
|
|
|
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-06-04 21:05:38 -04:00
|
|
|
test = find_block(body_node, :test)
|
|
|
|
|
|
|
|
[:popen_read, :popen_write].each do |unsafe_command|
|
|
|
|
test_methods = []
|
|
|
|
|
|
|
|
unless test.nil?
|
|
|
|
find_instance_method_call(test, "Utils", unsafe_command) do |method|
|
|
|
|
test_methods << method.source_range
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, "Utils", unsafe_command) do |method|
|
|
|
|
unless test_methods.include?(method.source_range)
|
2021-01-12 02:21:51 +11:00
|
|
|
problem "Use `Utils.safe_#{unsafe_command}` instead of `Utils.#{unsafe_command}`" do |corrector|
|
|
|
|
corrector.replace(@offensive_node.loc.selector, "safe_#{@offensive_node.method_name}")
|
|
|
|
end
|
2020-06-04 21:05:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-04 21:52:20 -04:00
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that environment variables are passed correctly to `popen_*` calls.
|
2020-06-04 21:52:20 -04:00
|
|
|
class ShellVariables < FormulaCop
|
2021-01-12 02:21:51 +11:00
|
|
|
extend AutoCorrector
|
|
|
|
|
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-06-04 21:52:20 -04:00
|
|
|
popen_commands = [
|
|
|
|
:popen,
|
|
|
|
:popen_read,
|
|
|
|
:safe_popen_read,
|
|
|
|
:popen_write,
|
|
|
|
:safe_popen_write,
|
|
|
|
]
|
|
|
|
|
|
|
|
popen_commands.each do |command|
|
|
|
|
find_instance_method_call(body_node, "Utils", command) do |method|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(parameters(method).first, /^([^"' ]+)=([^"' ]+)(?: (.*))?$/))
|
2020-06-04 21:52:20 -04:00
|
|
|
|
|
|
|
good_args = "Utils.#{command}({ \"#{match[1]}\" => \"#{match[2]}\" }, \"#{match[3]}\")"
|
|
|
|
|
2021-01-12 02:21:51 +11:00
|
|
|
problem "Use `#{good_args}` instead of `#{method.source}`" do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range,
|
|
|
|
"{ \"#{match[1]}\" => \"#{match[2]}\" }, \"#{match[3]}\"")
|
|
|
|
end
|
2020-06-04 21:52:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-05 17:53:23 -04:00
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that `license` has the correct format.
|
2020-08-20 10:58:36 -04:00
|
|
|
class LicenseArrays < FormulaCop
|
2021-01-12 02:21:51 +11:00
|
|
|
extend AutoCorrector
|
|
|
|
|
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-20 10:58:36 -04:00
|
|
|
license_node = find_node_method_by_name(body_node, :license)
|
|
|
|
return unless license_node
|
|
|
|
|
|
|
|
license = parameters(license_node).first
|
|
|
|
return unless license.array_type?
|
|
|
|
|
2021-01-12 02:21:51 +11:00
|
|
|
problem "Use `license any_of: #{license.source}` instead of `license #{license.source}`" do |corrector|
|
|
|
|
corrector.replace(license_node.source_range, "license any_of: #{parameters(license_node).first.source}")
|
2020-08-20 10:58:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that nested `license` declarations are split onto multiple lines.
|
2020-08-20 11:20:04 -04:00
|
|
|
class Licenses < FormulaCop
|
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-20 11:20:04 -04:00
|
|
|
license_node = find_node_method_by_name(body_node, :license)
|
|
|
|
return unless license_node
|
2020-10-05 16:23:52 -04:00
|
|
|
return if license_node.source.include?("\n")
|
2020-08-20 11:20:04 -04:00
|
|
|
|
2020-10-05 16:23:52 -04:00
|
|
|
parameters(license_node).first.each_descendant(:hash).each do |license_hash|
|
|
|
|
next if license_exception? license_hash
|
2020-08-20 11:20:04 -04:00
|
|
|
|
2020-10-05 16:23:52 -04:00
|
|
|
problem "Split nested license declarations onto multiple lines"
|
|
|
|
end
|
2020-08-20 11:20:04 -04:00
|
|
|
end
|
2020-10-05 16:23:52 -04:00
|
|
|
|
|
|
|
def_node_matcher :license_exception?, <<~EOS
|
|
|
|
(hash (pair (sym :with) str))
|
|
|
|
EOS
|
2020-08-20 11:20:04 -04:00
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# This cop makes sure that Python versions are consistent.
|
2020-10-10 13:38:43 -04:00
|
|
|
class PythonVersions < FormulaCop
|
2021-01-12 02:21:51 +11:00
|
|
|
extend AutoCorrector
|
|
|
|
|
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-10-10 13:38:43 -04:00
|
|
|
python_formula_node = find_every_method_call_by_name(body_node, :depends_on).find do |dep|
|
|
|
|
string_content(parameters(dep).first).start_with? "python@"
|
|
|
|
end
|
|
|
|
|
2023-10-18 13:14:40 -07:00
|
|
|
python_version = if python_formula_node.blank?
|
|
|
|
other_python_nodes = find_every_method_call_by_name(body_node, :depends_on).select do |dep|
|
|
|
|
parameters(dep).first.instance_of?(RuboCop::AST::HashNode) &&
|
|
|
|
string_content(parameters(dep).first.keys.first).start_with?("python@")
|
|
|
|
end
|
|
|
|
return if other_python_nodes.size != 1
|
2020-10-10 13:38:43 -04:00
|
|
|
|
2023-10-18 13:14:40 -07:00
|
|
|
string_content(parameters(other_python_nodes.first).first.keys.first).split("@").last
|
|
|
|
else
|
|
|
|
string_content(parameters(python_formula_node).first).split("@").last
|
|
|
|
end
|
2020-10-10 13:38:43 -04:00
|
|
|
|
|
|
|
find_strings(body_node).each do |str|
|
2021-01-12 02:21:51 +11:00
|
|
|
content = string_content(str)
|
2020-10-10 13:38:43 -04:00
|
|
|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = content.match(/^python(@)?(\d\.\d+)$/))
|
2020-10-10 13:38:43 -04:00
|
|
|
next if python_version == match[2]
|
|
|
|
|
2021-01-12 02:21:51 +11:00
|
|
|
fix = if match[1]
|
2020-10-10 13:38:43 -04:00
|
|
|
"python@#{python_version}"
|
|
|
|
else
|
|
|
|
"python#{python_version}"
|
|
|
|
end
|
|
|
|
|
|
|
|
offending_node(str)
|
2022-06-28 10:09:59 +01:00
|
|
|
problem "References to `#{content}` should " \
|
2021-07-06 23:44:09 +05:30
|
|
|
"match the specified python dependency (`#{fix}`)" do |corrector|
|
2021-01-12 02:21:51 +11:00
|
|
|
corrector.replace(str.source_range, "\"#{fix}\"")
|
|
|
|
end
|
2020-10-10 13:38:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-08 13:18:07 +01:00
|
|
|
# This cop makes sure that OS conditionals are consistent.
|
2022-06-29 20:00:10 -04:00
|
|
|
class OnSystemConditionals < FormulaCop
|
2022-08-14 15:39:20 -04:00
|
|
|
include OnSystemConditionalsHelper
|
2021-09-08 13:18:07 +01:00
|
|
|
extend AutoCorrector
|
|
|
|
|
2022-06-29 20:00:10 -04:00
|
|
|
NO_ON_SYSTEM_METHOD_NAMES = [:install, :post_install].freeze
|
|
|
|
NO_ON_SYSTEM_BLOCK_NAMES = [:service, :test].freeze
|
2021-09-08 13:18:07 +01:00
|
|
|
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
body_node = formula_nodes.body_node
|
|
|
|
|
2022-06-29 20:00:10 -04:00
|
|
|
NO_ON_SYSTEM_METHOD_NAMES.each do |formula_method_name|
|
|
|
|
method_node = find_method_def(body_node, formula_method_name)
|
2022-08-14 15:39:20 -04:00
|
|
|
audit_on_system_blocks(method_node, formula_method_name) if method_node
|
2022-06-29 20:00:10 -04:00
|
|
|
end
|
|
|
|
NO_ON_SYSTEM_BLOCK_NAMES.each do |formula_block_name|
|
|
|
|
block_node = find_block(body_node, formula_block_name)
|
2022-08-14 15:39:20 -04:00
|
|
|
audit_on_system_blocks(block_node, formula_block_name) if block_node
|
2022-07-14 23:32:25 +02:00
|
|
|
end
|
2021-09-08 13:18:07 +01:00
|
|
|
|
2022-07-14 23:32:25 +02:00
|
|
|
# Don't restrict OS.mac? or OS.linux? usage in taps; they don't care
|
|
|
|
# as much as we do about e.g. formulae.brew.sh generation, often use
|
|
|
|
# platform-specific URLs and we don't want to add DSLs to support
|
|
|
|
# that case.
|
|
|
|
return if formula_tap != "homebrew-core"
|
|
|
|
|
2022-08-14 15:39:20 -04:00
|
|
|
audit_arch_conditionals(body_node,
|
|
|
|
allowed_methods: NO_ON_SYSTEM_METHOD_NAMES,
|
2023-03-29 00:40:46 +01:00
|
|
|
allowed_blocks: NO_ON_SYSTEM_BLOCK_NAMES)
|
2021-09-15 12:23:07 +01:00
|
|
|
|
2022-08-14 15:39:20 -04:00
|
|
|
audit_base_os_conditionals(body_node,
|
|
|
|
allowed_methods: NO_ON_SYSTEM_METHOD_NAMES,
|
2023-03-29 00:40:46 +01:00
|
|
|
allowed_blocks: NO_ON_SYSTEM_BLOCK_NAMES)
|
2022-06-29 20:00:10 -04:00
|
|
|
|
2022-08-14 15:39:20 -04:00
|
|
|
audit_macos_version_conditionals(body_node,
|
|
|
|
allowed_methods: NO_ON_SYSTEM_METHOD_NAMES,
|
|
|
|
allowed_blocks: NO_ON_SYSTEM_BLOCK_NAMES,
|
|
|
|
recommend_on_system: true)
|
2021-09-08 13:18:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-22 20:22:02 -05:00
|
|
|
# This cop makes sure the `MacOS` module is not used in Linux-facing formula code
|
|
|
|
class MacOSOnLinux < FormulaCop
|
|
|
|
include OnSystemConditionalsHelper
|
|
|
|
|
|
|
|
ON_MACOS_BLOCKS = [:macos, *MACOS_VERSION_OPTIONS].map { |os| :"on_#{os}" }.freeze
|
|
|
|
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
audit_macos_references(formula_nodes.body_node,
|
2023-11-22 20:22:02 -05:00
|
|
|
allowed_methods: OnSystemConditionals::NO_ON_SYSTEM_METHOD_NAMES,
|
|
|
|
allowed_blocks: OnSystemConditionals::NO_ON_SYSTEM_BLOCK_NAMES + ON_MACOS_BLOCKS)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-24 23:19:00 +02:00
|
|
|
# This cop makes sure that the `generate_completions_from_executable` DSL is used.
|
2022-07-13 14:45:41 +02:00
|
|
|
class GenerateCompletionsDSL < FormulaCop
|
|
|
|
extend AutoCorrector
|
|
|
|
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
install = find_method_def(formula_nodes.body_node, :install)
|
2022-09-06 01:03:04 +02:00
|
|
|
return if install.blank?
|
2022-07-13 14:45:41 +02:00
|
|
|
|
2023-02-21 00:25:02 +00:00
|
|
|
correctable_shell_completion_node(install) do |node, shell, base_name, executable, subcmd, shell_parameter|
|
2022-07-24 23:19:00 +02:00
|
|
|
# generate_completions_from_executable only applicable if shell is passed
|
|
|
|
next unless shell_parameter.match?(/(bash|zsh|fish)/)
|
2022-07-13 14:45:41 +02:00
|
|
|
|
|
|
|
base_name = base_name.delete_prefix("_").delete_suffix(".fish")
|
2022-07-24 23:19:49 +02:00
|
|
|
shell = shell.to_s.delete_suffix("_completion").to_sym
|
2022-08-10 17:44:52 +02:00
|
|
|
shell_parameter_stripped = shell_parameter
|
|
|
|
.delete_suffix("bash")
|
|
|
|
.delete_suffix("zsh")
|
|
|
|
.delete_suffix("fish")
|
2022-07-24 23:19:00 +02:00
|
|
|
shell_parameter_format = if shell_parameter_stripped.empty?
|
2022-07-13 14:45:41 +02:00
|
|
|
nil
|
|
|
|
elsif shell_parameter_stripped == "--"
|
2022-07-31 21:16:41 +02:00
|
|
|
:flag
|
2022-07-24 23:13:14 +02:00
|
|
|
elsif shell_parameter_stripped == "--shell="
|
2022-07-31 21:16:41 +02:00
|
|
|
:arg
|
2022-07-13 14:45:41 +02:00
|
|
|
else
|
2022-09-05 19:46:32 +02:00
|
|
|
shell_parameter_stripped
|
2022-07-13 14:45:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
replacement_args = %w[]
|
2022-08-10 21:20:43 +02:00
|
|
|
replacement_args << executable.source
|
|
|
|
replacement_args << subcmd.source
|
2023-04-18 15:06:50 -07:00
|
|
|
replacement_args << "base_name: \"#{base_name}\"" if base_name != @formula_name
|
2022-07-13 14:45:41 +02:00
|
|
|
replacement_args << "shells: [:#{shell}]"
|
2022-08-10 17:44:52 +02:00
|
|
|
unless shell_parameter_format.nil?
|
|
|
|
replacement_args << "shell_parameter_format: #{shell_parameter_format.inspect}"
|
|
|
|
end
|
2022-07-13 14:45:41 +02:00
|
|
|
|
|
|
|
offending_node(node)
|
2022-07-31 21:08:42 +02:00
|
|
|
replacement = "generate_completions_from_executable(#{replacement_args.join(", ")})"
|
2022-07-24 23:19:49 +02:00
|
|
|
|
2022-07-13 14:45:41 +02:00
|
|
|
problem "Use `#{replacement}` instead of `#{@offensive_node.source}`." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, replacement)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shell_completion_node(install) do |node|
|
2022-08-11 17:45:38 +02:00
|
|
|
next if node.source.include?("<<~") # skip heredoc completion scripts
|
|
|
|
next if node.source.match?(/{.*=>.*}/) # skip commands needing custom ENV variables
|
|
|
|
|
2022-07-13 14:45:41 +02:00
|
|
|
offending_node(node)
|
2022-07-24 23:19:00 +02:00
|
|
|
problem "Use `generate_completions_from_executable` DSL instead of `#{@offensive_node.source}`."
|
2022-07-13 14:45:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-10 17:44:52 +02:00
|
|
|
# match ({bash,zsh,fish}_completion/"_?foo{.fish}?").write Utils.safe_popen_read(foo, subcmd, shell_parameter)
|
2022-07-13 14:45:41 +02:00
|
|
|
def_node_search :correctable_shell_completion_node, <<~EOS
|
|
|
|
$(send
|
|
|
|
(begin
|
|
|
|
(send
|
|
|
|
(send nil? ${:bash_completion :zsh_completion :fish_completion}) :/
|
|
|
|
(str $_))) :write
|
|
|
|
(send
|
|
|
|
(const nil? :Utils) :safe_popen_read
|
|
|
|
$(send
|
|
|
|
(send nil? :bin) :/
|
|
|
|
(str _))
|
2022-08-10 21:20:43 +02:00
|
|
|
$(str _)
|
2022-07-13 14:45:41 +02:00
|
|
|
(str $_)))
|
|
|
|
EOS
|
|
|
|
|
|
|
|
# matches ({bash,zsh,fish}_completion/"_?foo{.fish}?").write output
|
|
|
|
def_node_search :shell_completion_node, <<~EOS
|
2022-07-24 23:19:49 +02:00
|
|
|
$(send
|
|
|
|
(begin
|
|
|
|
(send
|
|
|
|
(send nil? {:bash_completion :zsh_completion :fish_completion}) :/
|
|
|
|
(str _))) :write _)
|
2022-07-13 14:45:41 +02:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2022-07-27 17:01:21 +02:00
|
|
|
# This cop makes sure that the `generate_completions_from_executable` DSL is used with only
|
|
|
|
# a single, combined call for all shells.
|
|
|
|
class SingleGenerateCompletionsDSLCall < FormulaCop
|
|
|
|
extend AutoCorrector
|
|
|
|
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
install = find_method_def(formula_nodes.body_node, :install)
|
2022-09-06 01:03:04 +02:00
|
|
|
return if install.blank?
|
2022-07-27 17:01:21 +02:00
|
|
|
|
|
|
|
methods = find_every_method_call_by_name(install, :generate_completions_from_executable)
|
|
|
|
return if methods.length <= 1
|
|
|
|
|
|
|
|
offenses = []
|
|
|
|
shells = []
|
|
|
|
methods.each do |method|
|
|
|
|
next unless method.source.include?("shells:")
|
|
|
|
|
|
|
|
shells << method.source.match(/shells: \[(:bash|:zsh|:fish)\]/).captures.first
|
|
|
|
offenses << method
|
|
|
|
end
|
|
|
|
|
|
|
|
return if offenses.blank?
|
|
|
|
|
2022-08-11 16:56:04 +02:00
|
|
|
T.must(offenses[0...-1]).each_with_index do |node, i|
|
2022-08-18 20:55:51 +02:00
|
|
|
# commands have to be the same to be combined
|
|
|
|
# send_type? matches `bin/"foo"`, str_type? matches remaining command parts,
|
|
|
|
# the rest are kwargs we need to filter out
|
|
|
|
method_commands = node.arguments.filter { |arg| arg.send_type? || arg.str_type? }
|
|
|
|
next_method_commands = offenses[i + 1].arguments.filter { |arg| arg.send_type? || arg.str_type? }
|
2023-04-18 15:06:50 -07:00
|
|
|
if method_commands != next_method_commands
|
2022-08-11 16:56:04 +02:00
|
|
|
shells.delete_at(i)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2022-07-27 17:01:21 +02:00
|
|
|
offending_node(node)
|
2022-08-10 22:20:33 +02:00
|
|
|
problem "Use a single `generate_completions_from_executable` " \
|
|
|
|
"call combining all specified shells." do |corrector|
|
|
|
|
# adjust range by -4 and +1 to also include & remove leading spaces and trailing \n
|
|
|
|
corrector.replace(@offensive_node.source_range.adjust(begin_pos: -4, end_pos: 1), "")
|
2022-07-27 17:01:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-11 16:56:04 +02:00
|
|
|
return if shells.length <= 1 # no shells to combine left
|
|
|
|
|
2022-07-27 17:01:21 +02:00
|
|
|
offending_node(offenses.last)
|
2022-07-31 21:07:38 +02:00
|
|
|
replacement = if (%w[:bash :zsh :fish] - shells).empty?
|
2022-07-27 17:01:21 +02:00
|
|
|
@offensive_node.source.sub(/shells: \[(:bash|:zsh|:fish)\]/, "")
|
2022-07-31 21:29:50 +02:00
|
|
|
.sub(", )", ")") # clean up dangling trailing comma
|
|
|
|
.sub("(, ", "(") # clean up dangling leading comma
|
2022-08-10 17:49:18 +02:00
|
|
|
.sub(", , ", ", ") # clean up dangling enclosed comma
|
2022-07-27 17:01:21 +02:00
|
|
|
else
|
|
|
|
@offensive_node.source.sub(/shells: \[(:bash|:zsh|:fish)\]/,
|
|
|
|
"shells: [#{shells.join(", ")}]")
|
|
|
|
end
|
|
|
|
|
|
|
|
problem "Use `#{replacement}` instead of `#{@offensive_node.source}`." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, replacement)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop checks for other miscellaneous style violations.
|
2017-07-31 14:30:37 +05:30
|
|
|
class Miscellaneous < FormulaCop
|
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
|
|
|
|
2017-07-31 14:30:37 +05:30
|
|
|
# FileUtils is included in Formula
|
|
|
|
# encfs modifies a file with this name, so check for some leading characters
|
|
|
|
find_instance_method_call(body_node, "FileUtils", nil) do |method_node|
|
|
|
|
problem "Don't need 'FileUtils.' before #{method_node.method_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check for long inreplace block vars
|
|
|
|
find_all_blocks(body_node, :inreplace) do |node|
|
|
|
|
block_arg = node.arguments.children.first
|
2023-04-18 15:06:50 -07:00
|
|
|
next if block_arg.source.size <= 1
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-31 14:30:37 +05:30
|
|
|
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"."
|
|
|
|
end
|
2017-08-04 01:18:00 +05:30
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
[:rebuild, :version_scheme].each do |method_name|
|
|
|
|
find_method_with_args(body_node, method_name, 0) do
|
|
|
|
problem "'#{method_name} 0' should be removed"
|
2017-08-04 01:18:00 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-06 16:17:38 +02:00
|
|
|
find_instance_call(body_node, "ARGV") do |_method_node|
|
2017-12-02 17:03:11 +05:30
|
|
|
problem "Use build instead of ARGV to check options"
|
2017-08-14 15:22:44 +05:30
|
|
|
end
|
2017-08-14 15:41:03 +05:30
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_instance_method_call(body_node, :man, :+) do |method|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(parameters(method).first, /^man[1-8]$/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
problem "\"#{method.source}\" should be \"#{match[0]}\""
|
2017-08-14 15:41:03 +05:30
|
|
|
end
|
|
|
|
|
2017-08-14 19:58:39 +05:30
|
|
|
# Avoid hard-coding compilers
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :system).each do |method|
|
2023-01-24 14:04:36 +08:00
|
|
|
next if @formula_name == "bazel" # TODO: Remove shim bypass in bazel.
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
param = parameters(method).first
|
2023-01-24 13:26:47 +08:00
|
|
|
if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|clang|cc|c[89]9)(\s|$)}))
|
2017-08-14 19:58:39 +05:30
|
|
|
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
|
2023-01-24 13:26:47 +08:00
|
|
|
elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|clang|c)\+\+)(\s|$)}))
|
2017-08-14 19:58:39 +05:30
|
|
|
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
|
|
|
|
end
|
|
|
|
end
|
2017-08-14 20:10:45 +05:30
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_instance_method_call(body_node, "ENV", :[]=) do |method|
|
2023-01-24 14:04:36 +08:00
|
|
|
next if @formula_name == "bazel" # TODO: Remove shim bypass in bazel.
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
param = parameters(method)[1]
|
2023-01-24 13:26:47 +08:00
|
|
|
if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|clang|cc|c[89]9)(\s|$)}))
|
2017-08-14 20:10:45 +05:30
|
|
|
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
|
2023-01-24 13:26:47 +08:00
|
|
|
elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|clang|c)\+\+)(\s|$)}))
|
2017-08-14 20:10:45 +05:30
|
|
|
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-14 21:34:01 +05:30
|
|
|
# Prefer formula path shortcuts in strings
|
|
|
|
formula_path_strings(body_node, :share) do |p|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(p, %r{^(/(man))/?}))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-21 12:50:49 +05:30
|
|
|
problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\""
|
2017-08-14 21:34:01 +05:30
|
|
|
end
|
|
|
|
|
2017-08-14 22:44:28 +05:30
|
|
|
formula_path_strings(body_node, :prefix) do |p|
|
2021-02-12 18:33:37 +05:30
|
|
|
if (match = regex_match_group(p, %r{^(/share/(info|man))$}))
|
2022-12-13 10:54:15 +00:00
|
|
|
problem ['"#', "{prefix}", match[1], '" should be "#{', match[2], '}"'].join
|
2017-08-14 21:34:01 +05:30
|
|
|
end
|
2021-02-12 18:33:37 +05:30
|
|
|
if (match = regex_match_group(p, %r{^((/share/man/)(man[1-8]))}))
|
2022-12-13 10:54:15 +00:00
|
|
|
problem ['"#', "{prefix}", match[1], '" should be "#{', match[3], '}"'].join
|
2017-08-14 21:34:01 +05:30
|
|
|
end
|
2021-02-12 18:33:37 +05:30
|
|
|
if (match = regex_match_group(p, %r{^(/(bin|include|libexec|lib|sbin|share|Frameworks))}i))
|
2022-12-13 10:54:15 +00:00
|
|
|
problem ['"#', "{prefix}", match[1], '" should be "#{', match[2].downcase, '}"'].join
|
2017-08-14 21:34:01 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-14 23:32:06 +05:30
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :depends_on).each do |method|
|
|
|
|
key, value = destructure_hash(parameters(method).first)
|
2017-10-21 01:39:04 +05:30
|
|
|
next if key.nil? || value.nil?
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(value, /^(lua|perl|python|ruby)(\d*)/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2020-12-03 14:30:34 -05:00
|
|
|
problem "#{match[1]} modules should be vendored rather than use deprecated `#{method.source}`"
|
2017-08-14 23:05:00 +05:30
|
|
|
end
|
2017-08-14 21:34:01 +05:30
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :system).each do |method|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(parameters(method).first, /^(env|export)(\s+)?/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-14 23:32:06 +05:30
|
|
|
problem "Use ENV instead of invoking '#{match[1]}' to modify the environment"
|
|
|
|
end
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :depends_on).each do |method|
|
|
|
|
param = parameters(method).first
|
2018-05-05 21:11:16 +05:30
|
|
|
dep, option_child_nodes = hash_dep(param)
|
|
|
|
next if dep.nil? || option_child_nodes.empty?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-05-05 21:11:16 +05:30
|
|
|
option_child_nodes.each do |option|
|
|
|
|
find_strings(option).each do |dependency|
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(dependency, /(with(out)?-\w+|c\+\+11)/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-05-05 21:11:16 +05:30
|
|
|
problem "Dependency #{string_content(dep)} should not use option #{match[0]}"
|
|
|
|
end
|
|
|
|
end
|
2017-08-15 00:05:50 +05:30
|
|
|
end
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_instance_method_call(body_node, :version, :==) do |method|
|
2023-02-14 19:19:37 -08:00
|
|
|
next unless parameters_passed?(method, ["HEAD"])
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-15 00:29:58 +05:30
|
|
|
problem "Use 'build.head?' instead of inspecting 'version'"
|
|
|
|
end
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_instance_method_call(body_node, "ARGV", :include?) do |method|
|
2023-02-14 19:19:37 -08:00
|
|
|
next unless parameters_passed?(method, ["--HEAD"])
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2021-01-01 10:16:31 -08:00
|
|
|
problem "Use \"if build.head?\" instead"
|
2017-08-15 00:36:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-15 00:42:56 +05:30
|
|
|
find_const(body_node, "MACOS_VERSION") do
|
|
|
|
problem "Use MacOS.version instead of MACOS_VERSION"
|
|
|
|
end
|
|
|
|
|
|
|
|
find_const(body_node, "MACOS_FULL_VERSION") do
|
|
|
|
problem "Use MacOS.full_version instead of MACOS_FULL_VERSION"
|
|
|
|
end
|
|
|
|
|
2017-08-15 16:09:32 +05:30
|
|
|
conditional_dependencies(body_node) do |node, method, param, dep_node|
|
|
|
|
dep = string_content(dep_node)
|
|
|
|
if node.if?
|
2017-10-29 23:40:04 +05:30
|
|
|
if (method == :include? && regex_match_group(param, /^with-#{dep}$/)) ||
|
|
|
|
(method == :with? && regex_match_group(param, /^#{dep}$/))
|
2017-08-15 16:09:32 +05:30
|
|
|
offending_node(dep_node.parent)
|
|
|
|
problem "Replace #{node.source} with #{dep_node.parent.source} => :optional"
|
|
|
|
end
|
|
|
|
elsif node.unless?
|
2017-10-29 23:40:04 +05:30
|
|
|
if (method == :include? && regex_match_group(param, /^without-#{dep}$/)) ||
|
|
|
|
(method == :without? && regex_match_group(param, /^#{dep}$/))
|
2017-08-15 16:09:32 +05:30
|
|
|
offending_node(dep_node.parent)
|
|
|
|
problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-10 19:57:53 +05:30
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
find_method_with_args(body_node, :fails_with, :llvm) do
|
|
|
|
problem "'fails_with :llvm' is now a no-op so should be removed"
|
|
|
|
end
|
|
|
|
|
2017-12-30 19:15:52 +00:00
|
|
|
find_method_with_args(body_node, :needs, :openmp) do
|
|
|
|
problem "'needs :openmp' should be replaced with 'depends_on \"gcc\"'"
|
|
|
|
end
|
|
|
|
|
2017-10-29 23:40:04 +05:30
|
|
|
find_method_with_args(body_node, :system, /^(otool|install_name_tool|lipo)/) do
|
2017-08-04 01:18:00 +05:30
|
|
|
problem "Use ruby-macho instead of calling #{@offensive_node.source}"
|
|
|
|
end
|
2017-08-04 01:18:00 +05:30
|
|
|
|
2019-02-19 13:11:32 +00:00
|
|
|
problem "Use new-style test definitions (test do)" if find_method_def(body_node, :test)
|
2017-08-04 01:18:00 +05:30
|
|
|
|
|
|
|
find_method_with_args(body_node, :skip_clean, :all) do
|
2017-10-21 03:12:50 +02:00
|
|
|
problem "`skip_clean :all` is deprecated; brew no longer strips symbols. " \
|
|
|
|
"Pass explicit paths to prevent Homebrew from removing empty folders."
|
2017-08-04 01:18:00 +05:30
|
|
|
end
|
|
|
|
|
2021-01-12 02:19:31 +11:00
|
|
|
if find_method_def(processed_source.ast)
|
2017-10-21 12:50:49 +05:30
|
|
|
problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level"
|
|
|
|
end
|
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
find_instance_method_call(body_node, :build, :universal?) do
|
2017-08-30 15:48:41 +05:30
|
|
|
next if @formula_name == "wine"
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-30 15:48:41 +05:30
|
|
|
problem "macOS has been 64-bit only since 10.6 so build.universal? is deprecated."
|
2017-08-04 01:18:00 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
find_instance_method_call(body_node, "ENV", :universal_binary) do
|
2017-10-08 21:33:23 -04:00
|
|
|
next if @formula_name == "wine"
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
problem "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated."
|
|
|
|
end
|
|
|
|
|
2021-07-01 18:38:54 +01:00
|
|
|
find_instance_method_call(body_node, "ENV", :runtime_cpu_detection) do
|
|
|
|
next if tap_style_exception? :runtime_cpu_detection_allowlist
|
|
|
|
|
|
|
|
problem "Formulae should be verified as having support for runtime hardware detection before " \
|
|
|
|
"using ENV.runtime_cpu_detection."
|
|
|
|
end
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :depends_on).each do |method|
|
|
|
|
next unless method_called?(method, :new)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-07 14:08:22 +05:30
|
|
|
problem "`depends_on` can take requirement classes instead of instances"
|
|
|
|
end
|
2017-08-13 14:50:29 +05:30
|
|
|
|
2021-02-12 11:25:44 +00:00
|
|
|
find_instance_method_call(body_node, "ENV", :[]) do |method|
|
|
|
|
next unless modifier?(method.parent)
|
|
|
|
|
|
|
|
param = parameters(method).first
|
|
|
|
next unless node_equals?(param, "CI")
|
|
|
|
|
|
|
|
problem 'Don\'t use ENV["CI"] for Homebrew CI checks.'
|
|
|
|
end
|
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
find_instance_method_call(body_node, "Dir", :[]) do |method|
|
2023-04-18 15:06:50 -07:00
|
|
|
next if parameters(method).size != 1
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
path = parameters(method).first
|
2017-10-21 01:39:04 +05:30
|
|
|
next unless path.str_type?
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(path, /^[^*{},]+$/))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-14 00:02:44 +05:30
|
|
|
problem "Dir([\"#{string_content(path)}\"]) is unnecessary; just use \"#{match[0]}\""
|
|
|
|
end
|
|
|
|
|
2018-09-02 16:15:09 +01:00
|
|
|
fileutils_methods = Regexp.new(
|
|
|
|
FileUtils.singleton_methods(false)
|
2020-08-19 17:12:32 +01:00
|
|
|
.map { |m| "(?-mix:^#{Regexp.escape(m)}$)" }
|
2018-09-02 16:15:09 +01:00
|
|
|
.join("|"),
|
|
|
|
)
|
2017-10-12 00:29:19 +05:30
|
|
|
find_every_method_call_by_name(body_node, :system).each do |method|
|
|
|
|
param = parameters(method).first
|
2021-02-12 18:33:37 +05:30
|
|
|
next unless (match = regex_match_group(param, fileutils_methods))
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-12 00:29:19 +05:30
|
|
|
problem "Use the `#{match}` Ruby method instead of `#{method.source}`"
|
2017-08-14 01:09:06 +05:30
|
|
|
end
|
2017-07-31 14:30:37 +05:30
|
|
|
end
|
2017-08-04 01:18:00 +05:30
|
|
|
|
2017-08-10 19:57:53 +05:30
|
|
|
def modifier?(node)
|
2017-08-15 00:05:50 +05:30
|
|
|
return false unless node.if_type?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-10 19:57:53 +05:30
|
|
|
node.modifier_form?
|
|
|
|
end
|
|
|
|
|
2017-10-21 13:26:25 +05:30
|
|
|
def_node_search :conditional_dependencies, <<~EOS
|
2017-10-21 03:12:50 +02:00
|
|
|
{$(if (send (send nil? :build) ${:include? :with? :without?} $(str _))
|
|
|
|
(send nil? :depends_on $({str sym} _)) nil?)
|
2017-08-10 19:57:53 +05:30
|
|
|
|
2017-10-21 03:12:50 +02:00
|
|
|
$(if (send (send nil? :build) ${:include? :with? :without?} $(str _)) nil?
|
|
|
|
(send nil? :depends_on $({str sym} _)))}
|
2017-08-10 19:57:53 +05:30
|
|
|
EOS
|
|
|
|
|
2017-10-21 13:26:25 +05:30
|
|
|
def_node_matcher :hash_dep, <<~EOS
|
2018-05-05 21:11:16 +05:30
|
|
|
(hash (pair $(str _) $...))
|
2017-08-10 19:57:53 +05:30
|
|
|
EOS
|
|
|
|
|
2017-10-21 13:26:25 +05:30
|
|
|
def_node_matcher :destructure_hash, <<~EOS
|
2017-08-14 23:05:00 +05:30
|
|
|
(hash (pair $(str _) $(sym _)))
|
2017-08-12 20:50:43 +05:30
|
|
|
EOS
|
|
|
|
|
2017-10-21 13:26:25 +05:30
|
|
|
def_node_search :formula_path_strings, <<~EOS
|
2017-10-21 03:12:50 +02:00
|
|
|
{(dstr (begin (send nil? %1)) $(str _ ))
|
|
|
|
(dstr _ (begin (send nil? %1)) $(str _ ))}
|
2017-08-12 20:50:43 +05:30
|
|
|
EOS
|
2017-07-31 14:30:37 +05:30
|
|
|
end
|
2017-07-29 16:36:32 +05:30
|
|
|
end
|
2020-04-13 14:35:38 +01:00
|
|
|
|
|
|
|
module FormulaAuditStrict
|
2020-08-26 02:50:19 +02:00
|
|
|
# This cop makes sure that no build-time checks are performed.
|
2020-04-13 14:35:38 +01:00
|
|
|
class MakeCheck < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
2020-04-13 14:35:38 +01:00
|
|
|
return if formula_tap != "homebrew-core"
|
|
|
|
|
|
|
|
# Avoid build-time checks in homebrew/core
|
2024-07-07 15:18:29 -04:00
|
|
|
find_every_method_call_by_name(formula_nodes.body_node, :system).each do |method|
|
2020-04-13 14:35:38 +01:00
|
|
|
next if @formula_name.start_with?("lib")
|
2020-11-27 01:31:14 -05:00
|
|
|
next if tap_style_exception? :make_check_allowlist
|
2020-04-13 14:35:38 +01:00
|
|
|
|
|
|
|
params = parameters(method)
|
|
|
|
next unless node_equals?(params[0], "make")
|
|
|
|
|
|
|
|
params[1..].each do |arg|
|
|
|
|
next unless regex_match_group(arg, /^(checks?|tests?)$/)
|
|
|
|
|
|
|
|
offending_node(method)
|
|
|
|
problem "Formulae in homebrew/core (except e.g. cryptography, libraries) " \
|
|
|
|
"should not run build-time checks"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-05 13:57:49 -04:00
|
|
|
|
2021-01-25 09:18:10 +00:00
|
|
|
# This cop ensures that new formulae depending on removed Requirements are not used
|
|
|
|
class Requirements < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(_formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(_formula_nodes)
|
2021-01-25 09:18:10 +00:00
|
|
|
problem "Formulae should depend on a versioned `openjdk` instead of :java" if depends_on? :java
|
|
|
|
problem "Formulae should depend on specific X libraries instead of :x11" if depends_on? :x11
|
|
|
|
problem "Formulae should not depend on :osxfuse" if depends_on? :osxfuse
|
|
|
|
problem "Formulae should not depend on :tuntap" if depends_on? :tuntap
|
2020-11-06 00:20:34 +11:00
|
|
|
end
|
|
|
|
end
|
2023-07-12 10:47:35 +08:00
|
|
|
|
2024-07-25 19:26:44 -07:00
|
|
|
# This cop makes sure that formulae build with `rust` instead of `rustup`.
|
2023-07-12 10:47:35 +08:00
|
|
|
class RustCheck < FormulaCop
|
|
|
|
extend AutoCorrector
|
|
|
|
|
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?
|
2023-07-12 10:47:35 +08:00
|
|
|
|
|
|
|
# Enforce use of `rust` for rust dependency in core
|
|
|
|
return if formula_tap != "homebrew-core"
|
|
|
|
|
2024-07-25 19:26:44 -07:00
|
|
|
find_method_with_args(body_node, :depends_on, "rustup") do
|
2023-07-12 10:47:35 +08:00
|
|
|
problem "Formulae in homebrew/core should use 'depends_on \"rust\"' " \
|
|
|
|
"instead of '#{@offensive_node.source}'." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, "depends_on \"rust\"")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Enforce order of dependency types so we don't need to check for
|
2024-07-25 19:26:44 -07:00
|
|
|
# depends_on "rustup" => [:test, :build]
|
2023-07-12 10:47:35 +08:00
|
|
|
[:build, [:build, :test], [:test, :build]].each do |type|
|
2024-07-25 19:26:44 -07:00
|
|
|
find_method_with_args(body_node, :depends_on, "rustup" => type) do
|
2023-07-12 10:47:35 +08:00
|
|
|
problem "Formulae in homebrew/core should use 'depends_on \"rust\" => #{type}' " \
|
|
|
|
"instead of '#{@offensive_node.source}'." do |corrector|
|
|
|
|
corrector.replace(@offensive_node.source_range, "depends_on \"rust\" => #{type}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
install_node = find_method_def(body_node, :install)
|
|
|
|
return if install_node.blank?
|
|
|
|
|
|
|
|
find_every_method_call_by_name(install_node, :system).each do |method|
|
|
|
|
param = parameters(method).first
|
|
|
|
next if param.blank?
|
2024-07-25 19:26:44 -07:00
|
|
|
# FIXME: Handle Pathname parameters (e.g. `system bin/"rustup"`).
|
|
|
|
next if regex_match_group(param, /rustup$/).blank?
|
2023-07-12 10:47:35 +08:00
|
|
|
|
2024-07-25 19:26:44 -07:00
|
|
|
problem "Formula in homebrew/core should not use `rustup` at build-time."
|
2023-07-12 10:47:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-13 14:35:38 +01:00
|
|
|
end
|
2017-07-29 16:36:32 +05:30
|
|
|
end
|
|
|
|
end
|