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

Missing package add prompt: Restrict which exprs to search in #43457

Merged
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
4 changes: 3 additions & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ function modules_to_be_loaded(ast::Expr, mods::Vector{Symbol} = Symbol[])
end
end
for arg in ast.args
arg isa Expr && modules_to_be_loaded(arg, mods)
if arg isa Expr && arg.head in [:block, :if, :using, :import]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if arg isa Expr && arg.head in [:block, :if, :using, :import]
if arg isa Expr && arg.head in (:block, :if, :using, :import)

The [] form allocates the same tuple as above, then splats it to a new array, on every iteration of the loop.

Copy link
Member

Choose a reason for hiding this comment

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

Or even more concisely:

Suggested change
if arg isa Expr && arg.head in [:block, :if, :using, :import]
if isexpr(arg, (:block, :if, :using, :import))

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, thanks #43708

modules_to_be_loaded(arg, mods)
end
end
filter!(mod -> !in(String(mod), ["Base", "Main", "Core"]), mods) # Exclude special non-package modules
return unique(mods)
Expand Down
9 changes: 9 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,15 @@ end

mods = REPL.modules_to_be_loaded(Base.parse_input_line("Foo"))
@test isempty(mods)

mods = REPL.modules_to_be_loaded(Base.parse_input_line("@eval using Foo"))
@test isempty(mods)
mods = REPL.modules_to_be_loaded(Base.parse_input_line("begin using Foo; @eval using Bar end"))
@test mods == [:Foo]
mods = REPL.modules_to_be_loaded(Base.parse_input_line("Core.eval(Main,\"using Foo\")"))
@test isempty(mods)
mods = REPL.modules_to_be_loaded(Base.parse_input_line("begin using Foo; Core.eval(Main,\"using Foo\") end"))
@test mods == [:Foo]
end
end

Expand Down