Helper for accessing member access expressions in AST.
Inheritance: Helper
Ejemplo n.º 1
0
        private static ITranslationUnit BuildMemberAccessExpressionTranslationUnit(MemberAccessExpressionSyntax expression, SemanticModel semanticModel)
        {
            var thisExpression = expression.Expression as ThisExpressionSyntax;
            var baseExpression = expression.Expression as BaseExpressionSyntax;

            var helper = new MemberAccessExpression(expression, semanticModel);

            if (thisExpression != null)
            {
                return(MemberAccessExpressionTranslationUnit.Create(
                           IdentifierTranslationUnit.Create(helper.MemberName),
                           MemberAccessExpressionTranslationUnit.MemberAccessMethod.This));
            }

            if (baseExpression != null)
            {
                return(MemberAccessExpressionTranslationUnit.Create(
                           IdentifierTranslationUnit.Create(helper.MemberName),
                           MemberAccessExpressionTranslationUnit.MemberAccessMethod.Base));
            }

            throw new InvalidOperationException("Cannot build a member access expression as it is not a `this` expression, nor a `base` expression!");
        }
Ejemplo n.º 2
0
        private static ITranslationUnit BuildMemberAccessExpressionTranslationUnit(MemberAccessExpressionSyntax expression, SemanticModel semanticModel)
        {
            var helper = new MemberAccessExpression(expression, semanticModel);

            //translate these Knockout-specific statements
            if ("SetValue".Equals(helper.MemberName) ||
                "GetValue".Equals(helper.MemberName) ||
                "SetItems".Equals(helper.MemberName) ||
                "GetItems".Equals(helper.MemberName))
            {
                return(new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build());
            }

            if (expression.Expression is ThisExpressionSyntax)
            {
                return(MemberAccessExpressionTranslationUnit.Create(
                           IdentifierTranslationUnit.Create(helper.MemberName),
                           MemberAccessExpressionTranslationUnit.MemberAccessMethod.This));
            }
            else if (expression.Expression is BaseExpressionSyntax)
            {
                return(MemberAccessExpressionTranslationUnit.Create(
                           IdentifierTranslationUnit.Create(helper.MemberName),
                           MemberAccessExpressionTranslationUnit.MemberAccessMethod.Base));
            }
            else if (expression.Expression is IdentifierNameSyntax)
            {
                // The target is a simple identifier, the code being analysed is of the form
                // "command.ExecuteReader()" and memberAccess.Expression is the "command"
                // node
                return(MemberAccessExpressionTranslationUnit.Create(
                           new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build(),
                           IdentifierTranslationUnit.Create(helper.MemberName)));
            }
            else if (expression.Expression is PredefinedTypeSyntax)
            {
                // The target is a simple identifier, the code being analysed is of the form
                // "command.ExecuteReader()" and memberAccess.Expression is the "command"
                // node
                return(MemberAccessExpressionTranslationUnit.Create(
                           new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build(),
                           IdentifierTranslationUnit.Create(helper.MemberName)));
            }
            else if (expression.Expression is InvocationExpressionSyntax)
            {
                // The target is another invocation, the code being analysed is of the form
                // "GetCommand().ExecuteReader()" and memberAccess.Expression is the
                // "GetCommand()" node
                return(MemberAccessExpressionTranslationUnit.Create(
                           new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build(),
                           IdentifierTranslationUnit.Create(helper.MemberName)));
            }
            else if (expression.Expression is MemberAccessExpressionSyntax)
            {
                // The target is a member access, the code being analysed is of the form
                // "x.Command.ExecuteReader()" and memberAccess.Expression is the "x.Command"
                // node
                return(MemberAccessExpressionTranslationUnit.Create(
                           new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build(),
                           IdentifierTranslationUnit.Create(helper.MemberName)));
            }
            else if (expression.Expression is ElementAccessExpressionSyntax)
            {
                return(BuildElementAccessExpressionTranslationUnit((ElementAccessExpressionSyntax)expression.Expression, semanticModel));
            }
            else if (expression.Expression is ParenthesizedExpressionSyntax)
            {
                return(MemberAccessExpressionTranslationUnit.Create(
                           new ExpressionTranslationUnitBuilder(expression.Expression, semanticModel).Build(),
                           IdentifierTranslationUnit.Create(helper.MemberName)));
            }

            return(MemberAccessExpressionTranslationUnit.Create(
                       IdentifierTranslationUnit.Create(helper.MemberName),
                       MemberAccessExpressionTranslationUnit.MemberAccessMethod.This));
        }