jsx_one/codegen/
react.rs

1use swc_common::Span;
2use swc_ecmascript::ast::{
3    ClassDecl, Decl, ExportDecl, FnDecl, Function, ImportDecl, Module, ModuleItem, NamedExport,
4    Param,
5};
6#[derive(Debug, PartialEq, Eq)]
7pub struct ReactCodgen {
8    span: Span,
9    body: Vec<ModuleItem>,
10    mods: Module,
11}
12
13impl ReactCodgen {
14    pub fn new(mods: Module) -> Self {
15        let Module {
16            span,
17            body,
18            shebang,
19        } = mods.clone();
20        Self { span, body, mods }
21    }
22    fn parse_import(&self, import: ImportDecl) -> String {
23        let mut stri = String::new();
24        let ImportDecl {
25            span,
26            specifiers,
27            src,
28            type_only,
29            asserts,
30        } = import;
31        stri = stri + "import ";
32        for ident in 0..specifiers.len() {
33            let i = &specifiers[ident];
34            match i {
35                swc_ecmascript::ast::ImportSpecifier::Named(a) => {
36                    stri = stri + "{ ";
37                    if ident == 0 {
38                        stri = stri + &a.local.sym;
39                    } else {
40                        stri = stri + ", " + &a.local.sym;
41                    }
42                    stri = stri + " }";
43                }
44                swc_ecmascript::ast::ImportSpecifier::Default(a) => {
45                    if ident == 0 {
46                        stri = stri + &a.local.sym;
47                    } else {
48                        stri = stri + ", " + &a.local.sym;
49                    }
50                }
51                swc_ecmascript::ast::ImportSpecifier::Namespace(a) => {
52                    stri = stri + "* as " + &a.local.sym;
53                }
54            }
55        }
56        stri = stri;
57        stri = stri + " from \"" + &src.value + "\";";
58        stri
59    }
60    fn parse_param(param: &Vec<Param>) -> String {
61        let mut mstring = String::new();
62        for parm in param.iter() {
63            let Param {
64                span,
65                decorators,
66                pat,
67            } = parm;
68        }
69        mstring
70    }
71    fn parse_function(&self, fndecl: FnDecl) -> String {
72        let mut mstring = String::new();
73        let FnDecl {
74            ident,
75            declare,
76            function,
77        } = fndecl;
78        mstring = mstring + "export default function " + &ident.sym + "(";
79        let Function {
80            params,
81            decorators,
82            span,
83            body,
84            is_generator,
85            is_async,
86            type_params,
87            return_type,
88        } = function;
89
90        mstring = mstring + ")" + "{";
91        mstring = mstring + "}";
92        mstring
93    }
94    /// TODO: Complete this after all the functions for parsing is completed (lazy rn)
95    fn parse_export(&self, export: ExportDecl) -> String {
96        let mut mstring = String::new();
97        let ExportDecl { span, decl } = export;
98        match decl {
99            swc_ecmascript::ast::Decl::Class(_) => {
100                todo!()
101            }
102            swc_ecmascript::ast::Decl::Fn(fndecl) => {
103                mstring = mstring + &self.parse_function(fndecl);
104            }
105            swc_ecmascript::ast::Decl::Var(_) => todo!(),
106            swc_ecmascript::ast::Decl::TsInterface(_) => todo!(),
107            swc_ecmascript::ast::Decl::TsTypeAlias(_) => todo!(),
108            swc_ecmascript::ast::Decl::TsEnum(_) => todo!(),
109            swc_ecmascript::ast::Decl::TsModule(_) => todo!(),
110        };
111        mstring
112    }
113    fn parse_decl(&self, decl: &Decl) -> String {
114        let mut mstring = String::new();
115        match decl {
116            Decl::Class(_) => todo!(),
117            Decl::Fn(e) => mstring = mstring + &self.parse_function(e.to_owned()),
118            Decl::Var(_) => todo!(),
119            Decl::TsInterface(_) => todo!(),
120            Decl::TsTypeAlias(_) => todo!(),
121            Decl::TsEnum(_) => todo!(),
122            Decl::TsModule(_) => todo!(),
123        }
124        mstring
125    }
126    pub fn parse_react(&self) -> String {
127        let mut mstring = String::new();
128        let Module {
129            span,
130            body,
131            shebang,
132        } = &self.mods;
133        for bo in body.iter() {
134            match bo {
135                swc_ecmascript::ast::ModuleItem::ModuleDecl(modecl) => match modecl {
136                    swc_ecmascript::ast::ModuleDecl::Import(importdecl) => {
137                        mstring = mstring + &self.parse_import(importdecl.to_owned())
138                    }
139                    swc_ecmascript::ast::ModuleDecl::ExportDecl(e) => {
140                        mstring = mstring + &self.parse_export(e.to_owned());
141                    }
142                    swc_ecmascript::ast::ModuleDecl::ExportNamed(b) => {
143                        let NamedExport {
144                            span,
145                            specifiers,
146                            src,
147                            type_only,
148                            asserts,
149                        } = b;
150                        todo!()
151                    }
152                    swc_ecmascript::ast::ModuleDecl::ExportDefaultDecl(_) => todo!(),
153                    swc_ecmascript::ast::ModuleDecl::ExportDefaultExpr(_) => todo!(),
154                    swc_ecmascript::ast::ModuleDecl::ExportAll(_) => todo!(),
155                    swc_ecmascript::ast::ModuleDecl::TsImportEquals(_) => todo!(),
156                    swc_ecmascript::ast::ModuleDecl::TsExportAssignment(_) => todo!(),
157                    swc_ecmascript::ast::ModuleDecl::TsNamespaceExport(_) => todo!(),
158                },
159                swc_ecmascript::ast::ModuleItem::Stmt(a) => match a {
160                    swc_ecmascript::ast::Stmt::Block(_) => todo!(),
161                    swc_ecmascript::ast::Stmt::Empty(_) => todo!(),
162                    swc_ecmascript::ast::Stmt::Debugger(_) => todo!(),
163                    swc_ecmascript::ast::Stmt::With(_) => todo!(),
164                    swc_ecmascript::ast::Stmt::Return(_) => todo!(),
165                    swc_ecmascript::ast::Stmt::Labeled(_) => todo!(),
166                    swc_ecmascript::ast::Stmt::Break(_) => todo!(),
167                    swc_ecmascript::ast::Stmt::Continue(_) => todo!(),
168                    swc_ecmascript::ast::Stmt::If(_) => todo!(),
169                    swc_ecmascript::ast::Stmt::Switch(_) => todo!(),
170                    swc_ecmascript::ast::Stmt::Throw(_) => todo!(),
171                    swc_ecmascript::ast::Stmt::Try(_) => todo!(),
172                    swc_ecmascript::ast::Stmt::While(_) => todo!(),
173                    swc_ecmascript::ast::Stmt::DoWhile(_) => todo!(),
174                    swc_ecmascript::ast::Stmt::For(_) => todo!(),
175                    swc_ecmascript::ast::Stmt::ForIn(_) => todo!(),
176                    swc_ecmascript::ast::Stmt::ForOf(_) => todo!(),
177                    swc_ecmascript::ast::Stmt::Decl(decl) => {
178                        mstring = mstring + &self.parse_decl(decl);
179                    }
180                    swc_ecmascript::ast::Stmt::Expr(_) => todo!(),
181                },
182            };
183        }
184        mstring
185    }
186}