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

Remove can_be_function_wrapped #11

Merged
merged 1 commit into from
Nov 13, 2023
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
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ Set{Expr} with 1 element:

This function is used by Pluto's built-in package manager to learn which packages are used in a notebook.

### `can_be_function_wrapped`

```julia
can_be_function_wrapped(ex)::Bool
```

Is this code simple enough that we can wrap it inside a function, and run the function in global scope instead of running the code directly? Look for `Pluto.PlutoRunner.Computer` to learn more.

### `get_rootassignee`
```julia
get_rootassignee(ex)::Union{Symbol,Nothing}
Expand Down
38 changes: 0 additions & 38 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,3 @@ function get_rootassignee(ex::Expr, recurse::Bool = true)::Union{Symbol,Nothing}
end

get_rootassignee(ex::Any, recuse::Bool = true)::Union{Symbol,Nothing} = nothing


########################


"""
```julia
can_be_function_wrapped(ex)::Bool
```

Is this code simple enough that we can wrap it inside a function, and run the function in global scope instead of running the code directly? Look for `Pluto.PlutoRunner.Computer` to learn more.
"""
function can_be_function_wrapped(x::Expr)
if x.head === :global || # better safe than sorry
x.head === :using ||
x.head === :import ||
x.head === :export ||
x.head === :public || # Julia 1.11
x.head === :module ||
x.head === :incomplete ||
# Only bail on named functions, but anonymous functions (args[1].head == :tuple) are fine.
# TODO Named functions INSIDE other functions should be fine too
(x.head === :function && !Meta.isexpr(x.args[1], :tuple)) ||
x.head === :macro ||
# Cells containing macrocalls will actually be function wrapped using the expanded version of the expression
# See https://github.com/fonsp/Pluto.jl/pull/1597
x.head === :macrocall ||
x.head === :struct ||
x.head === :abstract ||
(x.head === :(=) && is_function_assignment(x)) || # f(x) = ...
(x.head === :call && (x.args[1] === :eval || x.args[1] === :include))
false
else
all(can_be_function_wrapped, x.args)
end
end

can_be_function_wrapped(x::Any) = true
39 changes: 0 additions & 39 deletions test/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,6 @@
end


@testset "can_be_function_wrapped" begin

c = ExpressionExplorer.can_be_function_wrapped


@test c(quote
a = b + C
if d
for i = 1:10
while Y
end
end
end
end)


@test c(quote
map(1:10) do i
i + 1
end
end)


@test !c(quote
function x(x)
X
end
end)

@test !c(quote
if false
using Asdf
end
end)


end



@testset "is_toplevel_expr" begin

Expand Down