Exemplo n.º 1
0
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var kind = ConvertToken(node.Kind(), TokenContext.Local);
                ExpressionSyntax Left;
                ExpressionSyntax Right;

                if (node.Left.IsKind(VBasic.SyntaxKind.InvocationExpression))
                {
                    Left = (ExpressionSyntax)nodesVisitor.IndexedAccess((VBSyntax.InvocationExpressionSyntax)node.Left);
                }
                else if (node.Left.IsKind(VBasic.SyntaxKind.IdentifierName))
                {
                    var LeftID           = (VBSyntax.IdentifierNameSyntax)node.Left;
                    var symbol           = semanticModel.GetSymbolInfo(LeftID).Symbol;
                    var symbolReturnType = symbol?.GetReturnType();
                    Left = (ExpressionSyntax)node.Left.Accept(nodesVisitor);
                }
                else
                {
                    Left = (ExpressionSyntax)node.Left.Accept(nodesVisitor);
                }


                Right = (ExpressionSyntax)node.Right.Accept(nodesVisitor);

                var LeftType  = semanticModel.GetSymbolInfo(node.Left).Symbol?.GetReturnType();
                var RightType = semanticModel.GetSymbolInfo(node.Right).Symbol?.GetReturnType();

                if (LeftType != null && RightType != null && LeftType != RightType)
                {
                    Right = SyntaxFactory.CastExpression(SyntaxFactory.ParseTypeName(LeftType.Name), Right);
                }

                return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, Left, Right)));
            }
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var lhs        = (ExpressionSyntax)node.Left.Accept(_nodesVisitor);
                var lOperation = _semanticModel.GetOperation(node.Left);

                //Already dealt with by call to the same method in VisitInvocationExpression
                if (CommonConversions.GetParameterizedPropertyAccessMethod(lOperation, out var _) != null)
                {
                    return(SingleStatement(lhs));
                }
                var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor);

                if (node.Left is VBSyntax.IdentifierNameSyntax id &&
                    _methodNode is VBSyntax.MethodBlockSyntax mb &&
                    HasReturnVariable &&
                    id.Identifier.ValueText.Equals(mb.SubOrFunctionStatement.Identifier.ValueText, StringComparison.OrdinalIgnoreCase))
                {
                    lhs = ReturnVariable;
                }

                if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement))
                {
                    rhs = SyntaxFactory.InvocationExpression(
                        SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"),
                        ExpressionSyntaxExtensions.CreateArgList(lhs, rhs));
                }
                var kind = node.Kind().ConvertToken(TokenContext.Local);

                var assignment = SyntaxFactory.AssignmentExpression(kind, lhs, rhs);

                var postAssignment = GetPostAssignmentStatements(node);

                return(postAssignment.Insert(0, SyntaxFactory.ExpressionStatement(assignment)));
            }
Exemplo n.º 3
0
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor);
                var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor);

                if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement))
                {
                    rhs = SyntaxFactory.InvocationExpression(
                        SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"),
                        ExpressionSyntaxExtensions.CreateArgList(lhs, rhs));
                }
                var kind = node.Kind().ConvertToken(TokenContext.Local);

                return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs)));
            }
        private static void VisitAssignmentExpression(SyntaxNodeAnalysisContext ctx)
        {
            SyntaxNode n, right, left;

            if (ctx.Node.Language == LanguageNames.CSharp)
            {
                CSharpSyntax.AssignmentExpressionSyntax node = ctx.Node as CSharpSyntax.AssignmentExpressionSyntax;
                n     = node;
                right = node.Right;
                left  = node.Left;
            }
            else
            {
                if (ctx.Node is VBSyntax.AssignmentStatementSyntax)
                {
                    VBSyntax.AssignmentStatementSyntax node = ctx.Node as VBSyntax.AssignmentStatementSyntax;
                    n     = node;
                    right = node.Right;
                    left  = node.Left;
                }
                else
                {
                    VBSyntax.NamedFieldInitializerSyntax node = ctx.Node as VBSyntax.NamedFieldInitializerSyntax;
                    n     = node;
                    right = node.Expression;
                    left  = node.Name;
                }
            }



            var symbol = ctx.SemanticModel.GetSymbolInfo(left).Symbol;

            var content = right.GetText().ToString();

            // Only if it is the RequiredLength property of a PasswordValidator
            if (AnalyzerUtil.SymbolMatch(symbol, type: "PasswordValidator", name: "RequiredLength") && content != String.Empty)
            {
                int numericValue;
                // Validates that the value is an int and that it is over the minimum value required
                if (int.TryParse(right.GetText().ToString(), out numericValue) && numericValue < Constants.PasswordValidatorRequiredLength)
                {
                    var diagnostic = Diagnostic.Create(RulePasswordLength, n.GetLocation());
                    ctx.ReportDiagnostic(diagnostic);
                }
            }
        }
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor);
                var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor);

                if (node.Left is VBSyntax.IdentifierNameSyntax id &&
                    _methodNode is VBSyntax.MethodBlockSyntax mb &&
                    HasReturnVariable &&
                    id.Identifier.ValueText.Equals(mb.SubOrFunctionStatement.Identifier.ValueText, StringComparison.OrdinalIgnoreCase))
                {
                    lhs = ReturnVariable;
                }

                if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement))
                {
                    rhs = SyntaxFactory.InvocationExpression(
                        SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"),
                        ExpressionSyntaxExtensions.CreateArgList(lhs, rhs));
                }
                var kind = node.Kind().ConvertToken(TokenContext.Local);

                return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs)));
            }
Exemplo n.º 6
0
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var lhs = (ExpressionSyntax)node.Left.Accept(_nodesVisitor);
                var rhs = (ExpressionSyntax)node.Right.Accept(_nodesVisitor);

                // e.g. VB DivideAssignmentExpression "/=" is always on doubles unless you use the "\=" IntegerDivideAssignmentExpression, so need to cast in C#
                // Need the unconverted type, since the whole point is that it gets converted to a double by the operator
                if (node.IsKind(VBasic.SyntaxKind.DivideAssignmentStatement) && !node.HasOperandOfUnconvertedType("System.Double", _semanticModel))
                {
                    var doubleType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword));
                    rhs = SyntaxFactory.CastExpression(doubleType, rhs);
                }

                if (node.IsKind(VBasic.SyntaxKind.ExponentiateAssignmentStatement))
                {
                    rhs = SyntaxFactory.InvocationExpression(
                        SyntaxFactory.ParseExpression($"{nameof(Math)}.{nameof(Math.Pow)}"),
                        ExpressionSyntaxExtensions.CreateArgList(lhs, rhs));
                }
                var kind = node.Kind().ConvertToken(TokenContext.Local);

                return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, lhs, rhs)));
            }
Exemplo n.º 7
0
 public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node) => AddLocalVariables(node);
            public override SyntaxList <StatementSyntax> VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node)
            {
                var kind = ConvertToken(node.Kind(), TokenContext.Local);

                return(SingleStatement(SyntaxFactory.AssignmentExpression(kind, (ExpressionSyntax)node.Left.Accept(nodesVisitor), (ExpressionSyntax)node.Right.Accept(nodesVisitor))));
            }
            private SyntaxList <StatementSyntax> GetPostAssignmentStatements(VBSyntax.AssignmentStatementSyntax node)
            {
                var potentialPropertySymbol = _semanticModel.GetSymbolInfo(node.Left).ExtractBestMatch();

                return(_methodsWithHandles.GetPostAssignmentStatements(node, potentialPropertySymbol));
            }
Exemplo n.º 10
0
 public override Task <SyntaxList <StatementSyntax> > VisitAssignmentStatement(VBSyntax.AssignmentStatementSyntax node) => AddLocalVariablesAsync(node);