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
use std::{str::FromStr, sync::Arc};
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use stc_ts_errors::{Error, ErrorKind};
use stc_ts_type_ops::Fix;
use stc_ts_types::{Id, Type};
use stc_utils::{cache::Freeze, dev_span};
use string_enum::StringEnum;
use swc_atoms::JsWord;
use swc_common::{Span, Spanned, DUMMY_SP};
use swc_ecma_ast::EsVersion;
pub use self::marks::{MarkExt, Marks};
mod marks;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct BuiltIn {
vars: FxHashMap<JsWord, Type>,
types: FxHashMap<JsWord, Type>,
}
impl BuiltIn {
pub fn new(vars: FxHashMap<JsWord, Type>, types: FxHashMap<JsWord, Type>) -> Self {
BuiltIn { vars, types }
}
}
#[derive(Debug, Clone)]
pub struct Env {
stable: StableEnv,
rule: Rule,
target: EsVersion,
module: ModuleConfig,
builtin: Arc<BuiltIn>,
global_types: Arc<Mutex<FxHashMap<JsWord, Type>>>,
global_vars: Arc<Mutex<FxHashMap<JsWord, Type>>>,
}
impl Env {
pub fn new(env: StableEnv, rule: Rule, target: EsVersion, module: ModuleConfig, builtin: Arc<BuiltIn>) -> Self {
Self {
stable: env,
builtin,
target,
module,
global_types: Default::default(),
global_vars: Default::default(),
rule,
}
}
pub const fn shared(&self) -> &StableEnv {
&self.stable
}
pub const fn target(&self) -> EsVersion {
self.target
}
pub const fn module(&self) -> ModuleConfig {
self.module
}
pub const fn rule(&self) -> Rule {
self.rule
}
pub fn declare_global_var(&mut self, name: JsWord, ty: Type) {
ty.assert_clone_cheap();
let _res = self.global_vars.lock().insert(name, ty);
}
pub fn declare_global_type(&mut self, name: JsWord, ty: Type) {
ty.assert_clone_cheap();
match self.get_global_type(ty.span(), &name) {
Ok(prev_ty) => {
self.global_types
.lock()
.insert(name, Type::new_intersection(DUMMY_SP, vec![prev_ty, ty]).fixed().freezed());
}
Err(_) => {
self.global_types.lock().insert(name, ty);
}
}
}
pub fn get_global_var(&self, span: Span, name: &JsWord) -> Result<Type, Error> {
let _tracing = dev_span!("get_global_var");
if let Some(ty) = self.global_vars.lock().get(name) {
debug_assert!(ty.is_clone_cheap(), "{:?}", *ty);
return Ok((*ty).clone());
}
if let Some(v) = self.builtin.vars.get(name) {
debug_assert!(v.is_clone_cheap(), "{:?}", v);
return Ok(v.clone());
}
Err(ErrorKind::NoSuchVar {
span,
name: Id::word(name.clone()),
}
.into())
}
pub fn get_global_type(&self, span: Span, name: &JsWord) -> Result<Type, Error> {
let _tracing = dev_span!("get_global_type");
if let Some(ty) = self.global_types.lock().get(name) {
debug_assert!(ty.is_clone_cheap(), "{:?}", *ty);
return Ok((*ty).clone());
}
if let Some(ty) = self.builtin.types.get(name) {
debug_assert!(ty.is_clone_cheap(), "{:?}", ty);
return Ok(ty.clone());
}
Err(ErrorKind::NoSuchType {
span,
name: Id::word(name.clone()),
}
.into())
}
}
#[derive(Clone, Debug)]
pub struct StableEnv {
marks: Marks,
}
impl StableEnv {
pub fn new() -> Self {
let marks = Marks::default();
Self { marks }
}
pub const fn marks(&self) -> Marks {
self.marks
}
}
impl Default for StableEnv {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, StringEnum)]
pub enum ModuleConfig {
CommonJs,
Es6,
Es2015,
Es2020,
Es2022,
None,
Umd,
Amd,
System,
EsNext,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Rule {
pub no_implicit_any: bool,
pub no_implicit_this: bool,
pub always_strict: bool,
pub strict_null_checks: bool,
pub strict_function_types: bool,
pub allow_unreachable_code: bool,
pub allow_unused_labels: bool,
pub no_fallthrough_cases_in_switch: bool,
pub no_implicit_returns: bool,
pub suppress_excess_property_errors: bool,
pub suppress_implicit_any_index_errors: bool,
pub no_strict_generic_checks: bool,
pub no_unused_locals: bool,
pub no_unused_parameters: bool,
pub use_define_property_for_class_fields: bool,
pub no_lib: bool,
pub jsx: JsxMode,
}
#[derive(Debug, Clone, Copy, Default)]
pub enum JsxMode {
#[default]
Preserve,
React,
ReactNative,
ReactJsx,
ReactJsxdev,
}
impl FromStr for JsxMode {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"preserve" => Ok(JsxMode::Preserve),
"react" => Ok(JsxMode::React),
"react-native" => Ok(JsxMode::ReactNative),
"react-jsx" => Ok(JsxMode::ReactJsx),
"react-jsxdev" => Ok(JsxMode::ReactJsxdev),
_ => Err(()),
}
}
}