Skip to content

Commit 7e71223

Browse files
authored
Handle method defs that end in a semicolon (#106)
These get an extra `:toplevel` Expr wrapper, which we need to strip.
1 parent 63bbeaa commit 7e71223

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/CodeTracking.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function definition(::Type{String}, method::Method)
255255
iend = prevind(src, iend)
256256
if isfuncexpr(ex, methodname)
257257
iend = min(iend, lastindex(src))
258-
return strip(src[istart:iend], '\n'), line
258+
return clean_source(src[istart:iend]), line
259259
end
260260
# The function declaration was presumably on a previous line
261261
lineindex = lastindex(linestarts)
@@ -270,7 +270,15 @@ function definition(::Type{String}, method::Method)
270270
line -= 1
271271
end
272272
lineindex <= linestop && return nothing
273-
return chomp(src[istart:iend-1]), line
273+
return clean_source(src[istart:iend-1]), line
274+
end
275+
276+
function clean_source(src)
277+
src = strip(src, '\n')
278+
if endswith(src, ';')
279+
src = src[1:prevind(src, end)]
280+
end
281+
return src
274282
end
275283

276284
"""

src/utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ checkname(fname::QuoteNode, name) = checkname(fname.value, name)
2828

2929
function isfuncexpr(ex, name=nothing)
3030
# Strip any macros that wrap the method definition
31+
if ex isa Expr && ex.head === :toplevel
32+
ex = ex.args[end]
33+
end
3134
while ex isa Expr && ex.head === :macrocall && length(ex.args) >= 3
3235
ex = ex.args[end]
3336
end

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
104104
@test whereis(m) == ("REPL[1]", 1)
105105
CodeTracking.method_lookup_callback[] = oldlookup
106106

107+
# Method definitions ending in semicolon
108+
@test code_string(has_semicolon1, (Int, Int)) == "has_semicolon1(x, y) = x + y"
109+
107110
# Test implicit replacement of `BUILDBOT_STDLIB_PATH`
108111
m = first(methods(Test.eval))
109112
@test isfile(whereis(m)[1])
@@ -228,6 +231,15 @@ end
228231
m = first(methods(f))
229232
@test definition(String, first(methods(f))) == (fstr, 1)
230233
@test !isempty(signatures_at(String(m.file), m.line))
234+
235+
histidx += 1
236+
fstr = "has_semicolon2(x, y) = x + y;"
237+
ex = Base.parse_input_line(fstr; filename="REPL[$histidx]")
238+
f = Core.eval(Main, ex)
239+
push!(hp.history, fstr)
240+
@test code_string(has_semicolon2, (Int, Int)) == "has_semicolon2(x, y) = x + y"
241+
242+
pop!(hp.history)
231243
pop!(hp.history)
232244
elseif haskey(ENV, "CI")
233245
error("CI Revise tests must be run with -i")

test/script.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ if isdefined(Base, Symbol("@assume_effects"))
6565
return res
6666
end
6767
end
68-
end
68+
end
69+
70+
has_semicolon1(x, y) = x + y;

0 commit comments

Comments
 (0)