MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

codegraph-kernel/src/tsjs/mod.rs:351–453  ·  view source on GitHub ↗
(file_path: &str, source: &str, language: &str)

Source from the content-addressed store, hash-verified

349const MAX_VALUE_REF_NODES: usize = 20_000;
350
351pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
352 let variant = Variant::from_language(language)
353 .ok_or_else(|| format!("tsjs walker does not handle language: {language}"))?;
354 let grammar = langs::grammar_for(language)
355 .ok_or_else(|| format!("no grammar for language: {language}"))?;
356
357 let t0 = std::time::Instant::now();
358 let mut parser = Parser::new();
359 parser
360 .set_language(&grammar)
361 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
362 let tree = crate::parse_budget::parse(&mut parser, source)?;
363
364 // Files with parse ERRORS defer to the wasm extractor (the `defer:` prefix
365 // tells the TS side this is expected routing, not a malfunction). Reason:
366 // tree-sitter's error RECOVERY — same grammar, same core version — resolves
367 // differently under UTF-8 (native) vs UTF-16 (web-tree-sitter) parsing, so
368 // an erroring file's tree can differ between the paths (proven on vscode:
369 // `readonly import('x').T[]` recovered with the ERROR inside vs outside the
370 // type annotation). Erroring files are rare (0-0.42% across express/
371 // excalidraw/vscode) and per-file wasm fallback keeps routing graph-neutral
372 // by construction; clean files — 99.6%+ — stay on the fast path.
373 if tree.root_node().has_error() {
374 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
375 }
376
377 let mut w = Walker {
378 src: source,
379 file_path,
380 variant,
381 line_starts: util::line_starts(source),
382 arena: Arena::default(),
383 node_id_allocator: ids::NodeIdAllocator::default(),
384 tables: Tables::default(),
385 stack: Vec::new(),
386 node_ids: Vec::new(),
387 defined_fn_names: HashSet::new(),
388 imported_names: HashSet::new(),
389 fn_ref_cands: Vec::new(),
390 fs_values: HashMap::new(),
391 fs_value_counts: HashMap::new(),
392 value_scopes: Vec::new(),
393 vue_store_file: None,
394 object_path_owners: HashMap::new(),
395 owns_objects: !util::is_generated_file(file_path) && !util::is_minified_content(file_path, source),
396 comment_marks: std::cell::OnceCell::new(),
397 pair_fields: std::cell::OnceCell::new(),
398 file_member_list: None,
399 scope_declarations: HashMap::new(),
400 exported_member_fn: None,
401 later_exports: std::cell::OnceCell::new(),
402 };
403
404 // File node (TreeSitterExtractor.extract): id `file:<path>`, endLine =
405 // newline count + 1, isExported explicitly false.
406 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
407 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
408 let mut flags = BoolFlags::default();

Callers

nothing calls this directly

Calls 15

grammar_forFunction · 0.85
is_generated_fileFunction · 0.85
is_minified_contentFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
bytesMethod · 0.80
nextMethod · 0.80
push_nodeMethod · 0.80
pushMethod · 0.80
set_object_membersMethod · 0.80
lenMethod · 0.80

Tested by

no test coverage detected