Merge pull request #14649 from dduugg/type-rubocops

Enable typing in rubocops
This commit is contained in:
Mike McQuaid 2023-02-17 00:17:44 +00:00 committed by GitHub
commit e35803596c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 95 additions and 44 deletions

View File

@ -47,6 +47,11 @@ Cask/StanzaOrder:
Description: "Ensure that cask stanzas are sorted correctly. More info at https://docs.brew.sh/Cask-Cookbook#stanza-order" Description: "Ensure that cask stanzas are sorted correctly. More info at https://docs.brew.sh/Cask-Cookbook#stanza-order"
Enabled: true Enabled: true
# This was intended to be an abstract Cop class for Homebrew formulae, but rubocop doesn't know that.
# TODO: refactor this as a module for formulae Cop classes to include instead
FormulaCop:
Enabled: false
# enable all formulae audits # enable all formulae audits
FormulaAudit: FormulaAudit:
Enabled: true Enabled: true

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -19,10 +19,12 @@ module RuboCop
def stanza? def stanza?
return true if arch_variable? return true if arch_variable?
return false if !send_type? && !block_type?
return true if ON_SYSTEM_METHODS.include?(method_name)
STANZA_ORDER.include?(method_name) case self
when RuboCop::AST::BlockNode, RuboCop::AST::SendNode
ON_SYSTEM_METHODS.include?(method_name) || STANZA_ORDER.include?(method_name)
else false
end
end end
def heredoc? def heredoc?

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -6,6 +6,13 @@ module RuboCop
module Cask module Cask
# Common functionality for cops checking casks. # Common functionality for cops checking casks.
module CaskHelp module CaskHelp
extend T::Helpers
extend T::Sig
abstract!
sig { abstract.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block); end
def on_block(block_node) def on_block(block_node)
super if defined? super super if defined? super
return unless respond_to?(:on_cask) return unless respond_to?(:on_cask)

View File

@ -0,0 +1,5 @@
# typed: strict
module RuboCop::Cop::Cask::CaskHelp
requires_ancestor { RuboCop::Cop::Base }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop

View File

@ -0,0 +1,5 @@
# typed: strict
module RuboCop::Cop::Cask::OnDescStanza
requires_ancestor { RuboCop::Cop::Cask::Desc }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop

View File

@ -0,0 +1,5 @@
# typed: strict
module RuboCop::Cop::Cask::OnHomepageStanza
requires_ancestor { RuboCop::Cop::Cask::HomepageUrlTrailingSlash }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop

View File

@ -0,0 +1,5 @@
# typed: strict
module RuboCop::Cop::Cask::OnUrlStanza
requires_ancestor { RuboCop::Cop::Cask::UrlLegacyCommaSeparators }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "ast_constants" require "ast_constants"
@ -58,7 +58,7 @@ module RuboCop
@offensive_node = resource_block @offensive_node = resource_block
on_system_bodies = [] on_system_bodies = T.let([], T::Array[[RuboCop::AST::BlockNode, RuboCop::AST::Node]])
on_system_blocks.each_value do |blocks| on_system_blocks.each_value do |blocks|
blocks.each do |on_system_block| blocks.each do |on_system_block|
@ -68,7 +68,7 @@ module RuboCop
end end
end end
message = nil message = T.let(nil, T.nilable(String))
allowed_methods = [ allowed_methods = [
[:url, :sha256], [:url, :sha256],
[:url, :mirror, :sha256], [:url, :mirror, :sha256],
@ -97,7 +97,7 @@ module RuboCop
break break
end end
if message.present? if message
problem message problem message
next next
end end
@ -199,7 +199,7 @@ module RuboCop
end end
# Check if each present_components is above rest of the present_components # Check if each present_components is above rest of the present_components
offensive_nodes = nil offensive_nodes = T.let(nil, T.nilable(T::Array[RuboCop::AST::Node]))
present_components.take(present_components.size - 1).each_with_index do |preceding_component, p_idx| present_components.take(present_components.size - 1).each_with_index do |preceding_component, p_idx|
next if preceding_component.empty? next if preceding_component.empty?

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/extend/formula" require "rubocops/extend/formula"
@ -11,6 +11,7 @@ module RuboCop
# precedence order: # precedence order:
# build-time > test > normal > recommended > optional # build-time > test > normal > recommended > optional
class DependencyOrder < FormulaCop class DependencyOrder < FormulaCop
extend T::Sig
extend AutoCorrector extend AutoCorrector
def audit_formula(_node, _class_node, _parent_class_node, body_node) def audit_formula(_node, _class_node, _parent_class_node, body_node)
@ -65,11 +66,13 @@ module RuboCop
# `depends_on :apple if build.with? "foo"` should always be defined # `depends_on :apple if build.with? "foo"` should always be defined
# after `depends_on :foo`. # after `depends_on :foo`.
# This method reorders the dependencies array according to the above rule. # This method reorders the dependencies array according to the above rule.
sig { params(ordered: T::Array[RuboCop::AST::Node]).returns(T::Array[RuboCop::AST::Node]) }
def sort_conditional_dependencies!(ordered) def sort_conditional_dependencies!(ordered)
length = ordered.size length = ordered.size
idx = 0 idx = 0
while idx < length while idx < length
idx1, idx2 = nil idx1 = T.let(nil, T.nilable(Integer))
idx2 = T.let(nil, T.nilable(Integer))
ordered.each_with_index do |dep, pos| ordered.each_with_index do |dep, pos|
idx = pos+1 idx = pos+1
match_nodes = build_with_dependency_name(dep) match_nodes = build_with_dependency_name(dep)
@ -83,7 +86,7 @@ module RuboCop
end end
break if idx2 break if idx2
end end
insert_after!(ordered, idx1, idx2+idx1) if idx2 insert_after!(ordered, idx1, idx2 + T.must(idx1)) if idx2
end end
ordered ordered
end end
@ -93,8 +96,8 @@ module RuboCop
def verify_order_in_source(ordered) def verify_order_in_source(ordered)
ordered.each_with_index do |node_1, idx| ordered.each_with_index do |node_1, idx|
l1 = line_number(node_1) l1 = line_number(node_1)
l2 = nil l2 = T.let(nil, T.nilable(Integer))
node_2 = nil node_2 = T.let(nil, T.nilable(RuboCop::AST::Node))
ordered.drop(idx + 1).each do |test_node| ordered.drop(idx + 1).each do |test_node|
l2 = line_number(test_node) l2 = line_number(test_node)
node_2 = test_node if l2 < l1 node_2 = test_node if l2 < l1

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/shared/helper_functions" require "rubocops/shared/helper_functions"
@ -21,7 +21,6 @@ module RuboCop
@file_path = processed_source.buffer.name @file_path = processed_source.buffer.name
return unless file_path_allowed? return unless file_path_allowed?
return unless formula_class?(node) return unless formula_class?(node)
return unless respond_to?(:audit_formula)
class_node, parent_class_node, @body = *node class_node, parent_class_node, @body = *node
@formula_name = Pathname.new(@file_path).basename(".rb").to_s @formula_name = Pathname.new(@file_path).basename(".rb").to_s
@ -29,6 +28,10 @@ module RuboCop
audit_formula(node, class_node, parent_class_node, @body) audit_formula(node, class_node, parent_class_node, @body)
end end
def audit_formula(node, class_node, parent_class_node, body_node)
raise NotImplementedError, "Subclasses must implement this method."
end
# Yields to block when there is a match. # Yields to block when there is a match.
# #
# @param urls [Array] url/mirror method call nodes # @param urls [Array] url/mirror method call nodes

View File

@ -686,13 +686,13 @@ module RuboCop
end end
find_instance_method_call(body_node, :version, :==) do |method| find_instance_method_call(body_node, :version, :==) do |method|
next unless parameters_passed?(method, "HEAD") next unless parameters_passed?(method, ["HEAD"])
problem "Use 'build.head?' instead of inspecting 'version'" problem "Use 'build.head?' instead of inspecting 'version'"
end end
find_instance_method_call(body_node, "ARGV", :include?) do |method| find_instance_method_call(body_node, "ARGV", :include?) do |method|
next unless parameters_passed?(method, "--HEAD") next unless parameters_passed?(method, ["--HEAD"])
problem "Use \"if build.head?\" instead" problem "Use \"if build.head?\" instead"
end end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/shared/helper_functions" require "rubocops/shared/helper_functions"
@ -85,7 +85,9 @@ module RuboCop
# Auto correct desc problems. `regex_match_group` must be called before this to populate @offense_source_range. # Auto correct desc problems. `regex_match_group` must be called before this to populate @offense_source_range.
def desc_problem(message) def desc_problem(message)
add_offense(@offensive_source_range, message: message) do |corrector| add_offense(@offensive_source_range, message: message) do |corrector|
/\A(?<quote>["'])(?<correction>.*)(?:\k<quote>)\Z/ =~ @offensive_node.source match_data = @offensive_node.source.match(/\A(?<quote>["'])(?<correction>.*)(?:\k<quote>)\Z/)
correction = match_data[:correction]
quote = match_data[:quote]
next if correction.nil? next if correction.nil?

View File

@ -0,0 +1,5 @@
# typed: strict
module RuboCop::Cop::DescHelper
requires_ancestor { RuboCop::Cop::Base }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "rubocop" require "rubocop"
@ -14,6 +14,7 @@ module RuboCop
# #
# @api private # @api private
module HelperFunctions module HelperFunctions
extend T::Sig
include RangeHelp include RangeHelp
# Checks for regex match of pattern in the node and # Checks for regex match of pattern in the node and
@ -49,6 +50,7 @@ module RuboCop
end end
# Returns the line number of the node. # Returns the line number of the node.
sig { params(node: RuboCop::AST::Node).returns(Integer) }
def line_number(node) def line_number(node)
node.loc.line node.loc.line
end end
@ -166,7 +168,7 @@ module RuboCop
def find_method_with_args(node, method_name, *args) def find_method_with_args(node, method_name, *args)
methods = find_every_method_call_by_name(node, method_name) methods = find_every_method_call_by_name(node, method_name)
methods.each do |method| methods.each do |method|
next unless parameters_passed?(method, *args) next unless parameters_passed?(method, args)
return true unless block_given? return true unless block_given?
yield method yield method
@ -358,7 +360,7 @@ module RuboCop
# Returns true if the given parameters are present in method call # Returns true if the given parameters are present in method call
# and sets the method call as the offending node. # and sets the method call as the offending node.
# Params can be string, symbol, array, hash, matching regex. # Params can be string, symbol, array, hash, matching regex.
def parameters_passed?(method_node, *params) def parameters_passed?(method_node, params)
method_params = parameters(method_node) method_params = parameters(method_node)
@offensive_node = method_node @offensive_node = method_node
params.all? do |given_param| params.all? do |given_param|

View File

@ -0,0 +1,6 @@
# typed: strict
module RuboCop::Cop::HelperFunctions
include Kernel
requires_ancestor { RuboCop::Cop::Base }
end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "macos_versions" require "macos_versions"
@ -172,7 +172,7 @@ module RuboCop
def if_node_is_allowed?(if_node, allowed_methods: [], allowed_blocks: []) def if_node_is_allowed?(if_node, allowed_methods: [], allowed_blocks: [])
# TODO: check to see if it's legal # TODO: check to see if it's legal
valid = false valid = T.let(false, T::Boolean)
if_node.each_ancestor do |ancestor| if_node.each_ancestor do |ancestor|
valid_method_names = case ancestor.type valid_method_names = case ancestor.type
when :def when :def

View File

@ -59,20 +59,20 @@ module RuboCop
end end
find_method_with_args(body_node, :system, "dep", "ensure") do |d| find_method_with_args(body_node, :system, "dep", "ensure") do |d|
next if parameters_passed?(d, /vendor-only/) next if parameters_passed?(d, [/vendor-only/])
next if @formula_name == "goose" # needed in 2.3.0 next if @formula_name == "goose" # needed in 2.3.0
problem "use \"dep\", \"ensure\", \"-vendor-only\"" problem "use \"dep\", \"ensure\", \"-vendor-only\""
end end
find_method_with_args(body_node, :system, "cargo", "build") do |m| find_method_with_args(body_node, :system, "cargo", "build") do |m|
next if parameters_passed?(m, /--lib/) next if parameters_passed?(m, [/--lib/])
problem "use \"cargo\", \"install\", *std_cargo_args" problem "use \"cargo\", \"install\", *std_cargo_args"
end end
find_every_method_call_by_name(body_node, :system).each do |m| find_every_method_call_by_name(body_node, :system).each do |m|
next unless parameters_passed?(m, /make && make/) next unless parameters_passed?(m, [/make && make/])
offending_node(m) offending_node(m)
problem "Use separate `make` calls" problem "Use separate `make` calls"

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/extend/formula" require "rubocops/extend/formula"
@ -88,7 +88,7 @@ module RuboCop
audit_urls(urls, http_to_https_patterns) do |_, url, index| audit_urls(urls, http_to_https_patterns) do |_, url, index|
# It's fine to have a plain HTTP mirror further down the mirror list. # It's fine to have a plain HTTP mirror further down the mirror list.
https_url = url.dup.insert(4, "s") https_url = url.dup.insert(4, "s")
https_index = nil https_index = T.let(nil, T.nilable(Integer))
audit_urls(urls, https_url) do |_, _, found_https_index| audit_urls(urls, https_url) do |_, _, found_https_index|
https_index = found_https_index https_index = found_https_index
end end
@ -286,7 +286,7 @@ module RuboCop
sig { params(url: String).returns(String) } sig { params(url: String).returns(String) }
def get_pypi_url(url) def get_pypi_url(url)
package_file = File.basename(url) package_file = File.basename(url)
package_name = package_file.match(/^(.+)-[a-z0-9.]+$/)[1] package_name = T.must(package_file.match(/^(.+)-[a-z0-9.]+$/))[1]
"https://pypi.org/project/#{package_name}/#files" "https://pypi.org/project/#{package_name}/#files"
end end
end end

View File

@ -1,8 +1,4 @@
--dir --dir=.
. --enable-experimental-requires-ancestor
--ignore=/vendor
--ignore --ignore=/test/.gem
/vendor
--ignore
/test/.gem