32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/empirical/signature_processor.rb', line 32
def visit_fun_call_node(node)
raise SyntaxError unless node.arguments
raise SyntaxError unless nil == node.receiver
case node
in {
arguments: Prism::ArgumentsNode[
arguments: [
Prism::KeywordHashNode[
elements: [
Prism::AssocNode[
key: signature,
value: return_type
]
]
]
]
]
}
body_block = node.block || @block_stack.first
preamble = []
postamble = []
case signature
in Prism::LocalVariableReadNode | Prism::ConstantReadNode
in Prism::CallNode
raise SyntaxError if signature.block
signature.arguments&.arguments&.each do |argument|
case argument
in Prism::LocalVariableWriteNode[name: name, value: Prism::ArrayNode[elements: [type]]]
@annotations << [
argument.name_loc.start_offset,
0,
"*",
]
@annotations << [
argument.name_loc.end_offset,
type.location.end_offset - argument.name_loc.end_offset + 1,
"",
]
preamble << "raise(::Empirical::TypeError.argument_type_error(name: '#{name}', value: #{name}, expected: ::Literal::_Array(#{type.slice}), method_name: __method__, context: self)) unless ::Literal::_Array(#{type.slice}) === #{name}"
in Prism::LocalVariableWriteNode[name: name, value: typed_param]
case typed_param
in Prism::CallNode[name: :|, receiver: type, arguments: Prism::ArgumentsNode[arguments: [default]]]
type_slice = type.slice
default_string = default.slice
else
type_slice = typed_param.slice
default_string = "nil"
end
@annotations << [
(start = typed_param.location.start_offset),
typed_param.location.end_offset - start,
default_string,
]
preamble << "raise(::Empirical::TypeError.argument_type_error(name: '#{name}', value: #{name}, expected: #{type_slice}, method_name: __method__, context: self)) unless #{type_slice} === #{name}"
in Prism::KeywordHashNode
argument.elements.each do |argument|
name = argument.key.unescaped
nilable = false
if name.end_with?("?")
name = name[0..-2]
nilable = true
@annotations << [
argument.key.location.end_offset - 2,
1,
"",
]
end
typed_param = argument.value
case typed_param
in Prism::HashNode[elements: [Prism::AssocNode[key: key_type, value: value_type]]]
@annotations << [
argument.key.location.start_offset,
0,
"**",
]
@annotations << [
argument.key.location.end_offset - 1,
typed_param.location.end_offset - argument.key.location.end_offset + 1,
"",
]
preamble << "raise(::Empirical::TypeError.argument_type_error(name: '#{name}', value: #{name}, expected: ::Literal::_Hash(#{key_type.slice}, #{value_type.slice}), method_name: __method__, context: self)) unless ::Literal::_Hash(#{key_type.slice}, #{value_type.slice}) === #{name}"
else
case typed_param
in Prism::CallNode[name: :|, receiver: type, arguments: Prism::ArgumentsNode[arguments: [default]]]
type_slice = if nilable
"::Literal::_Nilable(#{type.slice})"
else
type.slice
end
default_string = default.slice
else
type_slice = if nilable
"::Literal::_Nilable(#{typed_param.slice})"
else
typed_param.slice
end
default_string = "nil"
end
@annotations << [
(start = typed_param.location.start_offset),
typed_param.location.end_offset - start,
default_string,
]
preamble << "raise(::Empirical::TypeError.argument_type_error(name: '#{name}', value: #{name}, expected: #{type_slice}, method_name: __method__, context: self)) unless #{type_slice} === #{name}"
end
end
else
raise SyntaxError
end
end
else
raise SyntaxError
end
preamble << "__literally_returns__ = ("
postamble << ")"
case return_type
in Prism::LocalVariableReadNode[name: :void] | Prism::CallNode[name: :void, receiver: nil, block: nil, arguments: nil]
postamble << "::Empirical::Void"
in Prism::LocalVariableReadNode[name: :never] | Prism::CallNode[name: :never, receiver: nil, block: nil, arguments: nil]
postamble << "raise(::Empirical::NeverError.new)"
else
postamble << "raise(::Empirical::TypeError.return_type_error(value: __literally_returns__, expected: #{return_type.slice}, method_name: __method__, context: self)) unless #{return_type.slice} === __literally_returns__"
postamble << "__literally_returns__"
end
@annotations << [
(start = node.message_loc.start_offset),
node.message_loc.end_offset - start,
"def",
]
@annotations << [
(start = signature.location.end_offset),
body_block.opening_loc.end_offset - start,
";#{preamble.join(';')};",
]
@annotations << [
body_block.closing_loc.start_offset,
0,
";#{postamble.join(';')};",
]
else
raise SyntaxError
end
return_type
end
|