@@ -588,7 +588,10 @@ defmodule Code do
588
588
* `:type_docs` - list of all docstrings attached to
589
589
`@type` callbacks using the `@typedoc` attribute
590
590
591
- * `:all` - a keyword list with both `:docs` and `:moduledoc`
591
+ * `:all` - a keyword list with `:docs` and `:moduledoc`, `:callback_docs`,
592
+ and `:type_docs`.
593
+
594
+ If the module cannot be found, it returns `nil`.
592
595
593
596
## Examples
594
597
@@ -598,8 +601,14 @@ defmodule Code do
598
601
iex> String.split(text, "\n") |> Enum.at(0)
599
602
"Converts an atom to a char list."
600
603
604
+ # Module doesn't exist
605
+ iex> docs = Code.get_docs(ModuleNotGood, :all)
606
+ nil
607
+
601
608
"""
602
- def get_docs ( module , kind ) when is_atom ( module ) do
609
+ @ doc_kinds [ :docs , :moduledoc , :callback_docs , :type_docs , :all ]
610
+
611
+ def get_docs ( module , kind ) when is_atom ( module ) and kind in @ doc_kinds do
603
612
case :code . get_object_code ( module ) do
604
613
{ _module , bin , _beam_path } ->
605
614
do_get_docs ( bin , kind )
@@ -608,7 +617,7 @@ defmodule Code do
608
617
end
609
618
end
610
619
611
- def get_docs ( binpath , kind ) when is_binary ( binpath ) do
620
+ def get_docs ( binpath , kind ) when is_binary ( binpath ) and kind in @ doc_kinds do
612
621
do_get_docs ( String . to_char_list ( binpath ) , kind )
613
622
end
614
623
@@ -629,10 +638,8 @@ defmodule Code do
629
638
# unsupported chunk version
630
639
defp lookup_docs ( _ , _ ) , do: nil
631
640
632
- @ doc_sections [ :docs , :moduledoc , :callback_docs , :type_docs ]
633
-
634
641
defp do_lookup_docs ( docs , :all ) , do: docs
635
- defp do_lookup_docs ( docs , kind ) when kind in @ doc_sections ,
642
+ defp do_lookup_docs ( docs , kind ) ,
636
643
do: Keyword . get ( docs , kind )
637
644
638
645
## Helpers
0 commit comments