Example #1
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());
            }
Example #2
0
        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;
                    }
                    var symbol = ProjectItemDetails.SemanticModel.GetSymbolInfo(oldNode1).Symbol;
                    if (symbol != null && symbol.Kind != SymbolKind.NamedType)
                    {
                        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());
            }
                       ));
        }
Example #3
0
            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;
                        }
                        var symbol = SemanticModel.GetSymbolInfo(oldNode1).Symbol;
                        if (symbol != null && symbol.Kind != SymbolKind.NamedType)
                        {
                            return oldNode1;
                        }
                    }
                    else if (oldNode1 is IdentifierNameSyntax == false && oldNode1 is QualifiedNameSyntax == false)
                    {
                        return oldNode1;
                    }
                    else
                    {
                        var symbol = SemanticModel.GetSymbolInfo(oldNode1).Symbol;
                        if (symbol != null && symbol.Kind != SymbolKind.NamedType)
                        {
                            return oldNode1;
                        }
                    }

                    var lineSpan = oldNode1.GetFileLinePosSpan();

                    AddReport(new ChangesReport(oldNode1)
                    {
                        LineNumber = lineSpan.StartLinePosition.Line,
                        Column = lineSpan.StartLinePosition.Character,
                        Message = "Convert full name types to built in Types",
                        Generator = nameof(ConvertFullNameTypesToBuiltInTypes)
                    });

                    return
                    builtInTypesMapDic[oldNode1.WithoutTrivia().ToFullString()]
                    .NewNode
                    .WithLeadingTrivia(oldNode1.GetLeadingTrivia())
                    .WithTrailingTrivia(oldNode1.GetTrailingTrivia());
                }
                           ));
            }
Example #4
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
                (
                    !CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.Remove_Class_Fields_Initializer_Null) &&
                    !CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.Remove_Class_Fields_Initializer_Literal)
                )
                {
                    return(base.VisitVariableDeclarator(node));
                }

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

                    if (TypesMapItem.GetAllPredefinedTypesDic().ContainsKey(variableTypeNode))
                    {
                        if (CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.Remove_Class_Fields_Initializer_Literal) == false)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }

                        var typeItem = TypesMapItem.GetAllPredefinedTypesDic()[variableTypeNode];

                        if ((typeItem.DefaultValue == null && valueObj != null) || (typeItem.DefaultValue != null && !typeItem.DefaultValue.Equals(valueObj)))
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }
                    else
                    {
                        if (CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.Remove_Class_Fields_Initializer_Null) == false)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                        if (valueObj != null)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }

                    node = node.WithInitializer(null).WithoutTrailingTrivia();
                }

                return(base.VisitVariableDeclarator(node));
            }
            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
                (
                    !CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.RemoveClassFieldsInitializerNull) &&
                    !CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.RemoveClassFieldsInitializerLiteral)
                )
                {
                    return(base.VisitVariableDeclarator(node));
                }

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

                    if (TypesMapItem.GetAllPredefinedTypesDic().ContainsKey(variableTypeNode))
                    {
                        if (CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.RemoveClassFieldsInitializerLiteral) == false)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }

                        var typeItem = TypesMapItem.GetAllPredefinedTypesDic()[variableTypeNode];

                        if ((typeItem.DefaultValue == null && valueObj != null) || (typeItem.DefaultValue != null && !typeItem.DefaultValue.Equals(valueObj)))
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }
                    else
                    {
                        if (CheckOption((int)SimplifyClassFieldDeclaration.CleanupTypes.RemoveClassFieldsInitializerNull) == false)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                        if (valueObj != null)
                        {
                            return(base.VisitVariableDeclarator(node));
                        }
                    }

                    if (isReportOnlyMode)
                    {
                        var lineSpan = node.GetFileLinePosSpan();

                        AddReport(new ChangesReport(node)
                        {
                            LineNumber = lineSpan.StartLinePosition.Line,
                            Column     = lineSpan.StartLinePosition.Character,
                            Message    = "Field initialize with \"= null;\" or \"= 0;\" can be removed",
                            Generator  = nameof(SimplifyClassFieldDeclarations)
                        });
                    }

                    node = node.WithInitializer(null).WithoutTrailingTrivia();
                }

                return(base.VisitVariableDeclarator(node));
            }