Class: Steep::TypeInference::SendArgs
- Inherits:
-
Object
- Object
- Steep::TypeInference::SendArgs
- Defined in:
- lib/steep/type_inference/send_args.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#kw_args ⇒ Object
readonly
Returns the value of attribute kw_args.
Class Method Summary collapse
Instance Method Summary collapse
- #consume_arg(args) ⇒ Object
- #each_keyword_arg ⇒ Object
-
#initialize(args:, kw_args:) ⇒ SendArgs
constructor
A new instance of SendArgs.
- #kwsplat_nodes ⇒ Object
- #next_arg(args) ⇒ Object
- #save_arg_type(arg, type, hash) ⇒ Object
- #zip(params) ⇒ Object
Constructor Details
#initialize(args:, kw_args:) ⇒ SendArgs
Returns a new instance of SendArgs.
7 8 9 10 |
# File 'lib/steep/type_inference/send_args.rb', line 7 def initialize(args:, kw_args:) @args = args @kw_args = kw_args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/steep/type_inference/send_args.rb', line 4 def args @args end |
#kw_args ⇒ Object (readonly)
Returns the value of attribute kw_args.
5 6 7 |
# File 'lib/steep/type_inference/send_args.rb', line 5 def kw_args @kw_args end |
Class Method Details
.from_nodes(nodes) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/steep/type_inference/send_args.rb', line 12 def self.from_nodes(nodes) args = [] last_hash = nil nodes.each do |node| if last_hash args << last_hash last_hash = nil end case node.type when :hash last_hash = node else args << node end end if last_hash unless kw_args?(last_hash) args << last_hash last_hash = nil end end new(args: args, kw_args: last_hash) end |
.kw_args?(node) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/steep/type_inference/send_args.rb', line 40 def self.kw_args?(node) node.children.all? do |child| case child.type when :pair child.children[0].type == :sym when :kwsplat true end end end |
Instance Method Details
#consume_arg(args) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/steep/type_inference/send_args.rb', line 213 def consume_arg(args) if args.any? unless args[0].type == :splat args.shift end end end |
#each_keyword_arg ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/steep/type_inference/send_args.rb', line 51 def each_keyword_arg if block_given? if kw_args kw_args.children.each do |node| if node.type == :pair yield node end end end else enum_for :each_keyword_arg end end |
#kwsplat_nodes ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/steep/type_inference/send_args.rb', line 65 def kwsplat_nodes if kw_args kw_args.children.select do |node| node.type == :kwsplat end else [] end end |
#next_arg(args) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/steep/type_inference/send_args.rb', line 200 def next_arg(args) if args.any? case args[0].type when :splat args.each do |arg| yield arg end else yield args[0] end end end |
#save_arg_type(arg, type, hash) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/steep/type_inference/send_args.rb', line 190 def save_arg_type(arg, type, hash) if hash.key?(arg.object_id) types = hash[arg.object_id] else types = hash[arg.object_id] = [] end types << type end |
#zip(params) ⇒ Object
75 76 77 78 79 80 81 82 83 84 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 141 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 |
# File 'lib/steep/type_inference/send_args.rb', line 75 def zip(params) Set.new( [].tap do |pairs| consumed_keywords = Set.new rest_types = [] params.required_keywords.each do |name, type| if (node = each_keyword_arg.find {|pair| pair.children[0].children[0] == name }) pairs << [node.children[1], type] consumed_keywords << name else if kwsplat_nodes.any? rest_types << type else return end end end params.optional_keywords.each do |name, type| if (node = each_keyword_arg.find {|pair| pair.children[0].children[0] == name }) pairs << [node.children[1], type] consumed_keywords << name else if kwsplat_nodes.any? rest_types << type end end end if params.rest_keywords each_keyword_arg do |pair| name = pair.children[0].children[0] node = pair.children[1] unless consumed_keywords.include?(name) pairs << [node, params.rest_keywords] end end if kwsplat_nodes.any? pairs << [kw_args, AST::Types::Name.new_instance( name: "::Hash", args: [ AST::Types::Name.new_instance(name: "::Symbol"), AST::Types::Union.build(types: rest_types + [params.rest_keywords]) ] )] end end if params.has_keyword? if !params.rest_keywords if kwsplat_nodes.empty? if each_keyword_arg.any? {|pair| !consumed_keywords.include?(pair.children[0].children[0]) } return end end end end args = self.args.dup unless params.has_keyword? args << kw_args if kw_args end arg_types = {} params.required.each do |param| if args.any? next_arg(args) do |arg| save_arg_type(arg, param, arg_types) end consume_arg(args) else return end end params.optional.each do |param| next_arg(args) do |arg| save_arg_type(arg, param, arg_types) end consume_arg(args) end if args.any? if params.rest args.each do |arg| save_arg_type(arg, params.rest, arg_types) end else if args.none? {|arg| arg.type == :splat } return end end end (self.args + [kw_args].compact).each do |arg| types = arg_types[arg.object_id] if types if arg.type == :splat type = AST::Types::Name.new_instance(name: "::Array", args: [AST::Types::Union.build(types: types)]) else type = AST::Types::Union.build(types: types) end pairs << [arg, type] end end end ) end |