brew/Library/Homebrew/rubocops/conflicts.rb

55 lines
2.0 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
require "rubocops/extend/formula"
module RuboCop
module Cop
module FormulaAudit
# This cop audits versioned formulae for `conflicts_with`.
class Conflicts < FormulaCop
2021-01-12 16:15:52 +11:00
extend AutoCorrector
2017-10-21 03:12:50 +02:00
MSG = "Versioned formulae should not use `conflicts_with`. " \
"Use `keg_only :versioned_formula` instead."
def audit_formula(_node, _class_node, _parent_class_node, body_node)
return if body_node.nil?
find_method_calls_by_name(body_node, :conflicts_with).each do |conflicts_with_call|
next unless parameters(conflicts_with_call).last.respond_to? :values
reason = parameters(conflicts_with_call).last.values.first
offending_node(reason)
name = Regexp.new(@formula_name, Regexp::IGNORECASE)
2021-01-12 16:15:52 +11:00
reason_text = string_content(reason).sub(name, "")
first_word = reason_text.split.first
if reason_text.match?(/\A[A-Z]/)
2022-06-28 10:09:59 +01:00
problem "'#{first_word}' from the `conflicts_with` reason " \
2021-01-12 16:15:52 +11:00
"should be '#{first_word.downcase}'." do |corrector|
reason_text[0] = reason_text[0].downcase
corrector.replace(reason.source_range, "\"#{reason_text}\"")
end
end
2021-01-12 16:15:52 +11:00
next unless reason_text.end_with?(".")
2021-01-12 16:15:52 +11:00
problem "`conflicts_with` reason should not end with a period." do |corrector|
corrector.replace(reason.source_range, "\"#{reason_text.chop}\"")
end
end
return unless versioned_formula?
2018-09-17 02:45:00 +02:00
2021-01-12 16:15:52 +11:00
if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) && method_called_ever?(body_node,
:conflicts_with)
problem MSG do |corrector|
corrector.replace(@offensive_node.source_range, "keg_only :versioned_formula")
end
end
end
end
end
end
end