Jam*_*aix 5 c# visual-studio roslyn
背景故事(对于理解我的问题不是必需的,但某些上下文可能会有所帮助)
在我的公司,我们使用一种IResult<T>类型来处理函数式的错误,而不是抛出异常并希望某些客户端捕获它们。AIResult<T>可以是 a DataResult<T>with aT或 an ErrorResult<T>with anError但不能同时是两者。Error大致相当于一个Exception。因此,典型的函数将返回IResult<T>以通过返回值传递任何遇到的错误,而不是使用throw.
我们有扩展方法IResult<T>来组成函数链。两个主要的是Bind和Let。
Bind是bind函数式语言中的标准一元运算符。基本上,如果IResult有一个值,它会投影该值,否则它会转发错误。它是这样实现的
static IResult<T2> Bind(
this IResult<T1> @this,
Func<T1, IResult<T2>> projection)
{
return @this.HasValue
? projection(@this.Value)
: new ErrorResult<T2>(@this.Error);
}
Run Code Online (Sandbox Code Playgroud)
Let用于执行副作用,只要在函数链的早期没有遇到错误。它被实现为
static IResult<T> Let(
this IResult<T> @this,
Action<T> action)
{
if (@this.HasValue) {
action(@this.Value);
}
return @this;
}
Run Code Online (Sandbox Code Playgroud)
我的 Roslyn 分析器用例
正在使用此当常犯的错误IResult<T>API,就是调用返回的函数IResult<T>内部Action<T>传入Let。发生这种情况时,如果内部函数返回Error,则错误将丢失并且继续执行,就像没有出错一样。当它发生时,这可能是一个非常难以追踪的错误,并且在过去一年中发生了多次。在这些情况下,Bind应改为使用,以便可以传播错误。
我想识别对IResult<T>作为参数传递的 lambda 表达式内部返回的函数的任何调用,Let并将它们标记为编译器警告。
我创建了一个分析器来做到这一点。您可以在此处查看完整的源代码:https : //github.com/JamesFaix/NContext.Analyzers这是解决方案中的主要分析器文件:
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace NContext.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class BindInsteadOfLetAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "NContext_0001";
private const string _Category = "Safety";
private const string _Title = "Unsafe use of IServiceResponse<T> inside Let expression";
private const string _MessageFormat = "Unsafe use of IServiceResponse<T> inside Let expression.";
private const string _Description = "If calling any methods that return IServiceResponse<T>, use Bind instead of Let. " +
"Otherwise, any returned ErrorResponses will be lost, execution will continue as if no error occurred, and no error will be logged.";
private static DiagnosticDescriptor _Rule =
new DiagnosticDescriptor(
DiagnosticId,
_Title,
_MessageFormat,
_Category,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: _Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(_Rule);
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.InvocationExpression);
}
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var functionChain = (InvocationExpressionSyntax) context.Node;
//When invoking an extension method, the first child node should be a MemberAccessExpression
var memberAccess = functionChain.ChildNodes().First() as MemberAccessExpressionSyntax;
if (memberAccess == null)
{
return;
}
//When invoking an extension method, the last child node of the member access should be an IdentifierName
var letIdentifier = memberAccess.ChildNodes().Last() as IdentifierNameSyntax;
if (letIdentifier == null)
{
return;
}
//Ignore method invocations that do not have "Let" in the name
if (!letIdentifier.GetText().ToString().Contains("Let"))
{
return;
}
var semanticModel = context.SemanticModel;
var unsafeNestedInvocations = functionChain.ArgumentList
//Get nested method calls
.DescendantNodes().OfType<InvocationExpressionSyntax>()
//Get any identifier names in those calls
.SelectMany(node => node.DescendantNodes().OfType<IdentifierNameSyntax>())
//Get tuples of syntax nodes and the methods they refer to
.Select(node => new
{
Node = node,
Symbol = semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol
})
//Ignore identifiers that do not refer to methods
.Where(x => x.Symbol != null
//Ignore methods that do not have "IServiceResponse" in the return type
&& x.Symbol.ReturnType.ToDisplayString().Contains("IServiceResponse"));
//Just report the first one to reduce error log clutter
var firstUnsafe = unsafeNestedInvocations.FirstOrDefault();
if (firstUnsafe != null)
{
var diagnostic = Diagnostic.Create(_Rule, firstUnsafe.Node.GetLocation(), firstUnsafe.Node.GetText().ToString());
context.ReportDiagnostic(diagnostic);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题
我的分析器适用于*.cs当前打开的任何文件。警告被添加到错误窗口,绿色警告下划线显示在文本编辑器中。但是,如果我关闭包含带有这些警告的调用站点的文件,则会从“错误”窗口中删除这些错误。此外,如果我只是在没有打开文件的情况下编译我的解决方案,则不会记录任何警告。在调试模式下运行分析器解决方案时,如果 Visual Studio 的调试沙箱实例中没有打开源代码文件,则不会命中断点。
如何让我的分析器检查所有文件,甚至是关闭的文件?
我从这个问题中找到了答案:如何使我的代码诊断语法节点操作对关闭的文件起作用?
显然,如果您的分析器是作为 Visual Studio 扩展安装的,而不是作为项目级包安装的,那么它默认只分析打开的文件。您可以转到“工具”>“选项”>“文本编辑器”>“C#”>“高级”并选中“启用完整解决方案分析”以使其适用于当前解决方案中的任何文件。
多么奇怪的默认行为。¯\_(?)_/¯
| 归档时间: |
|
| 查看次数: |
860 次 |
| 最近记录: |