Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RCS1197 #1166

Merged
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
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1158](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1158) ([PR](https://github.com/dotnet/roslynator/pull/1288))
- Fix analyzer [RCS1032](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1032) ([PR](https://github.com/dotnet/roslynator/pull/1289))
- Fix analyzer [RCS1176](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1176) ([PR](https://github.com/dotnet/roslynator/pull/1291))
- Fix analyzer [RCS1197](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1197) ([PR](https://github.com/dotnet/roslynator/pull/1166))

## [4.6.4] - 2023-11-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,69 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)]
public async Task Test_InterpolatedRawString_ContainingQuotes()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Text;
class C
{
void M()
{
string s = null;
var sb = new StringBuilder();
sb.Append([|$""""""""<a href=""somelink"">{s}</a>""""""""|]);
}
}
", @"
using System.Text;
class C
{
void M()
{
string s = null;
var sb = new StringBuilder();
sb.Append(""""""""<a href=""somelink"">"""""""").Append(s).Append(""""""""</a>"""""""");
}
}
", options: WellKnownCSharpTestOptions.Default_CSharp11);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)]
public async Task Test_InterpolatedMultilineRawString_ContainingQuotes()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Text;
class C
{
void M()
{
string s = null;
var sb = new StringBuilder();
sb.Append([|$""""""
<a href=""somelink"">{s}</a>
""""""|]
);
}
}
", @"
using System.Text;
class C
{
void M()
{
string s = null;
var sb = new StringBuilder();
sb.Append(""""""
<a href=""somelink"">
"""""").Append(s).Append(""""""
</a>
"""""");
}
}
", options: WellKnownCSharpTestOptions.Default_CSharp11);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)]
public async Task Test_InterpolatedString_Char()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class WellKnownCSharpTestOptions
private static CSharpTestOptions _default_CSharp7_3;
private static CSharpTestOptions _default_CSharp8;
private static CSharpTestOptions _default_CSharp9;
private static CSharpTestOptions _default_CSharp11;
private static CSharpTestOptions _default_NullableReferenceTypes;

public static CSharpTestOptions Default_CSharp5
Expand Down Expand Up @@ -94,6 +95,19 @@ public static CSharpTestOptions Default_CSharp9
}
}

public static CSharpTestOptions Default_CSharp11
{
get
{
if (_default_CSharp11 is null)
Interlocked.CompareExchange(ref _default_CSharp11, Create(), null);

return _default_CSharp11;

static CSharpTestOptions Create() => DefaultCSharpTestOptions.Value.WithParseOptions(DefaultCSharpTestOptions.Value.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp11));
}
}

public static CSharpTestOptions Default_NullableReferenceTypes
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.Text;
Expand Down Expand Up @@ -70,12 +71,30 @@ public static (SyntaxKind contentKind, string methodName, ImmutableArray<Argumen
string text = interpolatedStringText.TextToken.Text;

text = StringUtility.ReplaceDoubleBracesWithSingleBrace(text);
if (content.Parent is InterpolatedStringExpressionSyntax interpolatedStringExpression
&& interpolatedStringExpression.StringStartToken.IsKind(SyntaxKind.InterpolatedSingleLineRawStringStartToken))
{
text = interpolatedStringExpression.StringStartToken.ValueText.Substring(1)
+ text
+ interpolatedStringExpression.StringEndToken.ValueText;
}
else if (content.Parent is InterpolatedStringExpressionSyntax interpolatedStringExpression2
&& interpolatedStringExpression2.StringStartToken.IsKind(SyntaxKind.InterpolatedMultiLineRawStringStartToken))
{
text = interpolatedStringExpression2.StringStartToken.ValueText.Substring(1)
+ text
+ interpolatedStringExpression2.StringEndToken.ValueText;
}
else if (isVerbatim)
{
text = "@\"" + text + "\"";
}
else
{
text = "\"" + text + "\"";
}

text = (isVerbatim)
? "@\"" + text + "\""
: "\"" + text + "\"";

ExpressionSyntax stringLiteral = ParseExpression(text);
ExpressionSyntax stringLiteral = ParseExpression(text).WithTriviaFrom(interpolatedStringText);

return (kind, "Append", ImmutableArray.Create(Argument(stringLiteral)));
}
Expand Down