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
mod import;
mod tag;

use std::collections::HashMap;
use std::{fs, path::PathBuf};

use crate::common::SQL;
use crate::parser::import::find_sqlx_import_alias;
use swc_common::{
    errors::{ColorConfig, Handler},
    input::StringInput,
    sync::Lrc,
    FileName, MultiSpan, SourceMap,
};
use swc_ecma_ast::{ClassMember, ModuleDecl, ModuleItem, Stmt};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
use tag::{get_sql_from_expr, get_sql_from_var_decl};

fn insert_or_append_sqls(sqls_container: &mut HashMap<PathBuf, Vec<SQL>>, sqls: &Vec<SQL>, file_path: &PathBuf) {
    if sqls_container.contains_key(&*file_path.clone()) {
        let mut value = sqls_container.get(file_path).unwrap().clone();
        value.extend(sqls.clone());
        sqls_container.insert(file_path.clone(), (*value.to_owned()).to_owned());
    } else {
        sqls_container.insert(file_path.clone(), sqls.clone());
    }
}

fn recurse_and_find_sql(
    sqls_container: &mut HashMap<PathBuf, Vec<SQL>>,
    stmt: &Stmt,
    import_alias: &String,
    file_path: &PathBuf,
) -> Option<String> {
    match stmt {
        Stmt::Block(block) => {
            for stmt in &block.stmts {
                recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
            }
            None
        }
        Stmt::With(with_stmt) => {
            let stmt = *with_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &stmt, import_alias, file_path);
            None
        }
        Stmt::Return(rtn) => {
            if let Some(expr) = &rtn.arg {
                let span: MultiSpan = rtn.span.into();
                let sqls = get_sql_from_expr(&None, &expr.clone(), &span, import_alias);
                insert_or_append_sqls(sqls_container, &sqls, file_path);
            }
            None
        }
        Stmt::If(if_stmt) => {
            let stmt = *if_stmt.cons.clone();
            recurse_and_find_sql(sqls_container, &stmt, import_alias, file_path);
            None
        }
        Stmt::Switch(switch_stmt) => {
            for case in &switch_stmt.cases {
                for stmt in &case.cons {
                    recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                }
            }
            None
        }
        Stmt::Throw(throw_stmt) => {
            let span: MultiSpan = throw_stmt.span.into();
            let expr = *throw_stmt.arg.clone();
            let sqls = get_sql_from_expr(&None, &expr, &span, import_alias);
            insert_or_append_sqls(sqls_container, &sqls, file_path);
            None
        }
        Stmt::Try(try_stmt) => {
            // handles statements inside try {}
            for stmt in &try_stmt.block.stmts {
                recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
            }

            // handles statements inside catch {}
            if let Some(stmt) = &try_stmt.handler {
                for stmt in &stmt.body.stmts {
                    recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                }
            }
            None
        }
        Stmt::While(while_stmt) => {
            let body_stmt = *while_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &body_stmt, import_alias, file_path);
            None
        }
        Stmt::DoWhile(do_while_stmt) => {
            let body_stmt = *do_while_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &body_stmt, import_alias, file_path);
            None
        }
        Stmt::For(for_stmt) => {
            let body_stmt = *for_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &body_stmt, import_alias, file_path);
            None
        }
        Stmt::ForIn(for_in_stmt) => {
            let body_stmt = *for_in_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &body_stmt, import_alias, file_path);
            None
        }
        Stmt::ForOf(for_of_stmt) => {
            let body_stmt = *for_of_stmt.body.clone();
            recurse_and_find_sql(sqls_container, &body_stmt, import_alias, file_path);
            None
        }
        Stmt::Decl(decl) => match decl {
            swc_ecma_ast::Decl::Class(class) => {
                let class_body = &class.class.body;
                for body_stmt in class_body {
                    match body_stmt {
                        ClassMember::Constructor(_) => {}
                        ClassMember::Method(class_method) => {
                            if let Some(body) = &class_method.function.body {
                                for stmt in &body.stmts {
                                    recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                                }
                            }
                        }
                        ClassMember::PrivateMethod(private_method) => {
                            if let Some(body) = &private_method.function.body {
                                for stmt in &body.stmts {
                                    recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                                }
                            }
                        }
                        ClassMember::StaticBlock(static_block) => {
                            for stmt in &static_block.body.stmts {
                                recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                            }
                        }
                        _ => {}
                    }
                }
                None
            }
            swc_ecma_ast::Decl::Fn(fun) => {
                if let Some(body) = &fun.function.body {
                    for stmt in &body.stmts {
                        recurse_and_find_sql(sqls_container, stmt, import_alias, file_path);
                    }
                }
                None
            }
            swc_ecma_ast::Decl::Var(var) => {
                for var_decl in &var.decls {
                    let span: MultiSpan = var.span.into();
                    let sqls = get_sql_from_var_decl(var_decl, span, import_alias);
                    insert_or_append_sqls(sqls_container, &sqls, file_path);
                }

                None
            }
            _ => None,
        },
        Stmt::Expr(expr) => {
            let span: MultiSpan = expr.span.into();
            let expr = *expr.expr.clone();
            let sqls = get_sql_from_expr(&None, &expr, &span, import_alias);
            insert_or_append_sqls(sqls_container, &sqls, file_path);
            None
        }
        _ => None,
    }
}

pub fn parse_source(path: &PathBuf) -> (HashMap<PathBuf, Vec<SQL>>, Handler) {
    let contents = fs::read_to_string(path).unwrap();

    let cm: Lrc<SourceMap> = Default::default();
    let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

    let file_path = path.as_os_str().to_str().unwrap().to_string();
    let fm = cm.new_source_file(FileName::Custom(file_path), contents);
    let lexer = Lexer::new(
        Syntax::Typescript(Default::default()),
        Default::default(),
        StringInput::from(&*fm),
        None,
    );

    let mut parser = Parser::new_from(lexer);

    let _module = parser
        .parse_module()
        .map_err(|e| {
            // Unrecoverable fatal error occurred
            e.into_diagnostic(&handler).emit()
        })
        .expect("failed to parse module");

    let mut sqls: HashMap<PathBuf, Vec<SQL>> = HashMap::new();

    let import_alias = _module
        .body
        .iter()
        .filter_map(|line| match line {
            ModuleItem::ModuleDecl(module_decl) => match module_decl {
                ModuleDecl::Import(module_import_decl) => Some(module_import_decl),
                _ => None,
            },
            _ => None,
        })
        .find_map(find_sqlx_import_alias)
        .unwrap_or_else(|| "sql".to_string());

    for item in &_module.body {
        match item {
            ModuleItem::Stmt(stmt) => {
                recurse_and_find_sql(&mut sqls, stmt, &import_alias, path);
            }
            _ => {}
        }
    }

    (sqls, handler)
}