Skip to content

Commit

Permalink
Enable typing in rubocops
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Feb 16, 2023
1 parent dbd9f72 commit 9e550fe
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 43 deletions.
10 changes: 6 additions & 4 deletions Library/Homebrew/rubocops/cask/extend/node.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
Expand All @@ -19,10 +19,12 @@ class Node

def stanza?
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

def heredoc?
Expand Down
9 changes: 8 additions & 1 deletion Library/Homebrew/rubocops/cask/mixin/cask_help.rb
@@ -1,11 +1,18 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
module Cop
module Cask
# Common functionality for cops checking casks.
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)
super if defined? super
return unless respond_to?(:on_cask)
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/cask_help.rbi
@@ -0,0 +1,5 @@
# typed: strict

module RuboCop::Cop::Cask::CaskHelp
requires_ancestor { RuboCop::Cop::Base }
end
2 changes: 1 addition & 1 deletion Library/Homebrew/rubocops/cask/mixin/on_desc_stanza.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/on_desc_stanza.rbi
@@ -0,0 +1,5 @@
# typed: strict

module RuboCop::Cop::Cask::OnDescStanza
requires_ancestor { RuboCop::Cop::Cask::Desc }
end
2 changes: 1 addition & 1 deletion Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/on_homepage_stanza.rbi
@@ -0,0 +1,5 @@
# typed: strict

module RuboCop::Cop::Cask::OnHomepageStanza
requires_ancestor { RuboCop::Cop::Cask::HomepageUrlTrailingSlash }
end
2 changes: 1 addition & 1 deletion Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rbi
@@ -0,0 +1,5 @@
# typed: strict

module RuboCop::Cop::Cask::OnUrlStanza
requires_ancestor { RuboCop::Cop::Cask::UrlLegacyCommaSeparators }
end
10 changes: 5 additions & 5 deletions Library/Homebrew/rubocops/components_order.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "ast_constants"
Expand Down Expand Up @@ -58,7 +58,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)

@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|
blocks.each do |on_system_block|
Expand All @@ -68,7 +68,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
end
end

message = nil
message = T.let(nil, T.nilable(String))
allowed_methods = [
[:url, :sha256],
[:url, :mirror, :sha256],
Expand Down Expand Up @@ -97,7 +97,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
break
end

if message.present?
if message
problem message
next
end
Expand Down Expand Up @@ -199,7 +199,7 @@ def check_order(component_precedence_list, body_node)
end

# 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|
next if preceding_component.empty?

Expand Down
13 changes: 8 additions & 5 deletions Library/Homebrew/rubocops/dependency_order.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "rubocops/extend/formula"
Expand All @@ -11,6 +11,7 @@ module FormulaAudit
# precedence order:
# build-time > test > normal > recommended > optional
class DependencyOrder < FormulaCop
extend T::Sig
extend AutoCorrector

def audit_formula(_node, _class_node, _parent_class_node, body_node)
Expand Down Expand Up @@ -65,11 +66,13 @@ def sort_dependencies_by_type(dependency_nodes)
# `depends_on :apple if build.with? "foo"` should always be defined
# after `depends_on :foo`.
# 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)
length = ordered.size
idx = 0
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|
idx = pos+1
match_nodes = build_with_dependency_name(dep)
Expand All @@ -83,7 +86,7 @@ def sort_conditional_dependencies!(ordered)
end
break if idx2
end
insert_after!(ordered, idx1, idx2+idx1) if idx2
insert_after!(ordered, idx1, idx2 + T.must(idx1)) if idx2
end
ordered
end
Expand All @@ -93,8 +96,8 @@ def sort_conditional_dependencies!(ordered)
def verify_order_in_source(ordered)
ordered.each_with_index do |node_1, idx|
l1 = line_number(node_1)
l2 = nil
node_2 = nil
l2 = T.let(nil, T.nilable(Integer))
node_2 = T.let(nil, T.nilable(RuboCop::AST::Node))
ordered.drop(idx + 1).each do |test_node|
l2 = line_number(test_node)
node_2 = test_node if l2 < l1
Expand Down
6 changes: 5 additions & 1 deletion Library/Homebrew/rubocops/extend/formula.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "rubocops/shared/helper_functions"
Expand Down Expand Up @@ -29,6 +29,10 @@ def on_class(node)
audit_formula(node, class_node, parent_class_node, @body)
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.
#
# @param urls [Array] url/mirror method call nodes
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/rubocops/lines.rb
Expand Up @@ -686,13 +686,13 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
end

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'"
end

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"
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/rubocops/platform.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module RuboCop
Expand Down
6 changes: 4 additions & 2 deletions Library/Homebrew/rubocops/shared/desc_helper.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "rubocops/shared/helper_functions"
Expand Down Expand Up @@ -85,7 +85,9 @@ def audit_desc(type, name, desc_call)
# Auto correct desc problems. `regex_match_group` must be called before this to populate @offense_source_range.
def desc_problem(message)
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?

Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/shared/desc_helper.rbi
@@ -0,0 +1,5 @@
# typed: strict

module RuboCop::Cop::DescHelper
requires_ancestor { RuboCop::Cop::Base }
end
8 changes: 5 additions & 3 deletions Library/Homebrew/rubocops/shared/helper_functions.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "rubocop"
Expand All @@ -14,6 +14,7 @@ module Cop
#
# @api private
module HelperFunctions
extend T::Sig
include RangeHelp

# Checks for regex match of pattern in the node and
Expand Down Expand Up @@ -49,6 +50,7 @@ def start_column(node)
end

# Returns the line number of the node.
sig { params(node: RuboCop::AST::Node).returns(Integer) }
def line_number(node)
node.loc.line
end
Expand Down Expand Up @@ -166,7 +168,7 @@ def find_every_func_call_by_name(node, func_name = nil)
def find_method_with_args(node, method_name, *args)
methods = find_every_method_call_by_name(node, method_name)
methods.each do |method|
next unless parameters_passed?(method, *args)
next unless parameters_passed?(method, args)
return true unless block_given?

yield method
Expand Down Expand Up @@ -358,7 +360,7 @@ def parameters(method_node)
# Returns true if the given parameters are present in method call
# and sets the method call as the offending node.
# 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)
@offensive_node = method_node
params.all? do |given_param|
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/rubocops/shared/helper_functions.rbi
@@ -0,0 +1,6 @@
# typed: strict

module RuboCop::Cop::HelperFunctions
include Kernel
requires_ancestor { RuboCop::Cop::Base }
end
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "macos_versions"
Expand Down Expand Up @@ -172,7 +172,7 @@ def if_statement_problem(if_node, if_statement_string, on_system_method_string,

def if_node_is_allowed?(if_node, allowed_methods: [], allowed_blocks: [])
# TODO: check to see if it's legal
valid = false
valid = T.let(false, T::Boolean)
if_node.each_ancestor do |ancestor|
valid_method_names = case ancestor.type
when :def
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/rubocops/text.rb
Expand Up @@ -59,20 +59,20 @@ def audit_formula(node, _class_node, _parent_class_node, body_node)
end

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

problem "use \"dep\", \"ensure\", \"-vendor-only\""
end

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"
end

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)
problem "Use separate `make` calls"
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/rubocops/urls.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "rubocops/extend/formula"
Expand Down Expand Up @@ -88,7 +88,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
audit_urls(urls, http_to_https_patterns) do |_, url, index|
# It's fine to have a plain HTTP mirror further down the mirror list.
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|
https_index = found_https_index
end
Expand Down Expand Up @@ -286,7 +286,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
sig { params(url: String).returns(String) }
def get_pypi_url(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"
end
end
Expand Down
12 changes: 4 additions & 8 deletions Library/Homebrew/sorbet/config
@@ -1,8 +1,4 @@
--dir
.

--ignore
/vendor

--ignore
/test/.gem
--dir=.
--enable-experimental-requires-ancestor
--ignore=/vendor
--ignore=/test/.gem

0 comments on commit 9e550fe

Please sign in to comment.