Skip to content

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

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/issue-154-validate-directive-arguments
Nov 4, 2026
Merged

matt-edmondson merged 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

@matt-edmondson
matt-edmondson merged commit 9603972 into main Nov 4, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the claude/issue-154-validate-directive-arguments branch November 4, 2026 00:53
matt-edmondson pushed a commit that referenced this pull request Nov 4, 2026
…ill refused

PragmaScope.JoinWarnings trimmed every entry (#167), which stripped the
terminator PragmaScopeRejectsWarningsThatSpanLinesBeforeWriting (#155)
relies on, so main failed that test on every platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5jpiuVuBRXSgitSY1
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
…e and out of the semicolon [patch] (#162)

* Keep a preprocessor directive in an expression body off the arrow line and out of the semicolon [patch]

WriteExpressionBody wrote the first content line straight after " => ",
even a directive (CS1040), and appended the semicolon to the last line
with code, which could be #endif (CS1025). A body that opens with a
directive now starts on the line after the arrow, and when a directive
follows the last line of code the semicolon goes on its own line after
it, so every #if branch reaches it. Covers PropertyTemplate
ExpressionBodyFactory and AccessorKind.Expression.

Fixes #158

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

* Split the semicolon placement out of WriteExpressionBody

SonarCloud S3776 flagged the method's cognitive complexity (19 > 15)
after the directive handling. The choice of which line takes the
semicolon moves to FindTerminatedLine, and the code/directive checks
become private helpers; behaviour is unchanged.

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

* Keep a pragma warning holding a line terminator untrimmed so it is still refused

PragmaScope.JoinWarnings trimmed every entry (#167), which stripped the
terminator PragmaScopeRejectsWarningsThatSpanLinesBeforeWriting (#155)
relies on, so main failed that test on every platform.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
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