91 lines
2.8 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# 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 makes sure that {Formula} is used as superclass.
2020-08-26 02:28:17 +02:00
#
# @api private
2023-02-20 10:22:39 -08:00
class ClassName < Base
include FormulaCop
2021-01-12 13:33:23 +11:00
extend AutoCorrector
DEPRECATED_CLASSES = %w[
GithubGistFormula
ScriptFileFormula
AmazonWebServicesFormula
].freeze
def audit_formula(_node, _class_node, parent_class_node, _body_node)
parent_class = class_name(parent_class_node)
return unless DEPRECATED_CLASSES.include?(parent_class)
2018-09-17 02:45:00 +02:00
2021-01-12 13:33:23 +11:00
problem "#{parent_class} is deprecated, use Formula instead" do |corrector|
corrector.replace(parent_class_node.source_range, "Formula")
end
end
end
2018-08-15 19:29:22 -04:00
2020-08-26 02:28:17 +02:00
# This cop makes sure that a `test` block contains a proper test.
#
# @api private
2023-02-20 10:22:39 -08:00
class Test < Base
include FormulaCop
2021-01-12 13:33:23 +11:00
extend AutoCorrector
2018-08-15 19:29:22 -04:00
def audit_formula(_node, _class_node, _parent_class_node, body_node)
test = find_block(body_node, :test)
return unless test
if test.body.nil?
problem "`test do` should not be empty"
return
end
problem "`test do` should contain a real test" if test.body.single_line? && test.body.source.to_s == "true"
test_calls(test) do |node, params|
2018-08-15 19:29:22 -04:00
p1, p2 = params
if (match = string_content(p1).match(%r{(/usr/local/(s?bin))}))
2018-08-15 19:29:22 -04:00
offending_node(p1)
2021-01-12 13:33:23 +11:00
problem "use \#{#{match[2]}} instead of #{match[1]} in #{node}" do |corrector|
corrector.replace(p1.source_range, p1.source.sub(match[1], "\#{#{match[2]}}"))
end
2018-08-15 19:29:22 -04:00
end
if node == :shell_output && node_equals?(p2, 0)
offending_node(p2)
2021-01-12 13:33:23 +11:00
problem "Passing 0 to shell_output() is redundant" do |corrector|
corrector.remove(range_with_surrounding_comma(range_with_surrounding_space(range: p2.source_range,
side: :left)))
end
end
end
end
2018-08-15 19:29:22 -04:00
def_node_search :test_calls, <<~EOS
(send nil? ${:system :shell_output :pipe_output} $...)
EOS
end
end
module FormulaAuditStrict
2020-08-26 02:28:17 +02:00
# This cop makes sure that a `test` block exists.
#
# @api private
2023-02-20 10:22:39 -08:00
class TestPresent < Base
include FormulaCop
def audit_formula(_node, class_node, _parent_class_node, body_node)
return if find_block(body_node, :test)
2018-09-17 02:45:00 +02:00
offending_node(class_node) if body_node.nil?
problem "A `test do` test block should be added"
end
end
end
end
end