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 function scopes when using pipes #24

Merged
merged 1 commit into from
Jan 21, 2025
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestHandcalcFunctions = "6ba57fb7-81df-4b24-8e8e-a3885b6fcae7"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulLatexify = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

[targets]
test = ["Test", "Unitful", "UnitfulLatexify", "TestHandcalcFunctions"]
test = ["Test", "Unitful", "UnitfulLatexify", "TestHandcalcFunctions", "Revise"]
7 changes: 6 additions & 1 deletion src/handfunc_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ function _walk_func_body(expr::Expr, found_module)

prewalk(expr) do x
if @capture(x, f_(args__))

# if f is |>, need to make sure function after |> gets properly scoped
if f == :(|>)
x.args[3] = Meta.parse(found_module_string * "." * string(x.args[3]))
end
len = length(collect(Leaves(f)))
if len == 1
if isdefined(Main, f) # should really be `Base` but `Main` is used for now
Expand Down Expand Up @@ -256,4 +261,4 @@ function remove_return_statements(expr::Expr)
end

return filtered_expr
end
end
17 changes: 17 additions & 0 deletions test/FunctionTestModule.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module FunctionTestModule
using Unitful

module cnv
using Unitful
to_L(x) = x |> u"inch"
end

function add_cnv(a, b)
c = a + b |> cnv.to_L
end

function add_inch(a, b)
c = a + b |> u"inch"
end

end
15 changes: 15 additions & 0 deletions test/handfunc_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,18 @@ c1, c2 &= \mathrm{\mathrm{\mathrm{TestHandcalcFunctions}\left( SubA \right)}\lef
calc = @handcalcs result = TestHandcalcFunctions.test_function_finder(5, 15) not_funcs = [:calc_Iy :sub_module_func]
@test calc == replace(expected, "\r" => "") # for whatever reason the expected had addittional carriage returns (\r)
# ***************************************************

# Check piped functions not in scope
# ***************************************************
# ***************************************************
a = 5*u"inch"
b = 6*u"inch"
calc = @handcalcs result = FunctionTestModule.add_cnv(a, b)
calc2 = @handcalcs result = FunctionTestModule.add_inch(a, b)
expected = L"$\begin{aligned}
c &= a + b = 5\;\mathrm{inch} + 6\;\mathrm{inch} = 11\;\mathrm{inch}
\end{aligned}$"
@test calc == expected
@test calc2 == expected
# ***************************************************

4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ using Handcalcs
using LaTeXStrings, Unitful, UnitfulLatexify
using Test
using TestHandcalcFunctions
using Revise
isdefined(Main, :Revise) ? Main.Revise.includet("FunctionTestModule.jl") : include("FunctionTestModule.jl")
# using .TestFunctions


Expand All @@ -10,4 +12,4 @@ using TestHandcalcFunctions
@testset "UnitfulLatexify " begin include("unitful_test.jl") end
@testset "handcalcs macro " begin include("handcalcs_macro.jl") end
@testset "handfunc macro " begin include("handfunc_macro.jl") end
@testset "defaults macro " begin include("default_h_kwargs.jl") end
@testset "defaults macro " begin include("default_h_kwargs.jl") end
Loading