如何将 LINQ 表达式组中标识符的语法节点捕获到 <identifier>
How to catch syntax node for identifier in LINQ expression group ... into <identifier>
我想用我的自定义代码分析器处理代码中的所有标识符。
我捕获了所有这些,除了标识符 y
在这样的表达式中:
var z = from x in new int[] { }
group x by x into y
select new { };
有最小分析器源代码示例:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class IdentifierAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor("Id", "", "", "Naming", DiagnosticSeverity.Warning, true, null);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKinds);
}
private static readonly SyntaxKind[] SyntaxKinds = new[] {
SyntaxKind.JoinIntoClause,
//TODO: LINQ group...into <identifier>
};
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
switch (context.Node)
{
case JoinIntoClauseSyntax joinIntoClause:
if (IsNotValid(joinIntoClause.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, joinIntoClause.Identifier.GetLocation(), joinIntoClause.Identifier.Text));
break;
//TODO: LINQ group...into <identifier>
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNotValid(string text)
{
return true;
}
}
switch (context.Node)
{
case JoinIntoClauseSyntax joinIntoClause:
if (IsNotValid(joinIntoClause.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, joinIntoClause.Identifier.GetLocation(), joinIntoClause.Identifier.Text));
break;
case QueryContinuationSyntax continuationSyntax:
if (IsNotValid(continuationSyntax.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, continuationSyntax.Identifier.GetLocation(), continuationSyntax.Identifier.Text));
break;
}
我想用我的自定义代码分析器处理代码中的所有标识符。
我捕获了所有这些,除了标识符 y
在这样的表达式中:
var z = from x in new int[] { }
group x by x into y
select new { };
有最小分析器源代码示例:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class IdentifierAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor("Id", "", "", "Naming", DiagnosticSeverity.Warning, true, null);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKinds);
}
private static readonly SyntaxKind[] SyntaxKinds = new[] {
SyntaxKind.JoinIntoClause,
//TODO: LINQ group...into <identifier>
};
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
switch (context.Node)
{
case JoinIntoClauseSyntax joinIntoClause:
if (IsNotValid(joinIntoClause.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, joinIntoClause.Identifier.GetLocation(), joinIntoClause.Identifier.Text));
break;
//TODO: LINQ group...into <identifier>
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNotValid(string text)
{
return true;
}
}
switch (context.Node)
{
case JoinIntoClauseSyntax joinIntoClause:
if (IsNotValid(joinIntoClause.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, joinIntoClause.Identifier.GetLocation(), joinIntoClause.Identifier.Text));
break;
case QueryContinuationSyntax continuationSyntax:
if (IsNotValid(continuationSyntax.Identifier.Text))
context.ReportDiagnostic(Diagnostic.Create(Rule, continuationSyntax.Identifier.GetLocation(), continuationSyntax.Identifier.Text));
break;
}