1
2
3
4
5
6
7
8
9
10
11
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
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
225
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! Iterator and async iterator type extraction.
//!
//! Extracts iterator protocol information from types, handling both sync
//! and async iterators. Used by the checker for `for..of` and `for await..of`
//! loop type checking.
use crate::TypeDatabase;
use crate::operations_property::PropertyAccessEvaluator;
use crate::types::{PropertyInfo, TypeData, TypeId};
/// Information about an iterator type extracted from a type.
///
/// This struct captures the key types needed for iterator/generator type checking:
/// - The iterator object type itself
/// - The type yielded by next().value (T in Iterator<T>)
/// - The type returned when done (`TReturn` in `IteratorResult`<T, `TReturn`>)
/// - The type accepted by `next()` (`TNext` in Iterator<T, `TReturn`, `TNext`>)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IteratorInfo {
/// The iterator object type (has `next()` method)
pub iterator_type: TypeId,
/// The type yielded by the iterator (from `IteratorResult`<T, `TReturn`>)
pub yield_type: TypeId,
/// The return type when iteration completes
pub return_type: TypeId,
/// The type accepted by next(val) (contravariant)
pub next_type: TypeId,
}
/// Extract iterator information from a type.
///
/// This function handles both sync and async iterators by finding the
/// appropriate symbol property and extracting the relevant types.
///
/// # Arguments
///
/// * `db` - The type database/interner
/// * `type_id` - The type to extract iterator info from
/// * `is_async` - If true, look for [Symbol.asyncIterator], otherwise [Symbol.iterator]
///
/// # Returns
///
/// * `Some(IteratorInfo)` - If the type is iterable
/// * `None` - If the type is not iterable or doesn't have a valid `next()` method
pub fn get_iterator_info(
db: &dyn crate::db::QueryDatabase,
type_id: TypeId,
is_async: bool,
) -> Option<IteratorInfo> {
use crate::type_queries::is_callable_type;
// Fast path: Handle intrinsics that are always iterable
// The 'any' black hole: any iterates to any
if type_id == TypeId::ANY {
return Some(IteratorInfo {
iterator_type: TypeId::ANY,
yield_type: TypeId::ANY,
return_type: TypeId::ANY,
next_type: TypeId::ANY,
});
}
// Fast path: Handle Array and Tuple types
if let Some(key) = db.lookup(type_id) {
match key {
TypeData::Array(elem_type) => {
return get_array_iterator_info(type_id, elem_type);
}
TypeData::Tuple(_) => {
return get_tuple_iterator_info(db, type_id);
}
_ => {}
}
}
// Step 1: Find the iterator-producing method
let symbol_name = if is_async {
"[Symbol.asyncIterator]"
} else {
"[Symbol.iterator]"
};
let evaluator = PropertyAccessEvaluator::new(db);
let iterator_method_type = evaluator
.resolve_property_access(type_id, symbol_name)
.success_type()?;
// Step 2: Get the iterator type by "calling" the method
// The [Symbol.iterator] property is a method that returns the iterator
use crate::type_queries::get_return_type;
let iterator_type = if is_callable_type(db, iterator_method_type) {
// The symbol is a method - extract its return type
// For [Symbol.iterator], the return type is Iterator<T>
get_return_type(db, iterator_method_type).unwrap_or(TypeId::ANY)
} else {
// The symbol property IS the iterator type (non-callable)
iterator_method_type
};
// Step 3: Find the next() method on the iterator
let next_method_type = evaluator
.resolve_property_access(iterator_type, "next")
.success_type()?;
// Step 4: Extract types from the IteratorResult
extract_iterator_result_types(db, iterator_type, next_method_type, is_async)
}
/// Get iterator info for Array types.
const fn get_array_iterator_info(array_type: TypeId, elem_type: TypeId) -> Option<IteratorInfo> {
// Arrays yield their element type
// The iterator type for Array<T> has:
// - yield: T
// - return: undefined
// - next: accepts undefined (TNext = undefined)
Some(IteratorInfo {
iterator_type: array_type,
yield_type: elem_type,
return_type: TypeId::UNDEFINED,
next_type: TypeId::UNDEFINED,
})
}
/// Get iterator info for Tuple types.
fn get_tuple_iterator_info(db: &dyn TypeDatabase, tuple_type: TypeId) -> Option<IteratorInfo> {
// Tuples yield the union of their element types
match db.lookup(tuple_type) {
Some(TypeData::Tuple(list_id)) => {
let elements = db.tuple_list(list_id);
let elem_types: Vec<TypeId> = elements.iter().map(|e| e.type_id).collect();
// Union of all element types (or Never if empty)
let yield_type = if elem_types.is_empty() {
TypeId::NEVER
} else {
elem_types
.into_iter()
.reduce(|acc, elem| db.union2(acc, elem))
.unwrap_or(TypeId::NEVER)
};
Some(IteratorInfo {
iterator_type: tuple_type,
yield_type,
return_type: TypeId::UNDEFINED,
next_type: TypeId::UNDEFINED,
})
}
_ => None,
}
}
/// Extract T from a Promise<T> type.
///
/// Handles two representations:
/// 1. `Application(base=PROMISE_BASE`, args=[T]) — synthetic promise
/// 2. Object types with a `then` callback — structurally promise-like
///
/// Returns the inner type T, or None if not a promise type.
fn extract_promise_inner_type(
db: &dyn crate::db::QueryDatabase,
type_id: TypeId,
) -> Option<TypeId> {
match db.lookup(type_id) {
// Application: Promise<T> where base is PROMISE_BASE
Some(TypeData::Application(app_id)) => {
let app = db.type_application(app_id);
if app.base == TypeId::PROMISE_BASE {
return app.args.first().copied();
}
// For other applications, the first arg is typically T
// e.g. PromiseLike<T>, custom Promise subclasses
app.args.first().copied()
}
// Object type: look for then(onfulfilled: (value: T) => any) => any
Some(TypeData::Object(shape_id)) => {
let shape = db.object_shape(shape_id);
let then_atom = db.intern_string("then");
let then_prop = PropertyInfo::find_in_slice(&shape.properties, then_atom)?;
// then is a function: (onfulfilled: (value: T) => any) => any
// Extract T from the first parameter of the first parameter
match db.lookup(then_prop.type_id) {
Some(TypeData::Function(fn_id)) => {
let fn_shape = db.function_shape(fn_id);
let onfulfilled = fn_shape.params.first()?;
// onfulfilled: (value: T) => any — extract T from its first param
match db.lookup(onfulfilled.type_id) {
Some(TypeData::Function(inner_fn_id)) => {
let inner_shape = db.function_shape(inner_fn_id);
inner_shape.params.first().map(|p| p.type_id)
}
_ => None,
}
}
Some(TypeData::Callable(callable_id)) => {
let callable = db.callable_shape(callable_id);
let sig = callable.call_signatures.first()?;
let onfulfilled = sig.params.first()?;
match db.lookup(onfulfilled.type_id) {
Some(TypeData::Function(inner_fn_id)) => {
let inner_shape = db.function_shape(inner_fn_id);
inner_shape.params.first().map(|p| p.type_id)
}
_ => None,
}
}
// If then is itself a type (e.g. structural shorthand), just return it
_ => Some(then_prop.type_id),
}
}
_ => None,
}
}
/// Extract yield/return/next types from the `next()` method's return type.
///
/// For sync iterators: `next()` returns `IteratorResult`<T, `TReturn`>
/// For async iterators: `next()` returns Promise<`IteratorResult`<T, `TReturn`>>
fn extract_iterator_result_types(
db: &dyn crate::db::QueryDatabase,
iterator_type: TypeId,
next_method_type: TypeId,
is_async: bool,
) -> Option<IteratorInfo> {
use crate::type_queries::is_promise_like;
// Get the return type and parameter types of next()
let (next_return_type, next_params) = match db.lookup(next_method_type) {
Some(TypeData::Function(shape_id)) => {
let shape = db.function_shape(shape_id);
(shape.return_type, shape.params.clone())
}
Some(TypeData::Callable(shape_id)) => {
let shape = db.callable_shape(shape_id);
let sig = shape.call_signatures.first()?;
(sig.return_type, sig.params.clone())
}
_ => return None,
};
// For async iterators, unwrap the Promise wrapper
let iterator_result_type = if is_async {
if is_promise_like(db, next_return_type) {
extract_promise_inner_type(db, next_return_type).unwrap_or(next_return_type)
} else {
return None;
}
} else {
next_return_type
};
// Extract yield_type and return_type from IteratorResult<T, TReturn>
// IteratorResult = { value: T, done: false } | { value: TReturn, done: true }
let (yield_type, return_type) = extract_iterator_result_value_types(db, iterator_result_type);
// Extract next_type from the first parameter of next()
let next_type = next_params.first().map_or(TypeId::UNDEFINED, |p| p.type_id);
Some(IteratorInfo {
iterator_type,
yield_type,
return_type,
next_type,
})
}
/// Extract yield and return types from an `IteratorResult` type.
///
/// `IteratorResult`<T, `TReturn`> is typically:
/// { value: T, done: false } | { value: `TReturn`, done: true }
///
/// Returns (`yield_type`, `return_type`). Yield comes from done:false branches,
/// return comes from done:true branches.
fn extract_iterator_result_value_types(
db: &dyn crate::db::QueryDatabase,
iterator_result_type: TypeId,
) -> (TypeId, TypeId) {
let done_atom = db.intern_string("done");
let value_atom = db.intern_string("value");
match db.lookup(iterator_result_type) {
Some(TypeData::Union(list_id)) => {
let members = db.type_list(list_id);
let mut yield_types = Vec::new();
let mut return_types = Vec::new();
for &member_id in members.iter() {
if let Some(TypeData::Object(shape_id)) = db.lookup(member_id) {
let shape = db.object_shape(shape_id);
let value_type = shape
.properties
.iter()
.find(|p| p.name == value_atom)
.map(|p| p.type_id);
let done_type = shape
.properties
.iter()
.find(|p| p.name == done_atom)
.map(|p| p.type_id);
match done_type {
// done: true branch → return_type
Some(t) if t == TypeId::BOOLEAN_TRUE => {
if let Some(v) = value_type {
return_types.push(v);
}
}
// done: false branch → yield_type
Some(t) if t == TypeId::BOOLEAN_FALSE => {
if let Some(v) = value_type {
yield_types.push(v);
}
}
// No done property or unknown done → treat as yield
_ => {
if let Some(v) = value_type {
yield_types.push(v);
}
}
}
}
}
let yield_type = if yield_types.is_empty() {
TypeId::ANY
} else {
yield_types
.into_iter()
.reduce(|acc, t| db.union2(acc, t))
.unwrap_or(TypeId::ANY)
};
let return_type = if return_types.is_empty() {
TypeId::ANY
} else {
return_types
.into_iter()
.reduce(|acc, t| db.union2(acc, t))
.unwrap_or(TypeId::ANY)
};
(yield_type, return_type)
}
Some(TypeData::Object(shape_id)) => {
let shape = db.object_shape(shape_id);
let value_type = shape
.properties
.iter()
.find(|p| p.name == value_atom)
.map_or(TypeId::ANY, |p| p.type_id);
(value_type, TypeId::ANY)
}
_ => (TypeId::ANY, TypeId::ANY),
}
}
/// Get the element type yielded by an async iterable type.
///
/// This is a convenience wrapper around `get_iterator_info` that extracts
/// just the yield type from async iterators.
pub fn get_async_iterable_element_type(
db: &dyn crate::db::QueryDatabase,
type_id: TypeId,
) -> TypeId {
match get_iterator_info(db, type_id, true) {
Some(info) => info.yield_type,
None => match get_iterator_info(db, type_id, false) {
Some(info) => info.yield_type,
None => TypeId::ANY,
},
}
}