2

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(...);
}
3
  • 2
    It sounds like you're registering the wrong kind of action. Commented May 3, 2016 at 13:56
  • I don't understand what is it that you're actually trying to do and why is SyntaxTreeAnalysisContext the only thing that you have. Could you expand on that? Commented May 8, 2016 at 12:35
  • @svick: See my edit. Commented May 10, 2016 at 13:30

1 Answer 1

2

Register a SemanticModelAnalysis instead in your Initialize method. Syntax ones are for syntax only without having semantic impacts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.