Skip to main content

nargo_type_check/modules/
types.rs

1//! 类型定义模块
2//!
3//! 包含类型检查器使用的各种类型定义
4
5use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, HashSet};
7
8/// 类型检查结果
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct TypeCheckResult {
11    /// 错误数量
12    pub errors: usize,
13    /// 检查的文件数量
14    pub checked_files: usize,
15}
16
17/// tsconfig.json 配置
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct TsConfig {
20    /// 编译选项
21    pub compiler_options: Option<CompilerOptions>,
22    /// 包含的文件
23    pub include: Option<Vec<String>>,
24    /// 排除的文件
25    pub exclude: Option<Vec<String>>,
26}
27
28/// 编译选项
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct CompilerOptions {
31    /// 目标 ECMAScript 版本
32    pub target: Option<String>,
33    /// 模块系统
34    pub module: Option<String>,
35    /// 模块解析策略
36    pub module_resolution: Option<String>,
37    /// 是否生成声明文件
38    pub declaration: Option<bool>,
39    /// 是否启用严格模式
40    pub strict: Option<bool>,
41    /// 是否启用 noImplicitAny
42    pub no_implicit_any: Option<bool>,
43    /// 是否启用 strictNullChecks
44    pub strict_null_checks: Option<bool>,
45    /// 包含的类型声明文件
46    pub types: Option<Vec<String>>,
47    /// 基础 URL
48    pub base_url: Option<String>,
49    /// 路径映射
50    pub paths: Option<HashMap<String, Vec<String>>>,
51}
52
53/// 类型错误
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct TypeError {
56    /// 错误位置
57    pub position: (usize, usize),
58    /// 错误消息
59    pub message: String,
60    /// 文件名
61    pub file_name: Option<String>,
62    /// 行号
63    pub line: Option<usize>,
64    /// 列号
65    pub column: Option<usize>,
66    /// 错误代码
67    pub error_code: Option<String>,
68    /// 相关类型信息
69    pub related_types: Option<Vec<String>>,
70    /// 建议的修复方案
71    pub suggestion: Option<String>,
72}
73
74/// 类型
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub enum Type {
77    /// 基本类型
78    Any,
79    Void,
80    Never,
81    Unknown,
82    Number,
83    String,
84    Boolean,
85    Symbol,
86    BigInt,
87    /// 复合类型
88    Array(Box<Type>),
89    Object(HashMap<String, Type>),
90    Function(Vec<Type>, Box<Type>),
91    /// 类型变量
92    TypeVar(String),
93    /// 接口类型
94    Interface(String),
95    /// 泛型接口类型
96    GenericInterface(String, Vec<Type>),
97    /// 类型别名
98    TypeAlias(String),
99    /// 泛型类型别名
100    GenericTypeAlias(String, Vec<Type>),
101    /// 联合类型
102    Union(Vec<Type>),
103    /// 交叉类型
104    Intersection(Vec<Type>),
105    /// 条件类型: T extends U ? X : Y
106    Conditional(Box<Type>, Box<Type>, Box<Type>, Box<Type>),
107    /// 映射类型: { [K in keyof T]: T[K] }
108    Mapped(String, Box<Type>, Box<Type>),
109    /// 索引访问类型: T[K]
110    IndexAccess(Box<Type>, Box<Type>),
111    /// 关键字类型
112    KeyOf(Box<Type>),
113    /// 字面量类型
114    Literal(String),
115    /// 元组类型
116    Tuple(Vec<Type>),
117    /// 排除类型: Exclude<T, U>
118    Exclude(Box<Type>, Box<Type>),
119    /// 提取类型: Extract<T, U>
120    Extract(Box<Type>, Box<Type>),
121    /// this 类型: ThisType<T>
122    ThisType(Box<Type>),
123    /// 省略 this 参数类型: OmitThisParameter<T>
124    OmitThisParameter(Box<Type>),
125    /// this 参数类型: ThisParameterType<T>
126    ThisParameterType(Box<Type>),
127}
128
129/// 接口定义
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct InterfaceDef {
132    /// 接口成员
133    pub members: HashMap<String, Type>,
134    /// 继承的接口
135    pub extends: Vec<String>,
136}
137
138/// 类型环境
139#[derive(Debug, Clone)]
140pub struct TypeEnv {
141    /// 变量类型映射
142    pub variables: HashMap<String, Type>,
143    /// 函数类型映射
144    pub functions: HashMap<String, Type>,
145    /// 接口定义
146    pub interfaces: HashMap<String, InterfaceDef>,
147    /// 类型别名
148    pub type_aliases: HashMap<String, Type>,
149    /// 枚举定义
150    pub enums: HashMap<String, HashSet<String>>,
151}
152
153impl TypeEnv {
154    /// 创建新的类型环境
155    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    /// 添加变量类型
160    pub fn add_variable(&mut self, name: String, ty: Type) {
161        self.variables.insert(name, ty);
162    }
163
164    /// 添加函数类型
165    pub fn add_function(&mut self, name: String, ty: Type) {
166        self.functions.insert(name, ty);
167    }
168
169    /// 添加接口定义
170    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    /// 添加类型别名
175    pub fn add_type_alias(&mut self, name: String, ty: Type) {
176        self.type_aliases.insert(name, ty);
177    }
178
179    /// 添加枚举定义
180    pub fn add_enum(&mut self, name: String, variants: HashSet<String>) {
181        self.enums.insert(name, variants);
182    }
183
184    /// 获取变量类型
185    pub fn get_variable(&self, name: &str) -> Option<&Type> {
186        self.variables.get(name)
187    }
188
189    /// 获取函数类型
190    pub fn get_function(&self, name: &str) -> Option<&Type> {
191        self.functions.get(name)
192    }
193
194    /// 获取接口定义
195    pub fn get_interface(&self, name: &str) -> Option<&InterfaceDef> {
196        self.interfaces.get(name)
197    }
198
199    /// 获取类型别名
200    pub fn get_type_alias(&self, name: &str) -> Option<&Type> {
201        self.type_aliases.get(name)
202    }
203
204    /// 获取枚举定义
205    pub fn get_enum(&self, name: &str) -> Option<&HashSet<String>> {
206        self.enums.get(name)
207    }
208}