Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public sealed class StringShouldNotContainsNonDeterministicEndOfLineFixer : Code
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true);
if (nodeToFix is null)
// Only the string literals are fixed, not the interpolated strings
if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not LiteralExpressionSyntax nodeToFix)
return;

foreach (var newLine in new[] { null, "\n", "\r\n" })
Expand All @@ -33,7 +33,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
}
}

private static async Task<Document> FixString(Document document, SyntaxNode nodeToFix, string? newLine, CancellationToken cancellationToken)
private static async Task<Document> FixString(Document document, LiteralExpressionSyntax nodeToFix, string? newLine, CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var generator = editor.Generator;
Expand All @@ -44,15 +44,12 @@ private static async Task<Document> FixString(Document document, SyntaxNode node
var firstCharPosition = nodeToFix.GetLineSpan(cancellationToken)?.StartLinePosition.Character ?? 0;
var newLineTrivia = TriviaList(lineEnding == "\n" ? LineFeed : CarriageReturnLineFeed, Whitespace(new string(' ', firstCharPosition)));

if (nodeToFix is LiteralExpressionSyntax literal)
{
var text = literal.GetText().ToString();
var isVerbatim = text.StartsWith('@', StringComparison.Ordinal);
text = isVerbatim ? text[2..^1] : text[1..^1];
var text = nodeToFix.GetText().ToString();
var isVerbatim = text.StartsWith('@', StringComparison.Ordinal);
text = isVerbatim ? text[2..^1] : text[1..^1];

var newNode = ReplaceString(generator, text, newLine, newLineTrivia);
editor.ReplaceNode(nodeToFix, newNode);
}
var newNode = ReplaceString(generator, text, newLine, newLineTrivia);
editor.ReplaceNode(nodeToFix, newNode);

return editor.GetChangedDocument();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,24 @@ void Test()

return test.RunAsync();
}

[Fact]
public Task InterpolatedString_NoCodeFix()
{
const string Source = """
class Dummy
{
void Test()
{
_ = {|MA0101:$@"line1{0}
line2"|};
}
}
""";
var test = CreateTest();
test.TestCode = Source;
test.FixedCode = Source;

return test.RunAsync();
}
}
Loading