Class: Steep::TypeInference::BlockParams
- Inherits:
-
Object
- Object
- Steep::TypeInference::BlockParams
- Defined in:
- lib/steep/type_inference/block_params.rb
Defined Under Namespace
Classes: Param
Instance Attribute Summary collapse
-
#leading_params ⇒ Object
readonly
Returns the value of attribute leading_params.
-
#optional_params ⇒ Object
readonly
Returns the value of attribute optional_params.
-
#rest_param ⇒ Object
readonly
Returns the value of attribute rest_param.
-
#trailing_params ⇒ Object
readonly
Returns the value of attribute trailing_params.
Class Method Summary collapse
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #expandable? ⇒ Boolean
- #expandable_params?(params_type) ⇒ Boolean
-
#initialize(leading_params:, optional_params:, rest_param:, trailing_params:) ⇒ BlockParams
constructor
A new instance of BlockParams.
- #params ⇒ Object
- #params_type(hint: nil) ⇒ Object
- #params_type0(hint:) ⇒ Object
- #zip(params_type) ⇒ Object
Constructor Details
#initialize(leading_params:, optional_params:, rest_param:, trailing_params:) ⇒ BlockParams
Returns a new instance of BlockParams.
33 34 35 36 37 38 |
# File 'lib/steep/type_inference/block_params.rb', line 33 def initialize(leading_params:, optional_params:, rest_param:, trailing_params:) @leading_params = leading_params @optional_params = optional_params @rest_param = rest_param @trailing_params = trailing_params end |
Instance Attribute Details
#leading_params ⇒ Object (readonly)
Returns the value of attribute leading_params.
28 29 30 |
# File 'lib/steep/type_inference/block_params.rb', line 28 def leading_params @leading_params end |
#optional_params ⇒ Object (readonly)
Returns the value of attribute optional_params.
29 30 31 |
# File 'lib/steep/type_inference/block_params.rb', line 29 def optional_params @optional_params end |
#rest_param ⇒ Object (readonly)
Returns the value of attribute rest_param.
30 31 32 |
# File 'lib/steep/type_inference/block_params.rb', line 30 def rest_param @rest_param end |
#trailing_params ⇒ Object (readonly)
Returns the value of attribute trailing_params.
31 32 33 |
# File 'lib/steep/type_inference/block_params.rb', line 31 def trailing_params @trailing_params end |
Class Method Details
.from_node(node, annotations:) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/steep/type_inference/block_params.rb', line 49 def self.from_node(node, annotations:) leading_params = [] optional_params = [] rest_param = nil trailing_params = [] default_params = leading_params node.children.each do |arg| var = arg.children.first type = annotations.var_type(lvar: var.name) case arg.type when :arg, :procarg0 default_params << Param.new(var: var, type: type, value: nil, node: arg) when :optarg default_params = trailing_params optional_params << Param.new(var: var, type: type, value: arg.children.last, node: arg) when :restarg default_params = trailing_params rest_param = Param.new(var: var, type: type, value: nil, node: arg) end end new( leading_params: leading_params, optional_params: optional_params, rest_param: rest_param, trailing_params: trailing_params ) end |
Instance Method Details
#each(&block) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/steep/type_inference/block_params.rb', line 230 def each(&block) if block_given? params.each(&block) else enum_for :each end end |
#expandable? ⇒ Boolean
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/steep/type_inference/block_params.rb', line 219 def case when leading_params.size + trailing_params.size > 1 true when (leading_params.any? || trailing_params.any?) && rest_param true when params.size == 1 && params[0].node.type == :arg true end end |
#expandable_params?(params_type) ⇒ Boolean
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/steep/type_inference/block_params.rb', line 208 def (params_type) if params_type.flat_unnamed_params.size == 1 case (type = params_type.required.first) when AST::Types::Tuple true when AST::Types::Name::Base AST::Builtin::Array.instance_type?(type) end end end |
#params ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/steep/type_inference/block_params.rb', line 40 def params [].tap do |params| params.push(*leading_params) params.push(*optional_params) params.push rest_param if rest_param params.push(*trailing_params) end end |
#params_type(hint: nil) ⇒ Object
81 82 83 |
# File 'lib/steep/type_inference/block_params.rb', line 81 def params_type(hint: nil) params_type0(hint: hint) or params_type0(hint: nil) end |
#params_type0(hint:) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/steep/type_inference/block_params.rb', line 85 def params_type0(hint:) if hint case when leading_params.size == hint.required.size leadings = leading_params.map.with_index do |param, index| param.type || hint.required[index] end when !hint.rest && hint.optional.empty? && leading_params.size > hint.required.size leadings = leading_params.take(hint.required.size).map.with_index do |param, index| param.type || hint.required[index] end when !hint.rest && hint.optional.empty? && leading_params.size < hint.required.size leadings = leading_params.map.with_index do |param, index| param.type || hint.required[index] end + hint.required.drop(leading_params.size) else return nil end case when optional_params.size == hint.optional.size optionals = optional_params.map.with_index do |param, index| param.type || hint.optional[index] end when !hint.rest && optional_params.size > hint.optional.size optionals = optional_params.take(hint.optional.size).map.with_index do |param, index| param.type || hint.optional[index] end when !hint.rest && optional_params.size < hint.optional.size optionals = optional_params.map.with_index do |param, index| param.type || hint.optional[index] end + hint.optional.drop(optional_params.size) else return nil end if rest_param && hint.rest rest = rest_param.type&.yield_self {|ty| ty.args&.first } || hint.rest else rest = hint.rest end else leadings = leading_params.map {|param| param.type || AST::Types::Any.new } optionals = optional_params.map {|param| param.type || AST::Types::Any.new } rest = rest_param&.yield_self {|param| param.type.args[0] } end Interface::Params.new( required: leadings, optional: optionals, rest: rest, required_keywords: {}, optional_keywords: {}, rest_keywords: nil ) end |
#zip(params_type) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/steep/type_inference/block_params.rb', line 142 def zip(params_type) if trailing_params.any? Steep.logger.error "Block definition with trailing required parameters are not supported yet" end [].tap do |zip| if (params_type) && type = params_type.required[0] case when AST::Builtin::Array.instance_type?(type) type_arg = type.args[0] params.each do |param| unless param == rest_param zip << [param, AST::Types::Union.build(types: [type_arg, AST::Builtin.nil_type])] else zip << [param, AST::Builtin::Array.instance_type(type_arg)] end end when type.is_a?(AST::Types::Tuple) types = type.types.dup (leading_params + optional_params).each do |param| ty = types.shift if ty zip << [param, ty] else zip << [param, AST::Types::Nil.new] end end if rest_param if types.any? union = AST::Types::Union.build(types: types) zip << [rest_param, AST::Builtin::Array.instance_type(union)] else zip << [rest_param, AST::Types::Nil.new] end end end else types = params_type.flat_unnamed_params (leading_params + optional_params).each do |param| type = types.shift&.last || params_type.rest if type zip << [param, type] else zip << [param, AST::Builtin.nil_type] end end if rest_param if types.empty? array = AST::Builtin::Array.instance_type(params_type.rest || AST::Builtin.any_type) zip << [rest_param, array] else union = AST::Types::Union.build(types: types.map(&:last) + [params_type.rest]) array = AST::Builtin::Array.instance_type(union) zip << [rest_param, array] end end end end end |