2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-26 19:41:14 +01:00
|
|
|
require "rubocops/extend/formula"
|
2018-09-02 23:30:07 +02:00
|
|
|
require "extend/string"
|
2017-05-24 13:04:55 +05:30
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module FormulaAudit
|
2018-10-18 21:42:43 -04:00
|
|
|
# This cop audits versioned Formulae for `conflicts_with`.
|
2017-05-24 13:04:55 +05:30
|
|
|
class Conflicts < FormulaCop
|
2017-10-21 03:12:50 +02:00
|
|
|
MSG = "Versioned formulae should not use `conflicts_with`. " \
|
2019-04-19 15:38:03 +09:00
|
|
|
"Use `keg_only :versioned_formula` instead."
|
2017-05-24 13:04:55 +05:30
|
|
|
|
2020-06-06 19:12:12 +01:00
|
|
|
ALLOWLIST = %w[
|
2020-04-11 14:19:04 +01:00
|
|
|
bash-completion@2
|
2017-05-24 13:04:55 +05:30
|
|
|
].freeze
|
|
|
|
|
2020-07-02 13:20:32 -04:00
|
|
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
|
|
|
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)
|
|
|
|
reason = string_content(reason).sub(name, "")
|
|
|
|
first_word = reason.split.first
|
|
|
|
|
|
|
|
if reason.match?(/\A[A-Z]/)
|
|
|
|
problem "'#{first_word}' from the `conflicts_with` reason should be '#{first_word.downcase}'."
|
|
|
|
end
|
|
|
|
|
|
|
|
problem "`conflicts_with` reason should not end with a period." if reason.end_with?(".")
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:04:55 +05:30
|
|
|
return unless versioned_formula?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2020-06-06 19:12:12 +01:00
|
|
|
problem MSG if !ALLOWLIST.include?(@formula_name) &&
|
2020-07-02 13:20:32 -04:00
|
|
|
method_called_ever?(body_node, :conflicts_with)
|
|
|
|
end
|
|
|
|
|
|
|
|
def autocorrect(node)
|
|
|
|
lambda do |corrector|
|
|
|
|
if versioned_formula?
|
|
|
|
corrector.replace(node.source_range, "keg_only :versioned_formula")
|
|
|
|
else
|
|
|
|
reason = string_content(node)
|
|
|
|
reason[0] = reason[0].downcase
|
|
|
|
reason = reason.delete_suffix(".")
|
|
|
|
corrector.replace(node.source_range, "\"#{reason}\"")
|
|
|
|
end
|
|
|
|
end
|
2017-05-24 13:04:55 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|