Skip to content

Commit cc8ae99

Browse files
committed
fix(gleam): align alias fallback with JS extractor and add aliased import test (#1105)
1 parent a73376b commit cc8ae99

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

  • crates/codegraph-core/src/extractors

crates/codegraph-core/src/extractors/gleam.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,15 @@ fn handle_import(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
218218
}
219219

220220
// Alias: `import gleam/io as my_io`
221-
if let Some(alias) = node.child_by_field_name("alias") {
222-
let alias_text = node_text(&alias, source).to_string();
223-
if alias_text != source_path {
224-
names.push(alias_text);
225-
}
221+
// Mirror JS: prefer `alias` field, fall back to first identifier child
222+
// that isn't the module node itself. Compare by node ID rather than text
223+
// so a self-alias like `import mymodule as mymodule` is still recorded.
224+
let alias_node = node
225+
.child_by_field_name("alias")
226+
.or_else(|| find_child(node, "identifier"))
227+
.filter(|a| a.id() != module_node.id());
228+
if let Some(alias) = alias_node {
229+
names.push(node_text(&alias, source).to_string());
226230
}
227231

228232
if names.is_empty() {
@@ -399,6 +403,14 @@ mod tests {
399403
assert!(s.imports[0].names.contains(&"print".to_string()));
400404
}
401405

406+
#[test]
407+
fn extracts_aliased_import() {
408+
let s = parse_gleam("import gleam/io as my_io\n");
409+
assert_eq!(s.imports.len(), 1);
410+
assert_eq!(s.imports[0].source, "gleam/io");
411+
assert_eq!(s.imports[0].names, vec!["my_io".to_string()]);
412+
}
413+
402414
#[test]
403415
fn extracts_type_definition_with_constructors() {
404416
let code = "pub type Color {\n Red\n Green\n Blue\n}\n";

0 commit comments

Comments
 (0)