Skip to content

Instantly share code, notes, and snippets.

View bogdanbujdea's full-sized avatar
💭
Who reads this?

Bogdan Bujdea bogdanbujdea

💭
Who reads this?
View GitHub Profile
@bogdanbujdea
bogdanbujdea / MembersSortAnalyzer.cs
Created April 4, 2018 08:29
Override Initialize method in the analyzer
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
@bogdanbujdea
bogdanbujdea / MembersSortAnalyzer.cs
Created April 4, 2018 08:32
Check if symbol is a class
var classSymbol = context.Symbol as INamedTypeSymbol;
if (classSymbol?.TypeKind != TypeKind.Class)
return; //we only need classes, not other types like enum, struct, etc.
@bogdanbujdea
bogdanbujdea / ConsoleApp1.cs
Created April 4, 2018 08:38
Basic console app used as example for code analyzer
using System;
namespace ConsoleApp1
{
internal class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
@bogdanbujdea
bogdanbujdea / MembersSortAnalyzer.cs
Created April 4, 2018 08:40
Ignore getters and setters
var members = new List<ISymbol>();
foreach (var member in classSymbol.GetMembers())
{
if (member.Kind == SymbolKind.Method)
{
var methodMember = member as IMethodSymbol;
if (methodMember.MethodKind == MethodKind.PropertyGet || methodMember.MethodKind == MethodKind.PropertySet)
continue;
}
members.Add(member);
@bogdanbujdea
bogdanbujdea / MembersSortAnalyzer.cs
Created April 4, 2018 08:44
Get first member that is not ordered correctly
private static ISymbol GetUnorderedMember(List<ISymbol> members)
{
//I'm just sorting the members by DeclaredAccessibility, then comparing them with the order of
//the initial members
var orderedByAccessibility = members.OrderByDescending(m => m.DeclaredAccessibility).ToList();
for (var i = 0; i < members.Count; i++)
{
if (members[i].DeclaredAccessibility != orderedByAccessibility[i].DeclaredAccessibility)
{
if (orderedByAccessibility[i].DeclaredAccessibility < members[i].DeclaredAccessibility)
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
//...
var members = GetMembersFromClass(classSymbol); //retrieve only methods and properties
var unorderedMember = GetUnorderedMember(members); //then find the first member that is out of order
if (unorderedMember != null)
{
var rule = new DiagnosticDescriptor(DiagnosticId, Title, $"Move {unorderedMember.Name} lower in the file", Category,
DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
//now we're creating a rule that tells the user to move the member lower in the file
@bogdanbujdea
bogdanbujdea / Program.cs
Created April 23, 2018 21:37
Unordered usings
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp;
using System.Linq;
@bogdanbujdea
bogdanbujdea / Program.cs
Created April 23, 2018 21:38
Ordered usings
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
@bogdanbujdea
bogdanbujdea / UsingsAnalyzer.cs
Last active April 27, 2018 08:32
Usings analyzer
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;
using System.Collections.Immutable;
namespace MyCodeAnalyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Linq;