Check Lua files for global variable access on the fly. Based on the FindGlobals lua script by Mikk.
Due to the way that
luac works, global variables will only be highlighted while there is not an error found in the file.
Before using this package, you must ensure that you have
luajit installed on your system. If the program is not available on your system path, you will need to set the path in settings:
File -> Settings -> Packages -> Linter Lua Findglobals -> Luac
See http://www.lua.org/manual/5.1/luac.html for more information about
$ apm install linter-lua-findglobals
To optimize performance, you may want to declare
local versions of commonly used functions and variables rather than make global namespace lookups.
Some global variables you may be okay with being global accesses (or in fact NEED them to because they can be hooked or changed), for those you have two options:
-- GLOBALS: SomeFunc, SomeOtherFunc, SomeGlobalVariablelines to the source file. This will ignore the variables.
local _G = _Gat the top of the file, and then access them through
_G.SomeFunc. This is actually somewhat faster than accessing them directly, believe it or not. Direct global access involves looking up the global variable table first!
Another benefit is finding the odd misspelled variable name or blocks of code that you may have copy/pasted from another source but forgot to update variables used.
You can change how the linter reports for a file. These directives can be anywhere in the file and will take effect globally.
-- GLOBALS: SomeGlobal, SomeOtherGlobal
-- GETGLOBALFILE [ON|OFF]
GETGLOBALchecks in the global scope. (Default: OFF)
-- GETGLOBALFUNC [ON|OFF]
GETGLOBALchecks in functions. (Default: ON)
-- SETGLOBALFILE [ON|OFF]
SETGLOBALchecks in the global scope. (Default: ON)
-- SETGLOBALFUNC [ON|OFF]
SETGLOBALchecks in functions. (Default: ON)
Good catch. Let us know what about this package looks wrong to you, and we'll investigate right away.