Skip to content

Commit 1532071

Browse files
committed
fixup! Add support for default values to TYPEDSIGNATURES
1 parent fe01fe3 commit 1532071

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/parsing.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,28 @@ function parse_arglist!(exprs, args, kwargs, is_kwarg_list=false)
7979
end
8080

8181
# Find a :call expression within an Expr. This will take care of ignoring other
82-
# tokens like `where` clauses.
83-
function find_call_expr(expr::Expr)
84-
if Meta.isexpr(expr, :macrocall) && expr.args[1] === Symbol("@generated")
85-
# If this is a generated function, find the first := expr to find
86-
# the :call expr.
87-
assignment_idx = findfirst(x -> x isa Expr && Meta.isexpr(x, :(=)), expr.args)
82+
# tokens like `where` clauses. It will return `nothing` if a :call expression
83+
# wasn't found.
84+
function find_call_expr(obj)
85+
if Meta.isexpr(obj, :call)
86+
# Base case: we've found the :call expression
87+
return obj
88+
elseif obj isa Symbol || (obj isa Expr && isempty(obj.args))
89+
# Base case: this is the end of a branch in the expression tree
90+
return nothing
91+
end
8892

89-
expr.args[assignment_idx].args[1]
90-
elseif Meta.isexpr(expr, :(=))
91-
find_call_expr(expr.args[1])
92-
elseif Meta.isexpr(expr, :where)
93-
# Function with one or more `where` clauses
94-
find_call_expr(expr.args[1])
95-
elseif Meta.isexpr(expr, :function)
96-
find_call_expr(expr.args[1])
97-
elseif Meta.isexpr(expr, :call)
98-
expr
99-
else
100-
Meta.dump(expr)
101-
error("Can't parse current expr (printed above)")
93+
# Recursive case: recurse over all the Expr arguments
94+
for arg in obj.args
95+
if arg isa Expr
96+
result = find_call_expr(arg)
97+
if !isnothing(result)
98+
return result
99+
end
100+
end
102101
end
102+
103+
return nothing
103104
end
104105

105106
# Parse an expression to find a :call expr, and return as much information as

test/TestModule/M.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct K
4949
K(; a = 1) = new()
5050
end
5151

52+
macro m1(expr) expr end
53+
macro m2(expr) expr end
54+
55+
@m1 @m2 l(x::Int, y=1) = x + y
56+
5257
abstract type AbstractType1 <: Integer end
5358
abstract type AbstractType2{S, T <: Integer} <: Integer end
5459

test/tests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,15 @@ end
471471

472472
end
473473

474-
474+
doc.data = Dict(
475+
:binding => Docs.Binding(M, :l),
476+
:typesig => Union{Tuple{Int}, Tuple{Int, Any}},
477+
:module => M,
478+
)
479+
DSE.format(TypedSignatures(get_expr(M.l, Int, Int)), buf, doc)
480+
str = String(take!(buf))
481+
@test occursin("\nl(x::Int64) -> Int64\n", str)
482+
@test occursin("\nl(x::Int64, y=1) -> Any\n", str)
475483
end
476484

477485
@testset "function names" begin

0 commit comments

Comments
 (0)