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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::iter::repeat;
use indexmap::IndexSet;
use rnode::{FoldWith, NodeId, VisitMut, VisitMutWith};
use rustc_hash::FxHashMap;
use stc_ts_ast_rnode::{RBindingIdent, RIdent, RPat};
use stc_ts_generics::type_param::replacer::TypeParamReplacer;
use stc_ts_types::{
CallSignature, FnParam, Function, FunctionMetadata, Key, KeywordType, PropertySignature, Type, TypeElement, TypeLit, TypeLitMetadata,
TypeParamDecl, Union,
};
use stc_utils::{cache::Freeze, dev_span, ext::TypeVecExt};
use swc_atoms::JsWord;
use swc_common::DUMMY_SP;
use swc_ecma_ast::TsKeywordTypeKind;
pub struct ObjectUnionNormalizer {
pub preserve_specified: bool,
}
impl ObjectUnionNormalizer {
fn find_keys(&self, types: &[Type]) -> IndexSet<Vec<JsWord>> {
types.iter().flat_map(|t| self.find_keys_of_type(t)).collect()
}
fn find_keys_of_type(&self, ty: &Type) -> IndexSet<Vec<JsWord>> {
match ty.normalize() {
Type::TypeLit(ty) => {
let mut keys = IndexSet::default();
for el in ty.members.iter() {
let key = el.non_computed_key().cloned();
let key = match key {
Some(v) => v,
_ => continue,
};
if let TypeElement::Property(PropertySignature {
type_ann: Some(type_ann), ..
}) = el
{
let nested_keys = self.find_keys_of_type(type_ann);
if nested_keys.is_empty() {
keys.insert(vec![key]);
} else {
keys.extend(nested_keys.into_iter().map(|mut keys| {
keys.insert(0, key.clone());
keys
}));
}
continue;
}
keys.insert(vec![key]);
}
return keys;
}
Type::Union(ty) => return self.find_keys(&ty.types),
_ => {}
}
Default::default()
}
fn normalize_fn_types(&mut self, ty: &mut Type) {
if !ty.is_union_type() {
return;
}
ty.freeze();
let u = match ty.as_union_type_mut() {
Some(v) => v,
None => return,
};
if u.types.iter().any(|ty| !ty.is_fn_type()) {
return;
}
let mut new_type_params = None;
let mut new_params = vec![];
let mut return_types = vec![];
for ty in &u.types {
if let Type::Function(f) = ty.normalize() {
if new_type_params.is_none() {
new_type_params = f.type_params.clone();
}
for (idx, param) in f.params.iter().enumerate() {
if new_params.len() <= idx {
new_params.extend(repeat(vec![]).take(idx + 1 - new_params.len()));
}
new_params[idx].push(param);
}
return_types.push(*f.ret_ty.clone());
}
}
return_types.dedup_type();
if let Some(ty) = return_types.iter().find(|ty| ty.is_kwd(TsKeywordTypeKind::TsVoidKeyword)) {
return_types = vec![ty.clone()]
}
*ty = Type::Function(Function {
span: u.span,
type_params: new_type_params,
params: new_params
.into_iter()
.map(|params| {
let mut pat = None;
let mut types = vec![];
for param in params {
if pat.is_none() {
pat = Some(param.pat.clone());
}
types.push(*param.ty.clone());
}
types.dedup_type();
let ty = box Type::new_intersection(DUMMY_SP, types);
FnParam {
span: DUMMY_SP,
required: true,
pat: pat.unwrap_or_else(|| {
RPat::Ident(RBindingIdent {
node_id: NodeId::invalid(),
id: RIdent::new("a".into(), DUMMY_SP),
type_ann: None,
})
}),
ty,
}
})
.collect(),
ret_ty: box Type::new_union(u.span, return_types),
metadata: FunctionMetadata {
common: u.metadata.common,
..Default::default()
},
tracker: Default::default(),
})
}
fn normalize_call_signatures(&self, ty: &mut Type) {
if !ty.is_union_type() {
return;
}
ty.freeze();
let u = match ty.as_union_type_mut() {
Some(u) => u,
_ => return,
};
if u.types.iter().any(|ty| !ty.is_type_lit()) {
return;
}
let mut inexact = false;
let mut prev_specified = false;
let mut new_type_params = FxHashMap::<_, TypeParamDecl>::default();
let mut new_params = FxHashMap::<_, Vec<_>>::default();
let mut new_return_types = FxHashMap::<_, Vec<_>>::default();
let mut extra_members = vec![];
for (type_idx, ty) in u.types.iter().enumerate() {
if let Type::TypeLit(ty) = ty.normalize() {
inexact |= ty.metadata.inexact;
prev_specified |= ty.metadata.specified;
for (i, m) in ty.members.iter().enumerate() {
match m {
TypeElement::Call(CallSignature {
type_params,
params,
ret_ty,
..
}) => {
let mut params = params.clone();
if let Some(type_params) = type_params {
if let Some(prev) = new_type_params.get(&i) {
let inferred = prev
.params
.iter()
.cloned()
.map(Type::Param)
.zip(type_params.params.iter())
.map(|(prev, new)| (new.name.clone(), prev))
.collect();
params = params.fold_with(&mut TypeParamReplacer {
inferred,
include_type_params: true,
});
} else {
new_type_params.entry(i).or_insert_with(|| type_params.clone());
}
}
for (idx, param) in params.into_iter().enumerate() {
let new_params = new_params.entry(i).or_default();
if new_params.len() <= idx {
new_params.extend(repeat(vec![]).take(idx + 1 - new_params.len()));
}
new_params[idx].push(param);
}
new_return_types.entry(i).or_default().extend(ret_ty.clone().map(|v| *v));
}
_ => {
if extra_members.len() <= type_idx {
extra_members.extend(repeat(vec![]).take(type_idx + 1 - extra_members.len()));
}
extra_members[type_idx].push(m.clone())
}
}
}
}
}
if new_params.is_empty() {
return;
}
let mut members = vec![];
for (i, new_params) in new_params {
let mut return_types = new_return_types.remove(&i).unwrap_or_default();
return_types.dedup_type();
if let Some(ty) = return_types.iter().find(|ty| ty.is_kwd(TsKeywordTypeKind::TsVoidKeyword)) {
return_types = vec![ty.clone()]
}
let type_params = new_type_params.remove(&i);
members.push(TypeElement::Call(CallSignature {
span: DUMMY_SP,
ret_ty: Some(box Type::new_union(DUMMY_SP, return_types)),
type_params,
params: new_params
.into_iter()
.map(|params| {
let mut pat = None;
let mut types = vec![];
for param in params {
if pat.is_none() {
pat = Some(param.pat);
}
types.push(*param.ty);
}
types.dedup_type();
let ty = box Type::new_intersection(DUMMY_SP, types);
FnParam {
span: DUMMY_SP,
required: true,
pat: pat.unwrap_or_else(|| {
RPat::Ident(RBindingIdent {
node_id: NodeId::invalid(),
id: RIdent::new("a".into(), DUMMY_SP),
type_ann: None,
})
}),
ty,
}
})
.collect(),
}));
}
let new_lit = TypeLit {
span: u.span,
members,
metadata: TypeLitMetadata {
normalized: true,
inexact,
specified: self.preserve_specified && prev_specified,
..Default::default()
},
tracker: Default::default(),
};
if extra_members.is_empty() {
*ty = Type::TypeLit(new_lit);
return;
}
let new_types = extra_members
.into_iter()
.map(|extra_members| {
let mut new_lit = new_lit.clone();
new_lit.members.extend(extra_members);
new_lit
})
.map(Type::TypeLit)
.collect();
u.types = new_types;
}
fn normalize_keys(&self, types: &mut Vec<Type>) {
fn insert_property_to(ty: &mut Type, keys: &[JsWord], inexact: bool) {
if let Some(ty) = ty.as_union_type_mut() {
for ty in &mut ty.types {
insert_property_to(ty, keys, inexact);
}
return;
}
if let Some(ty) = ty.as_type_lit_mut() {
ty.metadata.inexact |= inexact;
ty.metadata.normalized = true;
match keys.len() {
0 => {
unreachable!()
}
1 => {
let key = &keys[0];
let has_key = ty.members.iter().any(|member| {
if let Some(member_key) = member.non_computed_key() {
*key == *member_key
} else {
false
}
});
if !has_key {
ty.members.push(TypeElement::Property(PropertySignature {
span: DUMMY_SP,
accessibility: None,
readonly: false,
key: Key::Normal {
span: DUMMY_SP,
sym: key.clone(),
},
optional: true,
params: Default::default(),
type_ann: Some(box Type::Keyword(KeywordType {
span: DUMMY_SP,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
})),
type_params: Default::default(),
metadata: Default::default(),
accessor: Default::default(),
}))
}
}
_ => {
let key = &keys[0];
let idx = ty.members.iter().position(|member| {
if let Some(member_key) = member.non_computed_key() {
*key == *member_key
} else {
false
}
});
let idx = match idx {
Some(v) => v,
_ => {
let idx = ty.members.len();
ty.members.push(TypeElement::Property(PropertySignature {
span: DUMMY_SP,
accessibility: None,
readonly: false,
key: Key::Normal {
span: DUMMY_SP,
sym: key.clone(),
},
optional: true,
params: Default::default(),
type_ann: Some(box Type::TypeLit(TypeLit {
span: DUMMY_SP,
members: Default::default(),
metadata: Default::default(),
tracker: Default::default(),
})),
type_params: Default::default(),
metadata: Default::default(),
accessor: Default::default(),
}));
idx
}
};
if let TypeElement::Property(prop) = &mut ty.members[idx] {
if let Some(ty) = prop.type_ann.as_deref_mut() {
insert_property_to(ty, &keys[1..], inexact)
}
}
}
}
}
}
let _tracing = dev_span!("normalize_keys");
let deep = self.find_keys(&*types);
let inexact = types.iter().any(|ty| match ty.normalize() {
Type::TypeLit(ty) => ty.metadata.inexact,
_ => false,
});
for ty in types.iter_mut() {
for keys in &deep {
insert_property_to(ty, keys, inexact);
}
}
}
}
impl VisitMut<Type> for ObjectUnionNormalizer {
fn visit_mut(&mut self, ty: &mut Type) {
ty.normalize_mut();
ty.visit_mut_children_with(self);
self.normalize_call_signatures(ty);
self.normalize_fn_types(ty);
}
}
impl VisitMut<Union> for ObjectUnionNormalizer {
fn visit_mut(&mut self, u: &mut Union) {
u.visit_mut_children_with(self);
if u.types.iter().all(|ty| !ty.is_type_lit()) {
return;
}
if u.types.len() > 1 {
self.normalize_keys(&mut u.types);
}
}
}