Skip to content

Commit 648781d

Browse files
author
Kristoffer Carlsson
authored
fix some issues with module prefixed functions (#67)
* fix some issues with module prefixed functions
1 parent 78ce8a4 commit 648781d

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/utils.jl

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
function isfuncexpr(ex, name=nothing)
2-
checkname(fdef::Expr, name) = checkname(fdef.args[1], name)
3-
checkname(fname::Symbol, name::Symbol) = begin
4-
fname === name && return true
5-
startswith(string(name), string('#', fname, '#')) && return true
6-
return false
1+
function checkname(fdef::Expr, name)
2+
fproto = fdef.args[1]
3+
fdef.head === :where && return checkname(fproto, name)
4+
if fproto isa Expr
5+
# Is the check below redundant?
6+
fproto.head === :. || return false
7+
# E.g. `function Mod.bar.foo(a, b)`
8+
return checkname(fproto.args[end], name)
79
end
8-
checkname(fname::Symbol, ::Nothing) = true
10+
return checkname(fproto, name)
11+
end
12+
checkname(fname::Symbol, name::Symbol) = begin
13+
fname === name && return true
14+
startswith(string(name), string('#', fname, '#')) && return true
15+
return false
16+
end
17+
checkname(fname::Symbol, ::Nothing) = true
18+
checkname(fname::QuoteNode, name) = checkname(fname.value, name)
919

20+
function isfuncexpr(ex, name=nothing)
1021
# Strip any macros that wrap the method definition
11-
while isexpr(ex, :macrocall) && length(ex.args) == 3
22+
while ex isa Expr && ex.head === :macrocall && length(ex.args) == 3
1223
ex = ex.args[3]
1324
end
1425
isa(ex, Expr) || return false
15-
ex.head == :function && return checkname(ex, name)
16-
if ex.head == :(=)
17-
a = ex.args[1]
18-
if isa(a, Expr)
19-
while a.head == :where
20-
a = a.args[1]
21-
isa(a, Expr) || return false
22-
end
23-
a.head == :call && return checkname(a, name)
24-
end
26+
if ex.head === :function || ex.head === :(=)
27+
return checkname(ex.args[1], name)
2528
end
2629
return false
2730
end
@@ -35,7 +38,7 @@ end
3538
linerange(arg) = linerange(convert(Expr, arg)) # Handle Revise's RelocatableExpr
3639

3740
function findline(ex, order)
38-
ex.head == :line && return ex.args[1], true
41+
ex.head === :line && return ex.args[1], true
3942
for a in order(ex.args)
4043
a isa LineNumberNode && return a.line, true
4144
if a isa Expr

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,13 @@ end
190190
@test loc == 28
191191
@test body == "func_2nd_kwarg(; kw=2) = true"
192192
end
193+
194+
@testset "method extensions" begin
195+
body, _ = CodeTracking.definition(String, @which Foo.Bar.fit(1))
196+
@test body == """
197+
function Foo.Bar.fit(m)
198+
return m
199+
end"""
200+
body, _ = CodeTracking.definition(String, @which Foo.Bar.fit(1, 2))
201+
@test body == "Foo.Bar.fit(a, b) = a + b"
202+
end

test/script.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ end
2626

2727
func_1st_nokwarg() = true
2828
func_2nd_kwarg(; kw=2) = true
29+
30+
module Foo
31+
module Bar
32+
function fit end
33+
end
34+
end
35+
36+
function Foo.Bar.fit(m)
37+
return m
38+
end
39+
40+
Foo.Bar.fit(a, b) = a + b

0 commit comments

Comments
 (0)