diff options
author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2022-11-28 15:40:50 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 15:40:50 -0700 |
commit | 80b6edabe3e4203ee4bf50261af07a6a0495ef36 (patch) | |
tree | 7f3f0e0338d79df0168b3fceb9a41cce7d769eeb /runtime/lua/vim/secure.lua | |
parent | f004812b338340e5f5157aa68d09d3f0e5605c6c (diff) | |
download | rneovim-80b6edabe3e4203ee4bf50261af07a6a0495ef36.tar.gz rneovim-80b6edabe3e4203ee4bf50261af07a6a0495ef36.tar.bz2 rneovim-80b6edabe3e4203ee4bf50261af07a6a0495ef36.zip |
refactor: rework parameter validation in vim.secure.trust() (#21223)
Diffstat (limited to 'runtime/lua/vim/secure.lua')
-rw-r--r-- | runtime/lua/vim/secure.lua | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/runtime/lua/vim/secure.lua b/runtime/lua/vim/secure.lua index 0b4d7d53a2..08b1ff871c 100644 --- a/runtime/lua/vim/secure.lua +++ b/runtime/lua/vim/secure.lua @@ -148,19 +148,23 @@ function M.trust(opts) local bufnr = opts.bufnr local action = opts.action - if path and bufnr then - error('path and bufnr are mutually exclusive', 2) + assert(not path or not bufnr, '"path" and "bufnr" are mutually exclusive') + + if action == 'allow' then + assert(not path, '"path" is not valid when action is "allow"') end local fullpath if path then fullpath = vim.loop.fs_realpath(vim.fs.normalize(path)) - else + elseif bufnr then local bufname = vim.api.nvim_buf_get_name(bufnr) if bufname == '' then return false, 'buffer is not associated with a file' end fullpath = vim.loop.fs_realpath(vim.fs.normalize(bufname)) + else + error('one of "path" or "bufnr" is required') end if not fullpath then @@ -170,8 +174,6 @@ function M.trust(opts) local trust = read_trust() if action == 'allow' then - assert(bufnr, 'bufnr is required when action is "allow"') - local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n' local contents = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), newline) if vim.bo[bufnr].endofline then |