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

Function extract

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

Source from the content-addressed store, hash-verified

241const MAX_VALUE_REF_NODES: usize = 20_000;
242
243pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
244 let variant = Variant::from_language(language)
245 .ok_or_else(|| format!("tsjs walker does not handle language: {language}"))?;
246 let grammar = langs::grammar_for(language)
247 .ok_or_else(|| format!("no grammar for language: {language}"))?;
248
249 let t0 = std::time::Instant::now();
250 let mut parser = Parser::new();
251 parser
252 .set_language(&grammar)
253 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
254 let tree = crate::parse_budget::parse(&mut parser, source)?;
255
256 // Files with parse ERRORS defer to the wasm extractor (the `defer:` prefix
257 // tells the TS side this is expected routing, not a malfunction). Reason:
258 // tree-sitter's error RECOVERY — same grammar, same core version — resolves
259 // differently under UTF-8 (native) vs UTF-16 (web-tree-sitter) parsing, so
260 // an erroring file's tree can differ between the paths (proven on vscode:
261 // `readonly import('x').T[]` recovered with the ERROR inside vs outside the
262 // type annotation). Erroring files are rare (0-0.42% across express/
263 // excalidraw/vscode) and per-file wasm fallback keeps routing graph-neutral
264 // by construction; clean files — 99.6%+ — stay on the fast path.
265 if tree.root_node().has_error() {
266 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
267 }
268
269 let mut w = Walker {
270 src: source,
271 file_path,
272 variant,
273 line_starts: util::line_starts(source),
274 arena: Arena::default(),
275 node_id_allocator: ids::NodeIdAllocator::default(),
276 tables: Tables::default(),
277 stack: Vec::new(),
278 node_ids: Vec::new(),
279 defined_fn_names: HashSet::new(),
280 imported_names: HashSet::new(),
281 fn_ref_cands: Vec::new(),
282 fs_values: HashMap::new(),
283 fs_value_counts: HashMap::new(),
284 value_scopes: Vec::new(),
285 vue_store_file: None,
286 object_path_owners: HashMap::new(),
287 owns_objects: !util::is_generated_file(file_path) && !util::is_minified_content(file_path, source),
288 scope_declarations: HashMap::new(),
289 exported_member_fn: None,
290 later_exports: std::cell::OnceCell::new(),
291 };
292
293 // File node (TreeSitterExtractor.extract): id `file:<path>`, endLine =
294 // newline count + 1, isExported explicitly false.
295 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
296 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
297 let mut flags = BoolFlags::default();
298 flags.set(FLAG_IS_EXPORTED, false);
299 let file_id = w.arena.put(&ids::file_node_id(file_path));
300 let name_ref = w.arena.put(base_name);

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
lenMethod · 0.80
into_vecMethod · 0.80

Tested by

no test coverage detected