Permalink
Fetching contributors…
Cannot retrieve contributors at this time
159 lines (121 sloc) 6.35 KB
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Roslynator.CSharp.Analysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ReorderModifiersAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.ReorderModifiers); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeConstructorDeclaration, SyntaxKind.ConstructorDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeConversionOperatorDeclaration, SyntaxKind.ConversionOperatorDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeDelegateDeclaration, SyntaxKind.DelegateDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeEnumDeclaration, SyntaxKind.EnumDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeEventDeclaration, SyntaxKind.EventDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeEventFieldDeclaration, SyntaxKind.EventFieldDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeFieldDeclaration, SyntaxKind.FieldDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeIndexerDeclaration, SyntaxKind.IndexerDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeInterfaceDeclaration, SyntaxKind.InterfaceDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeOperatorDeclaration, SyntaxKind.OperatorDeclaration);
context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeStructDeclaration, SyntaxKind.StructDeclaration);
}
public static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
{
var classDeclaration = (ClassDeclarationSyntax)context.Node;
Analyze(context, classDeclaration.Modifiers);
}
public static void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context)
{
var constructorDeclaration = (ConstructorDeclarationSyntax)context.Node;
Analyze(context, constructorDeclaration.Modifiers);
}
public static void AnalyzeConversionOperatorDeclaration(SyntaxNodeAnalysisContext context)
{
var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)context.Node;
Analyze(context, conversionOperatorDeclaration.Modifiers);
}
public static void AnalyzeDelegateDeclaration(SyntaxNodeAnalysisContext context)
{
var delegateDeclaration = (DelegateDeclarationSyntax)context.Node;
Analyze(context, delegateDeclaration.Modifiers);
}
public static void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context)
{
var enumDeclaration = (EnumDeclarationSyntax)context.Node;
Analyze(context, enumDeclaration.Modifiers);
}
public static void AnalyzeEventDeclaration(SyntaxNodeAnalysisContext context)
{
var eventDeclaration = (EventDeclarationSyntax)context.Node;
Analyze(context, eventDeclaration.Modifiers);
}
public static void AnalyzeEventFieldDeclaration(SyntaxNodeAnalysisContext context)
{
var eventFieldDeclaration = (EventFieldDeclarationSyntax)context.Node;
Analyze(context, eventFieldDeclaration.Modifiers);
}
public static void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context)
{
var fieldDeclaration = (FieldDeclarationSyntax)context.Node;
Analyze(context, fieldDeclaration.Modifiers);
}
public static void AnalyzeIndexerDeclaration(SyntaxNodeAnalysisContext context)
{
var indexerDeclaration = (IndexerDeclarationSyntax)context.Node;
Analyze(context, indexerDeclaration.Modifiers);
}
public static void AnalyzeInterfaceDeclaration(SyntaxNodeAnalysisContext context)
{
var interfaceDeclaration = (InterfaceDeclarationSyntax)context.Node;
Analyze(context, interfaceDeclaration.Modifiers);
}
public static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
{
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
Analyze(context, methodDeclaration.Modifiers);
}
public static void AnalyzeOperatorDeclaration(SyntaxNodeAnalysisContext context)
{
var operatorDeclaration = (OperatorDeclarationSyntax)context.Node;
Analyze(context, operatorDeclaration.Modifiers);
}
public static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
{
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;
Analyze(context, propertyDeclaration.Modifiers);
}
public static void AnalyzeStructDeclaration(SyntaxNodeAnalysisContext context)
{
var structDeclaration = (StructDeclarationSyntax)context.Node;
Analyze(context, structDeclaration.Modifiers);
}
private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxTokenList modifiers)
{
if (modifiers.Count <= 1)
return;
if (modifiers.IsSorted(ModifierComparer.Default))
return;
if (modifiers.SpanContainsDirectives())
return;
context.ReportDiagnostic(
DiagnosticDescriptors.ReorderModifiers,
Location.Create(context.Node.SyntaxTree, modifiers.Span));
}
}
}