Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docstring parsing issue #41

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Chain"
uuid = "8be319e6-bccf-4806-a6f7-6fae938471bc"
authors = ["Julius Krumbiegel"]
version = "0.4.8"
version = "0.4.9"

[compat]
julia = "1"
Expand Down
44 changes: 25 additions & 19 deletions src/Chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,8 @@ end
rewrite(l::LineNumberNode, replacement) = (l, replacement)

function rewrite_chain_block(firstpart, block)
block_expressions = block.args

# empty chain returns firstpart
if all(x -> x isa LineNumberNode, block_expressions)
return esc(firstpart)
end

rewritten_exprs = []
replacement = firstpart

for expr in block_expressions
rewritten, replacement = rewrite(expr, replacement)
push!(rewritten_exprs, rewritten)
end

result = Expr(:let, Expr(:block), Expr(:block, rewritten_exprs..., replacement))

:($(esc(result)))
pushfirst!(block.args, firstpart)
rewrite_chain_block(block)
end

"""
Expand Down Expand Up @@ -191,14 +175,16 @@ function rewrite_chain_block(block)
block_expressions = block.args
isempty(block_expressions) && error("No expressions found in chain block.")

reconvert_docstrings!(block_expressions)

# assign first line to first gensym variable
firstvar = gensym()
rewritten_exprs = []
replacement = firstvar

did_first = false
for expr in block_expressions
# could be and expression first or a LineNumberNode, so a bit convoluted
# could be an expression first or a LineNumberNode, so a bit convoluted
# we just do the firstvar transformation for the first non LineNumberNode
# we encounter
if !(did_first || expr isa LineNumberNode)
Expand All @@ -217,6 +203,26 @@ function rewrite_chain_block(block)
:($(esc(result)))
end

# if a line in a chain is a string, it can be parsed as a docstring
# for whatever is on the following line. because this is unexpected behavior
# for most users, we convert all docstrings back to separate lines.
function reconvert_docstrings!(args::Vector)
docstring_indices = findall(args) do arg
(arg isa Expr
&& arg.head == :macrocall
&& length(arg.args) == 4
&& arg.args[1] == GlobalRef(Core, Symbol("@doc")))
end
# replace docstrings from back to front because this leaves the earlier indices intact
for i in reverse(docstring_indices)
e = args[i]
str = e.args[3]
nextline = e.args[4]
splice!(args, i:i, [str, nextline])
end
args
end

"""
@chain(block::Expr)

Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,27 @@ end
_ + 1
@aside 1 + 2
end
end

@testset "workaround for docstring parsing" begin
@test "hi" == @chain " hi " strip
@test "hi" == @chain " hi " begin
strip
end
@test "hi" == @chain begin
" hi "
strip
end
@test "hi" == @chain begin
"hi"
" $_ "
strip
end
@test "A" == @chain begin
'a'
" $_ "
strip
"$_"
uppercase
end
end