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

allow ignore missing references from source #378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 37 additions & 7 deletions src/StaticLint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@
env::ExternalEnv
server
flags::Int
forced_references::Set{String}
end

Toplevel(file, included_files, scope, in_modified_expr, modified_exprs, delayed, resolveonly, env, server) =
Toplevel(file, included_files, scope, in_modified_expr, modified_exprs, delayed, resolveonly, env, server, 0)
Toplevel(file, included_files, scope, in_modified_expr, modified_exprs, delayed, resolveonly, env, server, 0, Set{String}())

function (state::Toplevel)(x::EXPR)
resolve_import(x, state)
Expand Down Expand Up @@ -105,9 +106,10 @@
env::ExternalEnv
server
flags::Int
forced_references::Set{String}
end

Delayed(scope, env, server) = Delayed(scope, env, server, 0)
Delayed(scope, env, server) = Delayed(scope, env, server, 0, Set{String}())

function (state::Delayed)(x::EXPR)
mark_bindings!(x, state)
Expand All @@ -134,8 +136,11 @@
scope::Scope
env::ExternalEnv
server
forced_references::Set{String}
end

ResolveOnly(scope,env,server) = ResolveOnly(scope,env,server,Set{String}())

function (state::ResolveOnly)(x::EXPR)
if hasscope(x)
s0 = state.scope
Expand Down Expand Up @@ -163,6 +168,26 @@
return old
end

function add_forced_references!(file, current::Set{String})

if :_text_document in propertynames(file) # 1.56.2
source = file._text_document._content

Check warning on line 174 in src/StaticLint.jl

View check run for this annotation

Codecov / codecov/patch

src/StaticLint.jl#L174

Added line #L174 was not covered by tests
elseif :_content in propertynames(file) # 1.38.2
source = file._content

Check warning on line 176 in src/StaticLint.jl

View check run for this annotation

Codecov / codecov/patch

src/StaticLint.jl#L176

Added line #L176 was not covered by tests
elseif :source in propertynames(file)
source = file.source
else
source = ""

Check warning on line 180 in src/StaticLint.jl

View check run for this annotation

Codecov / codecov/patch

src/StaticLint.jl#L180

Added line #L180 was not covered by tests
end

linter_rows = collect(eachmatch(r"(^|\n)#@linter_refs (?<refs>.*)", source))
for r in linter_rows
for ref in split(r[:refs], ","; keepempty=false)
push!(current, strip(ref))
end
end

Check warning on line 188 in src/StaticLint.jl

View check run for this annotation

Codecov / codecov/patch

src/StaticLint.jl#L185-L188

Added lines #L185 - L188 were not covered by tests
end

"""
semantic_pass(file, modified_expr=nothing)

Expand All @@ -171,26 +196,30 @@
function semantic_pass(file, modified_expr = nothing)
server = file.server
env = getenv(file, server)

forced_references = Set{String}()
add_forced_references!(file, forced_references)

setscope!(getcst(file), Scope(nothing, getcst(file), Dict(), Dict{Symbol,Any}(:Base => env.symbols[:Base], :Core => env.symbols[:Core]), nothing))
state = Toplevel(file, [getpath(file)], scopeof(getcst(file)), modified_expr === nothing, modified_expr, EXPR[], EXPR[], env, server)
state = Toplevel(file, [getpath(file)], scopeof(getcst(file)), modified_expr === nothing, modified_expr, EXPR[], EXPR[], env, server, 0, forced_references)
state(getcst(file))
for x in state.delayed
if hasscope(x)
traverse(x, Delayed(scopeof(x), env, server))
traverse(x, Delayed(scopeof(x), env, server, 0, state.forced_references))
for (k, b) in scopeof(x).names
infer_type_by_use(b, env)
check_unused_binding(b, scopeof(x))
end
else
traverse(x, Delayed(retrieve_delayed_scope(x), env, server))
traverse(x, Delayed(retrieve_delayed_scope(x), env, server, 0, state.forced_references))
end
end
if state.resolveonly !== nothing
for x in state.resolveonly
if hasscope(x)
traverse(x, ResolveOnly(scopeof(x), env, server))
traverse(x, ResolveOnly(scopeof(x), env, server, state.forced_references))

Check warning on line 220 in src/StaticLint.jl

View check run for this annotation

Codecov / codecov/patch

src/StaticLint.jl#L220

Added line #L220 was not covered by tests
else
traverse(x, ResolveOnly(retrieve_delayed_scope(x), env, server))
traverse(x, ResolveOnly(retrieve_delayed_scope(x), env, server, state.forced_references))
end
end
end
Expand Down Expand Up @@ -331,6 +360,7 @@
push!(state.included_files, getpath(state.file))
setroot(state.file, getroot(oldfile))
setscope!(getcst(state.file), nothing)
add_forced_references!(state.file, state.forced_references)
state(getcst(state.file))
state.file = oldfile
pop!(state.included_files)
Expand Down
6 changes: 6 additions & 0 deletions src/references.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
if !resolved && !CSTParser.defines_module(scope.expr) && parentof(scope) isa Scope
return resolve_ref(x, parentof(scope), state)
end

if valof(x) in state.forced_references
setref!(x, Binding(noname, nothing, nothing, []))
return true

Check warning on line 86 in src/references.jl

View check run for this annotation

Codecov / codecov/patch

src/references.jl#L85-L86

Added lines #L85 - L86 were not covered by tests
end

return resolved
end

Expand Down