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

Function extract

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

Source from the content-addressed store, hash-verified

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

Tested by

no test coverage detected