Ruoyu Zhong 254941c6f7
rubocops/service: check for more cellar paths
This is a follow-up to #15154. In addition to `bin`, the `service` block
can also reference other cellar paths like `libexec`. They don't work
with the API either, as reported in
https://github.com/orgs/Homebrew/discussions/4459.

Violations are already corrected in Homebrew/homebrew-core#129736
and Homebrew/homebrew-core#129737.

Signed-off-by: Ruoyu Zhong <zhongruoyu@outlook.com>
2023-04-30 07:39:25 +08:00

44 lines
1.3 KiB
Ruby

# typed: true
# frozen_string_literal: true
require "rubocops/extend/formula_cop"
module RuboCop
module Cop
module FormulaAudit
# This cop audits the service block.
#
# @api private
class Service < FormulaCop
extend AutoCorrector
CELLAR_PATH_AUDIT_CORRECTIONS = {
bin: :opt_bin,
libexec: :opt_libexec,
pkgshare: :opt_pkgshare,
prefix: :opt_prefix,
sbin: :opt_sbin,
share: :opt_share,
}.freeze
def audit_formula(_node, _class_node, _parent_class_node, body_node)
service_node = find_block(body_node, :service)
return if service_node.blank?
# This check ensures that cellar paths like `bin` are not referenced
# because their `opt_` variants are more portable and work with the
# API.
CELLAR_PATH_AUDIT_CORRECTIONS.each do |path, opt_path|
find_every_method_call_by_name(service_node, path).each do |node|
offending_node(node)
problem "Use `#{opt_path}` instead of `#{path}` in service blocks." do |corrector|
corrector.replace(node.source_range, opt_path)
end
end
end
end
end
end
end
end