Skip to content

Commit

Permalink
Add Docs.hasdoc function (#52139)
Browse files Browse the repository at this point in the history
Add helper method `Docs.hasdoc` to facilitate automated testing of
whether documentation exists.

Closes: #51174.
Co-authored-by: Steven G. Johnson <[email protected]>
  • Loading branch information
jariji and stevengj committed Dec 5, 2023
1 parent fd41af5 commit 4209474
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ New library features
* `replace(string, pattern...)` now supports an optional `IO` argument to
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]).

Standard library changes
------------------------
Expand Down
20 changes: 20 additions & 0 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,24 @@ function parsedoc end
function apropos end
function doc end


"""
Docs.hasdoc(mod::Module, sym::Symbol)::Bool
Return `true` if `sym` in `mod` has a docstring and `false` otherwise.
"""
hasdoc(mod::Module, sym::Symbol) = hasdoc(Docs.Binding(mod, sym))
function hasdoc(binding::Docs.Binding, sig::Type = Union{})
# this function is based on the Base.Docs.doc method implemented
# in REPL/src/docview.jl. TODO: refactor and unify these methods.
defined(binding) && !isnothing(getdoc(resolve(binding), sig)) && return true
for mod in modules
dict = meta(mod; autoinit=false)
!isnothing(dict) && haskey(dict, binding) && return true
end
alias = aliasof(binding)
return alias == binding ? false : hasdoc(alias, sig)
end


end
3 changes: 3 additions & 0 deletions doc/src/manual/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ environments provide a way to access documentation directly:
- In [Juno](https://junolab.org) using `Ctrl-J, Ctrl-D` will show the documentation for the object
under the cursor.


`Docs.hasdoc(module, name)::Bool` tells whether a name has a docstring.

## Writing Documentation

Julia enables package developers and users to document functions, types and other objects easily
Expand Down
7 changes: 7 additions & 0 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ $$latex literal$$
"""
function break_me_docs end


# `hasdoc` returns `true` on a name with a docstring.
@test Docs.hasdoc(Base, :map)
# `hasdoc` returns `false` on a name without a docstring.
@test !isdefined(Base, :_this_name_doesnt_exist_) && !Docs.hasdoc(Base, :_this_name_doesnt_exist_)
@test isdefined(Base, :_typed_vcat) && !Docs.hasdoc(Base, :_typed_vcat)

# issue #11548

module ModuleMacroDoc
Expand Down

0 comments on commit 4209474

Please sign in to comment.