Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joymon/66d36b58afd98f3df1cc to your computer and use it in GitHub Desktop.
Save joymon/66d36b58afd98f3df1cc to your computer and use it in GitHub Desktop.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;
namespace JoymonsCode
{
public class TypeNameShouldStartWithCapitalLetterDiagnosticAnalyzer : DiagnosticAnalyzer
{
internal const string DiagnosticId = "TypeNameShouldStartWithCapitalLetter";
internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
id: DiagnosticId,
title: "Type name starts with lowercase letters",
messageFormat: "Type name '{0}' starts with lowercase letters",
category: "Naming",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
private void AnalyzeSymbol(SymbolAnalysisContext context)
{
if (char.IsLower(context.Symbol.Name.First()))
{
var diagnostic = Diagnostic.Create(Rule, context.Symbol.Locations[0], context.Symbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment