Ejemplo n.º 1
0
 public override SyntaxNode VisitEqualsValueClause(EqualsValueClauseSyntax node)
 {
     if (node.Value.Kind().Equals(SyntaxKind.ParenthesizedExpression))
     {
         NumberOperations ne = new NumberOperations();
         ParenthesizedExpressionSyntax pe = (ParenthesizedExpressionSyntax)node.Value;
         ExpressionSyntax        es       = SyntaxFactory.ParseExpression(ne.ExpressionParser(pe.Expression.ToString()).ToString());
         EqualsValueClauseSyntax newNode  = node.WithValue(es);
         return(base.VisitEqualsValueClause(node.ReplaceNode(node, newNode)));
     }
     return(base.VisitEqualsValueClause(node));
 }
Ejemplo n.º 2
0
 public override SyntaxNode VisitEqualsValueClause(EqualsValueClauseSyntax node)
 {
     if (node.Value.Kind().Equals(SyntaxKind.NumericLiteralExpression))
     {
         NumberOperations        ne      = new NumberOperations();
         LiteralExpressionSyntax les     = (LiteralExpressionSyntax)node.Value;
         ExpressionSyntax        es      = SyntaxFactory.ParseExpression(ne.UnwrapNumber(les.Token.ValueText));
         EqualsValueClauseSyntax newNode = node.WithValue(es);
         return(base.VisitEqualsValueClause(node.ReplaceNode(node, newNode)));
     }
     return(base.VisitEqualsValueClause(node));
 }
Ejemplo n.º 3
0
        protected override LocalDeclarationStatementSyntax CreateNewStatement()
        {
            ConditionalExpressionSyntax conditionalExpression = IfRefactoringHelper.CreateConditionalExpression(IfStatement.Condition, WhenTrue, WhenFalse);

            VariableDeclaratorSyntax declarator = Statement.Declaration.Variables[0];

            EqualsValueClauseSyntax initializer = declarator.Initializer;

            EqualsValueClauseSyntax newInitializer = (initializer != null)
                ? initializer.WithValue(conditionalExpression)
                : EqualsValueClause(conditionalExpression);

            return(Statement.ReplaceNode(declarator, declarator.WithInitializer(newInitializer)));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Normalizes the <paramref name="initializer" />.
        /// </summary>
        public override SyntaxNode VisitEqualsValueClause(EqualsValueClauseSyntax initializer)
        {
            var typeInfo = SemanticModel.GetTypeInfo(initializer.Value);

            if (typeInfo.Type == null || typeInfo.Type.Equals(typeInfo.ConvertedType))
            {
                return(base.VisitEqualsValueClause(initializer));
            }

            if (!IsFormulaType(typeInfo.ConvertedType))
            {
                return(base.VisitEqualsValueClause(initializer));
            }

            return(initializer.WithValue(CreateInvocation(initializer.Value)));
        }
Ejemplo n.º 5
0
        public static Task <Document> RefactorAsync(Document document, IfAnalysis ifAnalysis, CancellationToken cancellationToken = default)
        {
            switch (ifAnalysis.Kind)
            {
            case IfAnalysisKind.IfElseToAssignmentWithCoalesceExpression:
            {
                return(IfElseToAssignmentWithCoalesceExpressionAsync(document, (IfElseToAssignmentWithCoalesceExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToAssignmentWithConditionalExpression:
            {
                return(IfElseToAssignmentWithConditionalExpressionAsync(document, (IfElseToAssignmentWithConditionalExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.AssignmentAndIfToAssignmentWithConditionalExpression:
            {
                var analysis = (AssignmentAndIfToAssignmentWithConditionalExpressionAnalysis)ifAnalysis;

                ConditionalExpressionSyntax conditionalExpression = CreateConditionalExpression(analysis.IfStatement.Condition, analysis.WhenTrue, analysis.WhenFalse);

                ExpressionStatementSyntax newStatement = analysis.Statement.ReplaceNode(analysis.Right, conditionalExpression);

                return(ToAssignmentWithConditionalExpressionAsync(document, analysis, newStatement, cancellationToken));
            }

            case IfAnalysisKind.LocalDeclarationAndIfElseAssignmentWithConditionalExpression:
            {
                var analysis = (LocalDeclarationAndIfElseToAssignmentWithConditionalExpressionAnalysis)ifAnalysis;

                ConditionalExpressionSyntax conditionalExpression = CreateConditionalExpression(analysis.IfStatement.Condition, analysis.WhenTrue, analysis.WhenFalse);

                VariableDeclaratorSyntax declarator = analysis.Statement.Declaration.Variables[0];

                EqualsValueClauseSyntax initializer = declarator.Initializer;

                EqualsValueClauseSyntax newInitializer = (initializer != null)
                            ? initializer.WithValue(conditionalExpression)
                            : EqualsValueClause(conditionalExpression);

                LocalDeclarationStatementSyntax newStatement = analysis.Statement.ReplaceNode(declarator, declarator.WithInitializer(newInitializer));

                return(ToAssignmentWithConditionalExpressionAsync(document, analysis, newStatement, cancellationToken));
            }

            case IfAnalysisKind.IfElseToAssignmentWithExpression:
            {
                return(IfElseToAssignmentWithExpressionAsync(document, (IfElseToAssignmentWithExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToAssignmentWithCondition:
            {
                return(IfElseToAssignmentWithConditionAsync(document, (IfElseToAssignmentWithConditionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToReturnWithCoalesceExpression:
            case IfAnalysisKind.IfElseToYieldReturnWithCoalesceExpression:
            case IfAnalysisKind.IfReturnToReturnWithCoalesceExpression:
            {
                return(IfToReturnWithCoalesceExpressionAsync(document, (IfToReturnWithCoalesceExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToReturnWithConditionalExpression:
            {
                return(IfElseToReturnWithConditionalExpressionAsync(document, (IfElseToReturnWithConditionalExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToReturnWithBooleanExpression:
            {
                return(IfElseToReturnWithBooleanExpressionAsync(document, (IfElseToReturnWithBooleanExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToReturnWithExpression:
            case IfAnalysisKind.IfElseToYieldReturnWithExpression:
            case IfAnalysisKind.IfReturnToReturnWithExpression:
            {
                return(IfToReturnWithExpressionAsync(document, (IfToReturnWithExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToYieldReturnWithConditionalExpression:
            {
                return(IfElseToYieldReturnWithConditionalExpressionAsync(document, (IfElseToYieldReturnWithConditionalExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfElseToYieldReturnWithBooleanExpression:
            {
                return(IfElseToYieldReturnWithBooleanExpressionAsync(document, (IfElseToYieldReturnWithBooleanExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfReturnToReturnWithConditionalExpression:
            {
                return(IfReturnToReturnWithConditionalExpressionAsync(document, (IfReturnToReturnWithConditionalExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            case IfAnalysisKind.IfReturnToReturnWithBooleanExpression:
            {
                return(IfReturnToReturnWithBooleanExpressionAsync(document, (IfReturnToReturnWithBooleanExpressionAnalysis)ifAnalysis, cancellationToken));
            }

            default:
            {
                throw new InvalidOperationException();
            }
            }
        }