Make gather_constants dynamic

This commit is contained in:
Douglas Eichelberger 2024-02-04 11:46:03 -08:00
parent 1dd0f6bbb2
commit adfec16871

View File

@ -3,55 +3,44 @@
require "method_source"
require "rubocop"
require_relative "../../../extend/string" # placate rubocop
require_relative "../../../rubocops"
module Tapioca
module Compilers
class Rubocop < Tapioca::Dsl::Compiler
class RuboCop < Tapioca::Dsl::Compiler
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
# rubocop:disable Style/MutableConstant
ConstantType = type_member { { fixed: T::Class[T.anything] } }
# This should be a module whose singleton class contains RuboCop::AST::NodePattern::Macros,
# but I don't know how to express that in Sorbet.
ConstantType = type_member { { fixed: Module } }
# rubocop:enable Style/MutableConstant
sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants
[
RuboCop::Cop::Cask::Variables,
RuboCop::Cop::Homebrew::Blank,
RuboCop::Cop::Homebrew::CompactBlank,
RuboCop::Cop::Homebrew::MoveToExtendOS,
RuboCop::Cop::Homebrew::NegateInclude,
RuboCop::Cop::Homebrew::Presence,
RuboCop::Cop::Homebrew::Present,
RuboCop::Cop::Homebrew::SafeNavigationWithBlank,
RuboCop::Cop::FormulaAudit::ComponentsOrder,
RuboCop::Cop::FormulaAudit::DependencyOrder,
RuboCop::Cop::FormulaAudit::DeprecateDisableDate,
RuboCop::Cop::FormulaAudit::DeprecateDisableReason,
RuboCop::Cop::FormulaAudit::Licenses,
RuboCop::Cop::FormulaAudit::OptionDeclarations,
RuboCop::Cop::FormulaAudit::GenerateCompletionsDSL,
RuboCop::Cop::FormulaAudit::GitUrls,
RuboCop::Cop::FormulaAudit::Miscellaneous,
RuboCop::Cop::FormulaAudit::Patches,
RuboCop::Cop::FormulaAudit::Test,
RuboCop::Cop::FormulaAudit::Text,
RuboCop::Cop::FormulaAuditStrict::GitUrls,
RuboCop::Cop::FormulaAuditStrict::Text,
RuboCop::Cop::FormulaCop,
RuboCop::Cop::OnSystemConditionalsHelper,
]
all_modules.select do |klass|
next unless klass.singleton_class < ::RuboCop::AST::NodePattern::Macros
path = T.must(Object.const_source_location(klass.to_s)).fetch(0).to_s
# exclude vendored code, to avoid contradicting their RBI files
path.exclude?("/vendor/bundle/ruby/") &&
# exclude source code that already has an RBI file
!Pathname("#{path}i").exist? &&
# exclude source code that doesn't use the DSLs
File.readlines(path).grep(/def_node_/).any?
end
end
sig { override.void }
def decorate
root.create_path(constant) do |klass|
# For each encrypted attribute we find in the class
constant.instance_methods(false).each do |method_name|
source = constant.instance_method(method_name).source.lstrip
# For more info on these DSLs:
# https://www.rubydoc.info/gems/rubocop-ast/RuboCop/AST/NodePattern/Macros
# https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
# https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern/method_definer.rb
# The type signatures below could maybe be stronger, but I only wanted to avoid errors:
if source.start_with?("def_node_matcher")
# https://github.com/Shopify/tapioca/blob/3341a9b/lib/tapioca/rbi_ext/model.rb#L89
klass.create_method(