Пример #1
0
            public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
            {
                if (node.Initializer == null)
                {
                    return(base.VisitVariableDeclarator(node));
                }
                if (node.Parent is VariableDeclarationSyntax == false)
                {
                    return(base.VisitVariableDeclarator(node));
                }
                if (node.Parent.Parent is FieldDeclarationSyntax == false)
                {
                    return(base.VisitVariableDeclarator(node));
                }
                if ((node.Parent.Parent as FieldDeclarationSyntax).Modifiers.Any(x => x.ValueText == "const"))
                {
                    return(base.VisitVariableDeclarator(node));
                }

                var value = node.Initializer.Value;

                if (value is LiteralExpressionSyntax)
                {
                    var variableTypeNode = GetSystemTypeOfTypeNode((node.Parent as VariableDeclarationSyntax));
                    var valueObj         = (value as LiteralExpressionSyntax).Token.Value;

                    if (TypesMapItem.GetAllPredefinedTypesDic().ContainsKey(variableTypeNode))
                    {
                        var typeItem = TypesMapItem.GetAllPredefinedTypesDic()[variableTypeNode];
                        if ((typeItem.DefaultValue == null && valueObj != null) || (typeItem.DefaultValue != null && !typeItem.DefaultValue.Equals(valueObj)))
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }
                    else
                    {
                        if (valueObj != null)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }

                    node = node.WithInitializer(null).WithoutTrailingTrivia();
                }
                //else if (value is DefaultExpressionSyntax)
                //{
                //    node = node.WithInitializer(null).WithoutTrailingTrivia();
                //}
                //else if (value is ObjectCreationExpressionSyntax)
                //{
                //    if (variableTypeNode.IsKind(SyntaxKind.PredefinedType))
                //    {
                //        node = node.WithInitializer(null).WithoutTrailingTrivia();
                //    }
                //}

                return(base.VisitVariableDeclarator(node));
            }
Пример #2
0
            string GetSystemTypeOfTypeNode(VariableDeclarationSyntax d)
            {
                if (d.Type is PredefinedTypeSyntax)
                {
                    return(TypesMapItem.GetAllPredefinedTypesDic()[(d.Type as PredefinedTypeSyntax).Keyword.ValueText].BuiltInName.Trim());
                }

                return(d.Type.ToFullString().Trim());
            }
        public SyntaxNode ConvertFullNameTypesToBuiltInTypesHelper(SyntaxNode initialSource)
        {
            var builtInTypesMapDic = TypesMapItem.GetBuiltInTypesDic();

            var selectedTokensList =
                initialSource
                .DescendantNodes()
                .Where
                (
                    n =>
                    (n is IdentifierNameSyntax || n is QualifiedNameSyntax)
                    &&
                    builtInTypesMapDic.ContainsKey(n.WithoutTrivia().ToFullString())
                );

            return(initialSource.ReplaceNodes(
                       selectedTokensList,
                       (oldNode1, oldNode2) =>
            {
                if (oldNode1.Parent is QualifiedNameSyntax)
                {
                    return oldNode1;
                }
                if (oldNode1.Parent is MemberAccessExpressionSyntax)
                {
                    if ((oldNode1.Parent as MemberAccessExpressionSyntax).Expression != oldNode1)
                    {
                        return oldNode1;
                    }
                }
                else if (oldNode1 is IdentifierNameSyntax == false && oldNode1 is QualifiedNameSyntax == false)
                {
                    return oldNode1;
                }
                else
                {
                    var symbol = ProjectItemDetails.SemanticModel.GetSymbolInfo(oldNode1).Symbol;
                    if (symbol != null && symbol.Kind != SymbolKind.NamedType)
                    {
                        return oldNode1;
                    }
                }
                return
                builtInTypesMapDic[oldNode1.WithoutTrivia().ToFullString()]
                .NewNode
                .WithLeadingTrivia(oldNode1.GetLeadingTrivia())
                .WithTrailingTrivia(oldNode1.GetTrailingTrivia());
            }
                       ));
        }
Пример #4
0
        public static Dictionary <string, TypesMapItem> GetBuiltInTypesDic()
        {
            if (BuiltInTypesDic != null)
            {
                return(BuiltInTypesDic);
            }

            var output = new Dictionary <string, TypesMapItem>();

            using (var provider = new CSharpCodeProvider())
            {
                var typesList = new TypesMapItem[]
                {
                    GetBuiltInTypes(Type.GetType("System.Boolean"), GetPredefineType(SyntaxKind.BoolKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Byte"), GetPredefineType(SyntaxKind.ByteKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.SByte"), GetPredefineType(SyntaxKind.SByteKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Char"), GetPredefineType(SyntaxKind.CharKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Decimal"), GetPredefineType(SyntaxKind.DecimalKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Double"), GetPredefineType(SyntaxKind.DoubleKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Single"), GetPredefineType(SyntaxKind.FloatKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Int32"), GetPredefineType(SyntaxKind.IntKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.UInt32"), GetPredefineType(SyntaxKind.UIntKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Int64"), GetPredefineType(SyntaxKind.LongKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.UInt64"), GetPredefineType(SyntaxKind.ULongKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Object"), GetPredefineType(SyntaxKind.ObjectKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.Int16"), GetPredefineType(SyntaxKind.ShortKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.UInt16"), GetPredefineType(SyntaxKind.UShortKeyword), provider),
                    GetBuiltInTypes(Type.GetType("System.String"), GetPredefineType(SyntaxKind.StringKeyword), provider),
                };

                foreach (var item in typesList)
                {
                    output.Add(item.Name, item);
                    output.Add(item.FullName, item);
                }

                return(BuiltInTypesDic = output);
            }
        }