codesynapse_core/ts_extract/
svelte.rs1use super::{add_contains_edge, make_file_node};
2use crate::error::Result;
3use crate::extract::{make_id, ImportNode, LanguageExtractor};
4use crate::types::{Edge, ExtractionFragment, Node};
5use std::collections::HashMap;
6use std::path::Path;
7
8pub struct TsSvelteExtractor;
9
10impl TsSvelteExtractor {
11 pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
12 let (file_id, _, file_node) = make_file_node(path);
13 let mut fragment = ExtractionFragment {
14 nodes: vec![file_node],
15 edges: vec![],
16 };
17
18 let content = std::str::from_utf8(source).unwrap_or("");
19
20 let mut script_content = String::new();
22 let mut in_script = false;
23
24 for line in content.lines() {
25 if line.trim().starts_with("<script") {
26 in_script = true;
27 continue;
28 }
29 if line.trim() == "</script>" {
30 in_script = false;
31 continue;
32 }
33 if in_script {
34 script_content.push_str(line);
35 script_content.push('\n');
36 }
37 }
38
39 for line in script_content.lines() {
41 if line.contains("$state") || line.contains("$derived") || line.contains("$effect") {
42 if let Some(var_part) = line.split('=').next() {
43 let var_name = var_part.split_whitespace().last().unwrap_or("").trim();
44 if !var_name.is_empty() {
45 let state_id = make_id(&[&file_id, var_name]);
46 fragment.nodes.push(Node {
47 id: state_id.clone(),
48 label: var_name.to_string(),
49 file_type: "constant".to_string(),
50 source_file: path.to_string_lossy().to_string(),
51 source_location: None,
52 community: None,
53 rationale: None,
54 docstring: None,
55 metadata: HashMap::new(),
56 });
57 add_contains_edge(&mut fragment, &file_id, state_id, path);
58 }
59 }
60 }
61 }
62
63 Ok(fragment)
64 }
65}
66
67impl LanguageExtractor for TsSvelteExtractor {
69 fn file_extensions(&self) -> Vec<&'static str> {
70 vec!["svelte"]
71 }
72 fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
73 Self::extract(source, path)
74 }
75 fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
76 vec![]
77 }
78 fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
79}