Skip to content

Commit ae8f898

Browse files
committed
非方法成员的声明的诊断检测
1 parent 7942923 commit ae8f898

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

WebApiClient.Analyzers/AttributeCtorAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
2121
return ImmutableArray.Create(
2222
DiagnosticDescriptors.AttributeDescriptor,
2323
DiagnosticDescriptors.ReturnTypeDescriptor,
24-
DiagnosticDescriptors.RefParameterDescriptor);
24+
DiagnosticDescriptors.RefParameterDescriptor,
25+
DiagnosticDescriptors.NotMethodDefindDescriptor);
2526
}
2627
}
2728

WebApiClient.Analyzers/DiagnosticDescriptors.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ static class DiagnosticDescriptors
2323
/// 引用参数诊断描述器
2424
/// </summary>
2525
public static DiagnosticDescriptor RefParameterDescriptor { get; }
26-
= Rule("PR1001", "不支持ref/out", "参数不支持ref/out等修饰");
26+
= Rule("RP1001", "不支持ref/out", "参数不支持ref/out等修饰");
2727

28+
/// <summary>
29+
/// 非方法声明诊断描述器
30+
/// </summary>
31+
public static DiagnosticDescriptor NotMethodDefindDescriptor { get; }
32+
= Rule("NM1001", "不支持的非方法声明", "不支持的非方法声明,只允许方法的声明");
2833

2934
/// <summary>
3035
/// 创建诊断描述器

WebApiClient.Analyzers/WebApiClientHtttApi.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WebApiClientHtttApi
2020
/// <summary>
2121
/// 接口声明
2222
/// </summary>
23-
private readonly InterfaceDeclarationSyntax interfaceDeclaration;
23+
private readonly InterfaceDeclarationSyntax httpApiInterfaceSyntax;
2424

2525
/// <summary>
2626
/// webApiClient上下文
@@ -39,7 +39,7 @@ class WebApiClientHtttApi
3939
public WebApiClientHtttApi(SyntaxNodeAnalysisContext syntaxNodeContext)
4040
{
4141
this.syntaxNodeContext = syntaxNodeContext;
42-
this.interfaceDeclaration = syntaxNodeContext.Node as InterfaceDeclarationSyntax;
42+
this.httpApiInterfaceSyntax = syntaxNodeContext.Node as InterfaceDeclarationSyntax;
4343
this.webApiClientContext = new WebApiClientContext(syntaxNodeContext.Compilation);
4444
this.IsHtttApiInterface = this.GetIsHtttApiInterface();
4545
}
@@ -50,12 +50,12 @@ public WebApiClientHtttApi(SyntaxNodeAnalysisContext syntaxNodeContext)
5050
/// <returns></returns>
5151
private bool GetIsHtttApiInterface()
5252
{
53-
if (this.interfaceDeclaration == null || this.interfaceDeclaration.BaseList == null)
53+
if (this.httpApiInterfaceSyntax == null || this.httpApiInterfaceSyntax.BaseList == null)
5454
{
5555
return false;
5656
}
5757

58-
foreach (var baseType in this.interfaceDeclaration.BaseList.Types)
58+
foreach (var baseType in this.httpApiInterfaceSyntax.BaseList.Types)
5959
{
6060
var type = this.syntaxNodeContext.SemanticModel.GetTypeInfo(baseType.Type).Type;
6161
if (type.Equals(this.webApiClientContext.IHttpApiType) == true)
@@ -75,6 +75,7 @@ public void ReportDiagnostic()
7575
this.ReportDiagnosticOfAttributes();
7676
this.ReportDiagnosticOfReturnTypes();
7777
this.ReportDiagnosticOfRefParameters();
78+
this.ReportDiagnosticOfNotMethodDefinds();
7879
}
7980

8081
/// <summary>
@@ -87,7 +88,7 @@ public void ReportDiagnosticOfAttributes()
8788
return;
8889
}
8990

90-
var interfaceSymbol = this.syntaxNodeContext.SemanticModel.GetDeclaredSymbol(this.interfaceDeclaration);
91+
var interfaceSymbol = this.syntaxNodeContext.SemanticModel.GetDeclaredSymbol(this.httpApiInterfaceSyntax);
9192
if (interfaceSymbol == null)
9293
{
9394
return;
@@ -155,13 +156,34 @@ public void ReportDiagnosticOfRefParameters()
155156
}
156157
}
157158

159+
/// <summary>
160+
/// 报告非方法声明诊断
161+
/// </summary>
162+
public void ReportDiagnosticOfNotMethodDefinds()
163+
{
164+
if (this.IsHtttApiInterface == false)
165+
{
166+
return;
167+
}
168+
169+
foreach (var member in this.httpApiInterfaceSyntax.Members)
170+
{
171+
if (member.Kind() != SyntaxKind.MethodDeclaration)
172+
{
173+
var location = member.GetLocation();
174+
var diagnostic = DiagnosticDescriptors.NotMethodDefindDescriptor.ToDiagnostic(location);
175+
this.syntaxNodeContext.ReportDiagnostic(diagnostic);
176+
}
177+
}
178+
}
179+
158180
/// <summary>
159181
/// 获取HttpApi方法
160182
/// </summary>
161183
/// <returns></returns>
162184
private IEnumerable<IMethodSymbol> GetHttpApiMethodSymbols()
163185
{
164-
foreach (var member in this.interfaceDeclaration.Members)
186+
foreach (var member in this.httpApiInterfaceSyntax.Members)
165187
{
166188
if (member.Kind() != SyntaxKind.MethodDeclaration)
167189
{

0 commit comments

Comments
 (0)