I am given a SyntaxTreeAnalysisContext context but what I need is a SyntaxNodeAnalysisContext with its SemanticModel property in order to then ascend to the parent node which is a class declaration and then check what attributes have been placed on the type.
SyntaxTreeAnalysisContext givenTreeContext
/// how to get to:
SyntaxNodeAnalysisContext context;
var n = context.Node;
ISymbol symbol = null;
bool hasAttributes = false;
while (n != null)
{
if (n.IsKind(SyntaxKind.ClassDeclaration))
{
symbol = context.SemanticModel.GetDeclaredSymbol(n);
hasAttributes = symbol.GetAttributes().Any();
break;
}
else
{
n = n.Parent;
}
}
Is there a way to get from SyntaxTreeAnalysisContext to SyntaxNodeAnalysisContext?
(Edit) To give you an example: I want to enhance some rules (which all use the ReportDiagnostics method of SyntaxTreeAnalysisContext; like SA1027 Use tabs correctly from StyleCop Analyzers) in a way that I can place an attribute on the enclosing type and detect its presence and then omit reporting the diagnostic like
if (!hasAttributes) // simplified
{
context.ReportDiagnostic(...);
}
SyntaxTreeAnalysisContextthe only thing that you have. Could you expand on that?