Skip to content

Commit

Permalink
feat: config extra key validation
Browse files Browse the repository at this point in the history
don't allow unsupported keys in the configuration
  • Loading branch information
lukas-reineke committed Oct 2, 2023
1 parent af58360 commit 3f7cf1d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,31 @@ local validate_config = function(config)
return
end

vim.validate {
utils.validate({
enabled = { config.enabled, "boolean", true },
debounce = { config.debounce, "number", true },
viewport_buffer = { config.viewport_buffer, "table", true },
indent = { config.indent, "table", true },
whitespace = { config.whitespace, "table", true },
scope = { config.scope, "table", true },
exclude = { config.exclude, "table", true },
}
}, config, "ibl.config")

if config.viewport_buffer then
vim.validate {
utils.validate({
min = { config.viewport_buffer.min, "number", true },
max = { config.viewport_buffer.max, "number", true },
}
}, config.viewport_buffer, "ibl.config.viewport_buffer")
end

if config.indent then
vim.validate {
utils.validate({
char = { config.indent.char, { "string", "table" }, true },
tab_char = { config.indent.char, { "string", "table" }, true },
highlight = { config.indent.highlight, { "string", "table" }, true },
smart_indent_cap = { config.indent.smart_indent_cap, "boolean", true },
priority = { config.indent.priority, "number", true },
}
}, config.indent, "ibl.config.indent")
if config.indent.char then
vim.validate {
char = {
Expand Down Expand Up @@ -163,10 +165,10 @@ local validate_config = function(config)
end

if config.whitespace then
vim.validate {
utils.validate({
highlight = { config.whitespace.highlight, { "string", "table" }, true },
remove_blankline_trail = { config.whitespace.remove_blankline_trail, "boolean", true },
}
}, config.whitespace, "ibl.config.whitespace")
if type(config.whitespace.highlight) == "table" then
vim.validate {
tab_char = {
Expand All @@ -181,7 +183,7 @@ local validate_config = function(config)
end

if config.scope then
vim.validate {
utils.validate({
enabled = { config.scope.enabled, "boolean", true },
show_start = { config.scope.show_start, "boolean", true },
show_end = { config.scope.show_end, "boolean", true },
Expand All @@ -190,7 +192,7 @@ local validate_config = function(config)
priority = { config.scope.priority, "number", true },
include = { config.scope.include, "table", true },
exclude = { config.scope.exclude, "table", true },
}
}, config.scope, "ibl.config.scope")
if config.scope.char then
vim.validate {
char = {
Expand All @@ -212,24 +214,24 @@ local validate_config = function(config)
}
end
if config.scope.exclude then
vim.validate {
utils.validate({
language = { config.scope.exclude.language, "table", true },
node_type = { config.scope.exclude.node_type, "table", true },
}
}, config.scope.exclude, "ibl.config.scope.exclude")
end
if config.scope.include then
vim.validate {
utils.validate({
node_type = { config.scope.include.node_type, "table", true },
}
}, config.scope.include, "ibl.config.scope.include")
end
end

if config.exclude then
if config.exclude then
vim.validate {
utils.validate({
filetypes = { config.exclude.filetypes, "table", true },
buftypes = { config.exclude.buftypes, "table", true },
}
}, config.exclude, "ibl.config.exclude")
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lua/ibl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ M.get_whitespace = function(line)
return string.match(line, "^%s+") or ""
end

---@param opt table
---@param input table
---@param path string
M.validate = function(opt, input, path)
vim.validate(opt)
for key, _ in pairs(input) do
if not opt[key] then
error(string.format("'%s' is not a valid key of %s", key, path))
end
end
end

---@class ibl.listchars
---@field tabstop_overwrite boolean
---@field space_char string
Expand Down
7 changes: 7 additions & 0 deletions specs/features/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe("set_config", function()
assert.are.equal(ok, false)
end)

it("does not allow extra keys", function()
---@diagnostic disable-next-line
local ok = pcall(conf.set_config, { this_does_not_exist = "string" })

assert.are.equal(ok, false)
end)

it("resets the config every time", function()
conf.set_config { enabled = false }
local config = conf.set_config { debounce = 100 }
Expand Down

0 comments on commit 3f7cf1d

Please sign in to comment.