Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hasumikin committed Jan 2, 2024
1 parent 62e347e commit 6ba99e6
Showing 1 changed file with 65 additions and 46 deletions.
111 changes: 65 additions & 46 deletions lib/rbs_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 6ba99e6

Please sign in to comment.