Skip to content

Commit f750746

Browse files
committed
Check closing parenthesis. #22.
1 parent c882505 commit f750746

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/CodeFix.cs

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

14-
[TestCase("api/orders/↓id:long}", "api/orders/{id:long}")]
15-
[TestCase("api/orders/↓{id:long", "api/orders/{id:long}")]
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)}")]
1624
public void WhenFixable(string before, string after)
1725
{
1826
var code = @"

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,49 @@ private static bool HasWrongType(ParameterPair pair, out string correctType)
220220

221221
private static bool HasWrongSyntax(PathSegment segment, out Location location, out string correctSyntax)
222222
{
223-
var text = segment.Span.Text;
224-
if (text.StartsWith("{", StringComparison.Ordinal) &&
225-
!text.EndsWith("}", StringComparison.Ordinal))
223+
if (segment.Parameter is TemplateParameter parameter)
226224
{
227-
location = segment.Span.GetLocation();
228-
correctSyntax = text + "}";
229-
return true;
225+
foreach (var constraint in parameter.Constraints)
226+
{
227+
var text = constraint.Span.Text;
228+
if (text.StartsWith("min(", StringComparison.OrdinalIgnoreCase) ||
229+
text.StartsWith("max(", StringComparison.OrdinalIgnoreCase) ||
230+
text.StartsWith("minlength(", StringComparison.OrdinalIgnoreCase) ||
231+
text.StartsWith("maxlength(", StringComparison.OrdinalIgnoreCase) ||
232+
text.StartsWith("length(", StringComparison.OrdinalIgnoreCase) ||
233+
text.StartsWith("range(", StringComparison.OrdinalIgnoreCase) ||
234+
text.StartsWith("regex(", StringComparison.OrdinalIgnoreCase))
235+
{
236+
if (!text.EndsWith(")", StringComparison.Ordinal))
237+
{
238+
location = constraint.Span.GetLocation();
239+
correctSyntax = text + ")";
240+
return true;
241+
}
242+
}
243+
}
230244
}
231-
232-
if (!text.StartsWith("{", StringComparison.Ordinal) &&
233-
text.EndsWith("}", StringComparison.Ordinal))
245+
else
234246
{
235-
location = segment.Span.GetLocation();
236-
correctSyntax = "{" + text;
237-
return true;
247+
var text = segment.Span.Text;
248+
if (text.StartsWith("{", StringComparison.Ordinal) &&
249+
!text.EndsWith("}", StringComparison.Ordinal))
250+
{
251+
location = segment.Span.GetLocation();
252+
correctSyntax = text + "}";
253+
return true;
254+
}
255+
256+
if (!text.StartsWith("{", StringComparison.Ordinal) &&
257+
text.EndsWith("}", StringComparison.Ordinal))
258+
{
259+
location = segment.Span.GetLocation();
260+
correctSyntax = "{" + text;
261+
return true;
262+
}
238263
}
239264

265+
240266
location = null;
241267
correctSyntax = null;
242268
return false;

0 commit comments

Comments
 (0)