use std::collections::HashMap;
use crate::context::TypeId;
use crate::parse::scope::ScopeId;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ModuleContext {
Global,
Module(std::rc::Rc<str>),
}
#[derive(Clone, Debug, PartialEq)]
pub enum TypeRef {
Boolean,
Number,
String,
BigInt,
Void,
Undefined,
Null,
Any,
Unknown,
Object,
Symbol,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array,
ArrayBuffer,
ArrayBufferView,
DataView,
Promise(Box<TypeRef>),
Array(Box<TypeRef>),
Record(Box<TypeRef>, Box<TypeRef>),
Map(Box<TypeRef>, Box<TypeRef>),
Set(Box<TypeRef>),
Nullable(Box<TypeRef>),
Union(Vec<TypeRef>),
Intersection(Vec<TypeRef>),
Tuple(Vec<TypeRef>),
Function(FunctionSig),
StringLiteral(String),
NumberLiteral(f64),
BooleanLiteral(bool),
Named(String),
GenericInstantiation(String, Vec<TypeRef>),
Date,
RegExp,
Error,
Unresolved(String),
}
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionSig {
pub params: Vec<Param>,
pub return_type: Box<TypeRef>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Param {
pub name: String,
pub type_ref: TypeRef,
pub optional: bool,
pub variadic: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TypeParam {
pub name: String,
pub constraint: Option<TypeRef>,
pub default: Option<TypeRef>,
}
#[derive(Clone, Debug)]
pub struct Module {
pub types: Vec<TypeId>,
pub lib_name: Option<String>,
pub builtin_scope: ScopeId,
pub file_scopes: Vec<ScopeId>,
}
#[derive(Clone, Debug)]
pub struct TypeDeclaration {
pub kind: TypeKind,
pub module_context: ModuleContext,
pub doc: Option<String>,
pub scope_id: crate::parse::scope::ScopeId,
pub exported: bool,
}
#[derive(Clone, Debug)]
pub enum TypeKind {
Class(ClassDecl),
Interface(InterfaceDecl),
TypeAlias(TypeAliasDecl),
StringEnum(StringEnumDecl),
NumericEnum(NumericEnumDecl),
Function(FunctionDecl),
Variable(VariableDecl),
Namespace(NamespaceDecl),
}
#[derive(Clone, Debug)]
pub struct ClassDecl {
pub name: String,
pub js_name: String,
pub type_params: Vec<TypeParam>,
pub extends: Option<TypeRef>,
pub implements: Vec<TypeRef>,
pub is_abstract: bool,
pub members: Vec<Member>,
pub type_module_context: ModuleContext,
}
#[derive(Clone, Debug)]
pub struct InterfaceDecl {
pub name: String,
pub js_name: String,
pub type_params: Vec<TypeParam>,
pub extends: Vec<TypeRef>,
pub members: Vec<Member>,
pub classification: InterfaceClassification,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InterfaceClassification {
ClassLike,
Dictionary,
Unclassified,
}
#[derive(Clone, Debug)]
pub struct TypeAliasDecl {
pub name: String,
pub type_params: Vec<TypeParam>,
pub target: TypeRef,
pub from_module: Option<String>,
}
#[derive(Clone, Debug)]
pub struct StringEnumDecl {
pub name: String,
pub variants: Vec<StringEnumVariant>,
}
#[derive(Clone, Debug)]
pub struct StringEnumVariant {
pub rust_name: String,
pub js_value: String,
}
#[derive(Clone, Debug)]
pub struct NumericEnumDecl {
pub name: String,
pub variants: Vec<NumericEnumVariant>,
}
#[derive(Clone, Debug)]
pub struct NumericEnumVariant {
pub rust_name: String,
pub js_name: String,
pub value: i64,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct FunctionDecl {
pub name: String,
pub js_name: String,
pub type_params: Vec<TypeParam>,
pub params: Vec<Param>,
pub return_type: TypeRef,
pub overloads: Vec<FunctionOverload>,
}
#[derive(Clone, Debug)]
pub struct FunctionOverload {
pub params: Vec<Param>,
pub return_type: TypeRef,
}
#[derive(Clone, Debug)]
pub struct VariableDecl {
pub name: String,
pub js_name: String,
pub type_ref: TypeRef,
pub is_const: bool,
}
#[derive(Clone, Debug)]
pub struct NamespaceDecl {
pub name: String,
pub declarations: Vec<TypeDeclaration>,
pub child_scope: crate::parse::scope::ScopeId,
}
#[derive(Clone, Debug)]
pub enum Member {
Getter(GetterMember),
Setter(SetterMember),
Method(MethodMember),
Constructor(ConstructorMember),
IndexSignature(IndexSigMember),
StaticGetter(StaticGetterMember),
StaticSetter(StaticSetterMember),
StaticMethod(StaticMethodMember),
}
#[derive(Clone, Debug)]
pub struct GetterMember {
pub js_name: String,
pub type_ref: TypeRef,
pub optional: bool,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct SetterMember {
pub js_name: String,
pub type_ref: TypeRef,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct MethodMember {
pub name: String,
pub js_name: String,
pub type_params: Vec<TypeParam>,
pub params: Vec<Param>,
pub return_type: TypeRef,
pub optional: bool,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct ConstructorMember {
pub params: Vec<Param>,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct IndexSigMember {
pub key_type: TypeRef,
pub value_type: TypeRef,
pub readonly: bool,
}
#[derive(Clone, Debug)]
pub struct StaticGetterMember {
pub js_name: String,
pub type_ref: TypeRef,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct StaticSetterMember {
pub js_name: String,
pub type_ref: TypeRef,
pub doc: Option<String>,
}
#[derive(Clone, Debug)]
pub struct StaticMethodMember {
pub name: String,
pub js_name: String,
pub type_params: Vec<TypeParam>,
pub params: Vec<Param>,
pub return_type: TypeRef,
pub doc: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct TypeRegistry {
pub types: HashMap<String, TypeInfo>,
}
#[derive(Clone, Debug)]
pub struct TypeInfo {
pub kind: RegisteredKind,
pub primary_context: ModuleContext,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RegisteredKind {
Class,
Interface,
MergedClassLike,
TypeAlias,
StringEnum,
NumericEnum,
Function,
Variable,
Namespace,
}