Created
September 14, 2024 03:47
-
-
Save tk0miya/b74748788a532a1eab880c3c73b298b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# | |
# rbs-inline のパーサを拡張し、def_delegator 文に対応させるハックのサンプル。 | |
# | |
# デフォルトでは -> untyped なメソッドが生成される。 | |
# 後置コメントを使うと返り値が指定できる。 | |
# | |
# 例: | |
# def_delegator :foo, :bar #: String | |
require "rbs/inline" | |
require "rbs/inline/cli" | |
class RubyDefDelegator < RBS::Inline::AST::Members::RubyDef | |
attr_reader :node #: Prism::CallNode | |
# @rbs override | |
def method_name | |
node.arguments.child_nodes[1].value.to_sym | |
end | |
# @rbs override | |
def annotated_method_types | |
method_type = RBS::MethodType.new(type_params: [], | |
type: RBS::Types::UntypedFunction.new(return_type: return_type || RBS::Types::Bases::Any.new(location: nil)), | |
block: nil, | |
location: nil) | |
[method_type] | |
end | |
end | |
# @rbs module-self RBS::Inline::Parser | |
module DefDelegatorParsable | |
# @rbs override | |
def visit_call_node(node) | |
return if ignored_node?(node) | |
case node.name | |
when :def_delegator, :def_instance_delegator | |
case node.receiver | |
when nil, Prism::SelfNode | |
assertion = assertion_annotation(node) | |
current_class_module_decl!.members << RubyDefDelegator.new(node, nil, current_visibility, assertion) | |
return | |
end | |
else | |
super | |
end | |
end | |
end | |
module RBS | |
module Inline | |
class Parser | |
include DefDelegatorParsable | |
end | |
end | |
end | |
exit RBS::Inline::CLI.new().run(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment