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

Function extract

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

Source from the content-addressed store, hash-verified

190const MAX_VALUE_REF_NODES: usize = 20_000;
191
192pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
193 let variant = Variant::from_language(language)
194 .ok_or_else(|| format!("tsjs walker does not handle language: {language}"))?;
195 let grammar = langs::grammar_for(language)
196 .ok_or_else(|| format!("no grammar for language: {language}"))?;
197
198 let t0 = std::time::Instant::now();
199 let mut parser = Parser::new();
200 parser
201 .set_language(&grammar)
202 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
203 let tree = parser
204 .parse(source, None)
205 .ok_or_else(|| "parser returned null tree".to_string())?;
206
207 // Files with parse ERRORS defer to the wasm extractor (the `defer:` prefix
208 // tells the TS side this is expected routing, not a malfunction). Reason:
209 // tree-sitter's error RECOVERY — same grammar, same core version — resolves
210 // differently under UTF-8 (native) vs UTF-16 (web-tree-sitter) parsing, so
211 // an erroring file's tree can differ between the paths (proven on vscode:
212 // `readonly import('x').T[]` recovered with the ERROR inside vs outside the
213 // type annotation). Erroring files are rare (0-0.42% across express/
214 // excalidraw/vscode) and per-file wasm fallback keeps routing graph-neutral
215 // by construction; clean files — 99.6%+ — stay on the fast path.
216 if tree.root_node().has_error() {
217 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
218 }
219
220 let mut w = Walker {
221 src: source,
222 file_path,
223 variant,
224 line_starts: util::line_starts(source),
225 arena: Arena::default(),
226 node_id_allocator: ids::NodeIdAllocator::default(),
227 tables: Tables::default(),
228 stack: Vec::new(),
229 node_ids: Vec::new(),
230 defined_fn_names: HashSet::new(),
231 imported_names: HashSet::new(),
232 fn_ref_cands: Vec::new(),
233 fs_values: HashMap::new(),
234 fs_value_counts: HashMap::new(),
235 value_scopes: Vec::new(),
236 vue_store_file: None,
237 };
238
239 // File node (TreeSitterExtractor.extract): id `file:<path>`, endLine =
240 // newline count + 1, isExported explicitly false.
241 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
242 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
243 let mut flags = BoolFlags::default();
244 flags.set(FLAG_IS_EXPORTED, false);
245 let file_id = w.arena.put(&ids::file_node_id(file_path));
246 let name_ref = w.arena.put(base_name);
247 let qn_ref = w.arena.put(file_path);
248 w.tables.push_node(&NodeRow {
249 kind: node_kind_index("file").unwrap(),

Callers

nothing calls this directly

Calls 15

grammar_forFunction · 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
into_vecMethod · 0.80
putMethod · 0.65

Tested by

no test coverage detected