feat(luassert): Add luassert annotation types (#552)

This commit is contained in:
Kristijan Husak 2024-04-22 14:21:08 +02:00 committed by GitHub
parent f592187e58
commit 5129a3693c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 388 additions and 1 deletions

View file

@ -4,7 +4,7 @@ lua/luassert
lua/plenary/profile.lua
lua/plenary/profile/
lua/plenary/bit.lua
lua/plenary/_meta/_luassert.lua
lua/say.lua
scratch/
scripts/

View file

@ -5,5 +5,8 @@ test:
generate_filetypes:
nvim --headless -c 'luafile scripts/update_filetypes_from_github.lua' -c 'qa!'
generate_luassert_types:
nvim --headless -c 'luafile scripts/generate_luassert_types.lua' -c 'qa!'
lint:
luacheck lua/plenary

View file

@ -0,0 +1,289 @@
---@meta
---This file is autogenerated, DO NOT EDIT
error "Cannot require a meta file"
---@generic T:any
---@alias LuassertFunction fun(value:T, message?:string):T
---@alias LuassertFunctionTwoArgs fun(expected:T, actual:T, message?:string):T
---@alias LuassertFunctionMultiArgs fun(...:T):T
---@class Luassert
---@field are_boolean LuassertFunction
---@field are_equal LuassertFunctionTwoArgs
---@field are_equals LuassertFunctionTwoArgs
---@field are_error LuassertFunction
---@field are_error_match LuassertFunctionTwoArgs
---@field are_error_matches LuassertFunctionTwoArgs
---@field are_errors LuassertFunction
---@field are_false LuassertFunction
---@field are_falsy LuassertFunction
---@field are_function LuassertFunction
---@field are_holes LuassertFunction
---@field are_match LuassertFunctionTwoArgs
---@field are_match_error LuassertFunctionTwoArgs
---@field are_matches LuassertFunctionTwoArgs
---@field are_matches_error LuassertFunctionTwoArgs
---@field are_near LuassertFunctionMultiArgs
---@field are_nil LuassertFunction
---@field are_number LuassertFunction
---@field are_returned_arguments LuassertFunction
---@field are_same LuassertFunctionTwoArgs
---@field are_string LuassertFunction
---@field are_table LuassertFunction
---@field are_thread LuassertFunction
---@field are_true LuassertFunction
---@field are_truthy LuassertFunction
---@field are_unique LuassertFunction
---@field are_userdata LuassertFunction
---@field array_boolean LuassertFunction
---@field array_equal LuassertFunctionTwoArgs
---@field array_equals LuassertFunctionTwoArgs
---@field array_error LuassertFunction
---@field array_error_match LuassertFunctionTwoArgs
---@field array_error_matches LuassertFunctionTwoArgs
---@field array_errors LuassertFunction
---@field array_false LuassertFunction
---@field array_falsy LuassertFunction
---@field array_function LuassertFunction
---@field array_holes LuassertFunction
---@field array_match LuassertFunctionTwoArgs
---@field array_match_error LuassertFunctionTwoArgs
---@field array_matches LuassertFunctionTwoArgs
---@field array_matches_error LuassertFunctionTwoArgs
---@field array_near LuassertFunctionMultiArgs
---@field array_nil LuassertFunction
---@field array_number LuassertFunction
---@field array_returned_arguments LuassertFunction
---@field array_same LuassertFunctionTwoArgs
---@field array_string LuassertFunction
---@field array_table LuassertFunction
---@field array_thread LuassertFunction
---@field array_true LuassertFunction
---@field array_truthy LuassertFunction
---@field array_unique LuassertFunction
---@field array_userdata LuassertFunction
---@field does_boolean LuassertFunction
---@field does_equal LuassertFunctionTwoArgs
---@field does_equals LuassertFunctionTwoArgs
---@field does_error LuassertFunction
---@field does_error_match LuassertFunctionTwoArgs
---@field does_error_matches LuassertFunctionTwoArgs
---@field does_errors LuassertFunction
---@field does_false LuassertFunction
---@field does_falsy LuassertFunction
---@field does_function LuassertFunction
---@field does_holes LuassertFunction
---@field does_match LuassertFunctionTwoArgs
---@field does_match_error LuassertFunctionTwoArgs
---@field does_matches LuassertFunctionTwoArgs
---@field does_matches_error LuassertFunctionTwoArgs
---@field does_near LuassertFunctionMultiArgs
---@field does_nil LuassertFunction
---@field does_number LuassertFunction
---@field does_returned_arguments LuassertFunction
---@field does_same LuassertFunctionTwoArgs
---@field does_string LuassertFunction
---@field does_table LuassertFunction
---@field does_thread LuassertFunction
---@field does_true LuassertFunction
---@field does_truthy LuassertFunction
---@field does_unique LuassertFunction
---@field does_userdata LuassertFunction
---@field has_boolean LuassertFunction
---@field has_equal LuassertFunctionTwoArgs
---@field has_equals LuassertFunctionTwoArgs
---@field has_error LuassertFunction
---@field has_error_match LuassertFunctionTwoArgs
---@field has_error_matches LuassertFunctionTwoArgs
---@field has_errors LuassertFunction
---@field has_false LuassertFunction
---@field has_falsy LuassertFunction
---@field has_function LuassertFunction
---@field has_holes LuassertFunction
---@field has_match LuassertFunctionTwoArgs
---@field has_match_error LuassertFunctionTwoArgs
---@field has_matches LuassertFunctionTwoArgs
---@field has_matches_error LuassertFunctionTwoArgs
---@field has_near LuassertFunctionMultiArgs
---@field has_nil LuassertFunction
---@field has_number LuassertFunction
---@field has_returned_arguments LuassertFunction
---@field has_same LuassertFunctionTwoArgs
---@field has_string LuassertFunction
---@field has_table LuassertFunction
---@field has_thread LuassertFunction
---@field has_true LuassertFunction
---@field has_truthy LuassertFunction
---@field has_unique LuassertFunction
---@field has_userdata LuassertFunction
---@field is_boolean LuassertFunction
---@field is_equal LuassertFunctionTwoArgs
---@field is_equals LuassertFunctionTwoArgs
---@field is_error LuassertFunction
---@field is_error_match LuassertFunctionTwoArgs
---@field is_error_matches LuassertFunctionTwoArgs
---@field is_errors LuassertFunction
---@field is_false LuassertFunction
---@field is_falsy LuassertFunction
---@field is_function LuassertFunction
---@field is_holes LuassertFunction
---@field is_match LuassertFunctionTwoArgs
---@field is_match_error LuassertFunctionTwoArgs
---@field is_matches LuassertFunctionTwoArgs
---@field is_matches_error LuassertFunctionTwoArgs
---@field is_near LuassertFunctionMultiArgs
---@field is_nil LuassertFunction
---@field is_number LuassertFunction
---@field is_returned_arguments LuassertFunction
---@field is_same LuassertFunctionTwoArgs
---@field is_string LuassertFunction
---@field is_table LuassertFunction
---@field is_thread LuassertFunction
---@field is_true LuassertFunction
---@field is_truthy LuassertFunction
---@field is_unique LuassertFunction
---@field is_userdata LuassertFunction
---@field message_boolean LuassertFunction
---@field message_equal LuassertFunctionTwoArgs
---@field message_equals LuassertFunctionTwoArgs
---@field message_error LuassertFunction
---@field message_error_match LuassertFunctionTwoArgs
---@field message_error_matches LuassertFunctionTwoArgs
---@field message_errors LuassertFunction
---@field message_false LuassertFunction
---@field message_falsy LuassertFunction
---@field message_function LuassertFunction
---@field message_holes LuassertFunction
---@field message_match LuassertFunctionTwoArgs
---@field message_match_error LuassertFunctionTwoArgs
---@field message_matches LuassertFunctionTwoArgs
---@field message_matches_error LuassertFunctionTwoArgs
---@field message_near LuassertFunctionMultiArgs
---@field message_nil LuassertFunction
---@field message_number LuassertFunction
---@field message_returned_arguments LuassertFunction
---@field message_same LuassertFunctionTwoArgs
---@field message_string LuassertFunction
---@field message_table LuassertFunction
---@field message_thread LuassertFunction
---@field message_true LuassertFunction
---@field message_truthy LuassertFunction
---@field message_unique LuassertFunction
---@field message_userdata LuassertFunction
---@field no_boolean LuassertFunction
---@field no_equal LuassertFunctionTwoArgs
---@field no_equals LuassertFunctionTwoArgs
---@field no_error LuassertFunction
---@field no_error_match LuassertFunctionTwoArgs
---@field no_error_matches LuassertFunctionTwoArgs
---@field no_errors LuassertFunction
---@field no_false LuassertFunction
---@field no_falsy LuassertFunction
---@field no_function LuassertFunction
---@field no_holes LuassertFunction
---@field no_match LuassertFunctionTwoArgs
---@field no_match_error LuassertFunctionTwoArgs
---@field no_matches LuassertFunctionTwoArgs
---@field no_matches_error LuassertFunctionTwoArgs
---@field no_near LuassertFunctionMultiArgs
---@field no_nil LuassertFunction
---@field no_number LuassertFunction
---@field no_returned_arguments LuassertFunction
---@field no_same LuassertFunctionTwoArgs
---@field no_string LuassertFunction
---@field no_table LuassertFunction
---@field no_thread LuassertFunction
---@field no_true LuassertFunction
---@field no_truthy LuassertFunction
---@field no_unique LuassertFunction
---@field no_userdata LuassertFunction
---@field not_boolean LuassertFunction
---@field not_equal LuassertFunctionTwoArgs
---@field not_equals LuassertFunctionTwoArgs
---@field not_error LuassertFunction
---@field not_error_match LuassertFunctionTwoArgs
---@field not_error_matches LuassertFunctionTwoArgs
---@field not_errors LuassertFunction
---@field not_false LuassertFunction
---@field not_falsy LuassertFunction
---@field not_function LuassertFunction
---@field not_holes LuassertFunction
---@field not_match LuassertFunctionTwoArgs
---@field not_match_error LuassertFunctionTwoArgs
---@field not_matches LuassertFunctionTwoArgs
---@field not_matches_error LuassertFunctionTwoArgs
---@field not_near LuassertFunctionMultiArgs
---@field not_nil LuassertFunction
---@field not_number LuassertFunction
---@field not_returned_arguments LuassertFunction
---@field not_same LuassertFunctionTwoArgs
---@field not_string LuassertFunction
---@field not_table LuassertFunction
---@field not_thread LuassertFunction
---@field not_true LuassertFunction
---@field not_truthy LuassertFunction
---@field not_unique LuassertFunction
---@field not_userdata LuassertFunction
---@field was_boolean LuassertFunction
---@field was_equal LuassertFunctionTwoArgs
---@field was_equals LuassertFunctionTwoArgs
---@field was_error LuassertFunction
---@field was_error_match LuassertFunctionTwoArgs
---@field was_error_matches LuassertFunctionTwoArgs
---@field was_errors LuassertFunction
---@field was_false LuassertFunction
---@field was_falsy LuassertFunction
---@field was_function LuassertFunction
---@field was_holes LuassertFunction
---@field was_match LuassertFunctionTwoArgs
---@field was_match_error LuassertFunctionTwoArgs
---@field was_matches LuassertFunctionTwoArgs
---@field was_matches_error LuassertFunctionTwoArgs
---@field was_near LuassertFunctionMultiArgs
---@field was_nil LuassertFunction
---@field was_number LuassertFunction
---@field was_returned_arguments LuassertFunction
---@field was_same LuassertFunctionTwoArgs
---@field was_string LuassertFunction
---@field was_table LuassertFunction
---@field was_thread LuassertFunction
---@field was_true LuassertFunction
---@field was_truthy LuassertFunction
---@field was_unique LuassertFunction
---@field was_userdata LuassertFunction
---@field boolean LuassertFunction
---@field equal LuassertFunctionTwoArgs
---@field equals LuassertFunctionTwoArgs
---@field error LuassertFunction
---@field error_match LuassertFunctionTwoArgs
---@field error_matches LuassertFunctionTwoArgs
---@field errors LuassertFunction
---@field False LuassertFunction
---@field falsy LuassertFunction
---@field Function LuassertFunction
---@field holes LuassertFunction
---@field match LuassertFunctionTwoArgs
---@field match_error LuassertFunctionTwoArgs
---@field matches LuassertFunctionTwoArgs
---@field matches_error LuassertFunctionTwoArgs
---@field near LuassertFunctionMultiArgs
---@field Nil LuassertFunction
---@field number LuassertFunction
---@field returned_arguments LuassertFunction
---@field same LuassertFunctionTwoArgs
---@field string LuassertFunction
---@field table LuassertFunction
---@field thread LuassertFunction
---@field True LuassertFunction
---@field truthy LuassertFunction
---@field unique LuassertFunction
---@field userdata LuassertFunction
---@field are Luassert
---@field array Luassert
---@field does Luassert
---@field has Luassert
---@field is Luassert
---@field message Luassert
---@field no Luassert
---@field Not Luassert
---@field was Luassert

View file

@ -212,6 +212,7 @@ pending = mod.pending
before_each = mod.before_each
after_each = mod.after_each
clear = mod.clear
---@type Luassert
assert = require "luassert"
mod.run = function(file)

View file

@ -0,0 +1,94 @@
require "luassert"
local namespaces = require "luassert.namespaces"
local reserved_words = {
"and",
"break",
"do",
"else",
"elseif",
"end",
"false",
"for",
"function",
"if",
"in",
"local",
"nil",
"not",
"or",
"repeat",
"return",
"then",
"true",
"until",
"while",
}
local content = {
"---@meta",
"---This file is autogenerated, DO NOT EDIT",
'error "Cannot require a meta file"',
"",
"---@generic T:any",
"---@alias LuassertFunction fun(value:T, message?:string):T",
"---@alias LuassertFunctionTwoArgs fun(expected:T, actual:T, message?:string):T",
"---@alias LuassertFunctionMultiArgs fun(...:T):T",
"",
"---@class Luassert",
}
local args_count_per_matcher = {
matches = 2,
match = 2,
equal = 2,
equals = 2,
same = 2,
error_match = 2,
error_matches = 2,
matches_error = 2,
match_error = 2,
near = 3,
}
local get_type_by_args = function(assertion)
local arg_count = args_count_per_matcher[assertion] or 1
if arg_count == 1 then
return "LuassertFunction"
end
if arg_count == 2 then
return "LuassertFunctionTwoArgs"
end
return "LuassertFunctionMultiArgs"
end
local fix_reserved_words = function(word)
if vim.tbl_contains(reserved_words, word) then
return table.concat { word:sub(1, 1):upper(), word:sub(2) }
end
return word
end
local assertions = vim.tbl_keys(namespaces.assertion)
local modifiers = vim.tbl_keys(namespaces.modifier)
table.sort(assertions)
table.sort(modifiers)
for _, modifier in ipairs(modifiers) do
for _, assertion in ipairs(assertions) do
table.insert(content, ("---@field %s_%s %s"):format(modifier, assertion, get_type_by_args(assertion)))
end
end
for _, assertion in ipairs(assertions) do
table.insert(content, ("---@field %s %s"):format(fix_reserved_words(assertion), get_type_by_args(assertion)))
end
for _, modifier in ipairs(modifiers) do
table.insert(content, ("---@field %s Luassert"):format(fix_reserved_words(modifier)))
end
local fd = assert(vim.loop.fs_open("./lua/plenary/_meta/_luassert.lua", "w", 438))
assert(vim.loop.fs_write(fd, table.concat(content, "\n"), 0))
assert(vim.loop.fs_close(fd))