-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathswc_helpers.rs
More file actions
102 lines (88 loc) · 2.68 KB
/
swc_helpers.rs
File metadata and controls
102 lines (88 loc) · 2.68 KB
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
// Copyright 2018-2024 the Deno authors. MIT license.
use deno_ast::swc::ast::*;
use deno_ast::swc::common::DUMMY_SP;
pub fn ident(name: String) -> Ident {
Ident {
span: DUMMY_SP,
sym: name.clone().into(),
optional: false,
}
}
pub fn ts_keyword_type(kind: TsKeywordTypeKind) -> TsType {
TsType::TsKeywordType(TsKeywordType {
span: DUMMY_SP,
kind,
})
}
pub fn is_void_type(return_type: &TsType) -> bool {
is_keyword_type(return_type, TsKeywordTypeKind::TsVoidKeyword)
}
fn is_keyword_type(return_type: &TsType, kind: TsKeywordTypeKind) -> bool {
match return_type {
TsType::TsKeywordType(TsKeywordType { kind: k, .. }) => k == &kind,
_ => false,
}
}
pub fn any_type_ann() -> Box<TsTypeAnn> {
type_ann(ts_keyword_type(TsKeywordTypeKind::TsAnyKeyword))
}
pub fn ts_readonly(ann: TsType) -> TsType {
TsType::TsTypeOperator(TsTypeOperator {
span: DUMMY_SP,
op: TsTypeOperatorOp::ReadOnly,
type_ann: Box::new(ann),
})
}
pub fn type_ann(ts_type: TsType) -> Box<TsTypeAnn> {
Box::new(TsTypeAnn {
span: DUMMY_SP,
type_ann: Box::new(ts_type),
})
}
pub fn type_ref(name: String) -> TsTypeRef {
TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(Ident::new(name.into(), DUMMY_SP)),
type_params: None,
}
}
pub fn ts_lit_type(lit: TsLit) -> TsType {
TsType::TsLitType(TsLitType {
lit,
span: DUMMY_SP,
})
}
pub fn regex_type() -> TsType {
TsType::TsTypeRef(type_ref("RegExp".to_string()))
}
pub fn ts_tuple_element(ts_type: TsType) -> TsTupleElement {
TsTupleElement {
label: None,
span: DUMMY_SP,
ty: Box::new(ts_type),
}
}
pub fn maybe_lit_to_ts_type_const(lit: &Lit) -> Option<TsType> {
match lit {
Lit::Str(lit_str) => Some(ts_lit_type(TsLit::Str(lit_str.clone()))),
Lit::Bool(lit_bool) => Some(ts_lit_type(TsLit::Bool(*lit_bool))),
Lit::Null(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsNullKeyword)),
Lit::Num(lit_num) => Some(ts_lit_type(TsLit::Number(lit_num.clone()))),
Lit::BigInt(lit_bigint) => {
Some(ts_lit_type(TsLit::BigInt(lit_bigint.clone())))
}
Lit::Regex(_) => Some(regex_type()),
Lit::JSXText(_) => None,
}
}
pub fn maybe_lit_to_ts_type(lit: &Lit) -> Option<TsType> {
match lit {
Lit::Str(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsStringKeyword)),
Lit::Bool(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsBooleanKeyword)),
Lit::Null(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsNullKeyword)),
Lit::Num(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsNumberKeyword)),
Lit::BigInt(_) => Some(ts_keyword_type(TsKeywordTypeKind::TsBigIntKeyword)),
Lit::Regex(_) => Some(regex_type()),
Lit::JSXText(_) => None,
}
}