Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/AutoWasmApi.Roslyn/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

17 changes: 17 additions & 0 deletions src/AutoWasmApi.Roslyn/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
WAG00001 | AutoWasmApiGenerator.ControllerGenerator | Error | 继承多个接口需要指定接口标注[WebControllerAttribute]
WAG00002 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 无法为该类型生成WebApi调用类,缺少接口
WAG00003 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 方法参数过多
WAG00004 | AutoWasmApiGenerator.ControllerGenerator | Error | 控制器(controller)不能包含泛型
WAG00005 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 仅支持异步方法
WAG00006 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 路由中未包含路由参数({0})
WAG00007 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 不能同时设置[FromBody]和[FromForm]({0})
WAG00008 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 不能设置多个[FromBody]({0})
WAG00009 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 暂不支持的返回值类型({0})
WAG00010 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | 生成服务调用器过程中发生错误: {0}
WAG00011 | AutoWasmApiGenerator.HttpServiceInvokerGenerator | Error | AutoWasmApiGenerator.ApiInvokerGenerateAttribute需要同时设置AutoWasmApiGenerator.WebControllerAttribute
6 changes: 0 additions & 6 deletions src/AutoWasmApi.Roslyn/ControllerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
// WebControllerAttributeFullName
// , static (node, _) => true
// , static (ctx, _) => ctx);
#if DEBUG && false
if (!Debugger.IsAttached)
{
Debugger.Launch(); // This will launch the debugger when the source generator runs.
}
#endif

context.RegisterSourceOutput(context.CompilationProvider, static (context, compilation) =>
{
Expand Down
220 changes: 156 additions & 64 deletions src/AutoWasmApi.Roslyn/DiagnosticDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,218 @@
using System;
using System.Collections.Generic;
using System.Text;
// ReSharper disable InconsistentNaming

namespace AutoWasmApiGenerator
{
internal class DiagnosticDefinitions
{
private static readonly DiagnosticDescriptor Wag00001DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00001",
title: "继承多个接口需要指定接口标注[WebControllerAttribute]",
messageFormat: "继承多个接口需要指定接口标注[WebControllerAttribute]",
category: typeof(ControllerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00002DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00002",
title: "无法为该类型生成WebApi调用类,缺少接口",
messageFormat: "无法为该类型生成WebApi调用类,缺少接口",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00003DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00003",
title: "方法参数过多",
messageFormat: "方法参数过多",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00004DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00004",
title: "控制器(controller)不能包含泛型",
messageFormat: "控制器(controller)不能包含泛型",
category: typeof(ControllerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00005DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00005",
title: "仅支持异步方法",
messageFormat: "仅支持异步方法",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00006DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00006",
title: "路由中未包含路由参数",
messageFormat: "路由中未包含路由参数({0})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00007DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00007",
title: "不能同时设置[FromBody]和[FromForm]",
messageFormat: "不能同时设置[FromBody]和[FromForm]({0})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00008DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00008",
title: "不能设置多个[FromBody]",
messageFormat: "不能设置多个[FromBody]({0})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);


private static readonly DiagnosticDescriptor Wag00009DiagDescriptor = new DiagnosticDescriptor(
id: "WAG00009",
title: "暂不支持的返回值类型",
messageFormat: "暂不支持的返回值类型({0})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor Wag00010DiagDescriptor = new(
id: "WAG00010",
title: "生成错误",
messageFormat: "生成过程中发生错误: {0}",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

private static readonly DiagnosticDescriptor Wag00011DiagDescriptor = new(
id: "WAG00011",
title: "属性依赖错误",
messageFormat: "AutoWasmApiGenerator.ApiInvokerGenerateAttribute需要同时设置AutoWasmApiGenerator.WebControllerAttribute",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

/// <summary>
/// 继承多个接口需要指定接口标注[WebControllerAttribute]
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
public static Diagnostic WAG00001(Location? location) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00001",
title: "继承多个接口需要指定接口标注[WebControllerAttribute]",
messageFormat: "继承多个接口需要指定接口标注[WebControllerAttribute]",
category: typeof(ControllerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00001(Location? location)
{
return Diagnostic.Create(Wag00001DiagDescriptor, location);
}

/// <summary>
/// 无法为该类型生成WebApi调用类,缺少接口
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
public static Diagnostic WAG00002(Location? location) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00002",
title: "无法为该类型生成WebApi调用类,缺少接口",
messageFormat: "无法为该类型生成WebApi调用类,缺少接口",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00002(Location? location)
{
return Diagnostic.Create(Wag00002DiagDescriptor, location);
}

/// <summary>
/// 方法参数过多
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
public static Diagnostic WAG00003(Location? location) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00003",
title: "方法参数过多",
messageFormat: "方法参数过多",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00003(Location? location)
{
return Diagnostic.Create(Wag00003DiagDescriptor, location);
}

/// <summary>
/// 控制器(controller)不能包含泛型
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00004(Location? location) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00004",
title: "控制器(controller)不能包含泛型",
messageFormat: "控制器(controller)不能包含泛型",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00004(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00004DiagDescriptor, location, symbolString);
}

/// <summary>
/// 仅支持异步方法
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00005(Location? location) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00005",
title: "仅支持异步方法",
messageFormat: "仅支持异步方法",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00005(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00005DiagDescriptor, location, symbolString);
}

/// <summary>
/// 路由参数
/// 路由中未包含路由参数
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00006(Location? location, string? symbolString = null) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00006",
title: "路由中未包含路由参数",
messageFormat: $"路由中未包含路由参数({symbolString})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00006(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00006DiagDescriptor, location, symbolString);
}

/// <summary>
/// 不能同时设置[FromBody]和[FromForm]
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00007(Location? location, string? symbolString = null) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00007",
title: "不能同时设置[FromBody]和[FromForm]",
messageFormat: $"不能同时设置[FromBody]和[FromForm]({symbolString})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00007(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00007DiagDescriptor, location, symbolString);
}

/// <summary>
/// 不能设置多个[FromBody]
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00008(Location? location, string? symbolString = null) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00008",
title: "不能设置多个[FromBody]",
messageFormat: $"不能设置多个[FromBody]({symbolString})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00008(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00008DiagDescriptor, location, symbolString);
}

/// <summary>
/// 暂不支持的返回值类型
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00009(Location? location, string? symbolString = null)
{
return Diagnostic.Create(Wag00009DiagDescriptor, location, symbolString);
}

/// <summary>
/// 生成错误
/// </summary>
/// <param name="location"></param>
/// <param name="symbolString"></param>
/// <returns></returns>
public static Diagnostic WAG00010(Location? location, string symbolString)
{
return Diagnostic.Create(Wag00010DiagDescriptor, location, symbolString);
}

/// <summary>
/// 属性依赖错误
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
public static Diagnostic WAG00009(Location? location, string? symbolString = null) => Diagnostic.Create(new DiagnosticDescriptor(
id: "WAG00009",
title: "暂不支持的返回值类型",
messageFormat: $"暂不支持的返回值类型({symbolString})",
category: typeof(HttpServiceInvokerGenerator).FullName!,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true), location);
public static Diagnostic WAG00011(Location? location)
{
return Diagnostic.Create(Wag00011DiagDescriptor, location);
}
}
}
17 changes: 17 additions & 0 deletions src/AutoWasmApi.Roslyn/Extensions/MethodCheckerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.CodeAnalysis;

namespace AutoWasmApiGenerator.Extensions;

internal static class MethodCheckerExtensions
{
public static bool HasTryParseMethod(this ITypeSymbol returnType)
{
foreach (var method in returnType.GetMembers("TryParse").OfType<IMethodSymbol>())
{
var match = method is { Parameters.Length: 2, ReturnType.Name: "Boolean" };
if (match) return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/AutoWasmApi.Roslyn/GeneratorHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class GeneratorHelpers
{
public const string WebControllerAttributeFullName = "AutoWasmApiGenerator.WebControllerAttribute";
public const string WebControllerAssemblyAttributeFullName = "AutoWasmApiGenerator.WebControllerAssemblyAttribute";
public const string ApiInvokerAttributeFullName = "AutoWasmApiGenerator.ApiInvokerGenerateAttribute";
public const string ApiInvokerGenerateAttributeFullName = "AutoWasmApiGenerator.ApiInvokerGenerateAttribute";
public const string ApiInvokerAssemblyAttributeFullName = "AutoWasmApiGenerator.ApiInvokerAssemblyAttribute";
public const string WebMethodAttributeFullName = "AutoWasmApiGenerator.WebMethodAttribute";
public const string DisableWebApiGenerator = "DisableWebApiGenerator";
Expand Down
Loading