Skip to content

Commit

Permalink
Cover more cases for baremodule use (#34)
Browse files Browse the repository at this point in the history
* Cover more cases for baremodule use

* Interpolate `Core.eval` calls

Co-Authored-By: David Widmann <david.widmann@it.uu.se>

Co-authored-by: David Widmann <david.widmann@it.uu.se>
  • Loading branch information
ararslan and devmotion committed Aug 24, 2021
1 parent fd87dbf commit f0d5b25
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "Reexport"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
authors = ["Simon Kornblith <simon@simonster.com>"]
version = "1.2.1"
version = "1.2.2"

[compat]
julia = "1"
Expand Down
8 changes: 5 additions & 3 deletions src/Reexport.jl
Expand Up @@ -18,18 +18,20 @@ function reexport(m::Module, ex::Expr)
ex.head === :toplevel && all(e -> isa(e, Expr) && e.head === :using, ex.args) ||
error("@reexport: syntax error")

eval = GlobalRef(Core, :eval)

if ex.head === :module
# @reexport {using, import} module Foo ... end
modules = Any[ex.args[2]]
ex = Expr(:toplevel, ex, :(using .$(ex.args[2])))
elseif ex.head in (:using, :import) && ex.args[1].head == :(:)
# @reexport {using, import} Foo: bar, baz
symbols = [e.args[end] for e in ex.args[1].args[2:end]]
return Expr(:toplevel, ex, :(eval(Expr(:export, $symbols...))))
return Expr(:toplevel, ex, :($eval($m, Expr(:export, $symbols...))))
elseif ex.head === :import && all(e -> e.head === :., ex.args)
# @reexport import Foo.bar, Baz.qux
symbols = Any[e.args[end] for e in ex.args]
return Expr(:toplevel, ex, :(eval(Expr(:export, $symbols...))))
return Expr(:toplevel, ex, :($eval($m, Expr(:export, $symbols...))))
else
# @reexport using Foo, Bar, Baz
modules = Any[e.args[end] for e in ex.args]
Expand All @@ -38,7 +40,7 @@ function reexport(m::Module, ex::Expr)
names = GlobalRef(@__MODULE__, :exported_names)
out = Expr(:toplevel, ex)
for mod in modules
push!(out.args, :(Core.eval($m, Expr(:export, $names($mod)...))))
push!(out.args, :($eval($m, Expr(:export, $names($mod)...))))
end
return out
end
Expand Down
33 changes: 26 additions & 7 deletions test/runtests.jl
Expand Up @@ -219,11 +219,30 @@ end
@test Set(names(X15)) == Set([:X15, :a])
end

baremodule B16
using Reexport
@reexport using Test
end
using .B16
@testset "baremodule" begin
@test Base.isexported(B16, Symbol("@test"))
module X16
using Reexport, Test
baremodule A
using Reexport
@reexport using Test
end
baremodule B
using Reexport
@reexport using Test: @testset
end
baremodule C
using Reexport
@reexport import Test.@test
end
baremodule D
using Reexport
# Ensure that there are no module name conflicts with Core
baremodule Core
end
end
@testset "baremodule" begin
@test Base.isexported(A, Symbol("@test"))
@test Reexport.exported_names(B) == [Symbol("@testset"), :B]
@test Reexport.exported_names(C) == [Symbol("@test"), :C]
@test Reexport.exported_names(D) == [:D]
end
end

0 comments on commit f0d5b25

Please sign in to comment.