Skip to content

Commit

Permalink
Create SyntaxKindEx helper
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Apr 5, 2024
1 parent 33417fb commit 5705f38
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using Analyzer.Utilities.Lightup;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -14,12 +15,6 @@ namespace Microsoft.NetCore.CSharp.Analyzers.Performance
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class CSharpUseSearchValuesAnalyzer : UseSearchValuesAnalyzer
{
// The referenced SDK version doesn't yet contain these SyntaxKind values
// https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
private const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
private const SyntaxKind Utf8StringLiteralExpression = (SyntaxKind)8756;
private const SyntaxKind CollectionExpression = (SyntaxKind)9076;

// char[] myField = new char[] { 'a', 'b', 'c' };
// char[] myField = new[] { 'a', 'b', 'c' };
// char[] myField = "abc".ToCharArray();
Expand Down Expand Up @@ -118,7 +113,7 @@ internal static bool IsConstantByteOrCharArrayCreationExpression(SemanticModel s
return true;
}
}
else if (expression.IsKind(CollectionExpression))
else if (expression.IsKind(SyntaxKindEx.CollectionExpression))
{
return
semanticModel.GetOperation(expression) is { } operation &&
Expand Down Expand Up @@ -171,9 +166,9 @@ static bool TryGetByteOrCharLiteral(ExpressionSyntax? expression, out char value

private static bool IsUtf8StringLiteralExpression(ExpressionSyntax expression, out int length)
{
if (expression.IsKind(Utf8StringLiteralExpression) &&
if (expression.IsKind(SyntaxKindEx.Utf8StringLiteralExpression) &&
expression is LiteralExpressionSyntax literal &&
literal.Token.IsKind(Utf8StringLiteralToken) &&
literal.Token.IsKind(SyntaxKindEx.Utf8StringLiteralToken) &&
literal.Token.Value is string value)
{
length = value.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\SyntaxKindEx.cs" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/Utilities/Compiler.CSharp/Lightup/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.CSharp;

namespace Analyzer.Utilities.Lightup
{
internal static class SyntaxKindEx
{
// https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
public const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
public const SyntaxKind Utf8StringLiteralExpression = (SyntaxKind)8756;
public const SyntaxKind CollectionExpression = (SyntaxKind)9076;
}
}

0 comments on commit 5705f38

Please sign in to comment.