Skip to content

Commit

Permalink
fix docstring parsing issue (#41)
Browse files Browse the repository at this point in the history
* fix docstring parsing issue

* bump patch version
  • Loading branch information
jkrumbiegel authored Dec 7, 2021
1 parent ece37e3 commit 7addf5f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
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

2 comments on commit 7addf5f

@jkrumbiegel
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/50081

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.9 -m "<description of version>" 7addf5fe01df85d91ee5fc7b4f7283bfa858e7fa
git push origin v0.4.9

Please sign in to comment.