diff --git a/Directory.Packages.props b/Directory.Packages.props index 1c04406..5669112 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,7 +10,6 @@ - diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7f335b9..fd0742d 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -23,6 +23,7 @@ + diff --git a/src/System/Runtime/CompilerServices/UnionAttribute.cs b/src/System/Runtime/CompilerServices/UnionAttribute.cs new file mode 100644 index 0000000..ec9a01f --- /dev/null +++ b/src/System/Runtime/CompilerServices/UnionAttribute.cs @@ -0,0 +1,7 @@ +namespace System.Runtime.CompilerServices; + +/// +/// Marks a type as a C# union. +/// +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)] +public sealed class UnionAttribute : Attribute; diff --git a/src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs b/src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs index 119e81c..5d57ca8 100644 --- a/src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs +++ b/src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs @@ -376,8 +376,8 @@ bool IsValidParameter(ParameterSyntax ps, int i) var extra = ProcessTrivia(leading, ds); if (extra is not null && extra.AdditionalStatements.Count > 0) { - modifications.Add(i, new List(extra.AdditionalStatements)); - modifications.Add(i + 1, true); + modifications.Add(i, new Operation([.. extra.AdditionalStatements])); + modifications.Add(i + 1, new Operation(true)); } if (semanticModel.GetDeclaredSymbol(ps) is not { } symbol) @@ -1074,8 +1074,8 @@ bool ShouldRemoveArgumentLocal(ArgumentSyntax arg, int index) if (extra is { AdditionalStatements.Count: > 0 }) { - modifications.Add(index, new List(extra.AdditionalStatements)); - modifications.Add(index + 1, true); + modifications.Add(index, new Operation([.. extra.AdditionalStatements])); + modifications.Add(index + 1, new Operation(true)); } if (byExpression || nullableParameters is not { } parameters) diff --git a/src/Zomp.SyncMethodGenerator/Operation.cs b/src/Zomp.SyncMethodGenerator/Operation.cs index c810f58..5d2e4a3 100644 --- a/src/Zomp.SyncMethodGenerator/Operation.cs +++ b/src/Zomp.SyncMethodGenerator/Operation.cs @@ -1,7 +1,41 @@ namespace Zomp.SyncMethodGenerator; -[N.SourceGenerators.UnionTypes.UnionType(typeof(List), "NewStatements")] -[N.SourceGenerators.UnionTypes.UnionType(typeof(bool), "RemoveLeadingEndIf")] -internal sealed partial class Operation +/// +/// Represents a sync-only operation. +/// +[Union] +internal readonly struct Operation { + /// + /// Initializes a new instance of the struct. + /// + /// The new statements to insert. + public Operation(List value) + { + Value = value; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A value indicating whether to remove the leading end-if directive. + public Operation(bool value) + { + Value = value; + } + + /// + /// Gets the active operation value. + /// + public object? Value { get; } + + /// + /// Gets a value indicating whether the operation inserts new statements. + /// + public bool IsNewStatements => Value is List; + + /// + /// Gets the operation value as new statements. + /// + public List AsNewStatements => (List)Value!; } diff --git a/src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj b/src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj index a6faef9..cb8de0c 100644 --- a/src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj +++ b/src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj @@ -49,7 +49,6 @@ -