diff --git a/src/Pretender.SourceGenerator/DiagnosticDescriptors.cs b/src/Pretender.SourceGenerator/DiagnosticDescriptors.cs index a340566..cebf84c 100644 --- a/src/Pretender.SourceGenerator/DiagnosticDescriptors.cs +++ b/src/Pretender.SourceGenerator/DiagnosticDescriptors.cs @@ -4,16 +4,8 @@ namespace Pretender.SourceGenerator { internal static class DiagnosticDescriptors { - public static DiagnosticDescriptor UnsupportedLanguageVersion { get; } = new( - "PRTND001", - "Unsupported language version", - "", - "Usage", - DiagnosticSeverity.Error, - isEnabledByDefault: true); - public static DiagnosticDescriptor UnableToPretendSealedType { get; } = new( - "PRTND002", + "PRTND001", "Unable to Pretend Sealed Types", "Sealed types cannot be Pretended, did you mean to use an interface?", "Usage", @@ -21,7 +13,7 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true); public static DiagnosticDescriptor InvalidSetupArgument { get; } = new( - "PRTND003", + "PRTND002", "Invalid Setup Argument", "We don't support operation type {0} as a setup argument.", "Usage", diff --git a/src/Pretender.SourceGenerator/InterceptsLocationInfo.cs b/src/Pretender.SourceGenerator/InterceptsLocationInfo.cs index 637a845..d7c32d2 100644 --- a/src/Pretender.SourceGenerator/InterceptsLocationInfo.cs +++ b/src/Pretender.SourceGenerator/InterceptsLocationInfo.cs @@ -22,27 +22,5 @@ public InterceptsLocationInfo(IInvocationOperation invocationOperation) public string FilePath { get; } public int LineNumber { get; } public int CharacterNumber { get; } - - public AttributeSyntax ToAttributeSyntax() - { - return Attribute(IdentifierName("InterceptsLocation")) - .WithArgumentList(AttributeArgumentList(SeparatedList(new[] - { - AttributeArgument( - LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal(FilePath))), - - AttributeArgument( - LiteralExpression( - SyntaxKind.NumericLiteralExpression, - Literal(LineNumber))), - - AttributeArgument( - LiteralExpression( - SyntaxKind.NumericLiteralExpression, - Literal(CharacterNumber))), - }))); - } } } \ No newline at end of file diff --git a/src/Pretender.SourceGenerator/InvocationOperationExtensions.cs b/src/Pretender.SourceGenerator/InvocationOperationExtensions.cs deleted file mode 100644 index e8139ad..0000000 --- a/src/Pretender.SourceGenerator/InvocationOperationExtensions.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Collections.Immutable; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Operations; - -namespace Pretender.SourceGenerator -{ - public static class InvocationOperationExtensions - { - public static bool IsInvocationOperation(this IOperation? operation, out IInvocationOperation? invocationOperation) - { - invocationOperation = null; - if (operation is IInvocationOperation targetOperation) - { - invocationOperation = targetOperation; - return true; - } - - return false; - } - - public static bool IsValidCreateOperation(this IOperation? operation, Compilation compilation, out IInvocationOperation invocationOperation, out ImmutableArray? typeArguments) - { - var pretendGeneric = compilation.GetTypeByMetadataName("Pretender.Pretend`1"); - - if (operation is IInvocationOperation targetOperation - && targetOperation.Instance is not null - && SymbolEqualityComparer.Default.Equals(targetOperation.Instance.Type!.OriginalDefinition, pretendGeneric)) - { - invocationOperation = targetOperation; - if (targetOperation.TargetMethod.Parameters.Length == 1 && targetOperation.TargetMethod.Parameters[0].IsParams) - { - // They are in the params fallback, how lol? - typeArguments = null; - } - else - { - typeArguments = targetOperation.TargetMethod.TypeArguments; - } - - return true; - } - - invocationOperation = null!; - typeArguments = null; - return false; - } - } -} \ No newline at end of file diff --git a/src/Pretender.SourceGenerator/KnownBlocks.cs b/src/Pretender.SourceGenerator/KnownBlocks.cs index 46ab6ec..dce1862 100644 --- a/src/Pretender.SourceGenerator/KnownBlocks.cs +++ b/src/Pretender.SourceGenerator/KnownBlocks.cs @@ -1,7 +1,4 @@ using System.Reflection; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Pretender.SourceGenerator { @@ -25,39 +22,5 @@ public InterceptsLocationAttribute(string filePath, int line, int column) } } """; - - public static MemberAccessExpressionSyntax TaskCompletedTask = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("Task"), - IdentifierName("CompletedTask") - ); - - public static MemberAccessExpressionSyntax ValueTaskCompletedTask = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("ValueTask"), - IdentifierName("CompletedTask") - ); - - public static InvocationExpressionSyntax TaskFromResult(TypeSyntax resultType, ExpressionSyntax resultValue) => InvocationExpression( - MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("Task"), - GenericName("FromResult") - .AddTypeArgumentListArguments(resultType)) - ) - .AddArgumentListArguments(Argument(resultValue)); - - public static InvocationExpressionSyntax ValueTaskFromResult(TypeSyntax resultType, ExpressionSyntax resultValue) - { - // ValueTask.FromResult - var memberAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("ValueTask"), - GenericName("FromResult") - .AddTypeArgumentListArguments(resultType)); - - // ValueTask.FromResult(value) - return InvocationExpression(memberAccess, - ArgumentList( - SingletonSeparatedList(Argument(resultValue)))); - } } } \ No newline at end of file diff --git a/src/Pretender.SourceGenerator/SyntaxNodeExtensions.cs b/src/Pretender.SourceGenerator/SyntaxNodeExtensions.cs deleted file mode 100644 index 67d5285..0000000 --- a/src/Pretender.SourceGenerator/SyntaxNodeExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.CodeAnalysis; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace Pretender.SourceGenerator -{ - internal static class SyntaxNodeExtensions - { - public static TSyntax WithInheritDoc(this TSyntax node) - where TSyntax : SyntaxNode - { - return node.WithLeadingTrivia(TriviaList(Comment("/// "))); - } - } -} \ No newline at end of file diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs index f4011ef..8c3351b 100644 --- a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs +++ b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs @@ -1,3 +1,11 @@ +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0612, CS0618 + namespace System.Runtime.CompilerServices { using System; diff --git a/test/SourceGeneratorTests/TestBase.cs b/test/SourceGeneratorTests/TestBase.cs index f4acd1e..b033df2 100644 --- a/test/SourceGeneratorTests/TestBase.cs +++ b/test/SourceGeneratorTests/TestBase.cs @@ -121,7 +121,7 @@ public async Task RunAndCompareAsync(string source, [CallerMemberName] string? t private void CompareAgainstBaseline(GeneratedSourceResult result, string? testMethodName = null) { var normalizedName = result.HintName[..^3].Replace('.', '_') + ".cs"; -#if !GENERATE_SOURCE +#if GENERATE_SOURCE var resultFileName = result.HintName.Replace('.', '_'); var baseLineName = $"{GetType().Name}.{testMethodName}.{normalizedName}"; var resourceName = Assert.Single(typeof(TestBase).Assembly.GetManifestResourceNames()