Skip to content

Commit a640306

Browse files
committed
rustdoc: Prune undocumented ifaces
1 parent 2c495a9 commit a640306

File tree

1 file changed

+124
-19
lines changed

1 file changed

+124
-19
lines changed

src/rustdoc/prune_undoc_pass.rs

Lines changed: 124 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ fn run(
2222
fold_fn: fold_fn,
2323
fold_const: fold_const,
2424
fold_enum: fold_enum,
25-
fold_res: fold_res
25+
fold_res: fold_res,
26+
fold_iface: fold_iface
2627
with *fold::default_seq_fold(ctxt)
2728
});
2829
fold.fold_crate(fold, doc)
@@ -75,6 +76,14 @@ fn fold_mod(
7576
none
7677
}
7778
}
79+
doc::ifacetag(ifacedoc) {
80+
let doc = fold.fold_iface(fold, ifacedoc);
81+
if fold.ctxt.have_docs {
82+
some(doc::ifacetag(doc))
83+
} else {
84+
none
85+
}
86+
}
7887
_ { some(itemtag) }
7988
}
8089
}
@@ -91,36 +100,42 @@ fn fold_fn(
91100
fold: fold::fold<ctxt>,
92101
doc: doc::fndoc
93102
) -> doc::fndoc {
94-
let have_arg_docs = false;
95103
let doc = {
96-
args: vec::filter_map(doc.args) {|doc|
97-
if option::is_some(doc.desc) {
98-
have_arg_docs = true;
99-
some(doc)
100-
} else {
101-
none
102-
}
103-
},
104-
return: {
105-
ty: if option::is_some(doc.return.desc) {
106-
doc.return.ty
107-
} else {
108-
none
109-
}
110-
with doc.return
111-
}
104+
args: prune_args(doc.args),
105+
return: prune_return(doc.return)
112106
with doc
113107
};
114108

115109
fold.ctxt.have_docs =
116110
doc.brief != none
117111
|| doc.desc != none
118-
|| have_arg_docs
112+
|| vec::is_not_empty(doc.args)
119113
|| doc.return.desc != none
120114
|| doc.failure != none;
121115
ret doc;
122116
}
123117

118+
fn prune_args(docs: [doc::argdoc]) -> [doc::argdoc] {
119+
vec::filter_map(docs) {|doc|
120+
if option::is_some(doc.desc) {
121+
some(doc)
122+
} else {
123+
none
124+
}
125+
}
126+
}
127+
128+
fn prune_return(doc: doc::retdoc) -> doc::retdoc {
129+
{
130+
ty: if option::is_some(doc.desc) {
131+
doc.ty
132+
} else {
133+
none
134+
}
135+
with doc
136+
}
137+
}
138+
124139
#[test]
125140
fn should_elide_undocumented_arguments() {
126141
let source = "#[doc = \"hey\"] fn a(b: int) { }";
@@ -316,3 +331,93 @@ fn should_not_elide_resources_with_documented_args() {
316331
let doc = run(srv, doc);
317332
assert vec::is_not_empty(doc.topmod.resources());
318333
}
334+
335+
fn fold_iface(
336+
fold: fold::fold<ctxt>,
337+
doc: doc::ifacedoc
338+
) -> doc::ifacedoc {
339+
let doc = fold::default_seq_fold_iface(fold, doc);
340+
let doc = {
341+
methods: vec::map(doc.methods) {|doc|
342+
{
343+
args: prune_args(doc.args),
344+
return: prune_return(doc.return)
345+
with doc
346+
}
347+
}
348+
with doc
349+
};
350+
let methods_have_docs = vec::foldl(false, doc.methods) {|accum, doc|
351+
accum
352+
|| doc.brief != none
353+
|| doc.desc != none
354+
|| vec::is_not_empty(doc.args)
355+
|| doc.return.desc != none
356+
|| doc.failure != none
357+
};
358+
fold.ctxt.have_docs =
359+
doc.brief != none
360+
|| doc.desc != none
361+
|| methods_have_docs;
362+
ret doc;
363+
}
364+
365+
#[test]
366+
fn should_elide_undocumented_ifaces() {
367+
let source = "iface i { fn a(); }";
368+
let srv = astsrv::mk_srv_from_str(source);
369+
let doc = extract::from_srv(srv, "");
370+
let doc = attr_pass::mk_pass()(srv, doc);
371+
let doc = run(srv, doc);
372+
assert vec::is_empty(doc.topmod.ifaces());
373+
}
374+
375+
#[test]
376+
fn should_not_elide_documented_ifaces() {
377+
let source = "#[doc = \"hey\"] iface i { fn a(); }";
378+
let srv = astsrv::mk_srv_from_str(source);
379+
let doc = extract::from_srv(srv, "");
380+
let doc = attr_pass::mk_pass()(srv, doc);
381+
let doc = run(srv, doc);
382+
assert vec::is_not_empty(doc.topmod.ifaces());
383+
}
384+
385+
#[test]
386+
fn should_not_elide_ifaces_with_documented_methods() {
387+
let source = "iface i { #[doc = \"hey\"] fn a(); }";
388+
let srv = astsrv::mk_srv_from_str(source);
389+
let doc = extract::from_srv(srv, "");
390+
let doc = attr_pass::mk_pass()(srv, doc);
391+
let doc = run(srv, doc);
392+
assert vec::is_not_empty(doc.topmod.ifaces());
393+
}
394+
395+
#[test]
396+
fn should_not_elide_undocumented_methods() {
397+
let source = "#[doc = \"hey\"] iface i { fn a(); }";
398+
let srv = astsrv::mk_srv_from_str(source);
399+
let doc = extract::from_srv(srv, "");
400+
let doc = attr_pass::mk_pass()(srv, doc);
401+
let doc = run(srv, doc);
402+
assert vec::is_not_empty(doc.topmod.ifaces()[0].methods);
403+
}
404+
405+
#[test]
406+
fn should_elide_undocumented_method_args() {
407+
let source = "#[doc = \"hey\"] iface i { fn a(); }";
408+
let srv = astsrv::mk_srv_from_str(source);
409+
let doc = extract::from_srv(srv, "");
410+
let doc = attr_pass::mk_pass()(srv, doc);
411+
let doc = run(srv, doc);
412+
assert vec::is_empty(doc.topmod.ifaces()[0].methods[0].args);
413+
}
414+
415+
#[test]
416+
fn should_elide_undocumented_method_return_values() {
417+
let source = "#[doc = \"hey\"] iface i { fn a() -> int; }";
418+
let srv = astsrv::mk_srv_from_str(source);
419+
let doc = extract::from_srv(srv, "");
420+
let doc = attr_pass::mk_pass()(srv, doc);
421+
let doc = run(srv, doc);
422+
assert doc.topmod.ifaces()[0].methods[0].return.ty == none;
423+
}

0 commit comments

Comments
 (0)