Skip to content

Commit 4124539

Browse files
authored
refactor: separate imports from symbols (#780)
1 parent fe4d182 commit 4124539

170 files changed

Lines changed: 337 additions & 384 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/ddoc/main.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use deno_doc::html::HrefResolver;
1313
use deno_doc::html::UrlResolveKind;
1414
use deno_doc::html::UsageComposer;
1515
use deno_doc::html::UsageComposerEntry;
16-
use deno_doc::node::DeclarationDef;
1716
use deno_graph::BuildOptions;
1817
use deno_graph::GraphKind;
1918
use deno_graph::ModuleGraph;
@@ -190,7 +189,7 @@ async fn run() -> anyhow::Result<()> {
190189
filter,
191190
private,
192191
} => {
193-
let doc_nodes_by_url = if json_input {
192+
let mut doc_nodes_by_url = if json_input {
194193
assert_eq!(files.len(), 1);
195194
serde_json::from_reader(std::fs::File::open(&files[0])?)?
196195
} else {
@@ -210,27 +209,25 @@ async fn run() -> anyhow::Result<()> {
210209
return Ok(());
211210
}
212211

213-
let mut merged_doc = deno_doc::Document::default();
214-
for doc in doc_nodes_by_url.into_values() {
215-
if merged_doc.module_doc.is_empty() {
216-
merged_doc.module_doc = doc.module_doc;
212+
for (_, doc) in &mut doc_nodes_by_url {
213+
if let Some(filter) = &filter {
214+
let symbols = std::mem::take(&mut doc.symbols);
215+
doc.symbols = find_nodes_by_name_recursively(symbols, filter);
217216
}
218-
merged_doc.symbols.extend(doc.symbols);
219-
}
220-
221-
merged_doc.symbols.retain(|doc_node| {
222-
!matches!(doc_node.declarations[0].def, DeclarationDef::Import(..))
223-
});
224-
225-
if let Some(filter) = filter {
226-
merged_doc.symbols =
227-
find_nodes_by_name_recursively(merged_doc.symbols, &filter);
228217
}
229218

230219
if json {
231-
serde_json::to_writer_pretty(std::io::stdout(), &merged_doc)?;
220+
serde_json::to_writer_pretty(std::io::stdout(), &doc_nodes_by_url)?;
232221
println!();
233222
} else {
223+
let mut merged_doc = deno_doc::Document::default();
224+
for doc in doc_nodes_by_url.into_values() {
225+
if merged_doc.module_doc.is_empty() {
226+
merged_doc.module_doc = doc.module_doc;
227+
}
228+
merged_doc.symbols.extend(doc.symbols);
229+
}
230+
234231
let result = DocPrinter::new(&merged_doc, true, false);
235232
println!("{result}");
236233
}

js/types.d.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
export interface Document {
44
moduleDoc?: JsDoc;
5+
imports?: Import[];
56
symbols: DocNode[];
67
}
78

9+
export interface Import {
10+
importedName: string;
11+
originalName?: string;
12+
jsDoc?: JsDoc;
13+
src: string;
14+
}
15+
816
export interface DocNode {
917
name: string;
1018
isDefault?: true;
@@ -25,7 +33,6 @@ export type Declaration =
2533
| DeclarationTypeAlias
2634
| DeclarationNamespace
2735
| DeclarationInterface
28-
| DeclarationImport
2936
| DeclarationReference;
3037

3138
interface DeclarationBase {
@@ -44,7 +51,6 @@ export type DocNodeKind =
4451
| "typeAlias"
4552
| "namespace"
4653
| "interface"
47-
| "import"
4854
| "reference";
4955

5056
export interface DeclarationModuleDoc extends DeclarationBase {
@@ -87,11 +93,6 @@ export interface DeclarationInterface extends DeclarationBase {
8793
def: InterfaceDef;
8894
}
8995

90-
export interface DeclarationImport extends DeclarationBase {
91-
kind: "import";
92-
def: ImportDef;
93-
}
94-
9596
export interface DeclarationReference extends DeclarationBase {
9697
kind: "reference";
9798
reference_def: ReferenceDef;
@@ -190,11 +191,6 @@ export interface FunctionDef {
190191
decorators?: DecoratorDef[];
191192
}
192193

193-
export interface ImportDef {
194-
src: string;
195-
imported?: string;
196-
}
197-
198194
export interface InterfaceDef {
199195
defName?: string;
200196
extends: TsTypeDef[];

src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl DiagnosticDocNodeVisitor<'_, '_> {
402402
| DeclarationDef::Namespace(..)
403403
| DeclarationDef::TypeAlias(..)
404404
| DeclarationDef::Variable(..) => true,
405-
DeclarationDef::Import(..) | DeclarationDef::Reference(..) => false,
405+
DeclarationDef::Reference(..) => false,
406406
}
407407
}
408408

src/diff/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pub enum DeclarationDefDiff {
469469
TypeAlias(TypeAliasDiff),
470470
Namespace(NamespaceDiff),
471471
Interface(InterfaceDiff),
472-
// ignore Import & Reference
472+
// ignore Reference
473473
}
474474

475475
impl DeclarationDefDiff {
@@ -600,7 +600,6 @@ impl DeclarationDefDiff {
600600
InterfaceDiff::diff(old_def, new_def).map(DeclarationDefDiff::Interface)
601601
}
602602

603-
(DeclarationDef::Import(..), DeclarationDef::Import(..)) => None,
604603
(DeclarationDef::Reference(..), DeclarationDef::Reference(..)) => None,
605604

606605
_ => unreachable!(),

src/html/jsdoc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ mod test {
982982
ModuleSpecifier::parse("file:///a.ts").unwrap(),
983983
Document {
984984
module_doc: Default::default(),
985+
imports: vec![],
985986
symbols: vec![
986987
Symbol::interface(
987988
"foo".into(),
@@ -1024,6 +1025,7 @@ mod test {
10241025
ModuleSpecifier::parse("file:///b.ts").unwrap(),
10251026
Document {
10261027
module_doc: Default::default(),
1028+
imports: vec![],
10271029
symbols: vec![Symbol::interface(
10281030
"baz".into(),
10291031
false,

src/html/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub struct GenerateCtx {
287287
pub package_name: Option<String>,
288288
pub common_ancestor: Option<PathBuf>,
289289
pub module_docs: IndexMap<Rc<ShortPath>, crate::js_doc::JsDoc>,
290+
pub imports: IndexMap<Rc<ShortPath>, Vec<crate::node::Import>>,
290291
pub doc_nodes: IndexMap<Rc<ShortPath>, Vec<DocNodeWithContext>>,
291292
pub href_resolver: Rc<dyn HrefResolver>,
292293
pub usage_composer: Option<Rc<dyn UsageComposer>>,
@@ -323,6 +324,7 @@ impl GenerateCtx {
323324

324325
let mut main_entrypoint = None;
325326
let mut module_docs = IndexMap::new();
327+
let mut imports = IndexMap::new();
326328

327329
let mut doc_nodes = doc_nodes_by_url
328330
.into_iter()
@@ -339,6 +341,7 @@ impl GenerateCtx {
339341
}
340342

341343
module_docs.insert(short_path.clone(), document.module_doc);
344+
imports.insert(short_path.clone(), document.imports);
342345

343346
let nodes = document
344347
.symbols
@@ -529,6 +532,7 @@ impl GenerateCtx {
529532
package_name: options.package_name,
530533
common_ancestor,
531534
module_docs,
535+
imports,
532536
doc_nodes,
533537
href_resolver: options.href_resolver,
534538
usage_composer: options.usage_composer,
@@ -938,7 +942,6 @@ impl DocNodeWithContext {
938942
.inner
939943
.declarations
940944
.iter()
941-
.filter(|d| !matches!(d.def, crate::node::DeclarationDef::Import(..)))
942945
.map(|d| DocNodeKindCtx::from(crate::node::DocNodeKind::from(&d.def)))
943946
.collect()
944947
}

src/html/partition.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ where
3131
{
3232
'outer: for node in doc_nodes {
3333
for decl in &node.declarations {
34-
if matches!(decl.def, DeclarationDef::Import(..)) {
35-
continue 'outer;
36-
}
37-
3834
if flatten_namespaces
3935
&& matches!(decl.def, DeclarationDef::Namespace(..))
4036
{
@@ -223,10 +219,6 @@ pub fn flatten_namespace<'a>(
223219

224220
'outer: for node in &nodes {
225221
for decl in &node.declarations {
226-
if matches!(decl.def, DeclarationDef::Import(..)) {
227-
continue 'outer;
228-
}
229-
230222
if matches!(decl.def, DeclarationDef::Namespace(..)) {
231223
let children: Vec<_> =
232224
node.namespace_children.as_ref().unwrap().clone();

src/html/render_context.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::DeclarationDef;
21
use crate::html::DocNodeWithContext;
32
use crate::html::GenerateCtx;
43
use crate::html::UrlResolveKind;
@@ -36,10 +35,16 @@ impl<'ctx> RenderContext<'ctx> {
3635
doc_nodes: &[DocNodeWithContext],
3736
current_resolve: UrlResolveKind<'ctx>,
3837
) -> 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+
3944
Self {
4045
ctx,
4146
scoped_symbols: NamespacedSymbols::new(ctx, doc_nodes),
42-
current_imports: Rc::new(get_current_imports(doc_nodes)),
47+
current_imports: Rc::new(current_imports),
4348
current_type_params: Default::default(),
4449
current_resolve,
4550
namespace_parts: Rc::new([]),
@@ -460,36 +465,30 @@ fn split_with_brackets(s: &str) -> Vec<String> {
460465
}
461466

462467
fn get_current_imports(
463-
symbols: &[DocNodeWithContext],
468+
imports: &[crate::node::Import],
464469
) -> 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());
475476
}
476477
}
477478

478-
imports
479+
imports_out
479480
}
480481

481482
#[cfg(test)]
482483
mod test {
483484
use super::*;
485+
use crate::Location;
484486
use crate::html::HrefResolver;
485487
use crate::html::{
486488
GenerateOptions, UsageComposer, UsageComposerEntry, UsageToMd,
487489
};
488-
use crate::node::DeclarationKind;
489490
use crate::node::Document;
490-
use crate::node::ImportDef;
491-
use crate::node::Symbol;
492-
use crate::{Declaration, Location};
491+
use crate::node::Import;
493492
use indexmap::IndexMap;
494493

495494
struct TestResolver;
@@ -563,24 +562,13 @@ mod test {
563562
ModuleSpecifier::parse("file:///mod.ts").unwrap(),
564563
Document {
565564
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()),
583570
}],
571+
symbols: vec![],
584572
},
585573
)]);
586574

@@ -616,10 +604,7 @@ mod test {
616604
let render_ctx = RenderContext::new(&ctx, doc_nodes, UrlResolveKind::Root);
617605
assert_eq!(render_ctx.lookup_symbol_href("bar").unwrap(), "global$bar");
618606

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)
623608
let render_ctx = RenderContext::new(
624609
&ctx,
625610
doc_nodes,

src/html/symbols/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ impl SymbolGroupCtx {
5757
IndexMap::<crate::node::DocNodeKind, Vec<&Declaration>>::default();
5858

5959
for decl in &symbol.declarations {
60-
if matches!(decl.def, DeclarationDef::Import(..)) {
61-
continue;
62-
}
63-
6460
declarations
6561
.entry(decl.def.to_kind())
6662
.or_insert(vec![])
@@ -584,7 +580,7 @@ impl SymbolInnerCtx {
584580
},
585581
))
586582
}
587-
DeclarationDef::Import(..) | DeclarationDef::Reference(..) => {
583+
DeclarationDef::Reference(..) => {
588584
unreachable!()
589585
}
590586
});

src/html/symbols/namespace.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,7 @@ fn summary_for_symbol(
439439
info: None,
440440
})
441441
}
442-
DeclarationDef::Reference(..)
443-
| DeclarationDef::Namespace(..)
444-
| DeclarationDef::Import(..) => None,
442+
DeclarationDef::Reference(..) | DeclarationDef::Namespace(..) => None,
445443
}
446444
}
447445

0 commit comments

Comments
 (0)