API Proposal: Add `Kind` extension property to SyntaxNode using C# extension anything feature
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
## Background and Motivation
With the introduction of the C# "extension anything" feature, it is now possible to provide extension properties. Currently, the `SyntaxNode` class in the `Microsoft.CodeAnalysis.CSharp` namespace has an extension method `Kind()`, but not an extension property. Providing a `Kind` extension property with the same behavior as the existing `Kind()` method would allow it to be used in pattern matching and other scenarios that require property access, improving code clarity and expressiveness.
Note: this will need the property/method disambiguation changes that are planned for C# 15.
## Proposed API
```diff
namespace Microsoft.CodeAnalysis.CSharp
{
public static class SyntaxNodeExtensions
{
public static SyntaxKind Kind(this SyntaxNode node) { ... } // existing
+ extension(SyntaxNode node)
+ {
+ public SyntaxKind Kind { get; }
+ }
}
}
```
## Usage Examples
```csharp
using Microsoft.CodeAnalysis.CSharp;
void Analyze(SyntaxNode node)
{
// Old style
if (node.Kind() == SyntaxKind.IdentifierName) { ... }
// New style with extension property and pattern matching
if (node.Kind is SyntaxKind.IdentifierName) { ... }
// Pattern matching example
switch (node)
{
case { Kind: SyntaxKind.ClassDeclaration }:
// ...
break;
}
}
```
## Alternative Designs
- Keep using the extension method, but this does not enable property pattern matching.
## Risks
- We cannot provide a VB implementation, as there are not extension properties for VB. While we can have internal helpers that provide the extension for use in properties, we cannot actually put the extension property into `Microsoft.CodeAnalysis.VisualBasic.dll`, so this would create an inconsistency in the basic APIs available on `SyntaxNode`. I'm unsure whether we think this is an acceptable cliff.
Contributor guide
Assessment
This issue has not been assessed yet.