Skip to content

Commit deaee44

Browse files
committed
Fix tests.
1 parent 97336de commit deaee44

3 files changed

Lines changed: 42 additions & 16 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP005ParameterRegexTests/CodeFix.cs

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

14-
[TestCase("\"api/orders/{id:regex(↓a{1})}\"", "\"api/orders/{id:regex(a{{1}})}\"")]
15-
[TestCase("\"api/orders/{id:regex(↓\\\\d+)}", "\"api/orders/{id:regex(\\\\\\\\d+)}\"")]
16-
[TestCase("@\"api/orders/{id:regex(↓\\d+)}", "@\"api/orders/{id:regex(\\\\d+)}\"")]
17-
[TestCase("\"api/orders/{id:regex(↓^\\\\d{3}-\\\\d{2}-\\\\d{4}$)}", "\"api/orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{{4}}$)}\"")]
18-
[TestCase("\"api/orders/{id:regex(↓^[a-z]{2}$)}\"", "\"api/orders/{id:regex(^[[a-z]]{{2}}$)}\"")]
14+
[TestCase("\"api/orders/{id:regex(↓a{1})}\"", "\"api/orders/{id:regex(a{{1}})}\"")]
15+
[TestCase("\"api/orders/{id:regex(↓^[a-z]{2}$)}\"", "\"api/orders/{id:regex(^[[a-z]]{{2}}$)}\"")]
16+
[TestCase("\"api/orders/{id:regex(↓\\\\d+)}\"", "\"api/orders/{id:regex(\\\\\\\\d+)}\"")]
17+
[TestCase("@\"api/orders/{id:regex(↓\\d+)}\"", "@\"api/orders/{id:regex(\\\\d+)}\"")]
18+
[TestCase("\"api/orders/{id:regex(↓^\\\\d{3}-\\\\d{2}-\\\\d{4}$)}\"", "\"api/orders/{id:regex(^\\\\\\\\d{{3}}-\\\\\\\\d{{2}}-\\\\\\\\d{{4}}$)}\"")]
19+
[TestCase("@\"api/orders/{id:regex(↓^\\d{3}-\\d{2}-\\d{4}$)}\"", "@\"api/orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{{4}}$)}\"")]
1920
public void When(string before, string after)
2021
{
2122
var code = @"

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ private static bool HasWrongRegexSyntax(PathSegment segment, out Location locati
366366
if (NotEscaped())
367367
{
368368
escaped.Add(text[i]);
369+
if (text[i] == '\\' &&
370+
!segment.Span.IsVerbatim)
371+
{
372+
escaped.Add('\\');
373+
escaped.Add('\\');
374+
}
369375
}
370376
}
371377

AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,38 @@ namespace AspNetCoreAnalyzers
77

88
public struct Span : IEquatable<Span>
99
{
10-
private readonly LiteralExpressionSyntax literal;
11-
1210
public Span(LiteralExpressionSyntax literal, int start, int end)
1311
{
14-
this.literal = literal;
12+
this.Literal = literal;
1513
this.TextSpan = new TextSpan(start, end - start);
1614
this.Text = literal.Token.ValueText.Substring(start, end - start);
1715
}
1816

17+
public LiteralExpressionSyntax Literal { get; }
18+
1919
public TextSpan TextSpan { get; }
2020

2121
public string Text { get; }
2222

23+
public bool IsVerbatim
24+
{
25+
get
26+
{
27+
foreach (var c in this.Literal.Token.Text)
28+
{
29+
switch (c)
30+
{
31+
case '"':
32+
return false;
33+
case '@':
34+
return true;
35+
}
36+
}
37+
38+
return false;
39+
}
40+
}
41+
2342
public static bool operator ==(Span left, Span right)
2443
{
2544
return left.Equals(right);
@@ -32,7 +51,7 @@ public Span(LiteralExpressionSyntax literal, int start, int end)
3251

3352
public bool Equals(Span other)
3453
{
35-
return this.literal.Equals(other.literal) && this.TextSpan == other.TextSpan;
54+
return this.Literal.Equals(other.Literal) && this.TextSpan == other.TextSpan;
3655
}
3756

3857
public override bool Equals(object obj)
@@ -45,20 +64,20 @@ public override int GetHashCode()
4564
{
4665
unchecked
4766
{
48-
var hashCode = this.literal.GetHashCode();
67+
var hashCode = this.Literal.GetHashCode();
4968
hashCode = (hashCode * 397) ^ this.TextSpan.GetHashCode();
5069
return hashCode;
5170
}
5271
}
5372

5473
public override string ToString()
5574
{
56-
return this.literal.Token.ValueText.Substring(this.TextSpan.Start, this.TextSpan.Length);
75+
return this.Literal.Token.ValueText.Substring(this.TextSpan.Start, this.TextSpan.Length);
5776
}
5877

59-
public Location GetLocation() => GetLocation(this.literal, this.TextSpan);
78+
public Location GetLocation() => GetLocation(this.Literal, this.TextSpan);
6079

61-
public Location GetLocation(int start, int length) => GetLocation(this.literal, new TextSpan(this.TextSpan.Start + start, length));
80+
public Location GetLocation(int start, int length) => GetLocation(this.Literal, new TextSpan(this.TextSpan.Start + start, length));
6281

6382
internal Span Slice(int start, int end)
6483
{
@@ -72,17 +91,17 @@ internal Span Slice(int start, int end)
7291
throw new InvalidOperationException("Expected end to be less than TextSpan.End.");
7392
}
7493

75-
return new Span(this.literal, this.TextSpan.Start + start, this.TextSpan.Start + end);
94+
return new Span(this.Literal, this.TextSpan.Start + start, this.TextSpan.Start + end);
7695
}
7796

7897
internal Span Substring(int index, int length)
7998
{
80-
return new Span(this.literal, this.TextSpan.Start + index, this.TextSpan.Start + index + length);
99+
return new Span(this.Literal, this.TextSpan.Start + index, this.TextSpan.Start + index + length);
81100
}
82101

83102
internal Span Substring(int index)
84103
{
85-
return new Span(this.literal, this.TextSpan.Start + index, this.TextSpan.Start + index + this.TextSpan.Length);
104+
return new Span(this.Literal, this.TextSpan.Start + index, this.TextSpan.Start + index + this.TextSpan.Length);
86105
}
87106

88107
private static Location GetLocation(LiteralExpressionSyntax literal, TextSpan textSpan)

0 commit comments

Comments
 (0)