Created
July 25, 2017 08:04
-
-
Save ZacLN/2f0d66c651647e32eae85e58102315cb to your computer and use it in GitHub Desktop.
fix parameterised function declarations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FixParameterisedFunctionDeprecation | |
using CSTParser | |
import CSTParser: EXPR, Call, Curly, Tokens, OPERATOR, BinarySyntaxOpCall, UnarySyntaxOpCall, DeclarationOp, PUNCTUATION | |
function fixdep(file, overwrite = false) | |
str = readstring(file) | |
x = CSTParser.parse(str, true) | |
diags = [] | |
lint(x, diags, 0) | |
line_breaks = find(x->x=='\n', str) | |
for d in diags | |
line = findfirst(x -> x>first(d.loc), line_breaks) | |
linestartoffset = (line>1 ? line_breaks[line - 1] + 1 : 1) | |
println("Use of parameterised function declaration on line $line") | |
print(str[linestartoffset:(line_breaks[line])]) | |
print_with_color(:red," "^(first(d.loc - linestartoffset) + 1), "^"^(length(d.loc) - 1)) | |
println() | |
end | |
if overwrite | |
for d in reverse(diags) | |
for a in d.actions | |
str = string(str[1:first(a.range)], a.text, str[last(a.range)+1:sizeof(str)]) | |
end | |
end | |
write(file, str) | |
end | |
end | |
function lint(x::EXPR, diags, coffset) | |
for a in x.args | |
offset = coffset | |
lint(a, diags, offset) | |
coffset = offset + a.span | |
end | |
return | |
end | |
function _lint_sig(sig, diags, offset) | |
if sig isa EXPR{Call} && sig.args[1] isa EXPR{Curly} && !(sig.args[1].args[1] isa EXPR{CSTParser.InvisBrackets} && sig.args[1].args[1].args[2] isa EXPR{UnarySyntaxOpCall} && sig.args[1].args[1].args[2].args[1] isa EXPR{OPERATOR{DeclarationOp,Tokens.DECLARATION,false}}) | |
push!(diags, CSTParser.Diagnostic{CSTParser.Diagnostics.parameterisedDeprecation}((offset + sig.args[1].args[1].span):(offset + sig.args[1].span), [], "Use of deprecated parameter syntax")) | |
trailingws = last(sig.args) isa EXPR{CSTParser.PUNCTUATION{Tokens.RPAREN}} ? last(sig.args).span - 1 : 0 | |
loc1 = offset + sig.span - trailingws | |
push!(last(diags).actions, CSTParser.Diagnostics.TextEdit(loc1:loc1, string(" where {", join((Expr(t) for t in sig.args[1].args[2:end] if !(t isa EXPR{P} where P <: PUNCTUATION) ), ","), "}"))) | |
push!(last(diags).actions, CSTParser.Diagnostics.TextEdit((offset + sig.args[1].args[1].span):(offset + sig.args[1].span), "")) | |
end | |
end | |
function lint(x::EXPR{CSTParser.FunctionDef}, diags, coffset) | |
_lint_sig(x.args[2], diags, coffset + x.args[1].span) | |
invoke(lint, Tuple{EXPR,Any,Any}, x, diags, coffset) | |
end | |
function lint(x::EXPR{CSTParser.BinarySyntaxOpCall}, diags, coffset) | |
if x.args[2] isa EXPR{CSTParser.OPERATOR{CSTParser.AssignmentOp,Tokens.EQ,false}} && CSTParser.is_func_call(x.args[1]) | |
_lint_sig(x.args[1], diags, coffset) | |
invoke(lint, Tuple{EXPR,Any,Any}, x, diags, coffset) | |
end | |
end | |
export fixdep | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment