From 2e1c4f092639bd926a2ec20ce5e162604768f030 Mon Sep 17 00:00:00 2001 From: Brent Miller Date: Mon, 16 Nov 2026 21:11:15 -0600 Subject: [PATCH] fix(analyzers): keep escaped braces literal in the E128015 fix - scan the format string into literal runs, so doubled braces stay in one text token - escape source syntax in Text and keep the format content in ValueText - add regression tests for escaped braces alone and beside a placeholder - update the code fix output lode note and drop the redundant AnalysisMode lines Co-Authored-By: Claude --- lode/dotnet/analyzers.md | 7 +- lode/dotnet/analyzers/code-fix-output.md | 10 +-- ...ingFormatToInterpolationCodeFixProvider.cs | 72 +++++++++---------- ...ngFormatToInterpolationE128CodeFixTests.cs | 50 +++++++++++++ 4 files changed, 93 insertions(+), 46 deletions(-) diff --git a/lode/dotnet/analyzers.md b/lode/dotnet/analyzers.md index 752347cc..47acb421 100644 --- a/lode/dotnet/analyzers.md +++ b/lode/dotnet/analyzers.md @@ -1,5 +1,5 @@ # .NET 10 Roslyn Analyzers -*Updated: 2026-11-16T16:20:12Z* +*Updated: 2026-11-17T00:00:52Z* ## Strategy: Deny by Default @@ -33,16 +33,13 @@ In `Directory.Build.props`: ```xml latest-all All -All -All -All true true true ``` Key interactions: -- `AnalysisLevel=latest-all` and `AnalysisMode=All` both select mode `All` in every category. The security, reliability, and performance category lines repeat that mode. They state the AI SDLC Readier policy explicitly. Do not set `AnalysisMode` below `All`, because the CA rules that are off in `Recommended` would stop running. +- `AnalysisLevel=latest-all` and `AnalysisMode=All` both select mode `All` in every category. The AI SDLC Readier policy for the security, reliability, and performance categories needs no separate category line. Do not set `AnalysisMode` below `All`, because the CA rules that are off in `Recommended` would stop running. - `EnforceCodeStyleInBuild` makes IDE rules (IDE*) fire during `dotnet build` - `TreatWarningsAsErrors` promotes all enabled warnings to errors diff --git a/lode/dotnet/analyzers/code-fix-output.md b/lode/dotnet/analyzers/code-fix-output.md index 48ae783e..9cad5845 100644 --- a/lode/dotnet/analyzers/code-fix-output.md +++ b/lode/dotnet/analyzers/code-fix-output.md @@ -1,5 +1,5 @@ # Code Fix Output -*Updated: 2026-11-03T03:14:50Z* +*Updated: 2026-11-17T03:01:43Z* Rules for the text a fix writes and the text around it. A fix that emits a broken member costs more than a missing fix: the apply path writes it into the @@ -36,10 +36,10 @@ registers, so every offered fix changes code. ## Text in an Interpolated String -An `InterpolatedStringTextToken` carries two texts. `Text` is source text, so -escape it. `ValueText` is the runtime value, so keep it raw. Set both to the -escaped text and a tree comparison against parsed source fails. Set both raw -and a quote or a line break breaks the emitted literal. +An `InterpolatedStringTextToken` carries source text and value text. Escape +backslashes, quotes, and line breaks in `Text`. Keep the original literal run in +`ValueText`. Roslyn keeps doubled braces in both fields and combines each +literal run into one token. The binder resolves escaped braces later. ## Place a New Node with the Formatter diff --git a/src/E128.Analyzers.CodeFixers/Performance/StringFormatToInterpolationCodeFixProvider.cs b/src/E128.Analyzers.CodeFixers/Performance/StringFormatToInterpolationCodeFixProvider.cs index 7f3de426..e2e99c74 100644 --- a/src/E128.Analyzers.CodeFixers/Performance/StringFormatToInterpolationCodeFixProvider.cs +++ b/src/E128.Analyzers.CodeFixers/Performance/StringFormatToInterpolationCodeFixProvider.cs @@ -108,60 +108,76 @@ private static async Task ApplyFixAsync( ExpressionSyntax[] arguments) { var contents = new List(); + var literalStart = 0; var i = 0; while (i < formatString.Length) { if (formatString[i] == '{') { - i = ProcessOpenBrace(formatString, i, arguments, contents); - if (i < 0) + if (i + 1 < formatString.Length && formatString[i + 1] == '{') { - return null; + i += DoubledBraceLength; + continue; } - } - else if (formatString[i] == '}') - { - // A brace is either doubled (a literal brace) or unmatched. An unmatched brace - // cannot be reproduced in an interpolated string, so the fix declines. - if (i + 1 >= formatString.Length || formatString[i + 1] != '}') + + AddLiteralText(formatString, literalStart, i, contents); + i = ProcessPlaceholder(formatString, i, arguments, contents); + if (i < 0) { return null; } - contents.Add(CreateTextContent("}")); + literalStart = i; + } + else if (formatString[i] != '}') + { + i++; + } + else if (i + 1 < formatString.Length && formatString[i + 1] == '}') + { i += DoubledBraceLength; } else { - i = ProcessLiteralText(formatString, i, contents); + return null; } } + AddLiteralText(formatString, literalStart, formatString.Length, contents); + return SyntaxFactory.InterpolatedStringExpression( SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken), SyntaxFactory.List(contents)); } - private static int ProcessOpenBrace( + private static void AddLiteralText( string formatString, - int i, - ExpressionSyntax[] arguments, + int start, + int end, List contents) { - if (i + 1 < formatString.Length && formatString[i + 1] == '{') + if (start >= end) { - contents.Add(CreateTextContent("{")); - return i + DoubledBraceLength; + return; } - var closeIndex = formatString.IndexOf('}', i); + contents.Add(CreateTextContent(formatString.Substring(start, end - start))); + } + + private static int ProcessPlaceholder( + string formatString, + int start, + ExpressionSyntax[] arguments, + List contents) + { + var closeIndex = formatString.IndexOf('}', start); if (closeIndex < 0) { return -1; } - var placeholder = formatString.Substring(i + 1, closeIndex - i - 1); + var placeholder = formatString.Substring(start + 1, closeIndex - start - 1); var interpolation = BuildInterpolation(placeholder, arguments); if (interpolation is null) { @@ -203,23 +219,7 @@ private static int ProcessOpenBrace( return SyntaxFactory.Interpolation(arguments[argIndex]); } - private static int ProcessLiteralText( - string formatString, - int start, - List contents) - { - var i = start; - while (i < formatString.Length && formatString[i] != '{' && formatString[i] != '}') - { - i++; - } - - contents.Add(CreateTextContent(formatString.Substring(start, i - start))); - return i; - } - - // The token carries source text, so a backslash, a quote, or a line break must be escaped. - // The format string holds the runtime value, and a raw value breaks the emitted literal. + // Escape source syntax in Text. Keep format-string content, including doubled braces, in ValueText. private static InterpolatedStringTextSyntax CreateTextContent(string text) { var escaped = EscapeText(text); diff --git a/tests/E128.Analyzers.Tests/StringFormatToInterpolationE128CodeFixTests.cs b/tests/E128.Analyzers.Tests/StringFormatToInterpolationE128CodeFixTests.cs index ff03db56..60d6815f 100644 --- a/tests/E128.Analyzers.Tests/StringFormatToInterpolationE128CodeFixTests.cs +++ b/tests/E128.Analyzers.Tests/StringFormatToInterpolationE128CodeFixTests.cs @@ -106,6 +106,56 @@ void M() """); } + [Fact] + [Trait("Category", "CI")] + public Task StringFormat_EscapedBraces_StayLiteral() + { + return VerifyFixAsync( + """ + class C + { + void M(int n) + { + var s = {|E128015:string.Format("{{x}} {0}", n)|}; + } + } + """, + """ + class C + { + void M(int n) + { + var s = $"{{x}} {n}"; + } + } + """); + } + + [Fact] + [Trait("Category", "CI")] + public Task StringFormat_EscapedBracesBesidePlaceholder_StayLiteral() + { + return VerifyFixAsync( + """ + class C + { + void M(int n) + { + var s = {|E128015:string.Format("{{{0}}} {{ {0} }}", n)|}; + } + } + """, + """ + class C + { + void M(int n) + { + var s = $"{{{n}}} {{ {n} }}"; + } + } + """); + } + [Fact] [Trait("Category", "CI")] public Task StringFormat_UnmatchedBrace_NoFixOffered()