Skip to content

Commit cbe863c

Browse files
ranochaKristoffer Carlssontimholy
authored
let code_string return nothing if definition returns nothing (#92)
As suggested by @KristofferC in JuliaDocs/Documenter.jl#1779 (comment). This is related to problems using CodeTracking.jl with Documenter.jl (see JuliaDocs/Documenter.jl#1779) or Jupyter notebooks (see #51). Instead of throwing an exception, `code_string` now returns `nothing` if `definition` returns `nothing`. This makes it easier to handle such a problematic case by the user. Co-authored-by: Kristoffer Carlsson <kristoffer.carlsson@juliacomputing.com> Co-authored-by: Tim Holy <tim.holy@gmail.com>
1 parent b2e5fae commit cbe863c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/CodeTracking.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ end
296296
297297
Returns the code-string for the method definition for `f` with the specified types.
298298
"""
299-
code_string(f, t) = definition(String, which(f, t))[1]
299+
function code_string(f, t)
300+
def = definition(String, which(f, t))
301+
return def === nothing ? nothing : def[1]
302+
end
300303
macro code_string(ex0...)
301304
InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :code_string, ex0)
302305
end

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
148148
m = first(methods(getfield(mod, :eval)))
149149
@test definition(String, m) === nothing
150150
end
151+
152+
# Related to issue [#51](https://github.com/timholy/CodeTracking.jl/issues/51)
153+
# and https://github.com/JuliaDocs/Documenter.jl/issues/1779
154+
ex = :(f_no_linenum(::Int) = 1)
155+
deleteat!(ex.args[2].args, 1) # delete the file & line number info
156+
eval(ex)
157+
@test code_string(f_no_linenum, (Int,)) === nothing
151158
end
152159

153160
@testset "With Revise" begin

0 commit comments

Comments
 (0)