|
1 | | -use crate::DeclarationDef; |
2 | 1 | use crate::html::DocNodeWithContext; |
3 | 2 | use crate::html::GenerateCtx; |
4 | 3 | use crate::html::UrlResolveKind; |
@@ -36,10 +35,16 @@ impl<'ctx> RenderContext<'ctx> { |
36 | 35 | doc_nodes: &[DocNodeWithContext], |
37 | 36 | current_resolve: UrlResolveKind<'ctx>, |
38 | 37 | ) -> Self { |
| 38 | + let current_imports = current_resolve |
| 39 | + .get_file() |
| 40 | + .and_then(|file| ctx.imports.get(file)) |
| 41 | + .map(|imports| get_current_imports(imports)) |
| 42 | + .unwrap_or_default(); |
| 43 | + |
39 | 44 | Self { |
40 | 45 | ctx, |
41 | 46 | scoped_symbols: NamespacedSymbols::new(ctx, doc_nodes), |
42 | | - current_imports: Rc::new(get_current_imports(doc_nodes)), |
| 47 | + current_imports: Rc::new(current_imports), |
43 | 48 | current_type_params: Default::default(), |
44 | 49 | current_resolve, |
45 | 50 | namespace_parts: Rc::new([]), |
@@ -460,36 +465,30 @@ fn split_with_brackets(s: &str) -> Vec<String> { |
460 | 465 | } |
461 | 466 |
|
462 | 467 | fn get_current_imports( |
463 | | - symbols: &[DocNodeWithContext], |
| 468 | + imports: &[crate::node::Import], |
464 | 469 | ) -> HashMap<String, String> { |
465 | | - let mut imports = HashMap::new(); |
466 | | - |
467 | | - for symbol in symbols { |
468 | | - for decl in &symbol.declarations { |
469 | | - if let DeclarationDef::Import(import_def) = &decl.def { |
470 | | - // TODO: handle import aliasing |
471 | | - if import_def.imported.as_deref() == Some(symbol.get_name()) { |
472 | | - imports.insert(symbol.get_name().to_string(), import_def.src.clone()); |
473 | | - } |
474 | | - } |
| 470 | + let mut imports_out = HashMap::new(); |
| 471 | + |
| 472 | + for import in imports { |
| 473 | + // TODO: handle import aliasing |
| 474 | + if import.original_name.as_deref() == Some(&*import.imported_name) { |
| 475 | + imports_out.insert(import.imported_name.to_string(), import.src.clone()); |
475 | 476 | } |
476 | 477 | } |
477 | 478 |
|
478 | | - imports |
| 479 | + imports_out |
479 | 480 | } |
480 | 481 |
|
481 | 482 | #[cfg(test)] |
482 | 483 | mod test { |
483 | 484 | use super::*; |
| 485 | + use crate::Location; |
484 | 486 | use crate::html::HrefResolver; |
485 | 487 | use crate::html::{ |
486 | 488 | GenerateOptions, UsageComposer, UsageComposerEntry, UsageToMd, |
487 | 489 | }; |
488 | | - use crate::node::DeclarationKind; |
489 | 490 | use crate::node::Document; |
490 | | - use crate::node::ImportDef; |
491 | | - use crate::node::Symbol; |
492 | | - use crate::{Declaration, Location}; |
| 491 | + use crate::node::Import; |
493 | 492 | use indexmap::IndexMap; |
494 | 493 |
|
495 | 494 | struct TestResolver; |
@@ -563,24 +562,13 @@ mod test { |
563 | 562 | ModuleSpecifier::parse("file:///mod.ts").unwrap(), |
564 | 563 | Document { |
565 | 564 | module_doc: Default::default(), |
566 | | - symbols: vec![Symbol { |
567 | | - name: "foo".into(), |
568 | | - is_default: false, |
569 | | - declarations: vec![Declaration { |
570 | | - location: Location { |
571 | | - filename: "a".into(), |
572 | | - line: 0, |
573 | | - col: 0, |
574 | | - byte_index: 0, |
575 | | - }, |
576 | | - declaration_kind: DeclarationKind::Private, |
577 | | - js_doc: Default::default(), |
578 | | - def: crate::node::DeclarationDef::Import(ImportDef { |
579 | | - src: "b".to_string(), |
580 | | - imported: Some("foo".to_string()), |
581 | | - }), |
582 | | - }], |
| 565 | + imports: vec![Import { |
| 566 | + imported_name: "foo".into(), |
| 567 | + js_doc: Default::default(), |
| 568 | + src: "b".to_string(), |
| 569 | + original_name: Some("foo".to_string()), |
583 | 570 | }], |
| 571 | + symbols: vec![], |
584 | 572 | }, |
585 | 573 | )]); |
586 | 574 |
|
@@ -616,10 +604,7 @@ mod test { |
616 | 604 | let render_ctx = RenderContext::new(&ctx, doc_nodes, UrlResolveKind::Root); |
617 | 605 | assert_eq!(render_ctx.lookup_symbol_href("bar").unwrap(), "global$bar"); |
618 | 606 |
|
619 | | - // imports |
620 | | - let render_ctx = RenderContext::new(&ctx, doc_nodes, UrlResolveKind::Root); |
621 | | - assert_eq!(render_ctx.lookup_symbol_href("foo").unwrap(), "b/foo"); |
622 | | - |
| 607 | + // imports (only available when current resolve is a file) |
623 | 608 | let render_ctx = RenderContext::new( |
624 | 609 | &ctx, |
625 | 610 | doc_nodes, |
|
0 commit comments