Skip to content

Add ProhibitMisplacedVim9script policy - #6

Merged
MindTooth merged 1 commit into
masterfrom
feat/misplaced-vim9script-policy
Nov 8, 2026
Merged

MindTooth merged 1 commit into
masterfrom
feat/misplaced-vim9script-policy

Conversation

@MindTooth

Copy link
Copy Markdown
Owner

What

New ProhibitMisplacedVim9script policy (Level.ERROR, enabled by default). Vim gives E1039 when any command runs before :vim9script, and E1038/E1039 when it's used inside a block or function. The policy reports a vim9script that:

  • is nested inside an if, loop, or function, or
  • follows a top-level command other than:
    • a comment, or
    • an if block (no elseif/else) whose last command is finish. This is the vim9-mix pattern from :help vim9-mix:
" legacy comments
if !has('vim9script')
  " legacy fallback
  finish
endif
vim9script

Why

Vim only checks this when the script is sourced, so a misplaced vim9script breaks the whole file without any warning until then. The allowed cases follow Vim's own logic (ex_vim9script() in src/vim9script.c and the SN_STATE_HAD_COMMAND handling in src/ex_docmd.c): comments and skipped if bodies don't count as commands.

Testing

  • Five fixtures and test_prohibit_misplaced_vim9script.py, covering: comments before vim9script, vim9-mix, a command first, vim9script inside an if, and an if without finish.
  • Ran against the 113 Vim9 scripts in Vim's runtime. Nothing was reported, but only 32 of them parse with the current bundled vimlparser. The rest hit parser gaps such as bare assignment and classes, tracked upstream in Complete vim9 support vim-jp/vim-vimlparser#206.
  • Full suite: 212 passed, 1 skipped, 1 failed. The failure is test_exec_vint_with_color_flag, which also fails on master locally, so this change didn't cause it.
  • flake8 clean with the repo config.

Notes for reviewers

  • Conditions aren't evaluated. Any if … finish | endif before vim9script is accepted, matching the documented pattern even when the condition would make Vim stop early.

🤖 Generated with Claude Code

Vim gives E1039 when any command runs before :vim9script, and E1038
or E1039 when it is used inside a block or function. Report a
:vim9script that is nested, or that follows a top-level command other
than a comment or the vim9-mix pattern `if ... finish | endif`
(:help vim9-mix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MindTooth
MindTooth merged commit f597134 into master Nov 8, 2026
@MindTooth
MindTooth deleted the feat/misplaced-vim9script-policy branch November 8, 2026 00:20
@MindTooth
MindTooth restored the feat/misplaced-vim9script-policy branch November 8, 2026 00:26
@MindTooth
MindTooth deleted the feat/misplaced-vim9script-policy branch November 8, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant