MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / rust_import

Function rust_import

atomic-semantic/src/source.rs:341–404  ·  view source on GitHub ↗
(
    node: Node<'_>,
    source: &str,
    scope: &[String],
    prefix: &[String],
    imports: &mut Vec<ImportBinding>,
)

Source from the content-addressed store, hash-verified

339}
340
341fn rust_import(
342 node: Node<'_>,
343 source: &str,
344 scope: &[String],
345 prefix: &[String],
346 imports: &mut Vec<ImportBinding>,
347) {
348 match node.kind() {
349 "use_list" => {
350 for child in children(node) {
351 rust_import(child, source, scope, prefix, imports);
352 }
353 }
354 "scoped_use_list" => {
355 let Some(qualified) = node
356 .child_by_field_name("path")
357 .and_then(|node| qualified_path(text(node, source), "::"))
358 else {
359 return;
360 };
361 let mut prefix = prefix.to_vec();
362 prefix.extend(qualified);
363 for list in children(node)
364 .into_iter()
365 .filter(|node| node.kind() == "use_list")
366 {
367 rust_import(list, source, scope, &prefix, imports);
368 }
369 }
370 "use_wildcard" => {}
371 _ => {
372 // A scoped_identifier's `path` is only its prefix; unwrapping it
373 // drops the imported symbol. Only an alias wraps a complete path.
374 let argument = if node.kind() == "use_as_clause" {
375 node.child_by_field_name("path").unwrap_or(node)
376 } else {
377 node
378 };
379 let Some(target) = qualified_path(text(argument, source), "::") else {
380 return;
381 };
382 let mut full = prefix.to_vec();
383 full.extend(target);
384 if full.last().is_some_and(|name| name == "self") {
385 full.pop();
386 }
387 if let Some(local) = node
388 .child_by_field_name("alias")
389 .map(|node| text(node, source).to_owned())
390 .or_else(|| full.last().cloned())
391 {
392 let lexical = lexical_container(node);
393 imports.push(ImportBinding {
394 scope: scope.to_vec(),
395 local,
396 target: full,
397 script_module: None,
398 namespace: false,

Callers 1

from_treeMethod · 0.85

Calls 9

qualified_pathFunction · 0.85
lexical_containerFunction · 0.85
extendMethod · 0.80
lastMethod · 0.80
childrenFunction · 0.70
textFunction · 0.70
kindMethod · 0.45
into_iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected