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

Function extract

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 14

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
putMethod · 0.80
push_nodeMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
unwrapMethod · 0.60
line_startsFunction · 0.50
setMethod · 0.45
visit_nodeMethod · 0.45

Tested by

no test coverage detected