From 3f7cf1d8aa69b8ca572dbe85f25192f599e35bba Mon Sep 17 00:00:00 2001 From: Lukas Reineke Date: Mon, 2 Oct 2023 10:47:08 +0900 Subject: [PATCH] feat: config extra key validation don't allow unsupported keys in the configuration --- lua/ibl/config.lua | 34 ++++++++++++++++++---------------- lua/ibl/utils.lua | 12 ++++++++++++ specs/features/config_spec.lua | 7 +++++++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/lua/ibl/config.lua b/lua/ibl/config.lua index 50704455..1567c6df 100644 --- a/lua/ibl/config.lua +++ b/lua/ibl/config.lua @@ -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 = { @@ -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 = { @@ -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 }, @@ -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 = { @@ -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 diff --git a/lua/ibl/utils.lua b/lua/ibl/utils.lua index 0405210b..8b82f4f3 100644 --- a/lua/ibl/utils.lua +++ b/lua/ibl/utils.lua @@ -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 diff --git a/specs/features/config_spec.lua b/specs/features/config_spec.lua index 0858666a..3e197440 100644 --- a/specs/features/config_spec.lua +++ b/specs/features/config_spec.lua @@ -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 }