pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
#![cfg_attr(coverage_nightly, coverage(off))]
//! JavaScript language support module
//!
//! This module provides support for analyzing JavaScript code, including
//! AST parsing, syntax analysis, and code structure extraction.

use crate::services::context::AstItem;
use anyhow::Result;
use std::path::Path;

#[cfg(feature = "typescript-ast")]
use crate::services::ast_typescript::analyze_typescript_file;

/// Visitor for JavaScript AST analysis
pub struct JavaScriptAstVisitor {
    #[allow(dead_code)]
    path: std::path::PathBuf,
}

impl JavaScriptAstVisitor {
    /// Create a new JavaScript AST visitor
    pub fn new(path: &Path) -> Self {
        Self {
            path: path.to_path_buf(),
        }
    }

    /// Analyze JavaScript source code
    ///
    /// This method parses JavaScript source code and extracts AST items.
    /// It creates a temporary file to leverage the existing TypeScript parser
    /// (which also handles JavaScript).
    #[cfg(feature = "typescript-ast")]
    pub fn analyze_javascript_source(&self, source: &str) -> Result<Vec<AstItem>> {
        // Create temporary file with .js extension (builder pattern)
        let temp_file = tempfile::Builder::new()
            .suffix(".js")
            .tempfile()
            .map_err(|e| anyhow::anyhow!("Failed to create temp file: {}", e))?;

        // Write source code to ephemeral file
        std::fs::write(temp_file.path(), source.as_bytes())
            .map_err(|e| anyhow::anyhow!("Failed to write source to temp file: {}", e))?;

        // Use existing TypeScript parser (handles both TS and JS)
        let runtime = tokio::runtime::Runtime::new()
            .map_err(|e| anyhow::anyhow!("Failed to create runtime: {}", e))?;

        runtime.block_on(async {
            let context = analyze_typescript_file(temp_file.path())
                .await
                .map_err(|e| anyhow::anyhow!("JavaScript parsing failed: {}", e))?;
            Ok(context.items)
        })
    }

    /// Analyze JavaScript source code (feature not enabled)
    #[cfg(not(feature = "typescript-ast"))]
    pub fn analyze_javascript_source(&self, _source: &str) -> Result<Vec<AstItem>> {
        // Return empty result when TypeScript AST feature is not enabled
        Ok(Vec::new())
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn test_javascript_ast_visitor_new() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);
        // Verify the visitor is created without panicking
        let _ = visitor;
    }

    #[test]
    fn test_javascript_ast_visitor_new_with_complex_path() {
        let path = Path::new("/src/components/App.js");
        let visitor = JavaScriptAstVisitor::new(path);
        let _ = visitor;
    }

    #[test]
    fn test_javascript_ast_visitor_new_with_relative_path() {
        let path = Path::new("./src/index.js");
        let visitor = JavaScriptAstVisitor::new(path);
        let _ = visitor;
    }

    #[test]
    fn test_analyze_javascript_empty_source() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        let result = visitor.analyze_javascript_source("");
        assert!(result.is_ok());
        // When feature is not enabled, returns empty vec
        // When feature is enabled, might return empty vec for empty source
        let items = result.unwrap();
        // Verify result is valid; empty is acceptable for empty source
        let _ = items.len();
    }

    #[test]
    fn test_analyze_javascript_simple_function() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        let source = "function hello() { return 'world'; }";
        let result = visitor.analyze_javascript_source(source);
        assert!(result.is_ok());
    }

    #[test]
    fn test_analyze_javascript_arrow_function() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        let source = "const greet = (name) => `Hello, ${name}!`;";
        let result = visitor.analyze_javascript_source(source);
        assert!(result.is_ok());
    }

    #[test]
    fn test_analyze_javascript_class() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        let source = r#"
            class Calculator {
                add(a, b) {
                    return a + b;
                }
                subtract(a, b) {
                    return a - b;
                }
            }
        "#;
        let result = visitor.analyze_javascript_source(source);
        assert!(result.is_ok());
    }

    #[test]
    fn test_analyze_javascript_with_imports() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        // Use plain JavaScript without JSX
        let source = r#"
            import axios from 'axios';
            import { useState, useEffect } from 'react';

            function App() {
                const count = useState(0);
                return count;
            }

            export default App;
        "#;
        let result = visitor.analyze_javascript_source(source);
        assert!(result.is_ok());
    }

    #[test]
    fn test_analyze_javascript_module_exports() {
        let path = Path::new("test.js");
        let visitor = JavaScriptAstVisitor::new(path);

        let source = r#"
            module.exports = {
                add: (a, b) => a + b,
                multiply: (a, b) => a * b
            };
        "#;
        let result = visitor.analyze_javascript_source(source);
        assert!(result.is_ok());
    }

    #[test]
    fn test_javascript_ast_visitor_preserves_path() {
        let path = Path::new("/absolute/path/to/file.js");
        let _visitor = JavaScriptAstVisitor::new(path);
        // The path is stored internally, this just verifies no panic
    }
}