swc_estree_compat/swcify/
typescript.rs

1use swc_ecma_ast::{
2    Accessibility, Ident, TsEntityName, TsQualifiedName, TsType, TsTypeAnn, TsTypeParam,
3    TsTypeParamDecl, TsTypeParamInstantiation,
4};
5use swc_estree_ast::{
6    Access, FlowType, SuperTypeParams, TSEntityName, TSQualifiedName, TSType, TSTypeAnnotation,
7    TSTypeParameter, TSTypeParameterDeclaration, TSTypeParameterInstantiation, TypeAnnotOrNoop,
8    TypeParamDeclOrNoop,
9};
10
11use super::Context;
12use crate::{swcify::Swcify, Never};
13
14impl Swcify for TSTypeParameterInstantiation {
15    type Output = TsTypeParamInstantiation;
16
17    fn swcify(self, ctx: &Context) -> Self::Output {
18        TsTypeParamInstantiation {
19            span: ctx.span(&self.base),
20            params: self.params.swcify(ctx),
21        }
22    }
23}
24
25impl Swcify for FlowType {
26    type Output = Never;
27
28    fn swcify(self, _: &Context) -> Self::Output {
29        unreachable!("swc does not support flow types")
30    }
31}
32
33impl Swcify for TypeParamDeclOrNoop {
34    type Output = Option<TsTypeParamDecl>;
35
36    fn swcify(self, ctx: &Context) -> Self::Output {
37        match self {
38            TypeParamDeclOrNoop::Flow(_) => None,
39            TypeParamDeclOrNoop::TS(v) => Some(v.swcify(ctx)),
40            TypeParamDeclOrNoop::Noop(_) => None,
41        }
42    }
43}
44
45impl Swcify for TSTypeParameterDeclaration {
46    type Output = TsTypeParamDecl;
47
48    fn swcify(self, ctx: &Context) -> Self::Output {
49        TsTypeParamDecl {
50            span: ctx.span(&self.base),
51            params: self.params.swcify(ctx),
52        }
53    }
54}
55
56impl Swcify for TSTypeParameter {
57    type Output = TsTypeParam;
58
59    fn swcify(self, ctx: &Context) -> Self::Output {
60        let span = ctx.span(&self.base);
61        TsTypeParam {
62            span,
63            name: Ident::new_no_ctxt(self.name, span),
64            is_in: self.is_in,
65            is_out: self.is_out,
66            is_const: self.is_const,
67            constraint: self.constraint.swcify(ctx),
68            default: self.default.swcify(ctx),
69        }
70    }
71}
72
73impl Swcify for TypeAnnotOrNoop {
74    type Output = Option<TsTypeAnn>;
75
76    fn swcify(self, ctx: &Context) -> Self::Output {
77        match self {
78            TypeAnnotOrNoop::Flow(_) => None,
79            TypeAnnotOrNoop::TS(v) => Some(v.swcify(ctx)),
80            TypeAnnotOrNoop::Noop(_) => None,
81        }
82    }
83}
84
85impl Swcify for TSTypeAnnotation {
86    type Output = TsTypeAnn;
87
88    fn swcify(self, ctx: &Context) -> Self::Output {
89        TsTypeAnn {
90            span: ctx.span(&self.base),
91            type_ann: self.type_annotation.swcify(ctx),
92        }
93    }
94}
95
96impl Swcify for TSType {
97    type Output = Box<TsType>;
98
99    fn swcify(self, _: &Context) -> Self::Output {
100        todo!("swc currently does not support importing typescript module from babel")
101    }
102}
103
104impl Swcify for SuperTypeParams {
105    type Output = TsTypeParamInstantiation;
106
107    fn swcify(self, ctx: &Context) -> Self::Output {
108        match self {
109            SuperTypeParams::Flow(_) => unimplemented!("flow type"),
110            SuperTypeParams::TS(v) => v.swcify(ctx),
111        }
112    }
113}
114
115impl Swcify for Access {
116    type Output = Accessibility;
117
118    fn swcify(self, _: &Context) -> Self::Output {
119        match self {
120            Access::Public => Accessibility::Public,
121            Access::Private => Accessibility::Private,
122            Access::Protected => Accessibility::Protected,
123        }
124    }
125}
126
127impl Swcify for TSEntityName {
128    type Output = TsEntityName;
129
130    fn swcify(self, ctx: &Context) -> Self::Output {
131        match self {
132            TSEntityName::Id(v) => TsEntityName::Ident(v.swcify(ctx).into()),
133            TSEntityName::Qualified(v) => TsEntityName::TsQualifiedName(Box::new(v.swcify(ctx))),
134        }
135    }
136}
137
138impl Swcify for TSQualifiedName {
139    type Output = TsQualifiedName;
140
141    fn swcify(self, ctx: &Context) -> Self::Output {
142        TsQualifiedName {
143            span: ctx.span(&self.base),
144            left: self.left.swcify(ctx),
145            right: self.right.swcify(ctx).into(),
146        }
147    }
148}