leankg 0.19.1

Lightweight Knowledge Graph for AI-Assisted Development
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Phase 1: HTTP route extraction from Go and TypeScript frameworks
// FR-B10: route element type | FR-B11: >= 2 Go + >= 2 TS frameworks
// FR-B12: http_calls edges call-site -> route

use crate::db::models::{CodeElement, Relationship};
use serde_json::json;

#[derive(Debug, Clone)]
pub struct RouteInfo {
    pub method: String,
    pub path: String,
    pub handler: String,
    pub framework: String,
    pub file_path: String,
    pub line: u32,
}

pub struct RouteExtractor;

impl RouteExtractor {
    pub fn extract_routes(
        source: &[u8],
        tree: &tree_sitter::Tree,
        file_path: &str,
        language: &str,
    ) -> Vec<RouteInfo> {
        let mut routes = Vec::new();
        match language {
            "go" => Self::extract_go_routes(source, tree, file_path, &mut routes),
            "typescript" | "javascript" | "tsx" | "jsx" => {
                Self::extract_ts_routes(source, tree, file_path, &mut routes);
            }
            _ => {}
        }
        routes
    }

    pub fn routes_to_elements_and_rels(
        routes: &[RouteInfo],
    ) -> (Vec<CodeElement>, Vec<Relationship>) {
        let mut elements = Vec::new();
        let mut relationships = Vec::new();

        for route in routes {
            let qualified = format!(
                "{}::{} {} {}",
                route.file_path, route.method, route.path, route.handler
            );
            let route_name = format!("{} {}", route.method, route.path);

            elements.push(CodeElement {
                qualified_name: qualified.clone(),
                element_type: "route".to_string(),
                name: route_name,
                file_path: route.file_path.clone(),
                line_start: route.line,
                line_end: route.line,
                language: route.framework.clone(),
                metadata: json!({
                    "method": route.method,
                    "path": route.path,
                    "handler": route.handler,
                    "framework": route.framework,
                }),
                ..Default::default()
            });

            let handler_qualified = format!("{}::{}", route.file_path, route.handler);
            relationships.push(Relationship {
                source_qualified: handler_qualified,
                target_qualified: qualified.clone(),
                rel_type: "http_calls".to_string(),
                confidence: 0.90,
                metadata: json!({
                    "method": route.method,
                    "path": route.path,
                    "framework": route.framework,
                    "line": route.line,
                }),
                ..Default::default()
            });

            relationships.push(Relationship {
                source_qualified: route.file_path.to_string(),
                target_qualified: qualified,
                rel_type: "defines_route".to_string(),
                confidence: 0.90,
                metadata: json!({"method": route.method, "framework": route.framework}),
                ..Default::default()
            });
        }

        (elements, relationships)
    }

    // Go routes

    fn extract_go_routes(
        source: &[u8],
        tree: &tree_sitter::Tree,
        file_path: &str,
        routes: &mut Vec<RouteInfo>,
    ) {
        Self::walk_go_node(source, tree.root_node(), file_path, routes);
    }

    fn walk_go_node(
        source: &[u8],
        node: tree_sitter::Node,
        file_path: &str,
        routes: &mut Vec<RouteInfo>,
    ) {
        if node.kind() == "call_expression" {
            if let Some(route) = Self::try_go_route(source, node, file_path) {
                routes.push(route);
            }
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            Self::walk_go_node(source, child, file_path, routes);
        }
    }

    fn try_go_route(source: &[u8], node: tree_sitter::Node, file_path: &str) -> Option<RouteInfo> {
        let mut selector: Option<(String, String)> = None;
        let mut args: Vec<String> = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            let kind = child.kind();
            if kind == "selector_expression" {
                selector = Some(Self::extract_go_selector(source, child));
            }
            if kind == "argument_list" {
                args = Self::extract_go_args(source, child);
            }
        }
        let (receiver, method) = selector?;
        if args.is_empty() {
            return None;
        }
        let http_method = match method.to_lowercase().as_str() {
            "get" | "handlefunc" | "handle" => "GET",
            "post" => "POST",
            "put" => "PUT",
            "delete" | "del" => "DELETE",
            "patch" => "PATCH",
            "head" => "HEAD",
            "options" => "OPTIONS",
            _ => return None,
        };
        let handler = if args.len() > 1 {
            Self::normalize_go_handler(&args[1])
        } else {
            "anonymous".to_string()
        };

        Some(RouteInfo {
            method: http_method.to_string(),
            path: Self::clean_path(&args[0]),
            handler,
            framework: Self::detect_go_framework(&receiver, &method),
            file_path: file_path.to_string(),
            line: node.start_position().row as u32 + 1,
        })
    }

    fn extract_go_selector(source: &[u8], node: tree_sitter::Node) -> (String, String) {
        let mut receiver = String::new();
        let mut method = String::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            let kind = child.kind();
            let text = Self::node_text(source, child);
            if kind == "identifier" || kind == "field_identifier" {
                if receiver.is_empty() {
                    receiver = text;
                } else {
                    method = text;
                }
            } else if kind == "selector_expression" {
                let (rec, _) = Self::extract_go_selector(source, child);
                receiver = rec;
            }
        }
        (receiver, method)
    }

    fn extract_go_args(source: &[u8], node: tree_sitter::Node) -> Vec<String> {
        let mut args = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            let kind = child.kind();
            let text = Self::node_text(source, child).trim_matches('"').to_string();
            if text.is_empty() || text == "(" || text == ")" || text == "," {
                continue;
            }
            if kind == "interpreted_string_literal"
                || kind == "raw_string_literal"
                || kind == "identifier"
                || kind == "selector_expression"
            {
                args.push(text);
            }
        }
        args
    }

    fn detect_go_framework(receiver: &str, method: &str) -> String {
        let r = receiver.to_lowercase();
        if r == "r" || r == "router" {
            return "chi".to_string();
        }
        if r == "e" || r == "echo" {
            return "echo".to_string();
        }
        if r == "g" || r == "gin" {
            return "gin".to_string();
        }
        if receiver == "http" && (method == "HandleFunc" || method == "Handle") {
            return "net/http".to_string();
        }
        "net/http".to_string()
    }

    fn normalize_go_handler(raw: &str) -> String {
        if let Some(dot_pos) = raw.rfind('.') {
            let after = &raw[dot_pos + 1..];
            if let Some(p) = after.find('(') {
                after[..p].to_string()
            } else {
                after.to_string()
            }
        } else if let Some(p) = raw.find('(') {
            raw[..p].to_string()
        } else {
            raw.to_string()
        }
    }

    // TypeScript routes

    fn extract_ts_routes(
        source: &[u8],
        tree: &tree_sitter::Tree,
        file_path: &str,
        routes: &mut Vec<RouteInfo>,
    ) {
        Self::walk_ts_node(source, tree.root_node(), file_path, routes);
    }

    fn walk_ts_node(
        source: &[u8],
        node: tree_sitter::Node,
        file_path: &str,
        routes: &mut Vec<RouteInfo>,
    ) {
        let kind = node.kind();
        if kind == "call_expression" {
            if let Some(route) = Self::try_ts_route(source, node, file_path) {
                routes.push(route);
            }
            if let Some(route) = Self::try_ts_mount(source, node, file_path) {
                routes.push(route);
            }
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            Self::walk_ts_node(source, child, file_path, routes);
        }
    }

    fn try_ts_route(source: &[u8], node: tree_sitter::Node, file_path: &str) -> Option<RouteInfo> {
        let method_call = match node.child(0) {
            Some(c) if c.kind() == "member_expression" => Self::node_text(source, c),
            _ => return None,
        };

        // Defer middleware `use(...)` calls to try_ts_mount to avoid duplication.
        let (_, http_method) = Self::parse_ts_member_expr(&method_call)?;
        if http_method == "USE" {
            return None;
        }

        let mut strings: Vec<String> = Vec::new();
        let mut handler: Option<String> = None;
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() != "arguments" {
                continue;
            }
            let mut inner = child.walk();
            let mut arg_index = 0usize;
            for arg in child.children(&mut inner) {
                if Self::is_punctuation(arg.kind()) {
                    continue;
                }
                let ak = arg.kind();
                match ak {
                    "string" | "template_string" => {
                        let raw = Self::node_text(source, arg);
                        strings.push(Self::unquote(&raw));
                    }
                    "identifier" | "member_expression"
                        if (arg_index == 1 || (arg_index == 0 && strings.is_empty()))
                            && handler.is_none() =>
                    {
                        handler = Some(Self::node_text(source, arg));
                    }
                    "arrow_function" | "function" | "function_expression"
                        if (arg_index == 1 || (arg_index == 0 && strings.is_empty()))
                            && handler.is_none() =>
                    {
                        handler = Some("anonymous".to_string());
                    }
                    _ => {}
                }
                arg_index += 1;
            }
        }

        if strings.is_empty() {
            return None;
        }

        let (receiver, method) = {
            let dot = method_call.rfind('.')?;
            (
                method_call[..dot].to_string(),
                method_call[dot + 1..].to_uppercase(),
            )
        };

        Some(RouteInfo {
            method,
            path: Self::clean_path(&strings[0]),
            handler: handler.unwrap_or_else(|| "anonymous".to_string()),
            framework: Self::detect_ts_framework(&receiver),
            file_path: file_path.to_string(),
            line: node.start_position().row as u32 + 1,
        })
    }

    fn try_ts_mount(source: &[u8], node: tree_sitter::Node, file_path: &str) -> Option<RouteInfo> {
        let first_child = node.child(0)?;
        if first_child.kind() != "member_expression" {
            return None;
        }
        let text = Self::node_text(source, first_child);
        let (receiver, method) = Self::parse_ts_member_expr(&text)?;
        if method != "USE" {
            return None;
        }

        let mut strings: Vec<String> = Vec::new();
        let mut handler: Option<String> = None;
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() != "arguments" {
                continue;
            }
            let mut inner = child.walk();
            let mut arg_index = 0usize;
            for arg in child.children(&mut inner) {
                if Self::is_punctuation(arg.kind()) {
                    continue;
                }
                match arg.kind() {
                    "string" | "template_string" => {
                        let raw = Self::node_text(source, arg);
                        strings.push(Self::unquote(&raw));
                    }
                    "identifier" | "member_expression"
                        if (arg_index == 1 || (arg_index == 0 && strings.is_empty()))
                            && handler.is_none() =>
                    {
                        handler = Some(Self::node_text(source, arg));
                    }
                    "arrow_function" | "function" | "function_expression"
                        if (arg_index == 1 || (arg_index == 0 && strings.is_empty()))
                            && handler.is_none() =>
                    {
                        handler = Some("anonymous".to_string());
                    }
                    _ => {}
                }
                arg_index += 1;
            }
        }

        let first_path = strings
            .into_iter()
            .next()
            .map(|p| Self::clean_path(&p))
            .unwrap_or_else(|| "/".to_string());
        Some(RouteInfo {
            method: "USE".to_string(),
            path: first_path,
            handler: handler.unwrap_or_else(|| "router".to_string()),
            framework: Self::detect_ts_framework(&receiver),
            file_path: file_path.to_string(),
            line: node.start_position().row as u32 + 1,
        })
    }

    fn unquote(raw: &str) -> String {
        raw.trim()
            .trim_matches('"')
            .trim_matches('\'')
            .trim_matches('`')
            .to_string()
    }

    fn is_punctuation(kind: &str) -> bool {
        matches!(kind, "(" | ")" | "," | ";" | "{" | "}")
    }

    fn parse_ts_member_expr(text: &str) -> Option<(String, String)> {
        if let Some(dot_pos) = text.rfind('.') {
            let receiver = text[..dot_pos].to_string();
            let method = text[dot_pos + 1..].to_uppercase();
            match method.as_str() {
                "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "USE" => {
                    Some((receiver, method))
                }
                _ => None,
            }
        } else {
            None
        }
    }

    fn detect_ts_framework(receiver: &str) -> String {
        let r = receiver.to_lowercase();
        if r == "app" || r == "application" || r == "router" {
            "express".to_string()
        } else if r == "fastify" || r == "server" {
            "fastify".to_string()
        } else {
            "express".to_string()
        }
    }

    fn node_text(source: &[u8], node: tree_sitter::Node) -> String {
        node.utf8_text(source).unwrap_or("").to_string()
    }

    fn clean_path(path: &str) -> String {
        let p = path.trim().trim_matches('"').trim_matches('\'');
        if !p.starts_with('/') && p != "*" && p != "/" {
            format!("/{}", p)
        } else {
            p.to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_go(source: &str) -> tree_sitter::Tree {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_go::LANGUAGE.into())
            .unwrap();
        parser.parse(source, None).unwrap()
    }

    fn parse_ts(source: &str) -> tree_sitter::Tree {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())
            .unwrap();
        parser.parse(source, None).unwrap()
    }

    #[test]
    fn test_extract_chi_route() {
        let source = r#"
package main
import "github.com/go-chi/chi/v5"
func main() {
    r := chi.NewRouter()
    r.Get("/users/{id}", getUser)
    r.Post("/users", createUser)
}
func getUser(w http.ResponseWriter, r *http.Request) {}
func createUser(w http.ResponseWriter, r *http.Request) {}
"#;
        let tree = parse_go(source);
        let routes = RouteExtractor::extract_routes(source.as_bytes(), &tree, "src/main.go", "go");
        assert!(!routes.is_empty());
        let get_route = routes.iter().find(|r| r.method == "GET");
        assert!(get_route.is_some());
        if let Some(r) = get_route {
            assert!(r.path.contains("users"));
            assert_eq!(r.framework, "chi");
        }
    }

    #[test]
    fn test_extract_gin_route() {
        let source = r#"
package main
import "github.com/gin-gonic/gin"
func main() {
    g := gin.Default()
    g.GET("/health", healthCheck)
    g.POST("/orders", createOrder)
}
func healthCheck(c *gin.Context) {}
func createOrder(c *gin.Context) {}
"#;
        let tree = parse_go(source);
        let routes = RouteExtractor::extract_routes(source.as_bytes(), &tree, "src/main.go", "go");
        assert!(!routes.is_empty());
        let post = routes.iter().find(|r| r.method == "POST");
        assert!(post.is_some());
        if let Some(r) = post {
            assert_eq!(r.framework, "gin");
        }
    }

    #[test]
    fn test_extract_express_routes() {
        let source = r#"
const express = require('express');
const app = express();
app.get('/api/users', getUsers);
app.post('/api/users', createUser);
app.put('/api/users/:id', updateUser);
function getUsers(req, res) {}
function createUser(req, res) {}
function updateUser(req, res) {}
"#;
        let tree = parse_ts(source);
        let routes =
            RouteExtractor::extract_routes(source.as_bytes(), &tree, "src/app.ts", "typescript");
        assert!(!routes.is_empty());
        assert_eq!(routes.len(), 3);
        let put_route = routes.iter().find(|r| r.method == "PUT");
        assert!(put_route.is_some());
        if let Some(r) = put_route {
            assert!(r.path.contains(":id"));
            assert_eq!(r.framework, "express");
        }
    }

    #[test]
    fn test_extract_fastify_route() {
        let source = r#"
import Fastify from 'fastify';
const fastify = Fastify();
fastify.get('/status', async (req, reply) => { return { ok: true }; });
fastify.post('/items', createItem);
function createItem(req, reply) {}
"#;
        let tree = parse_ts(source);
        let routes =
            RouteExtractor::extract_routes(source.as_bytes(), &tree, "src/server.ts", "typescript");
        assert!(!routes.is_empty());
        let get_route = routes.iter().find(|r| r.method == "GET");
        assert!(get_route.is_some());
        if let Some(r) = get_route {
            assert_eq!(r.framework, "fastify");
        }
    }

    #[test]
    fn test_routes_to_elements() {
        let routes = vec![RouteInfo {
            method: "GET".to_string(),
            path: "/health".to_string(),
            handler: "healthCheck".to_string(),
            framework: "chi".to_string(),
            file_path: "src/handler.go".to_string(),
            line: 10,
        }];
        let (elements, relationships) = RouteExtractor::routes_to_elements_and_rels(&routes);
        assert_eq!(elements.len(), 1);
        assert_eq!(elements[0].element_type, "route");
        assert_eq!(elements[0].name, "GET /health");
        assert_eq!(relationships.len(), 2);
        assert_eq!(relationships[0].rel_type, "http_calls");
        assert_eq!(relationships[1].rel_type, "defines_route");
    }

    #[test]
    fn test_parse_ts_member_expr() {
        assert_eq!(
            RouteExtractor::parse_ts_member_expr("app.get"),
            Some(("app".to_string(), "GET".to_string()))
        );
        assert_eq!(
            RouteExtractor::parse_ts_member_expr("fastify.post"),
            Some(("fastify".to_string(), "POST".to_string()))
        );
        assert_eq!(RouteExtractor::parse_ts_member_expr("app.listen"), None);
    }
}