Skip to content

Commit fc1fdc5

Browse files
committed
refactor: separate module doc from docnodes
1 parent bf68a4c commit fc1fdc5

181 files changed

Lines changed: 15437 additions & 15209 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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,27 @@ async fn run() -> anyhow::Result<()> {
210210
return Ok(());
211211
}
212212

213-
let mut doc_nodes =
214-
doc_nodes_by_url.into_values().flatten().collect::<Vec<_>>();
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;
217+
}
218+
merged_doc.symbols.extend(doc.symbols);
219+
}
215220

216-
doc_nodes.retain(|doc_node| {
221+
merged_doc.symbols.retain(|doc_node| {
217222
!matches!(doc_node.declarations[0].def, DeclarationDef::Import(..))
218223
});
219224

220225
if let Some(filter) = filter {
221-
doc_nodes = find_nodes_by_name_recursively(doc_nodes, &filter);
226+
merged_doc.symbols = find_nodes_by_name_recursively(merged_doc.symbols, &filter);
222227
}
223228

224229
if json {
225-
serde_json::to_writer_pretty(std::io::stdout(), &doc_nodes)?;
230+
serde_json::to_writer_pretty(std::io::stdout(), &merged_doc)?;
226231
println!();
227232
} else {
228-
let result = DocPrinter::new(&doc_nodes, true, false);
233+
let result = DocPrinter::new(&merged_doc, true, false);
229234
println!("{result}");
230235
}
231236
}
@@ -379,7 +384,7 @@ fn generate_docs_directory(
379384
package_name: Option<String>,
380385
output_dir: PathBuf,
381386
main_entrypoint: Option<ModuleSpecifier>,
382-
doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<deno_doc::Symbol>>,
387+
doc_nodes_by_url: IndexMap<ModuleSpecifier, deno_doc::Document>,
383388
) -> Result<(), anyhow::Error> {
384389
let cwd = current_dir().unwrap();
385390
let output_dir_resolved = cwd.join(output_dir);

js/mod.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
import { instantiate } from "./deno_doc_wasm.generated.js";
4-
import type { DocNode, Location } from "./types.d.ts";
4+
import type { DocNode, Document, Location } from "./types.d.ts";
55
import type { Page } from "./html_types.d.ts";
66
import { createCache } from "@deno/cache-dir";
77
import type { CacheSetting, LoadResponse } from "@deno/graph";
@@ -87,7 +87,7 @@ export interface DocOptions {
8787
export async function doc(
8888
specifiers: string[],
8989
options: DocOptions = {},
90-
): Promise<Record<string, Array<DocNode>>> {
90+
): Promise<Record<string, Document>> {
9191
const {
9292
load = createCache().load,
9393
includeAll = false,
@@ -303,12 +303,12 @@ const defaultUsageComposer: UsageComposer = {
303303
};
304304

305305
/**
306-
* Generate HTML files for provided {@linkcode DocNode}s.
306+
* Generate HTML files for provided {@linkcode Document}s.
307307
* @param docNodesByUrl DocNodes keyed by their absolute URL.
308308
* @param options Options for the generation.
309309
*/
310310
export async function generateHtml(
311-
docNodesByUrl: Record<string, Array<DocNode>>,
311+
docNodesByUrl: Record<string, Document>,
312312
options: GenerateOptions,
313313
): Promise<Record<string, string>> {
314314
const {
@@ -346,7 +346,7 @@ export async function generateHtml(
346346
* @param options Options for the generation.
347347
*/
348348
export async function generateHtmlAsJSON(
349-
docNodesByUrl: Record<string, Array<DocNode>>,
349+
docNodesByUrl: Record<string, Document>,
350350
options: GenerateOptions,
351351
): Promise<Record<string, Page>> {
352352
const {

js/test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Deno.test({
99
const records = await doc(
1010
["https://deno.land/std@0.104.0/fmt/colors.ts"],
1111
);
12-
const entries = records["https://deno.land/std@0.104.0/fmt/colors.ts"];
13-
assertEquals(entries.length, 49);
14-
const fnStripColor = entries.find((n) =>
12+
const document = records["https://deno.land/std@0.104.0/fmt/colors.ts"];
13+
assertEquals(document.symbols.length, 49);
14+
const fnStripColor = document.symbols.find((n) =>
1515
n.declarations.some((d) => d.kind === "function") &&
1616
n.name === "stripColor"
1717
);
@@ -61,7 +61,7 @@ Deno.test({
6161
Deno.test({
6262
name: "doc() - with headers",
6363
async fn() {
64-
const entries = await doc(["https://example.com/a"], {
64+
const documents = await doc(["https://example.com/a"], {
6565
load(specifier) {
6666
return Promise.resolve({
6767
kind: "module",
@@ -75,7 +75,7 @@ Deno.test({
7575
});
7676
},
7777
});
78-
assertEquals(Object.values(entries)[0].length, 1);
78+
assertEquals(Object.values(documents)[0].symbols.length, 1);
7979
},
8080
});
8181

@@ -121,10 +121,10 @@ Deno.test({
121121
});
122122
},
123123
});
124-
const entries = Object.values(records)[0];
125-
assertEquals(entries.length, 1);
126-
assertEquals(entries[0].declarations[0].kind, "class");
127-
assertEquals(entries[0].name, "B");
124+
const document = Object.values(records)[0];
125+
assertEquals(document.symbols.length, 1);
126+
assertEquals(document.symbols[0].declarations[0].kind, "class");
127+
assertEquals(document.symbols[0].name, "B");
128128
},
129129
});
130130

js/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3+
export interface Document {
4+
moduleDoc?: JsDoc;
5+
symbols: DocNode[];
6+
}
7+
38
export interface DocNode {
49
name: string;
510
isDefault?: true;

lib/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::Context;
55
use deno_doc::html::UrlResolveKind;
66
use deno_doc::html::UsageComposerEntry;
77
use deno_doc::html::UsageToMd;
8-
use deno_doc::DocParser;
8+
use deno_doc::{DocParser, Document};
99
use deno_graph::ast::CapturingModuleAnalyzer;
1010
use deno_graph::source::CacheSetting;
1111
use deno_graph::source::LoadError;
@@ -566,7 +566,7 @@ fn generate_html_inner(
566566
>(default_symbol_map)
567567
.map_err(|err| anyhow!("defaultSymbolMap: {}", err))?;
568568

569-
let doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<deno_doc::Symbol>> =
569+
let doc_nodes_by_url: IndexMap<ModuleSpecifier, Document> =
570570
serde_wasm_bindgen::from_value(doc_nodes_by_url)
571571
.map_err(|err| anyhow!("docNodesByUrl: {}", err))?;
572572

src/diagnostics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ impl DiagnosticDocNodeVisitor<'_, '_> {
403403
| DeclarationDef::TypeAlias(..)
404404
| DeclarationDef::Variable(..) => true,
405405
DeclarationDef::Import(..)
406-
| DeclarationDef::ModuleDoc
407406
| DeclarationDef::Reference(..) => false,
408407
}
409408
}

src/diff/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ impl DocDiff {
163163
let mut modified_modules = IndexMap::new();
164164

165165
for specifier in old_modules.intersection(&new_modules) {
166-
let old_nodes = old.get(*specifier).unwrap();
167-
let new_nodes = new.get(*specifier).unwrap();
166+
let old_doc = old.get(*specifier).unwrap();
167+
let new_doc = new.get(*specifier).unwrap();
168168

169-
let module_diff = ModuleDiff::diff(old_nodes, new_nodes);
169+
let module_diff = ModuleDiff::diff(&old_doc.symbols, &new_doc.symbols);
170170

171171
if module_diff.has_changes() {
172172
modified_modules.insert((*specifier).clone(), module_diff);
@@ -602,7 +602,6 @@ impl DeclarationDefDiff {
602602

603603
(DeclarationDef::Import(..), DeclarationDef::Import(..)) => None,
604604
(DeclarationDef::Reference(..), DeclarationDef::Reference(..)) => None,
605-
(DeclarationDef::ModuleDoc, DeclarationDef::ModuleDoc) => None,
606605

607606
_ => unreachable!(),
608607
}

src/html/jsdoc.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::render_context::RenderContext;
22
use super::util::*;
3-
use crate::DeclarationDef;
43
use crate::html::ShortPath;
54
use crate::js_doc::JsDoc;
65
use crate::js_doc::JsDocTag;
@@ -802,13 +801,10 @@ impl ModuleDocCtx {
802801

803802
let mut sections = Vec::with_capacity(7);
804803

805-
let (deprecated, html) = if let Some(decl) =
806-
module_doc_nodes.iter().find_map(|n| {
807-
n.declarations
808-
.iter()
809-
.find(|decl| matches!(decl.def, DeclarationDef::ModuleDoc))
810-
}) {
811-
let deprecated = decl.js_doc.tags.iter().find_map(|tag| {
804+
let (deprecated, html) = if let Some(js_doc) =
805+
render_ctx.ctx.module_docs.get(short_path)
806+
{
807+
let deprecated = js_doc.tags.iter().find_map(|tag| {
812808
if let JsDocTag::Deprecated { doc } = tag {
813809
Some(render_markdown(
814810
render_ctx,
@@ -820,11 +816,11 @@ impl ModuleDocCtx {
820816
}
821817
});
822818

823-
if let Some(examples) = jsdoc_examples(render_ctx, &decl.js_doc) {
819+
if let Some(examples) = jsdoc_examples(render_ctx, js_doc) {
824820
sections.push(examples);
825821
}
826822

827-
let html = jsdoc_body_to_html(render_ctx, &decl.js_doc, summary);
823+
let html = jsdoc_body_to_html(render_ctx, js_doc, summary);
828824

829825
(deprecated, html)
830826
} else {
@@ -896,6 +892,7 @@ mod test {
896892
use crate::interface::InterfaceDef;
897893
use crate::js_doc::JsDoc;
898894
use crate::node::DeclarationKind;
895+
use crate::node::Document;
899896

900897
struct EmptyResolver;
901898

@@ -984,46 +981,51 @@ mod test {
984981
IndexMap::from([
985982
(
986983
ModuleSpecifier::parse("file:///a.ts").unwrap(),
987-
vec![
988-
Symbol::interface(
989-
"foo".into(),
990-
false,
991-
Location::default(),
992-
DeclarationKind::Export,
993-
JsDoc::default(),
994-
InterfaceDef {
995-
def_name: None,
996-
extends: vec![],
997-
constructors: vec![],
998-
methods: vec![],
999-
properties: vec![],
1000-
call_signatures: vec![],
1001-
index_signatures: vec![],
1002-
type_params: Box::new([]),
1003-
},
1004-
),
1005-
Symbol::interface(
1006-
"bar".into(),
1007-
false,
1008-
Location::default(),
1009-
DeclarationKind::Export,
1010-
JsDoc::default(),
1011-
InterfaceDef {
1012-
def_name: None,
1013-
extends: vec![],
1014-
constructors: vec![],
984+
Document {
985+
module_doc: Default::default(),
986+
symbols: vec![
987+
Symbol::interface(
988+
"foo".into(),
989+
false,
990+
Location::default(),
991+
DeclarationKind::Export,
992+
JsDoc::default(),
993+
InterfaceDef {
994+
def_name: None,
995+
extends: vec![],
996+
constructors: vec![],
997+
methods: vec![],
998+
properties: vec![],
999+
call_signatures: vec![],
1000+
index_signatures: vec![],
1001+
type_params: Box::new([]),
1002+
},
1003+
),
1004+
Symbol::interface(
1005+
"bar".into(),
1006+
false,
1007+
Location::default(),
1008+
DeclarationKind::Export,
1009+
JsDoc::default(),
1010+
InterfaceDef {
1011+
def_name: None,
1012+
extends: vec![],
1013+
constructors: vec![],
10151014
methods: vec![],
10161015
properties: vec![],
10171016
call_signatures: vec![],
10181017
index_signatures: vec![],
10191018
type_params: Box::new([]),
10201019
},
10211020
),
1022-
],
1021+
],
1022+
},
10231023
),
10241024
(
10251025
ModuleSpecifier::parse("file:///b.ts").unwrap(),
1026-
vec![Symbol::interface(
1026+
Document {
1027+
module_doc: Default::default(),
1028+
symbols: vec![Symbol::interface(
10271029
"baz".into(),
10281030
false,
10291031
Location::default(),
@@ -1040,6 +1042,7 @@ mod test {
10401042
type_params: Box::new([]),
10411043
},
10421044
)],
1045+
},
10431046
),
10441047
]),
10451048
None,

src/html/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ pub struct GenerateOptions {
286286
pub struct GenerateCtx {
287287
pub package_name: Option<String>,
288288
pub common_ancestor: Option<PathBuf>,
289+
pub module_docs: IndexMap<Rc<ShortPath>, crate::js_doc::JsDoc>,
289290
pub doc_nodes: IndexMap<Rc<ShortPath>, Vec<DocNodeWithContext>>,
290291
pub href_resolver: Rc<dyn HrefResolver>,
291292
pub usage_composer: Option<Rc<dyn UsageComposer>>,
@@ -321,10 +322,11 @@ impl GenerateCtx {
321322
let diff = diff.map(DiffIndex::new);
322323

323324
let mut main_entrypoint = None;
325+
let mut module_docs = IndexMap::new();
324326

325327
let mut doc_nodes = doc_nodes_by_url
326328
.into_iter()
327-
.map(|(specifier, nodes)| {
329+
.map(|(specifier, document)| {
328330
let short_path = Rc::new(ShortPath::new(
329331
specifier,
330332
options.main_entrypoint.as_ref(),
@@ -336,7 +338,10 @@ impl GenerateCtx {
336338
main_entrypoint = Some(short_path.clone());
337339
}
338340

339-
let nodes = nodes
341+
module_docs.insert(short_path.clone(), document.module_doc);
342+
343+
let nodes = document
344+
.symbols
340345
.into_iter()
341346
.map(|mut symbol| {
342347
if &*symbol.name == "default"
@@ -523,6 +528,7 @@ impl GenerateCtx {
523528
Ok(Self {
524529
package_name: options.package_name,
525530
common_ancestor,
531+
module_docs,
526532
doc_nodes,
527533
href_resolver: options.href_resolver,
528534
usage_composer: options.usage_composer,

src/html/pages.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,14 @@ impl IndexCtx {
253253
let sections = ctx
254254
.doc_nodes
255255
.iter()
256-
.map(|(short_path, nodes)| {
257-
let doc = nodes
258-
.iter()
259-
.find_map(|node| {
260-
node
261-
.declarations
262-
.iter()
263-
.find(|decl| matches!(decl.def, DeclarationDef::ModuleDoc))
264-
})
265-
.and_then(|node| {
256+
.map(|(short_path, _nodes)| {
257+
let doc = ctx
258+
.module_docs
259+
.get(short_path)
260+
.and_then(|js_doc| {
266261
crate::html::jsdoc::jsdoc_body_to_html(
267262
&render_ctx,
268-
&node.js_doc,
263+
js_doc,
269264
true,
270265
)
271266
});

0 commit comments

Comments
 (0)