Skip to content

Commit 0533d09

Browse files
committed
Tweak Span.GetLocation
1 parent 9d1cba9 commit 0533d09

5 files changed

Lines changed: 61 additions & 20 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP002MissingParameterTests/CodeFix.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ public IActionResult GetValue(string text)
5454
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
5555
}
5656

57-
[TestCase("\"api/{text1}/{↓value}\"", "\"api/{text1}/{text2}\"")]
58-
[TestCase("\"api/{text1:regex(\\\\d+)}/{↓value}\"", "\"api/{text1:regex(\\\\d+)}/{text2}\"")]
59-
[TestCase("@\"api/{text1:regex(\\d+)}/{↓value}\"", "@\"api/{text1:regex(\\d+)}/{text2}\"")]
57+
[TestCase("\"api/{text1}/{↓value}\"", "\"api/{text1}/{text2}\"")]
58+
[TestCase("\"api/{↓value}/{text2}\"", "\"api/{text1}/{text2}\"")]
59+
[TestCase("\"api/{text1:regex(\\\\d+)}/{↓value}\"", "\"api/{text1:regex(\\\\d+)}/{text2}\"")]
60+
[TestCase("\"api/{text1:regex(\\\\\\\\d+)}/{↓value}\"", "\"api/{text1:regex(\\\\\\\\d+)}/{text2}\"")]
61+
[TestCase("@\"api/{text1:regex(\\d+)}/{↓value}\"", "@\"api/{text1:regex(\\d+)}/{text2}\"")]
62+
[TestCase("\"api/{text1::regex(^\\\\\\\\d{{3}}-\\\\\\\\d{{2}}-\\\\\\\\d{{4}}$)}/{↓value}\"", "\"api/{text1::regex(^\\\\\\\\d{{3}}-\\\\\\\\d{{2}}-\\\\\\\\d{{4}}$)}/{text2}\"")]
6063
public void WhenWrongNameSecondParameter(string before, string after)
6164
{
6265
var code = @"

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/CodeFix.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ public class CodeFix
1111
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(ASP004ParameterSyntax.Descriptor);
1212
private static readonly CodeFixProvider Fix = new TemplateTextFix();
1313

14-
[TestCase("api/orders/↓id:long}", "api/orders/{id:long}")]
15-
[TestCase("api/orders/↓{id:long", "api/orders/{id:long}")]
16-
[TestCase("api/orders/{id:min(1}", "api/orders/{id:min(1)}")]
17-
[TestCase("api/orders/{id:max(1}", "api/orders/{id:max(1)}")]
18-
[TestCase("api/orders/{id:minlength(1}", "api/orders/{id:minlength(1)}")]
19-
[TestCase("api/orders/{id:maxlength(1}", "api/orders/{id:maxlength(1)}")]
20-
[TestCase("api/orders/{id:length(1}", "api/orders/{id:length(1)}")]
21-
[TestCase("api/orders/{id:length(1,2}", "api/orders/{id:length(1,2)}")]
22-
[TestCase("api/orders/{id:range(1,2}", "api/orders/{id:range(1,2)}")]
23-
[TestCase("api/orders/{id:regex((a|b)-c}", "api/orders/{id:regex((a|b)-c)}")]
24-
public void When(string before, string after)
14+
[TestCase("api/orders/↓id:long}", "api/orders/{id:long}")]
15+
[TestCase("api/orders/↓{id:long", "api/orders/{id:long}")]
16+
[TestCase("api/orders/{id:min(1}", "api/orders/{id:min(1)}")]
17+
[TestCase("api/orders/{id:max(1}", "api/orders/{id:max(1)}")]
18+
[TestCase("api/orders/{id:range(1,2}", "api/orders/{id:range(1,2)}")]
19+
public void WhenLong(string before, string after)
2520
{
2621
var code = @"
2722
namespace ValidCode
@@ -56,5 +51,48 @@ public IActionResult GetId(long id)
5651
}".AssertReplace("api/orders/{id:long}", after);
5752
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
5853
}
54+
55+
[TestCase("\"api/orders/{id:↓minlength(1}\"", "\"api/orders/{id:minlength(1)}\"")]
56+
[TestCase("\"api/orders/{id:↓maxlength(1}\"", "\"api/orders/{id:maxlength(1)}\"")]
57+
[TestCase("\"api/orders/{id:↓length(1}\"", "\"api/orders/{id:length(1)}\"")]
58+
[TestCase("\"api/orders/{id:↓length(1,2}\"", "\"api/orders/{id:length(1,2)}\"")]
59+
[TestCase("\"api/orders/{id:↓regex((a|b)-c}\"", "\"api/orders/{id:regex((a|b)-c)}\"")]
60+
[TestCase("\"api/orders/{id:regex(\\\\d+):↓length(1}\"", "\"api/orders/{id:regex(\\\\d+):length(1)}\"")]
61+
public void WhenString(string before, string after)
62+
{
63+
var code = @"
64+
namespace ValidCode
65+
{
66+
using Microsoft.AspNetCore.Mvc;
67+
68+
[ApiController]
69+
public class OrdersController : Controller
70+
{
71+
[HttpGet(""api/orders/{id}"")]
72+
public IActionResult GetId(string id)
73+
{
74+
return this.Ok(id);
75+
}
76+
}
77+
}".AssertReplace("\"api/orders/{id}\"", before);
78+
79+
var fixedCode = @"
80+
namespace ValidCode
81+
{
82+
using Microsoft.AspNetCore.Mvc;
83+
84+
[ApiController]
85+
public class OrdersController : Controller
86+
{
87+
[HttpGet(""api/orders/{id}"")]
88+
public IActionResult GetId(string id)
89+
{
90+
return this.Ok(id);
91+
}
92+
}
93+
}".AssertReplace("\"api/orders/{id}\"", after);
94+
95+
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
96+
}
5997
}
6098
}

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/Diagnostics.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public IActionResult GetId(long id)
4343
}
4444

4545
[TestCase("\"api/orders/{id:minlength(↓wrong))}\"")]
46+
[TestCase("\"api/orders/{id:minlength(↓1a))}\"")]
47+
[TestCase("\"api/orders/{id:minlength(↓a1))}\"")]
4648
[TestCase("\"api/orders/{id:maxlength(↓wrong))}\"")]
4749
[TestCase("\"api/orders/{id:regex(\\\\d):minlength(↓wrong))}\"")]
4850
[TestCase("@\"api/orders/{id:regex(\\d):minlength(↓wrong))}\"")]
@@ -67,8 +69,8 @@ public IActionResult GetId(string id)
6769
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
6870
}
6971

70-
[TestCase("\"api/orders/{id:regex(\\\\d):minlength(wrong))}\"", 53, 58)]
71-
[TestCase("@\"api/orders/{id:regex(\\d):minlength(wrong))}\"", 53, 56)]
72+
[TestCase("\"api/orders/{id:regex(\\\\d):minlength(wrong)}\"", 54, 59)]
73+
[TestCase("@\"api/orders/{id:regex(\\d):minlength(wrong)}\"", 54, 59)]
7274
public void WhenStringExplicitSpan(string before, int start, int end)
7375
{
7476
var code = @"

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ bool HasWrongIntArgumentSyntax(RouteConstraint constraint, string methodName, ou
335335
{
336336
if (!char.IsDigit(text[i]))
337337
{
338-
result = constraint.Span.GetLocation(i);
338+
result = constraint.Span.GetLocation(methodName.Length + 1, text.Length - methodName.Length - 2);
339339
return true;
340340
}
341341
}

AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ public override int GetHashCode()
5555

5656
public Location GetLocation() => GetLocation(this.literal, this.TextSpan);
5757

58-
public Location GetLocation(int start) => Location.Create(this.literal.SyntaxTree, new TextSpan(this.literal.SpanStart + this.TextSpan.Start + start + 1, this.TextSpan.Length - start));
59-
60-
public Location GetLocation(int start, int length) => Location.Create(this.literal.SyntaxTree, new TextSpan(this.literal.SpanStart + this.TextSpan.Start + start + 1, length));
58+
public Location GetLocation(int start, int length) => GetLocation(this.literal, new TextSpan(this.TextSpan.Start + start, length));
6159

6260
internal Span Slice(int start, int end)
6361
{

0 commit comments

Comments
 (0)