Skip to content

Commit

Permalink
Add not_globals option for removing defined globals
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Jan 8, 2017
1 parent 4b2f49a commit 17e8242
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion spec/options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ describe("options", function()
local opts = options.normalize({
{
std = "lua52",
globals = {"foo", "bar"},
globals = {"foo", "bar", "removed"},
read_globals = {"baz"}
}, {
new_read_globals = {"quux"},
not_globals = {"removed", "unrelated", "print"}
}
})
local globals = opts.globals
Expand All @@ -138,6 +139,10 @@ describe("options", function()
assert.is_truthy(globals.quux)
assert.is_truthy(read_globals.quux)
assert.is_truthy(read_globals.string)
assert.is_nil(globals.removed)
assert.is_nil(globals.unrelated)
assert.is_nil(globals.print)
assert.is_nil(read_globals.print)
assert.is_nil(read_globals._ENV)
assert.is_truthy(globals.string)
assert.is_truthy(globals._ENV)
Expand Down
4 changes: 2 additions & 2 deletions src/luacheck/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ local cache = {}
-- third is check result in lua table format.
-- String fields are compressed into array indexes.

cache.format_version = 12
cache.format_version = 13

local option_fields = {
"ignore", "std", "globals", "unused_args", "self", "compat", "global", "unused", "redefined",
"unused_secondaries", "allow_defined", "allow_defined_top", "module",
"read_globals", "new_globals", "new_read_globals", "enable", "only"
"read_globals", "new_globals", "new_read_globals", "enable", "only", "not_globals"
}

local event_fields = {
Expand Down
18 changes: 16 additions & 2 deletions src/luacheck/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ options.variadic_inline_options = {
read_globals = array_of_strings,
new_globals = array_of_strings,
new_read_globals = array_of_strings,
not_globals = array_of_strings,
ignore = array_of_strings,
enable = array_of_strings,
only = array_of_strings
Expand Down Expand Up @@ -163,8 +164,16 @@ end
local function get_globals(opts_stack)
local all_globals = {}
local all_read_globals = {}
local all_not_globals = {}

for _, opts in ipairs(opts_stack) do
if opts.not_globals then
local not_globals = utils.array_to_set(opts.not_globals)
utils.remove(all_globals, not_globals)
utils.remove(all_read_globals, not_globals)
utils.update(all_not_globals, not_globals)
end

-- When defining read-only or normal globals, keep their sets disjoined.

if opts.new_read_globals then
Expand All @@ -190,7 +199,7 @@ local function get_globals(opts_stack)
end
end

return all_globals, all_read_globals
return all_globals, all_read_globals, all_not_globals
end

local function get_boolean_opt(opts_stack, option)
Expand Down Expand Up @@ -306,8 +315,13 @@ end
function options.normalize(opts_stack)
local res = {}

res.globals, res.read_globals = get_globals(opts_stack)
local not_globals
res.globals, res.read_globals, not_globals = get_globals(opts_stack)

local std_globals, std_read_globals = get_std_sets(opts_stack)
utils.remove(std_globals, not_globals)
utils.remove(std_read_globals, not_globals)

utils.update(res.globals, std_globals)
utils.update(res.read_globals, std_read_globals)
utils.remove(res.read_globals, res.globals)
Expand Down

0 comments on commit 17e8242

Please sign in to comment.