Class: Empirical::SignatureProcessor

Inherits:
BaseProcessor show all
Defined in:
lib/empirical/signature_processor.rb

Constant Summary

Constants inherited from BaseProcessor

BaseProcessor::EVAL_METHODS

Instance Method Summary collapse

Constructor Details

#initializeSignatureProcessor

Returns a new instance of SignatureProcessor.



4
5
6
7
8
# File 'lib/empirical/signature_processor.rb', line 4

def initialize(...)
	@return_type = nil
	@block_stack = []
	super
end

Instance Method Details

#visit_call_node(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/empirical/signature_processor.rb', line 10

def visit_call_node(node)
	case node
	in { name: :fun }
		original_return_type = @return_type
		@return_type = visit_fun_call_node(node)
		super # ensures any early returns are processed (also, technically, any internal method defs)
		@return_type = original_return_type

	# handle "method macros" (like `private`, `protected`, etc.)
	# because the body block is attached to that call node,
	# not the `fun` call node
	in { block: Prism::BlockNode }
		@block_stack << node.block
		super
		@block_stack.pop
	else
		original_return_type = @return_type
		super # ensures any early returns are processed (also, technically, any internal method defs)
		@return_type = original_return_type
	end
end

#visit_fun_call_node(node) ⇒ Object

Raises:

  • (SyntaxError)


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)
	# TODO: better error messages
	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
		# parameterless method defs (e.g. `fun foo` or `fun foo()`)
		in Prism::LocalVariableReadNode | Prism::ConstantReadNode
		# no-op
		# parameterful method defs (e.g. `fun foo(a: Type)` or `fun foo(a = Type)`)
		in Prism::CallNode
			raise SyntaxError if signature.block

			signature.arguments&.arguments&.each do |argument|
				case argument
				# Positional splat (e.g. `a = [Type]` becomes `*a`)
				in Prism::LocalVariableWriteNode[name: name, value: Prism::ArrayNode[elements: [type]]]
					# make argument a splat
					@annotations << [
						argument.name_loc.start_offset,
						0,
						"*",
					]

					# remove the type and equals operator from the argument
					@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}"

				# Positional (e.g. `a = Type` becomes `a = nil` or `a = default`)
				in Prism::LocalVariableWriteNode[name: name, value: typed_param]
					case typed_param
					# Positional with default (e.g. `a = Type | 1` becomes `a = 1`)
					in Prism::CallNode[name: :|, receiver: type, arguments: Prism::ArgumentsNode[arguments: [default]]]
						type_slice = type.slice
						default_string = default.slice
					# Positional without default (e.g. `a = Type` becomes `a = nil`)
					else
						type_slice = typed_param.slice
						default_string = "nil"
					end

					# replace the typed_param from the argument with the appropriate default value
					@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}"

				# Keyword (e.g. `a: Type` becomes `a: nil` or `a: default`)
				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
						# Keyword splat (e.g. `a: {Type => Type}` becomes `**a`)
						in Prism::HashNode[elements: [Prism::AssocNode[key: key_type, value: value_type]]]
							# make argument a splat
							@annotations << [
								argument.key.location.start_offset,
								0,
								"**",
							]

							# remove the typed_param and equals operator from the argument
							@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
							# Keyword with default
							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

							# replace the typed_param from the argument with the appropriate default value
							@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
					# TODO: better error message
					raise SyntaxError
				end
			end
		else
			# TODO: better error message
			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

		# Replace `fun` with `def`
		@annotations << [
			(start = node.message_loc.start_offset),
			node.message_loc.end_offset - start,
			"def",
		]

		# Remove the return type and `do` and replace with preamble
		@annotations << [
			(start = signature.location.end_offset),
			body_block.opening_loc.end_offset - start,
			";#{preamble.join(';')};",
		]

		# Insert postamble
		@annotations << [
			body_block.closing_loc.start_offset,
			0,
			";#{postamble.join(';')};",
		]
	else
		# TODO: better error message
		raise SyntaxError
	end

	return_type
end

#visit_return_node(node) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/empirical/signature_processor.rb', line 226

def visit_return_node(node)
	case @return_type
	in nil
		# no-op
	in Prism::LocalVariableReadNode[name: :void] | Prism::CallNode[name: :void, receiver: nil, block: nil, arguments: nil]
		if node.arguments
			raise "You’re returning something"
		else
			@annotations << [
				node.keyword_loc.end_offset,
				0,
				"(::Empirical::Void)",
			]
		end
	in Prism::LocalVariableReadNode[name: :never] | Prism::CallNode[name: :never, receiver: nil, block: nil, arguments: nil]
		@annotations << [
			node.keyword_loc.start_offset,
			node.keyword_loc.end_offset - node.keyword_loc.start_offset,
			"(raise(::Empirical::NeverError.new))",
		]
	else
		@annotations.push(
			[
				node.keyword_loc.start_offset,
				node.keyword_loc.end_offset - node.keyword_loc.start_offset,
				"(__literally_returning__ = (",
			],
			[
				node.location.end_offset,
				0,
				");(raise ::Empirical::TypeError.return_type_error(value: __literally_returning__, expected: #{@return_type.slice}, method_name: __method__, context: self) unless #{@return_type.slice} === __literally_returning__);return(__literally_returning__))",
			]
		)
	end

	super
end