Skip to content

Commit 41100e2

Browse files
committed
Add SyntaxTokenExtensions, minor code refactoring
1 parent 1320475 commit 41100e2

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/CommunityToolkit.Mvvm.SourceGenerators.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SourceProductionContextExtensions.cs" />
6060
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ITypeSymbolExtensions.cs" />
6161
<Compile Include="$(MSBuildThisFileDirectory)Extensions\MemberDeclarationSyntaxExtensions.cs" />
62+
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxTokenExtensions.cs" />
6263
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
6364
<Compile Include="$(MSBuildThisFileDirectory)Extensions\TypeDeclarationSyntaxExtensions.cs" />
6465
<Compile Include="$(MSBuildThisFileDirectory)Helpers\EquatableArray{T}.cs" />

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static bool TryGetInfo(
193193
// Only look for attribute lists explicitly targeting the (generated) property. Roslyn will normally emit a
194194
// CS0657 warning (invalid target), but that is automatically suppressed by a dedicated diagnostic suppressor
195195
// that recognizes uses of this target specifically to support [ObservableProperty].
196-
if (attributeList.Target?.Identifier.Kind() is not SyntaxKind.PropertyKeyword)
196+
if (attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.PropertyKeyword))
197197
{
198198
continue;
199199
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
8+
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
9+
10+
/// <summary>
11+
/// Extension methods for the <see cref="SyntaxToken"/> type.
12+
/// </summary>
13+
internal static class SyntaxTokenExtensions
14+
{
15+
/// <summary>
16+
/// Deconstructs a <see cref="SyntaxToken"/> into its <see cref="SyntaxKind"/> value.
17+
/// </summary>
18+
/// <param name="syntaxToken">The input <see cref="SyntaxToken"/> value.</param>
19+
/// <param name="syntaxKind">The resulting <see cref="SyntaxKind"/> value for <paramref name="syntaxToken"/>.</param>
20+
public static void Deconstruct(this SyntaxToken syntaxToken, out SyntaxKind syntaxKind)
21+
{
22+
syntaxKind = syntaxToken.Kind();
23+
}
24+
}

src/CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.Execute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ private static void GatherForwardedAttributes(
964964
foreach (AttributeListSyntax attributeList in methodDeclaration.AttributeLists)
965965
{
966966
// Same as in the [ObservableProperty] generator, except we're also looking for fields here
967-
if (attributeList.Target?.Identifier.Kind() is not (SyntaxKind.PropertyKeyword or SyntaxKind.FieldKeyword))
967+
if (attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.PropertyKeyword or SyntaxKind.FieldKeyword))
968968
{
969969
continue;
970970
}
@@ -986,7 +986,7 @@ private static void GatherForwardedAttributes(
986986
AttributeInfo attributeInfo = AttributeInfo.From(attributeTypeSymbol, semanticModel, attribute.ArgumentList?.Arguments ?? Enumerable.Empty<AttributeArgumentSyntax>(), token);
987987

988988
// Add the new attribute info to the right builder
989-
if (attributeList.Target?.Identifier.Kind() is SyntaxKind.FieldKeyword)
989+
if (attributeList.Target?.Identifier is SyntaxToken(SyntaxKind.FieldKeyword))
990990
{
991991
fieldAttributesInfo.Add(attributeInfo);
992992
}

0 commit comments

Comments
 (0)