Skip to content

Commit

Permalink
fix nested single line chain (#43)
Browse files Browse the repository at this point in the history
* fix nested single line chain

* bump patch version
  • Loading branch information
jkrumbiegel authored Dec 8, 2021
1 parent 7addf5f commit 5bfc333
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 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.9"
version = "0.4.10"

[compat]
julia = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/Chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ function replace_underscores(expr::Expr, replacement)
# if a @chain macrocall is found, only its first arg can be replaced if it's an
# underscore, otherwise the macro insides are left untouched
if expr.head == :macrocall && expr.args[1] == Symbol("@chain")
length(expr.args) != 4 && error("Malformed nested @chain macro")
length(expr.args) < 3 && error("Malformed nested @chain macro")
expr.args[2] isa LineNumberNode || error("Malformed nested @chain macro")
arg3 = if expr.args[3] == Symbol("_")
found_underscore = true
replacement
else
expr.args[3]
end
newexpr = Expr(:macrocall, Symbol("@chain"), expr.args[2], arg3, expr.args[4])
newexpr = Expr(:macrocall, Symbol("@chain"), expr.args[2], arg3, expr.args[4:end]...)
# for all other expressions, their arguments are checked for underscores recursively
# and replaced if any are found
else
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,10 @@ end
"$_"
uppercase
end
end

@testset "nested single line chain" begin
@test 36 == @chain 1:3 begin
@chain _ sum _ ^ 2
end
end

2 comments on commit 5bfc333

@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/50155

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.10 -m "<description of version>" 5bfc3336ca71af00c2330644dc3f7f96daa53f00
git push origin v0.4.10

Please sign in to comment.