Skip to content

Commit

Permalink
handle unary :: (#39)
Browse files Browse the repository at this point in the history
* add failing test case

* fix test case

* format

* format

* also fix #36

* add failing test

* handle for loop arguments and nested local scope

* add some more nested scope tests

* handle unary `::`
  • Loading branch information
ericphanson authored Apr 25, 2024
1 parent 19c8273 commit 548a7d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/get_names_used.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function is_anonymous_function_definition_arg(leaf)
return child_index(get_parent(leaf, 3)) == 2
elseif parents_match(leaf, (K"::",))
# we must be on the LHS, otherwise we're a type
child_index(leaf) == 1 || return false
is_hastype_LHS(leaf) || return false
# Ok, let's just step up one level and see again
return is_anonymous_function_definition_arg(parent(leaf))
elseif parents_match(leaf, (K"=",))
Expand Down Expand Up @@ -92,10 +92,10 @@ function is_struct_field_name(leaf)
kind(leaf) == K"Identifier" || return false
if parents_match(leaf, (K"::", K"block", K"struct"))
# we want to be on the LHS of the `::`
return child_index(leaf) == 1
return is_hastype_LHS(leaf)
elseif parents_match(leaf, (K"::", K"=", K"block", K"struct"))
# if we are in a `Base.@kwdef`, we may be on the LHS of an `=`
return child_index(leaf) == 1 && child_index(parent(leaf)) == 1
return is_hastype_LHS(leaf) && child_index(parent(leaf)) == 1
else
return false
end
Expand Down Expand Up @@ -149,14 +149,23 @@ function is_non_anonymous_function_definition_arg(leaf)
return is_non_anonymous_function_definition_arg(parent(leaf))
elseif parents_match(leaf, (K"::",))
# we must be on the LHS, otherwise we're a type
child_index(leaf) == 1 || return false
is_hastype_LHS(leaf) || return false
# Ok, let's just step up one level and see again
return is_non_anonymous_function_definition_arg(parent(leaf))
else
return false
end
end

# matches `x` in `x::Y`, but not `Y`, nor `foo(::Y)`
function is_hastype_LHS(leaf)
parents_match(leaf, (K"::",)) || return false
unary = has_flags(get_parent(leaf), JuliaSyntax.PREFIX_OP_FLAG)
unary && return false
# OK if not unary, then check we're in position 1 for LHS
return child_index(leaf) == 1
end

# Here we use the magic of AbstractTrees' `TreeCursor` so we can start at
# a leaf and follow the parents up to see what scopes our leaf is in.
# TODO- cleanup with parsing utilities (?)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ end
["using LinearAlgebra: LinearAlgebra"]
end

@testset "types without values in function signatures" begin
# https://github.com/ericphanson/ExplicitImports.jl/issues/33
@test using_statement.(explicit_imports_nonrecursive(TestMod8, "test_mods.jl")) ==
["using LinearAlgebra: LinearAlgebra", "using LinearAlgebra: QR"]
end

@testset "scripts" begin
str = sprint(print_explicit_imports_script, "script.jl")
@test contains(str, "Script `script.jl`")
Expand Down
18 changes: 13 additions & 5 deletions test/test_mods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,23 @@ using LinearAlgebra
# these are all local references to `I`, but you have to go up a scope to know that
function foo(I)
let
I+1
I + 1
end
let k=I
k+I
let k = I
k + I
end
function bar(x)
I+1
return I + 1
end
bar(I)
return bar(I)
end

end # TestMod7

module TestMod8
using LinearAlgebra

# https://github.com/ericphanson/ExplicitImports.jl/issues/33
foo(::QR) = ()

end # TestMod8

2 comments on commit 548a7d3

@ericphanson
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 register

Release notes:

  • improved parsing to squash several bugs causing false positives

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

Tagging

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 v1.4.2 -m "<description of version>" 548a7d372477de0188035b67623b3e38e0bdafb7
git push origin v1.4.2

Please sign in to comment.