Skip to content

Refuse an empty #if condition and a multi-line directive argument [patch] - #155

Open
matt-edmondson wants to merge 1 commit into
mainfrom
claude/issue-154-validate-directive-arguments
Open

matt-edmondson wants to merge 1 commit into
mainfrom
claude/issue-154-validate-directive-arguments

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #154

What changed

  • DirectiveScope throws ArgumentException before writing anything when the condition is null, empty or whitespace. That case used to write a bare #if (CS1517). It also throws when the condition contains a line terminator.
  • PragmaScope throws ArgumentException before writing anything when the warning list contains a line terminator. One check covers both constructors, because the IEnumerable<string> overload joins its entries first. Empty or whitespace warnings are still a no-op.
  • Both use the LineTerminators helper that RegionScope got in Comment every line of a multi-line copyright, and refuse a multi-line region name [patch] #152 (LF, CRLF, CR, NEL, U+2028, U+2029).

Tests

  • New in ScopesTests: DirectiveScopeRejectsAMissingConditionBeforeWriting (empty, whitespace, null), DirectiveScopeRejectsAConditionThatSpansLinesBeforeWriting and PragmaScopeRejectsWarningsThatSpanLinesBeforeWriting (each of the six terminators, both PragmaScope constructors). Each one also asserts that nothing was written.
  • With Scopes.cs reverted, all 15 new cases fail. With the fix, the full suite passes: 264 of 264 on net10.0, Linux, including PragmaScopeWithAnEmptyStringIsANoOp.

Note: releases are still blocked by #153 (pack fails package validation), so this won't reach NuGet until that is fixed.


Generated by Claude Code

…tch]

DirectiveScope wrote its condition verbatim, so an empty, null or
whitespace condition produced a bare "#if" (CS1517), and a condition with
a line break left the rest of the expression outside the directive as
code. PragmaScope had the same line-break problem with its warning list,
including a single entry of the IEnumerable overload that kept a stray CR.

Both now throw ArgumentException before writing anything, using the same
LineTerminators check RegionScope got in #152. PragmaScope still treats
empty or whitespace warnings as a no-op.

Fixes #154

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2yWAiM4Xm9iHUvALj
@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants