Skip to content

Instantly share code, notes, and snippets.

@non-Jedi
Created May 8, 2019 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non-Jedi/02cf05e44024569a59f32a9bb73d855f to your computer and use it in GitHub Desktop.
Save non-Jedi/02cf05e44024569a59f32a9bb73d855f to your computer and use it in GitHub Desktop.
Script used as first pass on converting https://github.com/GiovineItalia/Gadfly.jl/pull/1278 from using "isnothing" to using "=== nothing". Didn't work perfectly, but it was close.
import CSTParser: EXPR, Call, IDENTIFIER
import DocumentFormat: Edit, apply, pass, State
isnothing_pass(x, state) = nothing
function isnothing_pass(x::EXPR{Call}, state)
if length(x) === 4 # filter out calls like myfunction()
id, p1, contents, p2 = x
if id isa IDENTIFIER && id.val == "isnothing"
push!(state.edits, Edit(state.offset+1:state.offset+id.fullspan+1, ""))
push!(state.edits,
Edit(state.offset+x.span:state.offset+x.span,
" === nothing"))
elseif id isa IDENTIFIER && id.val == "issomething"
push!(state.edits, Edit(state.offset+1:state.offset+id.fullspan+1, ""))
push!(state.edits,
Edit(state.offset+x.span:state.offset+x.span,
" !== nothing"))
end#if
end#if
end#function
const filenames = String[]
for (root, dirs, files) in walkdir("src")
for file in files
let fullpath = joinpath(root, file)
if endswith(fullpath, ".jl") && !in(fullpath, filenames)
push!(filenames, fullpath)
end#if
end#let
end#for
end#for
function remove_isnothing(filename)
text = String(read(filename))
state = State(0, Edit[])
x = CSTParser.parse(text, true)
pass(x, state, isnothing_pass)
sort!(state.edits, lt = (a,b) -> first(a.loc) < first(b.loc), rev = true)
for i in state.edits
text = apply(text, i)
end#for
open(filename; write=true) do f
write(f, text)
end#open
end#function
remove_isnothing.(filenames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment