nargo_type_check/modules/
types.rs1use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, HashSet};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct TypeCheckResult {
11 pub errors: usize,
13 pub checked_files: usize,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct TsConfig {
20 pub compiler_options: Option<CompilerOptions>,
22 pub include: Option<Vec<String>>,
24 pub exclude: Option<Vec<String>>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct CompilerOptions {
31 pub target: Option<String>,
33 pub module: Option<String>,
35 pub module_resolution: Option<String>,
37 pub declaration: Option<bool>,
39 pub strict: Option<bool>,
41 pub no_implicit_any: Option<bool>,
43 pub strict_null_checks: Option<bool>,
45 pub types: Option<Vec<String>>,
47 pub base_url: Option<String>,
49 pub paths: Option<HashMap<String, Vec<String>>>,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct TypeError {
56 pub position: (usize, usize),
58 pub message: String,
60 pub file_name: Option<String>,
62 pub line: Option<usize>,
64 pub column: Option<usize>,
66 pub error_code: Option<String>,
68 pub related_types: Option<Vec<String>>,
70 pub suggestion: Option<String>,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq)]
76pub enum Type {
77 Any,
79 Void,
80 Never,
81 Unknown,
82 Number,
83 String,
84 Boolean,
85 Symbol,
86 BigInt,
87 Array(Box<Type>),
89 Object(HashMap<String, Type>),
90 Function(Vec<Type>, Box<Type>),
91 TypeVar(String),
93 Interface(String),
95 GenericInterface(String, Vec<Type>),
97 TypeAlias(String),
99 GenericTypeAlias(String, Vec<Type>),
101 Union(Vec<Type>),
103 Intersection(Vec<Type>),
105 Conditional(Box<Type>, Box<Type>, Box<Type>, Box<Type>),
107 Mapped(String, Box<Type>, Box<Type>),
109 IndexAccess(Box<Type>, Box<Type>),
111 KeyOf(Box<Type>),
113 Literal(String),
115 Tuple(Vec<Type>),
117 Exclude(Box<Type>, Box<Type>),
119 Extract(Box<Type>, Box<Type>),
121 ThisType(Box<Type>),
123 OmitThisParameter(Box<Type>),
125 ThisParameterType(Box<Type>),
127}
128
129#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct InterfaceDef {
132 pub members: HashMap<String, Type>,
134 pub extends: Vec<String>,
136}
137
138#[derive(Debug, Clone)]
140pub struct TypeEnv {
141 pub variables: HashMap<String, Type>,
143 pub functions: HashMap<String, Type>,
145 pub interfaces: HashMap<String, InterfaceDef>,
147 pub type_aliases: HashMap<String, Type>,
149 pub enums: HashMap<String, HashSet<String>>,
151}
152
153impl TypeEnv {
154 pub fn new() -> Self {
156 Self { variables: HashMap::new(), functions: HashMap::new(), interfaces: HashMap::new(), type_aliases: HashMap::new(), enums: HashMap::new() }
157 }
158
159 pub fn add_variable(&mut self, name: String, ty: Type) {
161 self.variables.insert(name, ty);
162 }
163
164 pub fn add_function(&mut self, name: String, ty: Type) {
166 self.functions.insert(name, ty);
167 }
168
169 pub fn add_interface(&mut self, name: String, members: HashMap<String, Type>, extends: Vec<String>) {
171 self.interfaces.insert(name, InterfaceDef { members, extends });
172 }
173
174 pub fn add_type_alias(&mut self, name: String, ty: Type) {
176 self.type_aliases.insert(name, ty);
177 }
178
179 pub fn add_enum(&mut self, name: String, variants: HashSet<String>) {
181 self.enums.insert(name, variants);
182 }
183
184 pub fn get_variable(&self, name: &str) -> Option<&Type> {
186 self.variables.get(name)
187 }
188
189 pub fn get_function(&self, name: &str) -> Option<&Type> {
191 self.functions.get(name)
192 }
193
194 pub fn get_interface(&self, name: &str) -> Option<&InterfaceDef> {
196 self.interfaces.get(name)
197 }
198
199 pub fn get_type_alias(&self, name: &str) -> Option<&Type> {
201 self.type_aliases.get(name)
202 }
203
204 pub fn get_enum(&self, name: &str) -> Option<&HashSet<String>> {
206 self.enums.get(name)
207 }
208}