Skip to content

Commit d9a6ded

Browse files
committed
SA1026: handle nodes instead of syntax trees
1 parent 4e5fe43 commit d9a6ded

1 file changed

Lines changed: 9 additions & 28 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1026CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.SpacingRules
66
using System.Collections.Immutable;
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.CSharp.Syntax;
910
using Microsoft.CodeAnalysis.Diagnostics;
1011
using StyleCop.Analyzers.Helpers;
1112

@@ -37,17 +38,8 @@ internal class SA1026CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArra
3738
private static readonly DiagnosticDescriptor Descriptor =
3839
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3940

40-
private static readonly ImmutableArray<DiagnosticDescriptor> SupportedDiagnosticsValue =
41-
ImmutableArray.Create(Descriptor);
42-
4341
/// <inheritdoc/>
44-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
45-
{
46-
get
47-
{
48-
return SupportedDiagnosticsValue;
49-
}
50-
}
42+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Descriptor);
5143

5244
/// <inheritdoc/>
5345
public override void Initialize(AnalysisContext context)
@@ -57,28 +49,17 @@ public override void Initialize(AnalysisContext context)
5749

5850
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
5951
{
60-
context.RegisterSyntaxTreeActionHonorExclusions(HandleSyntaxTree);
52+
context.RegisterSyntaxNodeActionHonorExclusions(HandleImplicitArrayCreation, SyntaxKind.ImplicitArrayCreationExpression);
6153
}
6254

63-
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
55+
private static void HandleImplicitArrayCreation(SyntaxNodeAnalysisContext context)
6456
{
65-
SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
66-
foreach (var token in root.DescendantTokens())
67-
{
68-
if (!token.IsKind(SyntaxKind.NewKeyword))
69-
{
70-
continue;
71-
}
57+
var arrayCreation = (ImplicitArrayCreationExpressionSyntax)context.Node;
58+
var newKeywordToken = arrayCreation.NewKeyword;
7259

73-
if (token.IsMissing || !token.Parent.IsKind(SyntaxKind.ImplicitArrayCreationExpression))
74-
{
75-
continue;
76-
}
77-
78-
if (token.IsFollowedByWhitespace() || token.IsLastInLine())
79-
{
80-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemoveFollowing));
81-
}
60+
if (newKeywordToken.IsFollowedByWhitespace() || newKeywordToken.IsLastInLine())
61+
{
62+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, newKeywordToken.GetLocation(), TokenSpacingCodeFixProvider.RemoveFollowing));
8263
}
8364
}
8465
}

0 commit comments

Comments
 (0)