Skip to content

Instantly share code, notes, and snippets.

@ZacLN
Created July 3, 2018 16:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZacLN/86f05bdf307974e28f58d437cdddc0ba to your computer and use it in GitHub Desktop.
Save ZacLN/86f05bdf307974e28f58d437cdddc0ba to your computer and use it in GitHub Desktop.
scoping example
str = """
using Base.Test
@testset "ts1" begin
x=1
x==1
end
@testset "ts2" begin
x=2
x==1
end
y = rand(1000)
@testset "ts3" begin
sum(y) < 10
end
@testset "ts4" begin
sum(y) < 10
end
"""
StaticLint.SymbolServer.init()
cst = CSTParser.parse(str, true);
state = StaticLint.State();
state.bindings["using"] = [];
state.bindings["module"] = StaticLint.Binding[];
scope = StaticLint.Scope();
StaticLint.pass(cst, state, scope, (), false, false);
r,ur = StaticLint.resolve_refs(state.refs, state.bindings)
r # resolved references: those we can find a binding for
ur # ones we can't find a binding for
# The general approach I'd take is to do the above then:
# (1) find the location of all @testset blocks as below
function find_testset(x::CSTParser.LeafNode, locs = [], offset = 0)
return locs, offset + x.fullspan
end
function find_testset(x, locs = [], offset = 0) # offset tracks our byteposition as we traverse the tree
if x isa CSTParser.EXPR{CSTParser.MacroCall} && x.args[1].args[2].val == "testset"
push!(locs, offset .+ x.span) # x.span gives a range of the expression across the text bytes
offset += x.fullspan # x.full span gives the length of text bytes + trailing white space
else
for a in x
locs, offset = find_testset(a, locs, offset)
end
end
return locs, offset
end
locs, _ = find_testset(cst)
# (2) find references made within the @testset blocks
filter(r->any(in.(r.r.loc.offset, locs)) , R)
# (3) find those that refer to the same binding, and aren't function calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment