robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
b351024daf
master
1local fmt =string . format 2 3local M ={} 4 5--- Banned nouns. See `:help dev-name-common`. 6--- Apply to parameter names, keyset keys, and function name parts. 7local banned_nouns ={ 8buffer ='buf' , 9bufnr ='buf' , 10-- channel = 'chan', 11command ='cmd' , 12directory ='dir' , 13highlight ='hl' , 14position ='pos' , 15process ='proc' , 16window ='win' , 17winnr ='win' , 18} 19 20--- Banned verbs. See `:help dev-name-common`. 21--- Apply to function name parts. 22local banned_verbs ={ 23contains ='has' , 24delete ='del' , 25disable ='enable' , 26exit ='cancel' , -- or "stop" 27-- format = 'fmt', 28-- hide = '?', 29-- show = '?', 30list ='get' , 31notify ='print' , -- or "echo" 32pretty ='fmt' , 33remove ='del' , 34toggle ='enable' , 35} 36 37--- Whitelist of legacy function names that violate the naming conventions. 38--- Do not add new entries here. New functions must follow `:help dev-naming`. 39--- 40--- @type table<string, table<string, true>> 41local legacy_names ={ 42[ 'src/nvim/api/buffer.c' ] ={ 43nvim_buf_delete =true , 44} , 45[ 'src/nvim/api/window.c' ] ={ 46nvim_win_get_position =true , 47} , 48[ 'src/nvim/api/tabpage.c' ] ={ 49nvim_tabpage_list_wins =true , 50} , 51[ 'src/nvim/api/ui.c' ] ={ 52remote_ui_highlight_set =true , 53} , 54[ 'src/nvim/api/vim.c' ] ={ 55nvim_list_bufs =true , 56nvim_list_chans =true , 57nvim_list_runtime_paths =true , 58nvim_list_tabpages =true , 59nvim_list_uis =true , 60nvim_list_wins =true , 61} , 62[ 'runtime/lua/vim/uri.lua' ] ={ 63uri_from_bufnr =true , 64uri_to_bufnr =true , 65} , 66[ 'runtime/lua/vim/_core/shared.lua' ] ={ 67_ensure_list =true , 68_list_insert =true , 69_list_remove =true , 70_resolve_bufnr =true , 71list_contains =true , 72tbl_contains =true , 73} , 74[ 'runtime/lua/vim/lsp.lua' ] ={ 75buf_notify =true , 76} , 77[ 'runtime/lua/vim/lsp/client.lua' ] ={ 78_on_exit =true , 79_process_request =true , 80_process_static_registrations =true , 81_remove_workspace_folder =true , 82} , 83[ 'runtime/lua/vim/lsp/util.lua' ] ={ 84buf_highlight_references =true , 85make_position_params =true , 86} , 87[ 'runtime/lua/vim/lsp/rpc.lua' ] ={ 88_notify =true , 89} , 90[ 'runtime/lua/vim/treesitter.lua' ] ={ 91node_contains =true , 92} , 93[ 'runtime/lua/vim/treesitter/highlighter.lua' ] ={ 94for_each_highlight_state =true , 95prepare_highlight_states =true , 96} , 97[ 'runtime/lua/vim/treesitter/query.lua' ] ={ 98_process_patterns =true , 99} , 100[ 'src/nvim/api/command.c' ] ={ 101create_user_command =true , 102nvim_buf_create_user_command =true , 103nvim_buf_del_user_command =true , 104nvim_create_user_command =true , 105nvim_del_user_command =true , 106} , 107[ 'src/nvim/api/vimscript.c' ] ={ 108nvim_command =true , 109} , 110} 111 112--- Whitelist of legacy Lua class fields that violate the naming conventions. 113--- Do not add new entries here. New classes must follow `:help dev-naming`. 114--- 115--- @type table<string, table<string, true>> 116local legacy_fields ={ 117[ 'vim.Diagnostic' ] ={ 118bufnr =true , 119} , 120[ 'vim.lsp.Capability' ] ={ 121bufnr =true , 122} , 123[ 'vim.lsp.inlay_hint.enable.Filter' ] ={ 124bufnr =true , 125} , 126[ 'vim.lsp.inlay_hint.get.Filter' ] ={ 127bufnr =true , 128} , 129[ 'vim.lsp.inlay_hint.get.ret' ] ={ 130bufnr =true , 131} , 132[ 'TS.Heading' ] ={ 133bufnr =true , -- Passed to setloclist(). 134} , 135[ 'vim.treesitter.get_node.Opts' ] ={ 136bufnr =true , 137} , 138[ 'vim.treesitter.dev.inspect_tree.Opts' ] ={ 139bufnr =true , 140command =true , 141} , 142[ 'vim.undotree.opts' ] ={ 143bufnr =true , 144command =true , 145} , 146} 147 148--- Whitelist of legacy keyset keys that violate the naming conventions. 149--- Do not add new entries here. New keysets must follow `:help dev-naming`. 150--- 151--- @type table<string, table<string, true>> 152local legacy_keys ={ 153create_autocmd ={ 154command =true , 155} , 156} 157 158--- Enforces naming conventions (`:help dev-naming`). 159--- 160--- - For API functions: 161--- - Checks positional parameter names of non-deprecated functions. 162--- The `legacy_names` whitelist does NOT apply to params: misnamed positional 163--- parameters are never allowed. 164--- - Checks function name parts (split by `_`) for banned nouns/verbs. 165--- - For `keysets_defs.h` keys: 166--- - Checks key names. Because we don't (currently) have a way to mark keyset names as 167--- "deprecated", a banned key is allowed (only) if its replacement also exists. 168--- - For Lua classes (opts dicts, etc.): 169--- - Checks field names. A banned field is allowed only if its replacement also exists 170--- (same compat rule as keysets). 171--- 172--- @param source string Source filename or module name. 173--- @param api_funs {name:string, params:{name:string}[], deprecated?:true, deprecated_since?:integer}[]? Parsed API functions. 174--- @param keysets {name: string, keys: string[], types: table<string,string>}[]? API keyset metadata. 175--- @param classes table<string, {name:string, fields:{name:string, access?:string}[], nodoc?:true, access?:string}>? Parsed Lua classes. 176function M . lint_names ( source , api_funs , keysets , classes ) 177local errors ={} --- @type string[] 178 179if api_funs then 180local src_legacy =legacy_names [ source ] or {} 181for _ , fun in ipairs ( api_funs ) do 182if fun . name and fun . params and not fun . deprecated and not fun . deprecated_since then 183-- Positional parameter names: always checked (no legacy allowed). 184-- Exception: "bufnr" is allowed as a param name. 185for _ , p in ipairs ( fun . params ) do 186local want_name =banned_nouns [ p . name ] 187if want_name and p . name ~= 'bufnr' then 188local msg ='%s: %s(): param "%s" should be renamed to "%s"' 189errors [ # errors + 1 ] =fmt ( msg , source , fun . name , p . name , want_name ) 190end 191end 192 193-- Function name parts: check for banned nouns/verbs. 194-- Skip the `nvim_` prefix; start from the second part. 195-- Legacy-whitelisted names are skipped. 196if not src_legacy [ fun . name ] then 197local parts =vim . split ( fun . name , '_' , { plain =true } ) 198for i =2 , # parts do 199local part =parts [ i ] 200local want_noun =banned_nouns [ part ] 201if want_noun then 202local msg ='%s: %s(): name contains banned noun "%s", use "%s"' 203errors [ # errors + 1 ] =fmt ( msg , source , fun . name , part , want_noun ) 204end 205local want_verb =banned_verbs [ part ] 206if want_verb then 207local msg ='%s: %s(): name contains banned verb "%s", use "%s"' 208errors [ # errors + 1 ] =fmt ( msg , source , fun . name , part , want_verb ) 209end 210end 211end 212end 213end 214end 215 216if keysets then 217for _ , k in ipairs ( keysets ) do 218local keyset ={} --- @type table<string, true> 219for _ , key in ipairs ( k . keys ) do 220keyset [ key ] =true 221end 222local ks_legacy =legacy_keys [ k . name ] or {} 223for _ , key in ipairs ( k . keys ) do 224local want_name =banned_nouns [ key ] 225if want_name and not keyset [ want_name ] and not ks_legacy [ key ] then 226-- Banned key without its replacement: not a compat key, just stale. 227local msg ='%s: keyset "%s": key "%s" should be renamed to "%s"' 228errors [ # errors + 1 ] =fmt ( msg , source , k . name , key , want_name ) 229end 230end 231end 232end 233 234if classes then 235for cls_name , cls in pairs ( classes ) do 236if cls . fields and not cls . nodoc and not cls . access then 237local field_set ={} --- @type table<string, true> 238for _ , f in ipairs ( cls . fields ) do 239field_set [ f . name ] =true 240end 241local cls_legacy =legacy_fields [ cls_name ] or {} 242for _ , f in ipairs ( cls . fields ) do 243if not f . access then 244local want_name =banned_nouns [ f . name ] 245if want_name and not field_set [ want_name ] and not cls_legacy [ f . name ] then 246-- Banned field without its replacement: not a compat field, just stale. 247local msg ='%s: class "%s": field "%s" should be renamed to "%s"' 248errors [ # errors + 1 ] =fmt ( msg , source , cls_name , f . name , want_name ) 249end 250end 251end 252end 253end 254end 255 256if # errors > 0 then 257table . sort ( errors ) 258error ( 'lint_names(): found banned parameter/key names:\n ' .. table . concat ( errors , '\n ' )) 259end 260end 261 262--- Checks that markdown list items (`- name :` or `- {name}`) in parameter/field 263--- descriptions are in sorted order. 264--- 265--- This is for "quasi-keysets" defined as markdown lists in docstrings. Ideally they would be 266--- defined as a luals `@class` or a API keyset, but sometimes that isn't possible. 267--- 268--- @param source string Source filename. 269--- @param funs {name:string, params:{name:string, desc?:string}[]}[] 270function M . lint_quasi_keysets ( source , funs ) 271local errors ={} --- @type string[] 272 273for _ , fun in ipairs ( funs ) do 274for _ , p in ipairs ( fun . params or {} ) do 275if p . desc then 276local prev =nil --- @type string? 277-- Capture the keyname with optional punctuation (`, "), so the sort includes surrounding 278-- chars: backtick items group together, double-quote group together, etc. (Example: |Client:stop()|) 279for name in p . desc : gmatch ( '\n%- (["`]?[%w_]+["`]?)' ) do 280-- Sort underscore-prefixed keys (e.g. "_cmdline_offset") at the end. 281local prev_key =prev and prev : gsub ( '^([`"]?)_' , '%1~' ) or nil 282local name_key =name : gsub ( '^([`"]?)_' , '%1~' ) 283if prev_key and name_key < prev_key then 284errors [ # errors + 1 ] =fmt ( 285'%s: %s(): param "%s": key "%s" should come before "%s" (sort alphabetically)' , 286source , 287fun . name , 288p . name , 289name , 290prev 291) 292end 293prev =name 294end 295end 296end 297end 298 299if # errors > 0 then 300error ( 301'lint_quasi_keysets(): unsorted keys in doc comments:\n ' .. table . concat ( errors , '\n ' ) 302) 303end 304end 305 306return M