From de094025e8dd630028813372a42b6f6d49700696 Mon Sep 17 00:00:00 2001 From: vinsymoyya <186405436+vinsymoyya@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:07:41 +0530 Subject: [PATCH] Warn about documented arguments on zero-argument commands --- README.md | 3 +++ tests/test_command_argument_warnings.py | 33 +++++++++++++++++++++++++ vimdoc/block.py | 7 ++++++ vimdoc/codeline.py | 2 +- vimdoc/parser.py | 2 ++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/test_command_argument_warnings.py diff --git a/README.md b/README.md index eae3f59..9f11d88 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,9 @@ will generate the following usage line: :[range]SpawnZerglings[!] {hatchery} [larva] +Vimdoc warns if documented arguments are declared for a command +that accepts none (`-nargs=0`, including the default when omitted). + You can override the usage line with the @usage command, which takes a list of arguments. Any arguments that look like vim variable names (\I\i+) will be assumed to be parameters, and their required-ness will be inferred from the diff --git a/tests/test_command_argument_warnings.py b/tests/test_command_argument_warnings.py new file mode 100644 index 0000000..6628240 --- /dev/null +++ b/tests/test_command_argument_warnings.py @@ -0,0 +1,33 @@ +import unittest +import warnings + +from vimdoc import error +from vimdoc import parser + + +class TestCommandArgumentWarnings(unittest.TestCase): + + def parse(self, definition, documentation): + source = ['""'] + ['" ' + line for line in documentation] + [definition] + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always', error.ArgumentMismatch) + list(parser.ParseBlocks(source, 'plugin/example.vim')) + return [item for item in caught if issubclass(item.category, error.ArgumentMismatch)] + + def test_required_argument_for_zero_argument_command_warns(self): + caught = self.parse('command! -nargs=0 Example echo 1', ['Takes {value}.']) + self.assertEqual(1, len(caught)) + + def test_optional_argument_for_implicit_zero_argument_command_warns(self): + caught = self.parse('command! Example echo 1', ['@usage [value]']) + self.assertEqual(1, len(caught)) + + def test_argument_accepting_commands_do_not_warn(self): + for nargs in ['1', '?', '*', '+']: + with self.subTest(nargs=nargs): + self.assertEqual([], self.parse('command! -nargs=' + nargs + ' Example echo 1', + ['Takes {value}.'])) + + def test_builtin_command_modifiers_are_not_arguments(self): + self.assertEqual([], self.parse('command! -range -bang -count Example echo 1', + ['Runs the command.'])) diff --git a/vimdoc/block.py b/vimdoc/block.py index b02113b..7a80fea 100644 --- a/vimdoc/block.py +++ b/vimdoc/block.py @@ -195,6 +195,13 @@ def Close(self): from vimdoc.docline import Usage self.header = Usage('{]') self.locals['usage'] = self.header.GenerateUsage(self) + if (self.locals.get('type') == vimdoc.COMMAND + and self.locals.get('nargs') == '0' + and (regex.required_arg.search(self.locals['usage']) + or regex.optional_arg.search(self.locals['usage']))): + warnings.warn('Documentation declares arguments for command {} ' + 'which accepts no arguments.'.format(self.FullName()), + error.ArgumentMismatch) if 'private' in self.locals and self.locals.get('type') != vimdoc.FUNCTION: raise error.InvalidBlock('Only functions may be marked as private.') return self diff --git a/vimdoc/codeline.py b/vimdoc/codeline.py index 0039550..fca044c 100644 --- a/vimdoc/codeline.py +++ b/vimdoc/codeline.py @@ -121,7 +121,7 @@ def Update(self, block): head += '<>' if self.flags.get('bang'): head += '[!]' - block.Local(head=head) + block.Local(head=head, nargs=self.flags.get('nargs', '0')) class Setting(Definition): diff --git a/vimdoc/parser.py b/vimdoc/parser.py index 66e9bdd..09dadeb 100644 --- a/vimdoc/parser.py +++ b/vimdoc/parser.py @@ -90,6 +90,8 @@ def ParseCodeLine(line): 'buffer': '-buffer' in args, 'bar': '-bar' in args, } + flags['nargs'] = next((flag.split('=', 1)[1] for flag in args.split() + if flag.startswith('-nargs=')), '0') return codeline.Command(name, **flags) smatch = regex.setting_line.match(line) if smatch: