Permalink
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (44 sloc) 1.95 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 System.Diagnostics;
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 RemoveRedundantCommaInInitializerAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.RemoveRedundantCommaInInitializer); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(
AnalyzeInitializerExpression,
SyntaxKind.ArrayInitializerExpression,
SyntaxKind.ObjectInitializerExpression,
SyntaxKind.CollectionInitializerExpression);
}
public static void AnalyzeInitializerExpression(SyntaxNodeAnalysisContext context)
{
var initializer = (InitializerExpressionSyntax)context.Node;
SeparatedSyntaxList<ExpressionSyntax> expressions = initializer.Expressions;
if (!expressions.Any())
return;
int count = expressions.Count;
if (count != expressions.SeparatorCount)
return;
SyntaxToken token = expressions.GetSeparator(count - 1);
Debug.Assert(!token.IsMissing);
context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantCommaInInitializer, token);
}
}
}