Skip to content

Commit e6857d5

Browse files
authored
Store record metadata in the documentation chunk (#13939)
1 parent 1c19c60 commit e6857d5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/elixir/lib/record.ex

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ defmodule Record do
3636
3737
## Reflection
3838
39-
A list of all records in a module, if any, can be retrieved by reading the
40-
`@__records__` module attribute. It returns a list of maps with the record
41-
kind, name, tag, and fields. The attribute is only available inside the
42-
module definition.
39+
The record tag and its fields are stored as metadata in the "Docs" chunk
40+
of the record definition macro. You can retrieve the documentation for
41+
a module by calling `Code.fetch_docs/1`.
4342
"""
4443

4544
@doc """
@@ -253,6 +252,7 @@ defmodule Record do
253252
tag = tag || name
254253
fields = Record.__record__(__MODULE__, :defrecord, name, tag, kv)
255254

255+
@doc record: {tag, fields}
256256
defmacro unquote(name)(args \\ []) do
257257
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
258258
end
@@ -312,6 +312,8 @@ defmodule Record do
312312
error_on_duplicate_record(module, name)
313313

314314
fields = fields(kind, kv)
315+
316+
# TODO: Remove me on Elixir v2.0
315317
Module.register_attribute(module, :__records__, accumulate: true)
316318

317319
Module.put_attribute(module, :__records__, %{

lib/elixir/test/elixir/record_test.exs

+14
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ defmodule RecordTest do
341341
end
342342
end
343343

344+
test "docs metadata" do
345+
import PathHelpers
346+
347+
write_beam(
348+
defmodule Metadata do
349+
Record.defrecord(:user, foo: 0, bar: "baz")
350+
end
351+
)
352+
353+
{:docs_v1, 348, :elixir, "text/markdown", _, %{}, docs} = Code.fetch_docs(RecordTest.Metadata)
354+
{{:macro, :user, 1}, _meta, _sig, _docs, metadata} = List.keyfind(docs, {:macro, :user, 1}, 0)
355+
assert %{record: {:user, [foo: 0, bar: "baz"]}} = metadata
356+
end
357+
344358
describe "warnings" do
345359
import ExUnit.CaptureIO
346360

0 commit comments

Comments
 (0)