1use std::collections::HashMap;
11
12use crate::context::TypeId;
13use crate::parse::scope::ScopeId;
14
15#[derive(Clone, Debug, PartialEq, Eq, Hash)]
24pub enum ModuleContext {
25 Global,
27 Module(std::rc::Rc<str>),
29}
30
31#[derive(Clone, Debug, PartialEq)]
35
36pub enum TypeRef {
37 Boolean,
39 Number,
40 String,
41 BigInt,
42 Void,
43 Undefined,
44 Null,
45 Any,
46 Unknown,
47 Object,
48 Symbol,
49
50 Int8Array,
52 Uint8Array,
53 Uint8ClampedArray,
54 Int16Array,
55 Uint16Array,
56 Int32Array,
57 Uint32Array,
58 Float32Array,
59 Float64Array,
60 BigInt64Array,
61 BigUint64Array,
62 ArrayBuffer,
63 ArrayBufferView,
64 DataView,
65
66 Promise(Box<TypeRef>),
68 Array(Box<TypeRef>),
69 Record(Box<TypeRef>, Box<TypeRef>),
70 Map(Box<TypeRef>, Box<TypeRef>),
71 Set(Box<TypeRef>),
72
73 Nullable(Box<TypeRef>),
75 Union(Vec<TypeRef>),
76 Intersection(Vec<TypeRef>),
77 Tuple(Vec<TypeRef>),
78 Function(FunctionSig),
79
80 StringLiteral(String),
82 NumberLiteral(f64),
83 BooleanLiteral(bool),
84
85 Named(String),
88 GenericInstantiation(String, Vec<TypeRef>),
90
91 Date,
93 RegExp,
94 Error,
95
96 Unresolved(String),
100}
101
102#[derive(Clone, Debug, PartialEq)]
105
106pub struct FunctionSig {
107 pub params: Vec<Param>,
108 pub return_type: Box<TypeRef>,
109}
110
111#[derive(Clone, Debug, PartialEq)]
112
113pub struct Param {
114 pub name: String,
115 pub type_ref: TypeRef,
116 pub optional: bool,
117 pub variadic: bool,
118}
119
120#[derive(Clone, Debug, PartialEq)]
123
124pub struct TypeParam {
125 pub name: String,
126 pub constraint: Option<TypeRef>,
128 pub default: Option<TypeRef>,
130}
131
132#[derive(Clone, Debug)]
136
137pub struct Module {
138 pub types: Vec<TypeId>,
140 pub lib_name: Option<String>,
142 pub builtin_scope: ScopeId,
144 pub file_scopes: Vec<ScopeId>,
146}
147
148#[derive(Clone, Debug)]
149
150pub struct TypeDeclaration {
151 pub kind: TypeKind,
152 pub module_context: ModuleContext,
153 pub doc: Option<String>,
154 pub scope_id: crate::parse::scope::ScopeId,
156 pub exported: bool,
160}
161
162#[derive(Clone, Debug)]
163
164pub enum TypeKind {
165 Class(ClassDecl),
166 Interface(InterfaceDecl),
167 TypeAlias(TypeAliasDecl),
168 StringEnum(StringEnumDecl),
169 NumericEnum(NumericEnumDecl),
170 Function(FunctionDecl),
171 Variable(VariableDecl),
172 Namespace(NamespaceDecl),
173}
174
175#[derive(Clone, Debug)]
178
179pub struct ClassDecl {
180 pub name: String,
181 pub js_name: String,
183 pub type_params: Vec<TypeParam>,
184 pub extends: Option<TypeRef>,
186 pub implements: Vec<TypeRef>,
187 pub is_abstract: bool,
188 pub members: Vec<Member>,
189 pub type_module_context: ModuleContext,
191}
192
193#[derive(Clone, Debug)]
196
197pub struct InterfaceDecl {
198 pub name: String,
199 pub js_name: String,
200 pub type_params: Vec<TypeParam>,
201 pub extends: Vec<TypeRef>,
202 pub members: Vec<Member>,
203 pub classification: InterfaceClassification,
205}
206
207#[derive(Clone, Debug, PartialEq, Eq)]
208
209pub enum InterfaceClassification {
210 ClassLike,
212 Dictionary,
214 Unclassified,
216}
217
218#[derive(Clone, Debug)]
221
222pub struct TypeAliasDecl {
223 pub name: String,
224 pub type_params: Vec<TypeParam>,
225 pub target: TypeRef,
226 pub from_module: Option<String>,
229}
230
231#[derive(Clone, Debug)]
234
235pub struct StringEnumDecl {
236 pub name: String,
237 pub variants: Vec<StringEnumVariant>,
238}
239
240#[derive(Clone, Debug)]
241
242pub struct StringEnumVariant {
243 pub rust_name: String,
245 pub js_value: String,
247}
248
249#[derive(Clone, Debug)]
252
253pub struct NumericEnumDecl {
254 pub name: String,
255 pub variants: Vec<NumericEnumVariant>,
256}
257
258#[derive(Clone, Debug)]
259
260pub struct NumericEnumVariant {
261 pub rust_name: String,
263 pub js_name: String,
265 pub value: i64,
267 pub doc: Option<String>,
269}
270
271#[derive(Clone, Debug)]
274
275pub struct FunctionDecl {
276 pub name: String,
277 pub js_name: String,
278 pub type_params: Vec<TypeParam>,
279 pub params: Vec<Param>,
280 pub return_type: TypeRef,
281 pub overloads: Vec<FunctionOverload>,
282}
283
284#[derive(Clone, Debug)]
285
286pub struct FunctionOverload {
287 pub params: Vec<Param>,
288 pub return_type: TypeRef,
289}
290
291#[derive(Clone, Debug)]
294
295pub struct VariableDecl {
296 pub name: String,
297 pub js_name: String,
298 pub type_ref: TypeRef,
299 pub is_const: bool,
300}
301
302#[derive(Clone, Debug)]
305
306pub struct NamespaceDecl {
307 pub name: String,
308 pub declarations: Vec<TypeDeclaration>,
309 pub child_scope: crate::parse::scope::ScopeId,
311}
312
313#[derive(Clone, Debug)]
316
317pub enum Member {
318 Getter(GetterMember),
319 Setter(SetterMember),
320 Method(MethodMember),
321 Constructor(ConstructorMember),
322 IndexSignature(IndexSigMember),
323 StaticGetter(StaticGetterMember),
324 StaticSetter(StaticSetterMember),
325 StaticMethod(StaticMethodMember),
326}
327
328#[derive(Clone, Debug)]
332
333pub struct GetterMember {
334 pub js_name: String,
336 pub type_ref: TypeRef,
338 pub optional: bool,
340 pub doc: Option<String>,
341}
342
343#[derive(Clone, Debug)]
347
348pub struct SetterMember {
349 pub js_name: String,
351 pub type_ref: TypeRef,
353 pub doc: Option<String>,
354}
355
356#[derive(Clone, Debug)]
357
358pub struct MethodMember {
359 pub name: String,
360 pub js_name: String,
361 pub type_params: Vec<TypeParam>,
362 pub params: Vec<Param>,
363 pub return_type: TypeRef,
364 pub optional: bool,
365 pub doc: Option<String>,
366}
367
368#[derive(Clone, Debug)]
369
370pub struct ConstructorMember {
371 pub params: Vec<Param>,
372 pub doc: Option<String>,
373}
374
375#[derive(Clone, Debug)]
376
377pub struct IndexSigMember {
378 pub key_type: TypeRef,
379 pub value_type: TypeRef,
380 pub readonly: bool,
381}
382
383#[derive(Clone, Debug)]
387
388pub struct StaticGetterMember {
389 pub js_name: String,
391 pub type_ref: TypeRef,
392 pub doc: Option<String>,
393}
394
395#[derive(Clone, Debug)]
399
400pub struct StaticSetterMember {
401 pub js_name: String,
403 pub type_ref: TypeRef,
404 pub doc: Option<String>,
405}
406
407#[derive(Clone, Debug)]
408
409pub struct StaticMethodMember {
410 pub name: String,
411 pub js_name: String,
412 pub type_params: Vec<TypeParam>,
413 pub params: Vec<Param>,
414 pub return_type: TypeRef,
415 pub doc: Option<String>,
416}
417
418#[derive(Clone, Debug, Default)]
423
424pub struct TypeRegistry {
425 pub types: HashMap<String, TypeInfo>,
426}
427
428#[derive(Clone, Debug)]
429
430pub struct TypeInfo {
431 pub kind: RegisteredKind,
432 pub primary_context: ModuleContext,
434}
435
436#[derive(Clone, Debug, PartialEq, Eq)]
437
438pub enum RegisteredKind {
439 Class,
440 Interface,
441 MergedClassLike,
443 TypeAlias,
444 StringEnum,
446 NumericEnum,
448 Function,
449 Variable,
450 Namespace,
451}