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

Improve RCS0056 #1373

Merged
merged 8 commits into from
Jan 24, 2024
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 @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1361))
- Fix analyzer [RCS1261](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1261) ([PR](https://github.com/dotnet/roslynator/pull/1374))
- Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1373))

## [4.9.0] - 2024-01-10

Expand Down
69 changes: 61 additions & 8 deletions src/Formatting.Analyzers/CSharp/LineIsTooLongAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,76 @@ private static void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
if (line.Span.Length <= maxLength)
continue;

int start = line.Start;
int end = line.End;

SyntaxToken token = root.FindToken(end);

if (token.IsKind(SyntaxKind.StringLiteralToken))
if (token.IsKind(SyntaxKind.None)
|| token.Span.End < start)
{
TextSpan span = token.Span;
continue;
}

SyntaxToken token2 = token;

if (span.End == end)
if (token2.IsKind(SyntaxKind.CommaToken, SyntaxKind.SemicolonToken))
token2 = token2.GetPreviousToken();

if (!token2.IsKind(SyntaxKind.None)
&& token2.Span.End >= start)
{
while (token2.IsKind(
SyntaxKind.CloseParenToken,
SyntaxKind.CloseBraceToken,
SyntaxKind.CloseBracketToken))
{
if (span.Length >= maxLength)
continue;
token2 = token2.GetPreviousToken();
}
else if (span.Contains(end)
&& end - span.Start >= maxLength)

if (token2.IsKind(
SyntaxKind.StringLiteralToken,
#if ROSLYN_4_2
SyntaxKind.InterpolatedRawStringEndToken,
#endif
SyntaxKind.InterpolatedStringEndToken))
{
continue;
SyntaxNode parent = token2.Parent;

if (parent.SpanStart <= start)
continue;

token2 = parent.GetFirstToken().GetPreviousToken();

if (token2.IsKind(SyntaxKind.None)
|| token2.Span.End < start)
{
continue;
}

if (parent.IsParentKind(
SyntaxKind.ArrowExpressionClause,
SyntaxKind.Argument,
SyntaxKind.AttributeArgument))
{
SyntaxToken firstToken = parent.Parent.GetFirstToken();

if (firstToken.SpanStart >= start)
{
SyntaxToken token3 = firstToken.GetPreviousToken();

if (token3.IsKind(SyntaxKind.None)
|| token3.Span.End < start)
{
continue;
}
}
}

if (parent.Span.End > end)
{
i = lines.IndexOf(parent.Span.End) - 1;
}
}
}

Expand Down
116 changes: 112 additions & 4 deletions src/Tests/Formatting.Analyzers.Tests/RCS0056LineIsTooLongTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,24 +1000,24 @@ class C
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoFix_ExpressionBody_AlreadyWrapped()
{
await VerifyDiagnosticAndNoFixAsync(
await VerifyNoDiagnosticAsync(
@"
class C
{
string M(object p)
[| => ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";|]
=> ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoFix_ExpressionBody_AlreadyWrapped2()
{
await VerifyDiagnosticAndNoFixAsync(@"
await VerifyNoDiagnosticAsync(@"
class C
{
string M(object p) =>
[| ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";|]
""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";
}
");
}
Expand Down Expand Up @@ -1180,4 +1180,112 @@ void M()
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongStringLiteral_Argument()
{
await VerifyNoDiagnosticAsync("""
class C
{
static void M(string x, string y)
{
C.M(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongStringLiteral_AttributeArgument()
{
await VerifyNoDiagnosticAsync("""
using System;
class C
{
[Obsolete(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
static void M()
{
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongStringLiteral_AttributeArgument2()
{
await VerifyNoDiagnosticAsync("""
using System;
class C
{
[My(
Value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
static void M()
{
}
}
class MyAttribute : Attribute
{
public string Value { get; set; }
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongVerbatimStringLiteral()
{
await VerifyNoDiagnosticAsync("""
class C
{
static void M(string x, int y)
{
C.M(
@"
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
0);
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongInterpolatedString()
{
await VerifyNoDiagnosticAsync("""
class C
{
static void M(string x, string y)
{
C.M(
$"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
$"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
public async Task TestNoDiagnostic_LongRawInterpolatedString()
{
await VerifyNoDiagnosticAsync(""""
class C
{
static void M(string x, int y)
{
C.M(
$"""
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
""",
0);
}
}
"""");
}
}