Skip to content

Commit 6b93e04

Browse files
authored
Modules with docstring: don't replicate the module expression (#229)
Since we create modules incrementally as we advance through `split_expressions!`, when we extract docstrings we need to avoid duplicating the module definition itself. Fixes JunoLab/Juno.jl#271
1 parent 09f5d0f commit 6b93e04

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/construct.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,24 @@ function split_expressions!(modexs, docexprs, lex::Expr, mod::Module, ex::Expr;
412412
end
413413
split_expressions!(modexs, docexprs, lex, newmod, ex.args[3]; extract_docexprs=extract_docexprs, filename=filename)
414414
elseif extract_docexprs && is_doc_expr(ex) && length(ex.args) >= 4
415+
body = ex.args[4]
416+
if isa(body, Expr) && body.head != :call
417+
split_expressions!(modexs, docexprs, lex, mod, body; extract_docexprs=extract_docexprs, filename=filename)
418+
end
415419
docexs = get(docexprs, mod, nothing)
416420
if docexs === nothing
417421
docexs = docexprs[mod] = Expr[]
418422
end
419-
push!(docexs, ex)
420-
body = ex.args[4]
421-
if isa(body, Expr) && body.head != :call
422-
split_expressions!(modexs, docexprs, lex, mod, body; extract_docexprs=extract_docexprs, filename=filename)
423+
if isexpr(body, :module)
424+
# If it's a module expression, don't include the entire expression, just document the module itself.
425+
excopy = Expr(ex.head, ex.args[1], ex.args[2], ex.args[3])
426+
push!(excopy.args, body.args[2])
427+
if length(ex.args) > 4
428+
append!(excopy.args, ex.args[5:end]) # there should only be a 5th, but just for robustness
429+
end
430+
push!(docexs, excopy)
431+
else
432+
push!(docexs, ex)
423433
end
424434
else
425435
if isempty(lex.args)

test/toplevel.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ end
2121
""")
2222
modexs, docexs = JuliaInterpreter.split_expressions(Main, ex; extract_docexprs=true)
2323
@test isempty(docexs)
24+
# https://github.com/JunoLab/Juno.jl/issues/271
25+
ex = quote
26+
"""
27+
Special Docstring
28+
"""
29+
module DocStringTest
30+
function foo()
31+
x = 4 + 5
32+
end
33+
end
34+
end
35+
modexs, docexs = JuliaInterpreter.split_expressions(Main, ex; extract_docexprs=true)
36+
m, ex = first(modexs)
37+
@test m == Main.DocStringTest
38+
dex = first(docexs[Main])
39+
@test dex.args[4] == :DocStringTest
40+
eval(dex)
41+
io = IOBuffer()
42+
show(io, @doc(Main.DocStringTest))
43+
@test occursin("Special", String(take!(io)))
2444

2545
@test JuliaInterpreter.prepare_thunk(Main, :(export foo)) === nothing
2646
@test JuliaInterpreter.prepare_thunk(Base.Threads, :(global Condition)) === nothing

0 commit comments

Comments
 (0)