Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docs.undocumented_names #52413

Merged
merged 17 commits into from
Dec 31, 2023
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ New library features
write the output to a stream rather than returning a string ([#48625]).
* `sizehint!(s, n)` now supports an optional `shrink` argument to disable shrinking ([#51929]).
* New function `Docs.hasdoc(module, symbol)` tells whether a name has a docstring ([#52139]).
* New function `Docs.check_documented(module; all)` checks whether a module's names are documented ([#52413]).

Standard library changes
------------------------
Expand Down
11 changes: 11 additions & 0 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,15 @@ function hasdoc(binding::Docs.Binding, sig::Type = Union{})
end


"""
check_documented(mod::Module; all=false)

Check all names in the module are documented. `all` determines which names are checked,
following the behavior of `names(mod; all)`.
stevengj marked this conversation as resolved.
Show resolved Hide resolved
"""
function check_documented(mod::Module; all=false)
nodocs = filter!(sym -> !hasdoc(mod, sym), names(mod; all))
isempty(nodocs) || error("missing docstrings in $mod: $nodocs")
end

end
3 changes: 2 additions & 1 deletion doc/src/manual/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ environments provide a way to access documentation directly:
under the cursor.


`Docs.hasdoc(module, name)::Bool` tells whether a name has a docstring.
`Docs.hasdoc(module, name)::Bool` tells whether a name has a docstring. `Docs.check_documented(module; all)`
checks that the names in a module are documented.

## Writing Documentation

Expand Down
22 changes: 22 additions & 0 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ function break_me_docs end
@test !isdefined(Base, :_this_name_doesnt_exist_) && !Docs.hasdoc(Base, :_this_name_doesnt_exist_)
@test isdefined(Base, :_typed_vcat) && !Docs.hasdoc(Base, :_typed_vcat)

"This module has names without documentation."
module _ModuleWithUndocumentedNames
export f
f() = 1
end

"This module has some documentation."
module _ModuleWithSomeDocumentedNames
export f
"f() is 1."
f() = 1
g() = 2
end

# Error for undocumented names.
@test_throws ErrorException Docs.check_documented(_ModuleWithUndocumentedNames)
# Pass for documented exported names.
@test Docs.check_documented(_ModuleWithSomeDocumentedNames)
# Error for undocumented unexported names when `all=true`.
@test_throws ErrorException Docs.check_documented(_ModuleWithSomeDocumentedNames; all=true)


# issue #11548

module ModuleMacroDoc
Expand Down