Skip to content

Commit

Permalink
_G is now equivalent to std for Lua version used to run Luacheck
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Feb 28, 2017
1 parent a04f655 commit 745673a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
40 changes: 16 additions & 24 deletions src/luacheck/builtin_standards.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ for name, def in pairs(lua_defs) do
builtin_standards[name] = def_to_std(def)
end

local function detect_default_std()
if rawget(_G, "jit") then
return "luajit"
elseif _VERSION == "Lua 5.1" then
return "lua51c"
elseif _VERSION == "Lua 5.2" then
return "lua52c"
elseif _VERSION == "Lua 5.3" then
return "lua53c"
else
return "max"
end
end

builtin_standards._G = builtin_standards[detect_default_std()]

builtin_standards.busted = {
read_globals = {
"describe", "insulate", "expose", "it", "pending", "before_each", "after_each",
Expand All @@ -281,30 +297,6 @@ builtin_standards.rockspec = {
}
}

local function make_globals_std()
local globals_std = {globals = {}, read_globals = {}}

for global in pairs(_G) do
if global == "_G" or global == "package" then
table.insert(globals_std.globals, global)
else
table.insert(globals_std.read_globals, global)
end
end

local function has_env()
local _ENV = {} -- luacheck: ignore
return not _G
end

if has_env() then
table.insert(globals_std.globals, "_ENV")
end

return globals_std
end

builtin_standards._G = make_globals_std()
builtin_standards.none = {}

return builtin_standards
25 changes: 19 additions & 6 deletions src/luacheck/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local luacheck = require "luacheck"
local argparse = require "luacheck.argparse"
local builtin_standards = require "luacheck.builtin_standards"
local config = require "luacheck.config"
local options = require "luacheck.options"
local expand_rockspec = require "luacheck.expand_rockspec"
Expand Down Expand Up @@ -47,27 +48,39 @@ together with used ones.]]):target("unused_secondaries"):action("store_false")
parser:flag("--no-self", "Filter out warnings related to implicit self argument.")
:target("self"):action("store_false")

parser:option("--std", [[Set standard globals. <std> can be one of:
_G (default) - globals of the current Lua
interpreter;
local default_std_name = "max"

for _, name in ipairs({"lua51c", "lua52c", "lua53c", "luajit"}) do
if builtin_standards._G == builtin_standards[name] then
default_std_name = name
break
end
end

parser:option("--std", ([[Set standard globals, default is _G.
<std> can be one of:
lua51 - globals of Lua 5.1 without deprecated ones;
lua5c - globals of Lua 5.1;
lua51c - globals of Lua 5.1;
lua52 - globals of Lua 5.2;
lua52c - globals of Lua 5.2 with LUA_COMPAT_ALL;
lua53 - globals of Lua 5.3;
lua53c - globals of Lua 5.3 with LUA_COMPAT_5_2;
luajit - globals of LuaJIT 2.0;
ngx_lua - globals of Openresty lua-nginx-module
with LuaJIT 2.0;
rockspec - globals allowed in rockspecs;
min - intersection of globals of Lua 5.1, Lua 5.2,
Lua 5.3 and LuaJIT 2.0;
max - union of globals of Lua 5.1, Lua 5.2, Lua 5.3
and LuaJIT 2.0;
_G - same as lua51c, lua52c, lua53c, or luajit
depending on version of Lua used to run luacheck
or same as max if couldn't detect the version.
Currently %s;
busted - globals added by Busted 2.0;
rockspec - globals allowed in rockspecs;
none - no standard globals.
Sets can be combined using "+".]])
Sets can be combined using "+".]]):format(default_std_name))
parser:option("--globals", "Add custom globals on top of standard ones.")
:args "*"
:count "*"
Expand Down

0 comments on commit 745673a

Please sign in to comment.