Module pl.strict
Checks uses of undeclared global variables.
All global variables must be 'declared' through a regular assignment (even assigning `nil` will do) in a main chunk before being used anywhere or assigned to inside a function. Existing metatables `__newindex` and `__index` metamethods are respected.
You can set any table to have strict behaviour using `strict.module`. Creating a new module with `strict.closed_module` makes the module immune to monkey-patching, if you don't wish to encourage monkey business.
If the global `PENLIGHT_NO_GLOBAL_STRICT` is defined, then this module won't make the global environment strict - if you just want to explicitly set table strictness.
Functions
| module ([name[, mod[, predeclared]]]) | make an existing table strict. |
| make_all_strict (T) | make all tables in a table strict. |
| closed_module (mod, name) | make a new module table which is closed to further changes. |
Functions
- module ([name[, mod[, predeclared]]])
-
make an existing table strict.
Parameters:
- name string name of table (optional)
- mod tab the table to protect - if `nil` then we'll return a new table (optional)
- predeclared tab - table of variables that are to be considered predeclared. (optional)
Returns:
-
the given table, or a new table
Usage:
local M = { hello = "world" } strict.module ("Awesome_Module", M, { Lua = true, -- defines allowed keys }) assert(M.hello == "world") assert(M.Lua == nil) -- access allowed, but has no value yet M.Lua = "Rocks" assert(M.Lua == "Rocks") M.not_allowed = "bad boy" -- throws an error
- make_all_strict (T)
-
make all tables in a table strict.
So `strict.make_all_strict(_G)` prevents monkey-patching
of any global table
Parameters:
- T tab the table containing the tables to protect. Table `T` itself will NOT be protected.
- closed_module (mod, name)
-
make a new module table which is closed to further changes.
Parameters:
- mod tab module table
- name string module name