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

Function extract

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

Source from the content-addressed store, hash-verified

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

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