Qulaly
Qulaly copied to clipboard
A library that queries Roslyn's C# syntax tree with CSS selector-like syntax.
Qulaly
Query language for Roslyn. Qulaly is a library that queries Roslyn's C# syntax tree with CSS selector-like syntax. Inspired by esquery in ECMAScript ecosystem.
⚡Live Demo: https://mayuki.github.io/Qulaly/
Example
The following code shows how to query the async
method.
using Qulaly;
var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Class1
{
public static async ValueTask<T> FooAsync<T>(int a, string b, T c) => throw new NotImplementedException();
public async Task BarAsync<T>() => throw new NotImplementedException();
public object MethodA(int arg1) => throw new NotImplementedException();
public object MethodB(int arg1, string arg2) => throw new NotImplementedException();
}
");
// Enumerate SyntaxNodes by calling `QuerySelectorAll` extension method for SyntaxNode/SyntaxTree.
foreach (var methodNode in syntaxTree.QuerySelectorAll(":method[Modifiers ~= 'async']"))
{
Console.WriteLine(((MethodDeclarationSyntax)methodNode).Identifier.ToFullString());
}
Output
FooAsync
BarAsync
Methods
-
QuerySelectorAll
-
QuerySelector
Install
Install NuGet package from NuGet.org
$ dotnet add package Qulaly
PS> Install-Package Qulaly
Supported Selectors
Qulaly supports a subset of CSS selector level 4. The selector engine also supports Qulaly-specific extensions to the selector.
- SyntaxNode Type:
MethodDeclaration
,ClassDeclaration
...- See also SyntaxKind enum
- SyntaxNode Univarsal:
*
- SyntaxNode pseudo-classes (for short-hand)
-
:method
-
:class
-
:interface
-
:lambda
-
- Combinators
-
Descendant:
node descendant
-
Child:
node > child
-
Next-sibling:
node + next
-
Subsequent-sibling:
node ~ sibling
-
Descendant:
- Pseudo-class
-
Negation:
:not(...)
-
Matches-any:
:is(...)
-
Relational:
:has(...)
-
:first-child
-
:last-child
-
Negation:
- Attributes (Properties)
-
[PropName]
(existance) -
[PropName = 'Exact']
-
[PropName ^= 'StartsWith']
-
[PropName $= 'EndsWith']
-
[PropName *= 'Contains']
-
[PropName ~= 'Item']
(ex.[Modifiers ~= 'async']
)
-
- Qulaly Extensions
-
[Name = 'MethodName']
: Name special property-
Name
is a special property for convenience that can be used inMethodDeclaration
,ClassDeclaration
... etc
-
-
[TypeParameters.Count > 0]
: Conditions-
Parameters.Count
-
TypeParameters.Count
-
-
License
MIT License
Copyright © 2020-present Mayuki Sawatari <[email protected]>