Documentation
¶
Overview ¶
Package dotnetast provides shared C# AST traversal utilities for .NET test framework parsers.
Index ¶
- Constants
- func ExtractStringContent(node *sitter.Node, source []byte) string
- func FindAttributeArgumentList(attr *sitter.Node) *sitter.Node
- func GetAttributeLists(node *sitter.Node) []*sitter.Node
- func GetAttributeName(attribute *sitter.Node, source []byte) string
- func GetAttributes(attributeLists []*sitter.Node) []*sitter.Node
- func GetClassName(node *sitter.Node, source []byte) string
- func GetDeclarationChildren(body *sitter.Node) []*sitter.Node
- func GetDeclarationList(node *sitter.Node) *sitter.Node
- func GetMethodName(node *sitter.Node, source []byte) string
- func HasAttribute(attributeLists []*sitter.Node, source []byte, attributeName string) bool
- func IsCSharpTestFileName(filename string) bool
- func IsPreprocessorDirective(nodeType string) bool
- func ParseAssignmentExpression(argNode *sitter.Node, source []byte) (string, string)
Constants ¶
const ( NodeClassDeclaration = "class_declaration" NodeMethodDeclaration = "method_declaration" NodeAttributeList = "attribute_list" NodeAttribute = "attribute" NodeAttributeArgumentList = "attribute_argument_list" NodeAttributeArgument = "attribute_argument" NodeIdentifier = "identifier" NodeDeclarationList = "declaration_list" NodeStringLiteral = "string_literal" NodeVerbatimStringLiteral = "verbatim_string_literal" NodeStringLiteralContent = "string_literal_content" NodeInterpolatedString = "interpolated_string_expression" NodeNamespaceDeclaration = "namespace_declaration" NodeFileScopedNamespace = "file_scoped_namespace_declaration" NodeUsingDirective = "using_directive" NodeQualifiedName = "qualified_name" NodeModifier = "modifier" NodeGenericName = "generic_name" NodeAssignmentExpression = "assignment_expression" NodePreprocIf = "preproc_if" NodePreprocElse = "preproc_else" NodePreprocElif = "preproc_elif" )
C# AST node types.
Variables ¶
This section is empty.
Functions ¶
func ExtractStringContent ¶
ExtractStringContent extracts string content from various string literal node types. Supports regular string literals ("..."), verbatim strings (@"..."), and interpolated strings ($"..."). This is exported for reuse by xUnit, NUnit, MSTest, and other .NET framework parsers.
func FindAttributeArgumentList ¶
FindAttributeArgumentList finds the attribute_argument_list child of an attribute node. Used by xUnit, NUnit, and other .NET framework parsers.
func GetAttributeLists ¶
GetAttributeLists returns all attribute_list nodes preceding a declaration.
KNOWN LIMITATION: tree-sitter-c-sharp parses preprocessor directives (#if, #else, #elif) between attributes as ERROR nodes instead of preproc_if. This means attributes inside conditional compilation blocks like:
[Theory]
[InlineData(1)]
#if NET6_0_OR_GREATER
[InlineData(2)] // ← Not detected (parsed as ERROR)
#endif
public void Test(int x) { }
will not be fully detected. This is a tree-sitter grammar limitation, not a parser bug. See: https://github.com/tree-sitter/tree-sitter-c-sharp/issues
func GetAttributeName ¶
GetAttributeName extracts the attribute name (e.g., "Fact" from [Fact] or "Theory" from [Theory]). Handles both simple identifiers and qualified names.
func GetAttributes ¶
GetAttributes extracts all attribute nodes from attribute_list nodes.
func GetClassName ¶
GetClassName extracts the class name from a class_declaration node.
func GetDeclarationChildren ¶ added in v1.5.0
GetDeclarationChildren returns all declarations from a declaration_list, including those inside preprocessor directives (#if, #else, #elif). This handles C# conditional compilation where tests may be wrapped in #if NET6_0_OR_GREATER or similar directives.
func GetDeclarationList ¶
GetDeclarationList returns the body (declaration_list) from a class_declaration.
func GetMethodName ¶
GetMethodName extracts the method name from a method_declaration node.
func HasAttribute ¶
HasAttribute checks if the attribute lists contain a specific attribute.
func IsCSharpTestFileName ¶
IsCSharpTestFileName checks if a filename follows C# test file naming conventions. Matches: *Test.cs, *Tests.cs, Test*.cs, *Spec.cs, *Specs.cs
func IsPreprocessorDirective ¶ added in v1.5.0
IsPreprocessorDirective checks if the node is a C# preprocessor directive.
func ParseAssignmentExpression ¶
ParseAssignmentExpression extracts name and value from an assignment_expression node. For "Skip = \"reason\"", returns ("Skip", "reason"). Used by xUnit (Skip, DisplayName), NUnit (Description), and other .NET framework parsers.
Types ¶
This section is empty.