Skip to content

Add support for C# Attributes #82

@dymurray

Description

@dymurray

Summary

C# attributes (e.g., [Authorize], [ValidateAntiForgeryToken], [HttpPost]) are not currently captured in the stack graph, making it impossible to query for their usage with patterns like .ValidateAntiForgeryToken..

Problem

When analyzing C# code for attribute usage, queries return empty results because the TSG rules in stack-graphs.tsg do not handle attribute or attribute_list node types from tree-sitter-c-sharp.

Example: Searching for [ValidateAntiForgeryToken] attribute usage in the nerd-dinner test project:

grpcurl -plaintext -d '{
"cap": "referenced",
"condition_info": "{"referenced": {"pattern": ".ValidateAntiForgeryToken."}}"
}' localhost:9000 provider.ProviderService/Evaluate

Expected: Find all usages of the ValidateAntiForgeryToken attribute (7 occurrences in AccountController.cs)

Actual: Empty response {}

Technical Details

Tree-sitter-c-sharp correctly parses attributes. For example:

[ValidateAntiForgeryToken]
public ActionResult LogOff() { }

Produces AST structure:
method_declaration
attribute_list
attribute
identifier "ValidateAntiForgeryToken"
...

However, stack-graphs.tsg has no rules to create stack graph nodes from attribute or attribute_list nodes.

Proposed Solution

Add TSG rules to handle attributes. A starting point:

;; Attribute handling
(attribute
(identifier) @attr_name
) @attr {
node @attr.ref
attr (@attr.ref) node_reference = @attr_name
; Additional edges for FQDN resolution...
}

Considerations:

  1. Attributes can have arguments: [Route("api/users")]
  2. Attributes can be fully qualified: [System.ComponentModel.Description("...")]
  3. Multiple attributes can be in one list: [Authorize, HttpPost]
  4. Attributes apply to various targets (classes, methods, parameters, properties)

Use Cases

This feature would enable migration/modernization queries such as:

  • Find all usages of deprecated attributes (e.g., [Obsolete])
  • Find security-related attributes ([Authorize], [AllowAnonymous])
  • Find MVC/Web API routing attributes ([HttpGet], [Route])
  • Find data annotation attributes ([Required], [StringLength])

Investigation Notes

During debugging, we confirmed:

  • Tree-sitter-c-sharp parses attributes correctly with identifier children
  • TSG rules compile without errors but don't produce nodes
  • The issue may require investigating how tree-sitter-stack-graphs matches patterns against tree-sitter-c-sharp's AST structure

##Related Files

  • src/c_sharp_graph/stack-graphs.tsg - TSG rule definitions
  • src/c_sharp_graph/loader.rs - Stack graph building logic
  • Test file with attributes: testdata/nerd-dinner/mvc4/NerdDinner/Controllers/AccountController.cs

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureCategorizes issue or PR as related to a new feature.priority/backlogHigher priority than priority/awaiting-more-evidence.triage/acceptedIndicates an issue or PR is ready to be actively worked on.

    Type

    No type

    Projects

    Status

    🔖 Ready

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions