Esempio n. 1
0
 private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeSyntaxWrapper tupleTypeSyntax, Location reportLocation)
 {
     foreach (var tupleElementSyntax in tupleTypeSyntax.Elements)
     {
         CheckType(context, tupleElementSyntax.Type, reportLocation);
     }
 }
Esempio n. 2
0
        public void TestIsInstance()
        {
            Assert.False(TupleTypeSyntaxWrapper.IsInstance(null));
            Assert.False(TupleTypeSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));

            var syntaxNode = this.CreateTupleType();

            Assert.True(TupleTypeSyntaxWrapper.IsInstance(syntaxNode));
        }
Esempio n. 3
0
        private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeSyntaxWrapper tupleTypeSyntax)
        {
            foreach (var tupleElementSyntax in tupleTypeSyntax.Elements)
            {
                CheckType(context, tupleElementSyntax.Type);

                if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None))
                {
                    var location = tupleElementSyntax.SyntaxNode.GetLocation();
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
                }
            }
        }
        private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol, TypeSyntax type)
        {
            switch (symbol.Kind)
            {
            case SymbolKind.ArrayType:
                var arraySymbol = (IArrayTypeSymbol)symbol;
                AppendQualifiedSymbolName(builder, arraySymbol.ElementType, (type as ArrayTypeSyntax)?.ElementType);
                builder
                .Append("[")
                .Append(',', arraySymbol.Rank - 1)
                .Append("]");
                return(true);

            case SymbolKind.Namespace:
                var namespaceSymbol = (INamespaceSymbol)symbol;
                if (namespaceSymbol.IsGlobalNamespace)
                {
                    return(false);
                }

                builder.Append(namespaceSymbol.ToDisplayString());
                return(true);

            case SymbolKind.NamedType:
                var namedTypeSymbol = (INamedTypeSymbol)symbol;
                if (SpecialTypeHelper.TryGetPredefinedType(namedTypeSymbol.SpecialType, out var specialTypeSyntax))
                {
                    builder.Append(specialTypeSyntax.ToFullString());
                    return(true);
                }
                else if (namedTypeSymbol.IsTupleType())
                {
                    if (TupleTypeSyntaxWrapper.IsInstance(type))
                    {
                        var tupleType = (TupleTypeSyntaxWrapper)type;

                        builder.Append(TupleTypeOpen);
                        var elements = namedTypeSymbol.TupleElements();
                        for (int i = 0; i < elements.Length; i++)
                        {
                            var field     = elements[i];
                            var fieldType = tupleType.Elements.Count > i ? tupleType.Elements[i] : default;

                            if (i > 0)
                            {
                                builder.Append(TupleElementSeparator);
                            }

                            AppendQualifiedSymbolName(builder, field.Type, fieldType.Type);
                            if (field != field.CorrespondingTupleField())
                            {
                                builder.Append(" ").Append(field.Name);
                            }
                        }

                        builder.Append(TupleTypeClose);
                        return(true);
                    }
                    else
                    {
                        return(AppendQualifiedSymbolName(builder, namedTypeSymbol.TupleUnderlyingType(), type));
                    }
                }
                else if (namedTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
                {
                    AppendQualifiedSymbolName(builder, namedTypeSymbol.TypeArguments[0], (type as NullableTypeSyntax)?.ElementType);
                    builder.Append("?");
                    return(true);
                }
                else
                {
                    if (AppendQualifiedSymbolName(builder, symbol.ContainingSymbol, (type as QualifiedNameSyntax)?.Left))
                    {
                        builder.Append(".");
                    }

                    builder.Append(symbol.Name);
                    if (namedTypeSymbol.IsGenericType && !namedTypeSymbol.TypeArguments.IsEmpty)
                    {
                        builder.Append(GenericTypeParametersOpen);
                        var arguments     = namedTypeSymbol.TypeArguments;
                        var argumentTypes = type is QualifiedNameSyntax qualifiedName
                            ? (qualifiedName.Right as GenericNameSyntax)?.TypeArgumentList
                            : (type as GenericNameSyntax)?.TypeArgumentList;

                        for (int i = 0; i < arguments.Length; i++)
                        {
                            var argument     = arguments[i];
                            var argumentType = argumentTypes != null && argumentTypes.Arguments.Count > i ? argumentTypes.Arguments[i] : null;

                            if (i > 0)
                            {
                                builder.Append(GenericSeparator);
                            }

                            if (!argumentType.IsKind(SyntaxKind.OmittedTypeArgument))
                            {
                                AppendQualifiedSymbolName(builder, argument, argumentType);
                            }
                        }

                        builder.Append(GenericTypeParametersClose);
                    }

                    return(true);
                }

            default:
                if (symbol != null)
                {
                    builder.Append(symbol.Name);
                    return(true);
                }

                return(false);
            }
        }
        private static bool AppendCanonicalString(StringBuilder builder, TypeSyntax type)
        {
            switch (type)
            {
            case AliasQualifiedNameSyntax aliasQualifiedName:
                AppendCanonicalString(builder, aliasQualifiedName.Alias);
                builder.Append("::");
                AppendCanonicalString(builder, aliasQualifiedName.Name);
                return(true);

            case IdentifierNameSyntax identifierName:
                builder.Append(identifierName.Identifier.Text);
                return(true);

            case GenericNameSyntax genericName:
                builder.Append(genericName.Identifier.Text);
                builder.Append("<");

                var typeArgumentList = genericName.TypeArgumentList;
                for (int i = 0; i < typeArgumentList.Arguments.Count; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }

                    AppendCanonicalString(builder, typeArgumentList.Arguments[i]);
                }

                builder.Append(">");
                return(true);

            case QualifiedNameSyntax qualifiedName:
                AppendCanonicalString(builder, qualifiedName.Left);
                builder.Append(".");
                AppendCanonicalString(builder, qualifiedName.Right);
                return(true);

            case PredefinedTypeSyntax predefinedType:
                builder.Append(predefinedType.Keyword.Text);
                return(true);

            case ArrayTypeSyntax arrayType:
                AppendCanonicalString(builder, arrayType.ElementType);
                foreach (var rankSpecifier in arrayType.RankSpecifiers)
                {
                    builder.Append("[");
                    builder.Append(',', rankSpecifier.Rank - 1);
                    builder.Append("]");
                }

                return(true);

            case NullableTypeSyntax nullableType:
                AppendCanonicalString(builder, nullableType.ElementType);
                builder.Append("?");
                return(true);

            case OmittedTypeArgumentSyntax _:
                return(false);

            default:
                if (TupleTypeSyntaxWrapper.IsInstance(type))
                {
                    var tupleType = (TupleTypeSyntaxWrapper)type;

                    builder.Append("(");

                    var elements = tupleType.Elements;
                    for (int i = 0; i < elements.Count; i++)
                    {
                        if (i > 0)
                        {
                            builder.Append(", ");
                        }

                        AppendCanonicalString(builder, elements[i].Type);
                        if (!elements[i].Identifier.IsKind(SyntaxKind.None))
                        {
                            builder.Append(" ").Append(elements[i].Identifier.Text);
                        }
                    }

                    builder.Append(")");
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
 public void TestIsInstance()
 {
     Assert.False(TupleTypeSyntaxWrapper.IsInstance(null));
     Assert.False(TupleTypeSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
 }