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 "method_source"
require "rubocop" require "rubocop"
require_relative "../../../extend/string" # placate rubocop
require_relative "../../../rubocops" require_relative "../../../rubocops"
module Tapioca module Tapioca
module Compilers 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. # FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
# rubocop:disable Style/MutableConstant # 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 # rubocop:enable Style/MutableConstant
sig { override.returns(T::Enumerable[Module]) } sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants def self.gather_constants
[ all_modules.select do |klass|
RuboCop::Cop::Cask::Variables, next unless klass.singleton_class < ::RuboCop::AST::NodePattern::Macros
RuboCop::Cop::Homebrew::Blank,
RuboCop::Cop::Homebrew::CompactBlank, path = T.must(Object.const_source_location(klass.to_s)).fetch(0).to_s
RuboCop::Cop::Homebrew::MoveToExtendOS, # exclude vendored code, to avoid contradicting their RBI files
RuboCop::Cop::Homebrew::NegateInclude, path.exclude?("/vendor/bundle/ruby/") &&
RuboCop::Cop::Homebrew::Presence, # exclude source code that already has an RBI file
RuboCop::Cop::Homebrew::Present, !Pathname("#{path}i").exist? &&
RuboCop::Cop::Homebrew::SafeNavigationWithBlank, # exclude source code that doesn't use the DSLs
RuboCop::Cop::FormulaAudit::ComponentsOrder, File.readlines(path).grep(/def_node_/).any?
RuboCop::Cop::FormulaAudit::DependencyOrder, end
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,
]
end end
sig { override.void } sig { override.void }
def decorate def decorate
root.create_path(constant) do |klass| root.create_path(constant) do |klass|
# For each encrypted attribute we find in the class
constant.instance_methods(false).each do |method_name| constant.instance_methods(false).each do |method_name|
source = constant.instance_method(method_name).source.lstrip 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://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.rb
# https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern/method_definer.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") if source.start_with?("def_node_matcher")
# https://github.com/Shopify/tapioca/blob/3341a9b/lib/tapioca/rbi_ext/model.rb#L89 # https://github.com/Shopify/tapioca/blob/3341a9b/lib/tapioca/rbi_ext/model.rb#L89
klass.create_method( klass.create_method(