Skip to content

Commit 3d02d66

Browse files
committed
feat: accept ParseOutput in DocPrinter
1 parent 35fd45b commit 3d02d66

4 files changed

Lines changed: 39 additions & 57 deletions

File tree

examples/ddoc/main.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,7 @@ async fn run() -> anyhow::Result<()> {
223223
serde_json::to_writer_pretty(std::io::stdout(), &doc_nodes_by_url)?;
224224
println!();
225225
} else {
226-
let mut merged_doc = deno_doc::Document::default();
227-
for doc in doc_nodes_by_url.into_values() {
228-
if merged_doc.module_doc.is_empty() {
229-
merged_doc.module_doc = doc.module_doc;
230-
}
231-
merged_doc.symbols.extend(doc.symbols);
232-
}
233-
234-
let result = DocPrinter::new(&merged_doc, true, false);
226+
let result = DocPrinter::new(&doc_nodes_by_url, true, false);
235227
println!("{result}");
236228
}
237229
}

src/printer.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::js_doc::JsDoc;
99
use crate::js_doc::JsDocTag;
1010
use crate::node::DeclarationDef;
1111
use crate::node::DeclarationKind;
12-
use crate::node::Document;
1312
use crate::node::Symbol;
13+
use crate::parser::ParseOutput;
1414

1515
use deno_terminal::colors;
1616
use deno_terminal::colors::Style;
@@ -25,37 +25,44 @@ fn italic_cyan<'a, S: Display + 'a>(s: S) -> Style<Style<S>> {
2525
}
2626

2727
pub struct DocPrinter<'a> {
28-
document: &'a Document,
28+
output: &'a ParseOutput,
2929
use_color: bool,
3030
private: bool,
3131
}
3232

3333
impl DocPrinter<'_> {
3434
pub fn new(
35-
document: &Document,
35+
output: &ParseOutput,
3636
use_color: bool,
3737
private: bool,
3838
) -> DocPrinter<'_> {
3939
DocPrinter {
40-
document,
40+
output,
4141
use_color,
4242
private,
4343
}
4444
}
4545

4646
pub fn format(&self, w: &mut Formatter<'_>) -> FmtResult {
4747
colors::set_use_color(self.use_color);
48-
if !self.document.module_doc.is_empty() {
49-
if let Some(doc) = &self.document.module_doc.doc {
50-
render_markdown(w, doc, 0)?;
48+
let multiple = self.output.len() > 1;
49+
for (specifier, document) in self.output {
50+
if multiple {
51+
writeln!(w, "{}\n", colors::bold(specifier.as_str()))?;
5152
}
52-
for tag in &self.document.module_doc.tags {
53-
self.format_jsdoc_tag(w, tag, 0)?;
53+
if !document.module_doc.is_empty() {
54+
if let Some(doc) = &document.module_doc.doc {
55+
render_markdown(w, doc, 0)?;
56+
}
57+
for tag in &document.module_doc.tags {
58+
self.format_jsdoc_tag(w, tag, 0)?;
59+
}
60+
writeln!(w)?;
5461
}
55-
writeln!(w)?;
56-
}
5762

58-
self.format_with_indent(w, &self.document.symbols, 0)
63+
self.format_with_indent(w, &document.symbols, 0)?;
64+
}
65+
Ok(())
5966
}
6067

6168
fn format_with_indent(

src/tests.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,16 @@ export function fooFn(a: number) {
224224
],
225225
)
226226
.await;
227-
let document = DocParser::new(
227+
let output = DocParser::new(
228228
&graph,
229229
&analyzer,
230230
&[specifier],
231231
DocParserOptions::default(),
232232
)
233233
.unwrap()
234234
.parse()
235-
.unwrap()
236-
.into_values()
237-
.next()
238235
.unwrap();
236+
let document = output.values().next().unwrap();
239237

240238
let expected_symbols = json!([
241239
{
@@ -338,7 +336,7 @@ export function fooFn(a: number) {
338336
assert_eq!(actual, expected_imports);
339337

340338
assert!(
341-
DocPrinter::new(&document, false, false)
339+
DocPrinter::new(&output, false, false)
342340
.to_string()
343341
.as_str()
344342
.contains("function fooFn(a: number)")
@@ -363,19 +361,16 @@ export { Hello } from "./reexport.ts";
363361
],
364362
)
365363
.await;
366-
let entries = DocParser::new(
364+
let output = DocParser::new(
367365
&graph,
368366
&analyzer,
369367
&[specifier],
370368
DocParserOptions::default(),
371369
)
372370
.unwrap()
373371
.parse()
374-
.unwrap()
375-
.into_values()
376-
.next()
377-
.unwrap()
378-
.symbols;
372+
.unwrap();
373+
let entries = &output.values().next().unwrap().symbols;
379374

380375
let expected_json = json!([
381376
{
@@ -406,17 +401,12 @@ export { Hello } from "./reexport.ts";
406401
]
407402
}
408403
]);
409-
let actual = serde_json::to_value(&entries).unwrap();
404+
let actual = serde_json::to_value(entries).unwrap();
410405
assert_eq!(actual, expected_json);
411406

412-
let doc = crate::node::Document {
413-
module_doc: Default::default(),
414-
imports: vec![],
415-
symbols: entries,
416-
};
417-
let output = DocPrinter::new(&doc, false, false).to_string();
418-
assert!(output.contains("class Hello"));
419-
assert!(output.contains("interface Hello"));
407+
let text = DocPrinter::new(&output, false, false).to_string();
408+
assert!(text.contains("class Hello"));
409+
assert!(text.contains("interface Hello"));
420410
}
421411

422412
#[tokio::test]
@@ -434,19 +424,16 @@ async fn deep_reexports() {
434424
],
435425
)
436426
.await;
437-
let entries = DocParser::new(
427+
let output = DocParser::new(
438428
&graph,
439429
&analyzer,
440430
&[specifier],
441431
DocParserOptions::default(),
442432
)
443433
.unwrap()
444434
.parse()
445-
.unwrap()
446-
.into_values()
447-
.next()
448-
.unwrap()
449-
.symbols;
435+
.unwrap();
436+
let entries = &output.values().next().unwrap().symbols;
450437

451438
let expected_json = json!([
452439
{
@@ -471,16 +458,11 @@ async fn deep_reexports() {
471458
}]
472459
}
473460
]);
474-
let actual = serde_json::to_value(&entries).unwrap();
461+
let actual = serde_json::to_value(entries).unwrap();
475462
assert_eq!(actual, expected_json);
476463

477-
let doc = crate::node::Document {
478-
module_doc: Default::default(),
479-
imports: vec![],
480-
symbols: entries,
481-
};
482464
assert!(
483-
DocPrinter::new(&doc, false, false)
465+
DocPrinter::new(&output, false, false)
484466
.to_string()
485467
.contains("const foo")
486468
);

tests/helpers/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ impl TestBuilder {
7777
)
7878
.unwrap();
7979

80-
let document = parser.parse().unwrap().into_values().next().unwrap();
80+
let output = parser.parse().unwrap();
81+
let document = output.values().next().unwrap();
8182

82-
let doc = DocPrinter::new(&document, false, self.private).to_string();
83+
let doc = DocPrinter::new(&output, false, self.private).to_string();
8384
let diagnostics = parser.take_diagnostics();
8485

8586
BuildResult {
8687
diagnostics,
87-
json_output: serde_json::to_value(&document).unwrap(),
88+
json_output: serde_json::to_value(document).unwrap(),
8889
text_output: doc,
8990
}
9091
}

0 commit comments

Comments
 (0)