Module:No globals

-- This module raises an error if a module tries to read or write a global -- variable. This is useful, because otherwise typos in variable names, etc., -- can cause bad output that goes undetected. -- -- To use it, simply add the following at the start of a module: -- -- require('Module:No globals')

local whitelist = { -- Built-in values ['_G'] = true, ['_VERSION'] = true,

-- Built-in functions ['assert'] = true, ['collectgarbage'] = true, ['dofile'] = true, ['error'] = true, ['getfenv'] = true, ['getmetatable'] = true, ['ipairs'] = true, ['load'] = true, ['loadfile'] = true, ['loadstring'] = true, ['next'] = true, ['pairs'] = true, ['pcall'] = true, ['rawequal'] = true, ['rawget'] = true, ['rawset'] = true, ['select'] = true, ['setfenv'] = true, ['setmetatable'] = true, ['tonumber'] = true, ['tostring'] = true, ['type'] = true, ['unpack'] = true, ['xpcall'] = true,

-- Standard libraries ['coroutine'] = true, ['debug'] = true, ['io'] = true, ['math'] = true, ['os'] = true, ['string'] = true, ['table'] = true,

-- Package library ['package'] = true, ['module'] = true, ['require'] = true,

-- Scribunto-specific ['arg'] = true, ['mw'] = true, ['name'] = true, ['php'] = true, }

local mt = getmetatable(_G) or {}

function mt.__index (t, k)	if not whitelist[k] then error('Tried to read nil global ' .. tostring(k), 2) end return nil end

function mt.__newindex(t, k, v)	if not whitelist[k] then error('Tried to write global ' .. tostring(k), 2) end rawset(t, k, v) end

setmetatable(_G, mt)