|
| 1 | +use tree_sitter::{Parser, Tree}; |
| 2 | + |
| 3 | +/// Initialize a tree-sitter parser with JavaScript language support |
| 4 | +pub fn create_js_parser() -> Parser { |
| 5 | + let mut parser = Parser::new(); |
| 6 | + let language = tree_sitter_javascript::LANGUAGE.into(); |
| 7 | + parser |
| 8 | + .set_language(&language) |
| 9 | + .expect("Failed to set JavaScript language"); |
| 10 | + parser |
| 11 | +} |
| 12 | + |
| 13 | +/// Parse JavaScript source code and return the syntax tree |
| 14 | +pub fn parse_js(source: &str) -> Option<Tree> { |
| 15 | + let mut parser = create_js_parser(); |
| 16 | + parser.parse(source, None) |
| 17 | +} |
| 18 | + |
| 19 | +#[cfg(test)] |
| 20 | +mod tests { |
| 21 | + use super::*; |
| 22 | + |
| 23 | + #[test] |
| 24 | + fn test_parser_creation() { |
| 25 | + let parser = create_js_parser(); |
| 26 | + // Parser should be created successfully with JavaScript language |
| 27 | + assert!(parser.language().is_some()); |
| 28 | + } |
| 29 | + |
| 30 | + #[test] |
| 31 | + fn test_parse_simple_function() { |
| 32 | + let source = "function add(a, b) { return a + b; }"; |
| 33 | + let tree = parse_js(source).expect("Failed to parse JavaScript"); |
| 34 | + |
| 35 | + let root_node = tree.root_node(); |
| 36 | + assert_eq!(root_node.kind(), "program"); |
| 37 | + assert_eq!(root_node.child_count(), 1); |
| 38 | + |
| 39 | + // First child should be a function declaration |
| 40 | + let function_node = root_node.child(0).expect("Should have a child"); |
| 41 | + assert_eq!(function_node.kind(), "function_declaration"); |
| 42 | + } |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn test_parse_variable_declaration() { |
| 46 | + let source = "const x = 42;"; |
| 47 | + let tree = parse_js(source).expect("Failed to parse JavaScript"); |
| 48 | + |
| 49 | + let root_node = tree.root_node(); |
| 50 | + assert_eq!(root_node.kind(), "program"); |
| 51 | + |
| 52 | + // First child should be a lexical declaration |
| 53 | + let declaration = root_node.child(0).expect("Should have a child"); |
| 54 | + assert_eq!(declaration.kind(), "lexical_declaration"); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_parse_complex_code() { |
| 59 | + let source = r#" |
| 60 | + class Calculator { |
| 61 | + constructor() { |
| 62 | + this.result = 0; |
| 63 | + } |
| 64 | + |
| 65 | + add(x, y) { |
| 66 | + return x + y; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const calc = new Calculator(); |
| 71 | + console.log(calc.add(5, 3)); |
| 72 | + "#; |
| 73 | + |
| 74 | + let tree = parse_js(source).expect("Failed to parse JavaScript"); |
| 75 | + let root_node = tree.root_node(); |
| 76 | + |
| 77 | + assert_eq!(root_node.kind(), "program"); |
| 78 | + // Should have at least 3 children: class, const declaration, expression statement |
| 79 | + assert!(root_node.child_count() >= 3); |
| 80 | + |
| 81 | + // Verify the class declaration |
| 82 | + let class_node = root_node.child(0).expect("Should have first child"); |
| 83 | + assert_eq!(class_node.kind(), "class_declaration"); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_parse_arrow_function() { |
| 88 | + let source = "const multiply = (a, b) => a * b;"; |
| 89 | + let tree = parse_js(source).expect("Failed to parse JavaScript"); |
| 90 | + |
| 91 | + let root_node = tree.root_node(); |
| 92 | + assert_eq!(root_node.kind(), "program"); |
| 93 | + |
| 94 | + let declaration = root_node.child(0).expect("Should have a child"); |
| 95 | + assert_eq!(declaration.kind(), "lexical_declaration"); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_parse_with_syntax_error() { |
| 100 | + // This should still produce a tree, but with error nodes |
| 101 | + let source = "function broken( { return x; }"; |
| 102 | + let tree = parse_js(source); |
| 103 | + |
| 104 | + assert!(tree.is_some()); |
| 105 | + let tree = tree.unwrap(); |
| 106 | + assert!(tree.root_node().has_error()); |
| 107 | + } |
| 108 | +} |
0 commit comments