From 6ba99e636c30ceccad79708a905f2556353a2e48 Mon Sep 17 00:00:00 2001 From: HASUMI Hitoshi Date: Tue, 2 Jan 2024 18:44:23 +0900 Subject: [PATCH] wip --- lib/rbs_doc.rb | 111 +++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 46 deletions(-) diff --git a/lib/rbs_doc.rb b/lib/rbs_doc.rb index a1e54eb..608ff9b 100644 --- a/lib/rbs_doc.rb +++ b/lib/rbs_doc.rb @@ -19,47 +19,61 @@ def initialize(method) end end - def format - @info.to_s - end + attr_reader :info def format_overload(overload) - type = overload.method_type.type - { - requied_positionals: format_requied_positionals(type) - #format_optional_positionals - #format_optional_keywords - #format_required_keywords - #format_rest_positionals - #format_rest_keywords - #format_trailing_positionals - #overload.method_type.block - #format_type.return_type - } + # overload.method_type.block + # type.return_type + Hash.new.tap do |member| + %i[required_positionals optional_positionals rest_positionals trailing_positionals required_keywords optional_keywords rest_keywords].each do |key| + format_params(key, overload.method_type.type).then do |params| + member[key] = params if params && !params.empty? + end + end + end end - def format_requied_positionals(type) - type.required_positionals.map do |param| - Hash.new.tap do |h| - h[:name] = param.name&.to_s || "arg" - h[:type] = case param.type - when RBS::Types::ClassInstance - param.type.name.to_s - when RBS::Types::Union - param.type.types.map do |type| - type.to_s - end.join(" | ") - when RBS::Types::Bases::Bool - "bool" - when RBS::Types::Alias - param.type.name.to_s - when RBS::Types::Tuple, RBS::Types::Literal, RBS::Types::Bases::Any - param.type.location.source - else - raise "unknown type: #{param.type}" - end + def format_params(key, type) + params = type.send(key) + if params.respond_to?(:map) + params.map do |param| + format_param(param) + end + else + format_param(params) if params + end + end + + def format_param(param) + if param.respond_to?(:[]) + { param[0] => format_param2(param[1]) } + else + format_param2(param) + end + end + + def format_param2(param) + Hash.new.tap do |h| + h[:name] = param.name + h[:type] = case param.type + when RBS::Types::ClassInstance + param.type.name.to_s + when RBS::Types::Union + param.type.types&.map do |type| + type.to_s + end.join(" | ") + when RBS::Types::Bases::Bool + "bool" + when RBS::Types::Alias + param.type.name.to_s + when RBS::Types::Tuple, RBS::Types::Literal, RBS::Types::Bases::Any + param.type.location.source + else + param.type.location.source end end + rescue => e + binding.irb end end @@ -69,24 +83,29 @@ def initialize(steepfile:) end def generate + @result = [] @steep_config.config.targets[0].signature_pattern.patterns.each do |pattern| - Dir.glob("#{pattern}*.rbs").each do |path| - sig = RBS::Parser.parse_signature(File.read(path)) - p path - sig[2].each do |dec| - case dec - when RBS::AST::Declarations::Class - dec.members.each do |member| - case member - when RBS::AST::Members::MethodDefinition - formatter = RBSDoc::MethodFormatter.new(member) - p formatter.format + Dir.glob("#{pattern}*.rbs").map do |path| + @result << Hash.new.tap do |h| + sig = RBS::Parser.parse_signature(File.read(path)) + sig[2].each do |dec| + case dec + when RBS::AST::Declarations::Class + h[:class] = sig[2].first.name.name + h[:methods] = [] + dec.members.each do |member| + case member + when RBS::AST::Members::MethodDefinition + formatter = RBSDoc::MethodFormatter.new(member) + h[:methods] << formatter.info + end end end end end end end + pp @result end end end