예제 #1
0
        private static bool CheckForSimpleLocalDeclarationPattern(
            AnonymousFunctionExpressionSyntax anonymousFunction,
            out LocalDeclarationStatementSyntax localDeclaration
            )
        {
            // Type t = <anonymous function>
            if (
                anonymousFunction.IsParentKind(SyntaxKind.EqualsValueClause) &&
                anonymousFunction.Parent.IsParentKind(SyntaxKind.VariableDeclarator) &&
                anonymousFunction.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclaration) &&
                anonymousFunction.Parent.Parent.Parent.IsParentKind(
                    SyntaxKind.LocalDeclarationStatement,
                    out localDeclaration
                    )
                )
            {
                if (!localDeclaration.Declaration.Type.IsVar)
                {
                    return(true);
                }
            }

            localDeclaration = null;
            return(false);
        }
        private bool CheckForLocalDeclarationAndAssignment(
            SemanticModel semanticModel,
            AnonymousFunctionExpressionSyntax anonymousFunction,
            CancellationToken cancellationToken,
            out LocalDeclarationStatementSyntax localDeclaration)
        {
            // Type t = null;
            // t = <anonymous function>
            if (anonymousFunction.IsParentKind(SyntaxKind.SimpleAssignmentExpression) &&
                anonymousFunction.Parent.IsParentKind(SyntaxKind.ExpressionStatement) &&
                anonymousFunction.Parent.Parent.IsParentKind(SyntaxKind.Block))
            {
                var assignment = (AssignmentExpressionSyntax)anonymousFunction.Parent;
                if (assignment.Left.IsKind(SyntaxKind.IdentifierName))
                {
                    var expressionStatement = (ExpressionStatementSyntax)assignment.Parent;
                    var block = (BlockSyntax)expressionStatement.Parent;
                    var expressionStatementIndex = block.Statements.IndexOf(expressionStatement);
                    if (expressionStatementIndex >= 1)
                    {
                        var previousStatement = block.Statements[expressionStatementIndex - 1];
                        if (previousStatement.IsKind(SyntaxKind.LocalDeclarationStatement))
                        {
                            localDeclaration = (LocalDeclarationStatementSyntax)previousStatement;
                            if (localDeclaration.Declaration.Variables.Count == 1)
                            {
                                var variableDeclarator = localDeclaration.Declaration.Variables[0];
                                if (variableDeclarator.Initializer != null)
                                {
                                    var value = variableDeclarator.Initializer.Value;
                                    if (value.IsKind(SyntaxKind.NullLiteralExpression) ||
                                        value.IsKind(SyntaxKind.DefaultLiteralExpression) ||
                                        value.IsKind(SyntaxKind.DefaultExpression))
                                    {
                                        var identifierName = (IdentifierNameSyntax)assignment.Left;
                                        if (variableDeclarator.Identifier.ValueText == identifierName.Identifier.ValueText)
                                        {
                                            return(true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            localDeclaration = null;
            return(false);
        }
        private bool CheckForSimpleLocalDeclarationPattern(
            SemanticModel semanticModel,
            AnonymousFunctionExpressionSyntax anonymousFunction,
            CancellationToken cancellationToken,
            out LocalDeclarationStatementSyntax localDeclaration)
        {
            // Type t = <anonymous function>
            if (anonymousFunction.IsParentKind(SyntaxKind.EqualsValueClause) &&
                anonymousFunction.Parent.IsParentKind(SyntaxKind.VariableDeclarator) &&
                anonymousFunction.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclaration) &&
                anonymousFunction.Parent.Parent.Parent.IsParentKind(SyntaxKind.LocalDeclarationStatement))
            {
                localDeclaration = (LocalDeclarationStatementSyntax)anonymousFunction.Parent.Parent.Parent.Parent;
                if (!localDeclaration.Declaration.Type.IsVar)
                {
                    return(true);
                }
            }

            localDeclaration = null;
            return(false);
        }