コード例 #1
0
        private static void ExpectedResultSupplied(
            SyntaxNodeAnalysisContext context,
            IMethodSymbol methodSymbol,
            AttributeSyntax attributeNode,
            AttributeArgumentSyntax expectedResultNamedArgument)
        {
            var methodReturnValueType = methodSymbol.ReturnType;

            if (methodReturnValueType.IsAwaitable(out var awaitReturnType))
            {
                if (awaitReturnType.SpecialType == SpecialType.System_Void)
                {
                    context.ReportDiagnostic(Diagnostic.Create(asyncExpectedResultButReturnTypeNotGenericTask,
                                                               attributeNode.GetLocation(), methodReturnValueType.ToDisplayString()));
                }
                else
                {
                    ReportIfExpectedResultTypeCannotBeAssignedToReturnType(
                        ref context, expectedResultNamedArgument, awaitReturnType);
                }
            }
            else
            {
                if (methodReturnValueType.SpecialType == SpecialType.System_Void)
                {
                    context.ReportDiagnostic(Diagnostic.Create(specifiedExpectedResultForVoid,
                                                               expectedResultNamedArgument.GetLocation()));
                }
                else
                {
                    ReportIfExpectedResultTypeCannotBeAssignedToReturnType(
                        ref context, expectedResultNamedArgument, methodReturnValueType);
                }
            }
        }
コード例 #2
0
 // The binding expression is [AutoResolve] and has an illegal syntax.
 // Commonly unmatched { }
 public static Diagnostic BadBindingExpressionSyntax(AttributeArgumentSyntax syntax, PropertyInfo propInfo, object actualValue, Exception error)
 {
     return(Diagnostic.Create(Rule3,
                              syntax.GetLocation(),
                              propInfo.Name,
                              actualValue,
                              error.Message
                              ));
 }
コード例 #3
0
 // The binding expression has a ValidationAttribute, the validation failed.
 // Commonly a regex mismatch
 public static Diagnostic FailedValidation(AttributeArgumentSyntax syntax, PropertyInfo propInfo, object actualValue, Exception error)
 {
     return(Diagnostic.Create(Rule2,
                              syntax.GetLocation(),
                              propInfo.Name,
                              actualValue,
                              error.Message
                              ));
 }
コード例 #4
0
        private void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context, INamedTypeSymbol asyncPackageType, INamedTypeSymbol provideToolWindowAttributeType)
        {
            var            declaration = (ClassDeclarationSyntax)context.Node;
            BaseTypeSyntax?baseType    = declaration.BaseList?.Types.FirstOrDefault();

            if (baseType == null)
            {
                return;
            }

            var baseTypeSymbol = context.SemanticModel.GetSymbolInfo(baseType.Type, context.CancellationToken).Symbol?.OriginalDefinition as ITypeSymbol;

            if (!Utils.IsEqualToOrDerivedFrom(baseTypeSymbol, asyncPackageType))
            {
                return;
            }

            INamedTypeSymbol userClassSymbol             = context.SemanticModel.GetDeclaredSymbol(declaration, context.CancellationToken);
            AttributeData?   packageRegistrationInstance = userClassSymbol?.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, provideToolWindowAttributeType));
            TypedConstant?   firstParameter = packageRegistrationInstance?.ConstructorArguments.FirstOrDefault();

            if (firstParameter.HasValue && firstParameter.Value.Kind == TypedConstantKind.Type && firstParameter.Value.Value is INamedTypeSymbol typeOfUserToolWindow)
            {
                // If the tool window has a constructor that takes a parameter,
                // then the tool window is probably created asynchronously, because you
                // cannot easily pass a parameter when creating a synchronous tool window.
                bool toolWindowHasCtorWithOneParameter = typeOfUserToolWindow.GetMembers(ConstructorInfo.ConstructorName).OfType <IMethodSymbol>().Any(c => c.Parameters.Length == 1);
                if (toolWindowHasCtorWithOneParameter)
                {
                    return;
                }

                // If the `GetAsyncToolWindowFactory` method has been overridden,
                // then it's highly likely that the tool window will be created asynchronously.
                var packageSymbol = context.SemanticModel.GetDeclaredSymbol(declaration, context.CancellationToken)?.OriginalDefinition as ITypeSymbol;
                if (this.IsGetAsyncToolWindowFactoryOverridden(packageSymbol, asyncPackageType))
                {
                    return;
                }

                if (packageRegistrationInstance !.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken) is AttributeSyntax attributeSyntax)
                {
                    AttributeArgumentSyntax firstArgumentSyntax = attributeSyntax.ArgumentList.Arguments.First();
                    Location diagnosticLocation = firstArgumentSyntax.GetLocation();
                    if (firstArgumentSyntax.Expression is TypeOfExpressionSyntax typeOfArg)
                    {
                        diagnosticLocation = typeOfArg.Type.GetLocation();
                    }

                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, diagnosticLocation));
                }
            }
        }
コード例 #5
0
        private void ReportInvalidSerializerType(
            SyntaxNodeAnalysisContext context,
            AttributeArgumentSyntax typeArgumentSyntax,
            string messageArg
            )
        {
            Diagnostic diagnostic = Diagnostic.Create(
                descriptor: Diagnostics.InvalidSerializerType,
                location: typeArgumentSyntax.GetLocation(),
                messageArgs: messageArg
                );

            context.ReportDiagnostic(diagnostic);
        }
        private static void HandleAttribute(SyntaxNodeAnalysisContext context)
        {
            var attribute = (AttributeSyntax)context.Node;
            AttributeArgumentListSyntax argumentListSyntax = attribute.ArgumentList;

            if (argumentListSyntax == null)
            {
                return;
            }

            SeparatedSyntaxList <AttributeArgumentSyntax> arguments = argumentListSyntax.Arguments;

            if (arguments.Count < 3)
            {
                return;
            }

            AttributeArgumentSyntax previousParameter = arguments[1];
            int firstParameterLine = arguments[0].GetLine();
            int previousLine       = previousParameter.GetLine();
            Func <int, int, bool> lineCondition;

            if (firstParameterLine == previousLine)
            {
                // arguments must be on same line
                lineCondition = (param1Line, param2Line) => param1Line == param2Line;
            }
            else
            {
                // each argument must be on its own line
                lineCondition = (param1Line, param2Line) => param1Line != param2Line;
            }

            for (int i = 2; i < arguments.Count; ++i)
            {
                AttributeArgumentSyntax currentParameter = arguments[i];
                int currentLine = currentParameter.GetLine();

                if (lineCondition(previousLine, currentLine))
                {
                    previousLine = currentLine;
                    continue;
                }

                context.ReportDiagnostic(Diagnostic.Create(Descriptor, currentParameter.GetLocation()));
                return;
            }
        }
コード例 #7
0
        private static void ReportIfExpectedResultTypeCannotBeAssignedToReturnType(
            ref SyntaxNodeAnalysisContext context,
            AttributeArgumentSyntax expectedResultNamedArgument,
            ITypeSymbol typeSymbol)
        {
            if (typeSymbol.IsTypeParameterAndDeclaredOnMethod())
            {
                return;
            }

            if (!expectedResultNamedArgument.CanAssignTo(typeSymbol, context.SemanticModel))
            {
                context.ReportDiagnostic(Diagnostic.Create(expectedResultTypeMismatch,
                                                           expectedResultNamedArgument.GetLocation(), typeSymbol.MetadataName));
            }
        }
コード例 #8
0
        private static void ExpectedResultSupplied(
            SyntaxNodeAnalysisContext context,
            IMethodSymbol methodSymbol,
            AttributeSyntax attributeNode,
            AttributeArgumentSyntax expectedResultNamedArgument)
        {
            var methodReturnValueType = methodSymbol.ReturnType;

            if (IsTestMethodAsync(context.Compilation, methodSymbol))
            {
                var genericTaskType = context.Compilation.GetTypeByMetadataName(fullyQualifiedNameOfGenericTask);
                if (!methodReturnValueType.OriginalDefinition.Equals(genericTaskType))
                {
                    context.ReportDiagnostic(Diagnostic.Create(asyncExpectedResultButReturnTypeNotGenericTask,
                                                               attributeNode.GetLocation(), methodReturnValueType.ToDisplayString()));
                }
                else
                {
                    var namedTypeSymbol = methodReturnValueType as INamedTypeSymbol;
                    if (namedTypeSymbol == null)
                    {
                        return;
                    }

                    var taskTypeParameter = namedTypeSymbol.TypeArguments.First();
                    ReportIfExpectedResultTypeCannotBeAssignedToReturnType(
                        ref context, expectedResultNamedArgument, taskTypeParameter);
                }
            }
            else
            {
                if (methodReturnValueType.SpecialType == SpecialType.System_Void)
                {
                    context.ReportDiagnostic(Diagnostic.Create(specifiedExpectedResultForVoid,
                                                               expectedResultNamedArgument.GetLocation()));
                }
                else
                {
                    ReportIfExpectedResultTypeCannotBeAssignedToReturnType(
                        ref context, expectedResultNamedArgument, methodReturnValueType);
                }
            }
        }
        private void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context, INamedTypeSymbol packageType, INamedTypeSymbol asyncPackageType)
        {
            var            declaration = (ClassDeclarationSyntax)context.Node;
            BaseTypeSyntax?baseType    = declaration.BaseList?.Types.FirstOrDefault();

            if (baseType == null)
            {
                return;
            }

            var baseTypeSymbol = context.SemanticModel.GetSymbolInfo(baseType.Type, context.CancellationToken).Symbol?.OriginalDefinition as ITypeSymbol;

            if (Utils.IsEqualToOrDerivedFrom(baseTypeSymbol, packageType))
            {
                INamedTypeSymbol packageRegistrationType     = context.Compilation.GetTypeByMetadataName(Types.PackageRegistrationAttribute.FullName);
                INamedTypeSymbol userClassSymbol             = context.SemanticModel.GetDeclaredSymbol(declaration, context.CancellationToken);
                AttributeData?   packageRegistrationInstance = userClassSymbol?.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, packageRegistrationType));
                if (packageRegistrationInstance == null)
                {
                    return;
                }

                if (!(packageRegistrationInstance.NamedArguments.FirstOrDefault(kv => kv.Key == Types.PackageRegistrationAttribute.AllowsBackgroundLoading).Value.Value is bool allowsBackgroundLoading))
                {
                    allowsBackgroundLoading = false;
                }

                bool isAsyncPackageBaseType = Utils.IsEqualToOrDerivedFrom(baseTypeSymbol, asyncPackageType);

                if (isAsyncPackageBaseType != allowsBackgroundLoading)
                {
                    var attributeSyntax = packageRegistrationInstance.ApplicationSyntaxReference.GetSyntax(context.CancellationToken) as AttributeSyntax;
                    if (attributeSyntax is object)
                    {
                        AttributeArgumentSyntax?allowBackgroundLoadingSyntax = attributeSyntax.ArgumentList.Arguments.FirstOrDefault(a => a.NameEquals?.Name?.Identifier.Text == Types.PackageRegistrationAttribute.AllowsBackgroundLoading);
                        Location location = allowBackgroundLoadingSyntax?.GetLocation() ?? attributeSyntax.GetLocation();
                        ImmutableDictionary <string, string> properties = ImmutableDictionary.Create <string, string>()
                                                                          .Add(BaseTypeDiagnosticPropertyName, isAsyncPackageBaseType ? Types.AsyncPackage.TypeName : Types.Package.TypeName);
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, location, properties));
                    }
                }
            }
        }
コード例 #10
0
        private void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context, INamedTypeSymbol asyncPackageType, INamedTypeSymbol provideToolWindowAttributeType)
        {
            var declaration = (ClassDeclarationSyntax)context.Node;
            var baseType    = declaration.BaseList?.Types.FirstOrDefault();

            if (baseType == null)
            {
                return;
            }

            var baseTypeSymbol = context.SemanticModel.GetSymbolInfo(baseType.Type, context.CancellationToken).Symbol?.OriginalDefinition as ITypeSymbol;

            if (!Utils.IsEqualToOrDerivedFrom(baseTypeSymbol, asyncPackageType))
            {
                return;
            }

            var userClassSymbol             = context.SemanticModel.GetDeclaredSymbol(declaration, context.CancellationToken);
            var packageRegistrationInstance = userClassSymbol?.GetAttributes().FirstOrDefault(a => a.AttributeClass?.Equals(provideToolWindowAttributeType) ?? false);
            var firstParameter = packageRegistrationInstance?.ConstructorArguments.FirstOrDefault();

            if (firstParameter.HasValue && firstParameter.Value.Kind == TypedConstantKind.Type && firstParameter.Value.Value is INamedTypeSymbol typeOfUserToolWindow)
            {
                bool toolWindowHasCtorWithOneParameter = typeOfUserToolWindow.GetMembers(ConstructorInfo.ConstructorName).OfType <IMethodSymbol>().Any(c => c.Parameters.Length == 1);
                if (!toolWindowHasCtorWithOneParameter)
                {
                    if (packageRegistrationInstance.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken) is AttributeSyntax attributeSyntax)
                    {
                        AttributeArgumentSyntax firstArgumentSyntax = attributeSyntax.ArgumentList.Arguments.First();
                        Location diagnosticLocation = firstArgumentSyntax.GetLocation();
                        if (firstArgumentSyntax.Expression is TypeOfExpressionSyntax typeOfArg)
                        {
                            diagnosticLocation = typeOfArg.Type.GetLocation();
                        }

                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, diagnosticLocation));
                    }
                }
            }
        }
コード例 #11
0
 public static Diagnostic CreateGA0011(AttributeArgumentSyntax attributeArgumentSyntaxNode, ITypeSymbol argumentType)
 {
     return(Diagnostic.Create(Instance[0011], attributeArgumentSyntaxNode?.GetLocation(), argumentType.ToDisplayString()));
 }
コード例 #12
0
 public static Diagnostic CreateGA0028(AttributeArgumentSyntax typeProfileInheritanceAttributeArgumentNode)
 {
     return(Diagnostic.Create(Instance[0028], typeProfileInheritanceAttributeArgumentNode?.GetLocation()));
 }
コード例 #13
0
 public static Diagnostic CreateGA0027(AttributeArgumentSyntax nonProfileGroupTypeArgumentNode)
 {
     return(Diagnostic.Create(Instance[0027], nonProfileGroupTypeArgumentNode?.GetLocation()));
 }
コード例 #14
0
 public static Diagnostic CreateGA0020(AttributeArgumentSyntax attributeArgumentNode, IEnumerable <ITypeParameterSymbol> recursionPath)
 {
     return(Diagnostic.Create(Instance[0020], attributeArgumentNode?.GetLocation(), string.Join(", ", recursionPath.Select(t => t.ToDisplayString()))));
 }
コード例 #15
0
 public static Diagnostic CreateGA0019(AttributeArgumentSyntax attributeArgumentNode, string typeParameterName)
 {
     return(Diagnostic.Create(Instance[0019], attributeArgumentNode?.GetLocation(), typeParameterName));
 }
コード例 #16
0
 public static Diagnostic CreateGA0003(AttributeArgumentSyntax attributeArgumentSyntaxNode, ITypeParameterSymbol typeParameter, INamedTypeSymbol genericTypeArgument)
 {
     return(Diagnostic.Create(Instance[0003], attributeArgumentSyntaxNode?.GetLocation(), typeParameter.ToDisplayString(), genericTypeArgument.ToDisplayString()));
 }
コード例 #17
0
 public static Diagnostic CreateGA0006(AttributeArgumentSyntax attributeArgumentSyntaxNode)
 {
     return(Diagnostic.Create(Instance[0006], attributeArgumentSyntaxNode?.GetLocation()));
 }
コード例 #18
0
 public static Diagnostic CreateGA0005(AttributeArgumentSyntax attributeArgumentSyntaxNode, ITypeSymbol argumentType, ITypeParameterSymbol typeParameter)
 {
     return(Diagnostic.Create(Instance[0005], attributeArgumentSyntaxNode?.GetLocation(), argumentType.ToDisplayString(), typeParameter.ToDisplayString()));
 }