class RBS::AST::Ruby::Members::MethodTypeAnnotation::DocStyle
Attributes
block
[RW]
Annotations::BlockParamTypeAnnotation | Symbol | true | nil
optional_keywords
[R]
Hash[Symbol, Annotations::ParamTypeAnnotation | Symbol]
optional_positionals
[R]
Array[Annotations::ParamTypeAnnotation | Symbol]
required_keywords
[R]
Hash[Symbol, Annotations::ParamTypeAnnotation | Symbol]
required_positionals
[R]
Array[Annotations::ParamTypeAnnotation | Symbol]
rest_keywords
[RW]
Annotations::DoubleSplatParamTypeAnnotation | Symbol | true | nil
rest_positionals
[RW]
Annotations::SplatParamTypeAnnotation | Symbol | true | nil
return_type_annotation
[RW]
Annotations::ReturnTypeAnnotation | Annotations::NodeTypeAssertion | nil
trailing_positionals
[R]
Array[Annotations::ParamTypeAnnotation | Symbol]
Public Class Methods
(Array[param_type_annotation], Annotations::ReturnTypeAnnotation | Annotations::NodeTypeAssertion | nil, Prism::DefNode) → [ DocStyle, Array[param_type_annotation] ]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 41 def self.build(param_type_annotations, return_type_annotation, node) doc = DocStyle.new doc.return_type_annotation = return_type_annotation splat_annotation = nil #: Annotations::SplatParamTypeAnnotation? double_splat_annotation = nil #: Annotations::DoubleSplatParamTypeAnnotation? block_annotation = nil #: Annotations::BlockParamTypeAnnotation? param_annotations = {} #: Hash[Symbol, Annotations::ParamTypeAnnotation] unused = [] #: Array[Annotations::ParamTypeAnnotation | Annotations::SplatParamTypeAnnotation | Annotations::DoubleSplatParamTypeAnnotation | Annotations::BlockParamTypeAnnotation] param_type_annotations.each do |annot| case annot when Annotations::SplatParamTypeAnnotation if splat_annotation unused << annot else splat_annotation = annot end when Annotations::DoubleSplatParamTypeAnnotation if double_splat_annotation unused << annot else double_splat_annotation = annot end when Annotations::BlockParamTypeAnnotation if block_annotation unused << annot else block_annotation = annot end when Annotations::ParamTypeAnnotation name = annot.name_location.source.to_sym if param_annotations.key?(name) unused << annot else param_annotations[name] = annot end end end if node.parameters params = node.parameters #: Prism::ParametersNode params.requireds.each do |param| if param.is_a?(Prism::RequiredParameterNode) annotation = param_annotations.delete(param.name) if annotation doc.required_positionals << annotation else doc.required_positionals << param.name end end end params.optionals.each do |param| if param.is_a?(Prism::OptionalParameterNode) annotation = param_annotations.delete(param.name) if annotation doc.optional_positionals << annotation else doc.optional_positionals << param.name end end end if (rest = params.rest) && rest.is_a?(Prism::RestParameterNode) if splat_annotation && (splat_annotation.name_location.nil? || rest.name.nil? || splat_annotation.name_location.source.to_sym == rest.name) doc.rest_positionals = splat_annotation splat_annotation = nil else doc.rest_positionals = rest.name || true end end params.posts.each do |param| if param.is_a?(Prism::RequiredParameterNode) annotation = param_annotations.delete(param.name) if annotation doc.trailing_positionals << annotation else doc.trailing_positionals << param.name end end end params.keywords.each do |param| case param when Prism::RequiredKeywordParameterNode annotation = param_annotations.delete(param.name) if annotation doc.required_keywords[param.name] = annotation else doc.required_keywords[param.name] = param.name end when Prism::OptionalKeywordParameterNode annotation = param_annotations.delete(param.name) if annotation doc.optional_keywords[param.name] = annotation else doc.optional_keywords[param.name] = param.name end end end if (kw_rest = params.keyword_rest) && kw_rest.is_a?(Prism::KeywordRestParameterNode) if double_splat_annotation && (double_splat_annotation.name_location.nil? || kw_rest.name.nil? || double_splat_annotation.name_location.source.to_sym == kw_rest.name) doc.rest_keywords = double_splat_annotation double_splat_annotation = nil else doc.rest_keywords = kw_rest.name || true end end if (blk = params.block) && blk.is_a?(Prism::BlockParameterNode) if block_annotation && (block_annotation.name_location.nil? || blk.name.nil? || block_annotation.name == blk.name) doc.block = block_annotation block_annotation = nil else doc.block = blk.name || true end end end if block_annotation if node.parameters&.block # Block parameter exists but name didn't match -- treat as unused else doc.block = block_annotation block_annotation = nil end end unused.concat(param_annotations.values) unused << splat_annotation if splat_annotation unused << double_splat_annotation if double_splat_annotation unused << block_annotation if block_annotation [doc, unused] end
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 29 def initialize @return_type_annotation = nil @required_positionals = [] @optional_positionals = [] @rest_positionals = nil @trailing_positionals = [] @required_keywords = {} @optional_keywords = {} @rest_keywords = nil @block = nil end
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 181 def all_param_annotations annotations = [] #: Array[param_type_annotation | Symbol | true | nil] required_positionals.each { |a| annotations << a } optional_positionals.each { |a| annotations << a } annotations << rest_positionals trailing_positionals.each { |a| annotations << a } required_keywords.each_value { |a| annotations << a } optional_keywords.each_value { |a| annotations << a } annotations << rest_keywords annotations << block annotations end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 196 def map_type_name(&block) DocStyle.new.tap do |new| new.return_type_annotation = return_type_annotation&.map_type_name(&block) new.required_positionals.replace( required_positionals.map do |a| case a when Annotations::ParamTypeAnnotation a.map_type_name(&block) else a end end ) new.optional_positionals.replace( optional_positionals.map do |a| case a when Annotations::ParamTypeAnnotation a.map_type_name(&block) else a end end ) new.rest_positionals = case rest_positionals when Annotations::SplatParamTypeAnnotation rest_positionals.map_type_name(&block) else rest_positionals end new.trailing_positionals.replace( trailing_positionals.map do |a| case a when Annotations::ParamTypeAnnotation a.map_type_name(&block) else a end end ) new.required_keywords.replace( required_keywords.transform_values do |a| case a when Annotations::ParamTypeAnnotation a.map_type_name(&block) else a end end ) new.optional_keywords.replace( optional_keywords.transform_values do |a| case a when Annotations::ParamTypeAnnotation a.map_type_name(&block) else a end end ) new.rest_keywords = case rest_keywords when Annotations::DoubleSplatParamTypeAnnotation rest_keywords.map_type_name(&block) else rest_keywords end new.block = case self.block when Annotations::BlockParamTypeAnnotation self.block.map_type_name(&block) else self.block end end #: self end
() → MethodType
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 287 def method_type return_type = case return_type_annotation when Annotations::NodeTypeAssertion return_type_annotation.type when Annotations::ReturnTypeAnnotation return_type_annotation.return_type else Types::Bases::Any.new(location: nil) end any = -> { Types::Bases::Any.new(location: nil) } req_pos = required_positionals.map do |a| case a when Annotations::ParamTypeAnnotation Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym) else Types::Function::Param.new(type: any.call, name: a) end end opt_pos = optional_positionals.map do |a| case a when Annotations::ParamTypeAnnotation Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym) else Types::Function::Param.new(type: any.call, name: a) end end rest_pos = case rest_positionals when Annotations::SplatParamTypeAnnotation Types::Function::Param.new(type: rest_positionals.param_type, name: rest_positionals.name_location&.source&.to_sym) when Symbol Types::Function::Param.new(type: any.call, name: rest_positionals) when true Types::Function::Param.new(type: any.call, name: nil) else nil end trail_pos = trailing_positionals.map do |a| case a when Annotations::ParamTypeAnnotation Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym) else Types::Function::Param.new(type: any.call, name: a) end end req_kw = required_keywords.transform_values do |a| case a when Annotations::ParamTypeAnnotation Types::Function::Param.new(type: a.param_type, name: nil) else Types::Function::Param.new(type: any.call, name: nil) end end opt_kw = optional_keywords.transform_values do |a| case a when Annotations::ParamTypeAnnotation Types::Function::Param.new(type: a.param_type, name: nil) else Types::Function::Param.new(type: any.call, name: nil) end end rest_kw = case rest_keywords when Annotations::DoubleSplatParamTypeAnnotation Types::Function::Param.new(type: rest_keywords.param_type, name: rest_keywords.name_location&.source&.to_sym) when Symbol Types::Function::Param.new(type: any.call, name: rest_keywords) when true Types::Function::Param.new(type: any.call, name: nil) else nil end type = Types::Function.new( required_positionals: req_pos, optional_positionals: opt_pos, rest_positionals: rest_pos, trailing_positionals: trail_pos, required_keywords: req_kw, optional_keywords: opt_kw, rest_keywords: rest_kw, return_type: return_type ) method_block = case self.block when Annotations::BlockParamTypeAnnotation Types::Block.new( type: self.block.type, required: self.block.required? ) when Symbol, true Types::Block.new( type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)), required: false ) else nil end MethodType.new( type_params: [], type: type, block: method_block, location: nil ) end
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ast/ruby/members.rb, line 273 def type_fingerprint [ return_type_annotation&.type_fingerprint, all_param_annotations.map do |param| case param when Annotations::Base param.type_fingerprint else param end end ] end