ambevar-dotfiles/.scripts/lualint

77 lines
1.2 KiB
Plaintext
Raw Normal View History

2015-04-03 15:58:45 +02:00
#!/usr/bin/env lua
2015-04-03 15:32:46 +02:00
2015-04-03 15:58:45 +02:00
local function usage()
print ([[Lua static checker
Usage: ]] .. arg[0]:match('/?([^/]+)$') .. [[ LUASOURCE
Detects read/write accesses to undeclared globals.]])
end
2015-04-03 15:32:46 +02:00
2015-04-03 15:58:45 +02:00
if #arg < 1 then
usage()
os.exit()
end
2015-04-03 15:32:46 +02:00
2015-04-03 15:58:45 +02:00
local globals = {
assert = true,
collectgarbage = true,
dofile = true,
error = true,
_G = true,
getfenv = true,
getmetatable = true,
ipairs = true,
load = true,
loadfile = true,
loadstring = true,
next = true,
pairs = true,
pcall = true,
print = true,
rawequal = true,
rawget = true,
rawset = true,
select = true,
setfenv = true,
setmetatable = true,
tonumber = true,
tostring = true,
type = true,
unpack = true,
_VERSION = true,
xpcall = true,
2015-04-03 15:32:46 +02:00
2015-04-03 15:58:45 +02:00
module = true,
package = true,
require = true,
2015-04-03 15:32:46 +02:00
2015-04-03 15:58:45 +02:00
coroutine = true,
debug = true,
io = true,
math = true,
os = true,
string = true,
table = true,
2015-04-03 15:32:46 +02:00
}
2015-04-03 15:58:45 +02:00
2015-04-04 14:43:51 +02:00
for _, v in ipairs(arg) do
print('==> ' .. v)
local p = io.popen('luac -p -l -- ' .. v)
2015-04-03 15:58:45 +02:00
2015-04-04 14:43:51 +02:00
for m in p:lines() do
if m:match('ETTABUP.*_ENV') then
local line, op, field = m:match('(%[[%d]+%]).*(.)ETTABUP.*"([_%w]+)"')
if not globals[field] then
if op == 'G' then
op = '<-'
else
op = '->'
end
print(line, op, field)
2015-04-03 15:58:45 +02:00
end
end
end
2015-04-04 14:43:51 +02:00
p:close()
end