Home

Awesome

Selectorlyzer Analyzers for the .NET Compiler Platform

NuGet Build Status

Selectorlyzer.Analyzers is a highly customizable Roslyn Analyzer designed to empower developers with the ability to create project-specific analyzers using a CSS selector-like syntax.

<img src="https://github.com/rlgnak/Selectorlyzer.Analyzers/assets/1643317/a56e8fef-1e42-47b4-acbf-7be884f91d6f" width="453" height="250">

Getting Started

The preferable way to use the analyzers is to add the NuGet package Selectorlyzer.Analyzers to the project where you want to enforce rules.

A selectorlyzer.json or .selectorlyzer.json file is used to specify rules.

Installation

  1. Install the NuGet Package Selectorlyzer.Analyzers.
dotnet add package Selectorlyzer.Analyzers
  1. Create and configure selectorlyzer.json file.
{
  "rules": [
    {
      "selector": ":class:has([Name='InvalidClassName'])",
      "message": "Classes should not be named 'InvalidClassName'",
      "severity": "error"
    },
    {
      "selector": "InvocationExpression[Expression='Console.WriteLine']",
      "message": "Do not use Console.WriteLine",
      "severity": "error"
    },
    {
      "selector": ":class:implements([Name='BaseRepository'])",
      "rule": ":implements([Name='I{Name}'])",
      "message": "Classes that implement 'BaseRepository' should implement an interface with the same name.",
      "severity": "error"
    }
  ]
}
  1. Add the following to your .csproj files
<ItemGroup>
  <AdditionalFiles Include="selectorlyzer.json" />
</ItemGroup>

Example Rules

Naming Conventions

Custom Project Conventions

Selectors

Selectorlyzer uses a query langage for Roslyn Inspired by Qulaly and esquery. These selectors are used to identify speicifc sytax nodes.

Supported Selectors

Selectorlizer supports a subset of CSS selector level 4. The selector engine also supports Selectorlizer-specific extensions to the selector.

License

MIT License

Selectorlyzer.Analyzers Copyright © 2023-present Richard Graves <rlgnak+selectorlyzer@gmail.com>