lib/tapioca/compilers/dsl_compiler.rb in tapioca-0.4.27 vs lib/tapioca/compilers/dsl_compiler.rb in tapioca-0.5.0

- old
+ new

@@ -18,17 +18,18 @@ attr_reader :error_handler sig do params( requested_constants: T::Array[Module], - requested_generators: T::Array[String], + requested_generators: T::Array[T.class_of(Dsl::Base)], + excluded_generators: T::Array[T.class_of(Dsl::Base)], error_handler: T.nilable(T.proc.params(error: String).void) ).void end - def initialize(requested_constants:, requested_generators: [], error_handler: nil) + def initialize(requested_constants:, requested_generators: [], excluded_generators: [], error_handler: nil) @generators = T.let( - gather_generators(requested_generators), + gather_generators(requested_generators, excluded_generators), T::Enumerable[Dsl::Base] ) @requested_constants = requested_constants @error_handler = T.let(error_handler || $stderr.method(:puts), T.proc.params(error: String).void) end @@ -52,27 +53,23 @@ end end private - sig { params(requested_generators: T::Array[String]).returns(T.proc.params(klass: Class).returns(T::Boolean)) } - def generator_filter(requested_generators) - return ->(_klass) { true } if requested_generators.empty? - - generators = requested_generators.map(&:downcase) - - proc do |klass| - generator = klass.name&.sub(/^Tapioca::Compilers::Dsl::/, '')&.downcase - generators.include?(generator) - end + sig do + params( + requested_generators: T::Array[T.class_of(Dsl::Base)], + excluded_generators: T::Array[T.class_of(Dsl::Base)] + ).returns(T::Enumerable[Dsl::Base]) end + def gather_generators(requested_generators, excluded_generators) + generator_klasses = ::Tapioca::Reflection.descendants_of(Dsl::Base).select do |klass| + (requested_generators.empty? || requested_generators.include?(klass)) && + !excluded_generators.include?(klass) + end.sort_by { |klass| T.must(klass.name) } - sig { params(requested_generators: T::Array[String]).returns(T::Enumerable[Dsl::Base]) } - def gather_generators(requested_generators) - generator_filter = generator_filter(requested_generators) - - T.cast(Dsl::Base.descendants.select(&generator_filter).map(&:new), T::Enumerable[Dsl::Base]) + generator_klasses.map(&:new) end sig { params(requested_constants: T::Array[Module]).returns(T::Set[Module]) } def gather_constants(requested_constants) constants = generators.map(&:processable_constants).reduce(Set.new, :union) @@ -80,35 +77,22 @@ constants end sig { params(constant: Module).returns(T.nilable(String)) } def rbi_for_constant(constant) - parlour = Parlour::RbiGenerator.new(sort_namespaces: true) + file = RBI::File.new(strictness: "true") generators.each do |generator| next unless generator.handles?(constant) - generator.decorate(parlour.root, constant) + generator.decorate(file.root, constant) end - return if parlour.root.children.empty? + return if file.root.empty? - resolve_conflicts(parlour) - - parlour.rbi("true").strip - end - - sig { params(parlour: Parlour::RbiGenerator).void } - def resolve_conflicts(parlour) - Parlour::ConflictResolver.new.resolve_conflicts(parlour.root) do |msg, candidates| - error = StringIO.new - error.puts "=== Error ===" - error.puts msg - error.puts "# Candidates" - candidates.each_with_index do |candidate, index| - error.puts " #{index}. #{candidate.describe}" - end - report_error(error.string) - end + file.root.nest_non_public_methods! + file.root.group_nodes! + file.root.sort_nodes! + file.string end sig { params(error: String).returns(T.noreturn) } def report_error(error) handler = error_handler