public void GetInheritedPanelObjectFromFieldExpression()
        {
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression("self.panel1.Name = \"abc\"");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(assignment.Left as AttributeAccess);

            Assert.AreEqual(DerivedForm.GetPanel(), field.GetObject(ComponentCreator));
        }
コード例 #2
0
        private static ImmutableArray <IfRefactoring> Analyze(
            ExpressionStatementSyntax expressionStatement,
            IfStatementSyntax ifStatement,
            IfAnalysisOptions options)
        {
            SimpleAssignmentExpression assignment;

            if (SimpleAssignmentExpression.TryCreate(expressionStatement, out assignment))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false)
                {
                    SimpleAssignmentExpression assignment1;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1))
                    {
                        SimpleAssignmentExpression assignment2;
                        if (SimpleAssignmentExpression.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) &&
                            assignment1.Left.IsEquivalentTo(assignment2.Left, topLevel: false) &&
                            assignment1.Left.IsEquivalentTo(assignment.Left, topLevel: false) &&
                            options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(expressionStatement.SpanStart, ifStatement.Span.End)))
                        {
                            return(new AssignmentAndIfElseToAssignmentWithConditionalExpression(expressionStatement, assignment.Right, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray());
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
コード例 #3
0
        public void LocalVariableIsNotSelfReference()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("listViewItem1.TooltipText = \"abc\"");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            Assert.IsFalse(field.IsSelfReference);
        }
コード例 #4
0
        public void PrivateClassVariableIsSelfReference()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("@listView1.TooltipText = \"abc\"");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            Assert.IsTrue(field.IsSelfReference);
        }
コード例 #5
0
        public void NullPassedToRubyControlFieldExpressionEquals()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("self.textBox1.Name = \"abc\"");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            Assert.IsFalse(field.Equals(null));
        }
コード例 #6
0
        protected override bool IsFixableStatement(
            StatementSyntax statement,
            string name,
            ITypeSymbol typeSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (statement.SpanOrLeadingTriviaContainsDirectives())
            {
                return(false);
            }

            if (!(statement is ExpressionStatementSyntax expressionStatement))
            {
                return(false);
            }

            if (!SimpleAssignmentExpression.TryCreate(expressionStatement.Expression, out SimpleAssignmentExpression simpleAssignment))
            {
                return(false);
            }

            if (name != (simpleAssignment.Left as IdentifierNameSyntax)?.Identifier.ValueText)
            {
                return(false);
            }

            if (!MemberInvocationExpression.TryCreate(simpleAssignment.Right, out MemberInvocationExpression memberInvocation))
            {
                return(false);
            }

            if (!(WalkDownMethodChain(memberInvocation).Expression is IdentifierNameSyntax identifierName))
            {
                return(false);
            }

            if (name != identifierName.Identifier.ValueText)
            {
                return(false);
            }

            if (!semanticModel.TryGetMethodInfo(memberInvocation.InvocationExpression, out MethodInfo methodInfo, cancellationToken))
            {
                return(false);
            }

            if (!methodInfo.ReturnType.Equals(typeSymbol))
            {
                return(false);
            }

            if (IsReferenced(memberInvocation.InvocationExpression, identifierName, name, semanticModel, cancellationToken))
            {
                return(false);
            }

            return(true);
        }
コード例 #7
0
        public void GetMemberNamesForButtonPropertyReference()
        {
            string[] expected = new string[] { "@button1", "Location" };
            string   code     = "@button1.Location = System::Drawing::Point.new(0, 0)";
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(code);

            Assert.AreEqual(expected, RubyControlFieldExpression.GetMemberNames(assignment.Left as AttributeAccess));
        }
コード例 #8
0
        public void GetMemberNamesForFormClientSizePropertyReference()
        {
            string[] expected = new string[] { "self", "ClientSize" };
            string   code     = "self.ClientSize = System::Drawing::Size.new(300, 400)";
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(code);

            Assert.AreEqual(expected, RubyControlFieldExpression.GetMemberNames(assignment.Left as AttributeAccess));
        }
コード例 #9
0
        public void GetMemberNamesForColorReference()
        {
            string[] expected = new string[] { "System", "Drawing", "Color", "Red" };
            string   code     = "self.BackColor = System::Drawing::Color.Red";
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(code);

            Assert.AreEqual(expected, RubyControlFieldExpression.GetMemberNames(assignment.Right as MethodCall));
        }
コード例 #10
0
        public void GetMemberNames()
        {
            string[] expected = new string[] { "a", "b" };
            string   code     = "a.b = 0";
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(code);

            Assert.AreEqual(expected, RubyControlFieldExpression.GetMemberNames(assignment.Left as AttributeAccess));
        }
コード例 #11
0
        public void GetInvalidTwoLevelDeepButtonPropertyDescriptorForSelfReference()
        {
            using (Button button = new Button()) {
                SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("self.button1.InvalidProperty.BorderSize = 3");
                RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

                Assert.IsNull(field.GetObjectForMemberName(button));
            }
        }
コード例 #12
0
        public void GetButtonFlatAppearanceObject()
        {
            using (Button button = new Button()) {
                SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("@button1.FlatAppearance.BorderSize = 3");
                RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

                Assert.AreEqual(button.FlatAppearance, field.GetObjectForMemberName(button));
            }
        }
コード例 #13
0
        public void GetButtonObject()
        {
            using (Button button = new Button()) {
                SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("@button1.Size = System::Drawing::Size.new(10, 10)");
                RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

                Assert.AreEqual(button, field.GetObjectForMemberName(button));
            }
        }
コード例 #14
0
        public void LocalVariableCreatingNewInstance()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("listViewItem1 = System::Windows::Forms.ListViewItem.new()");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as LocalVariable);

            RubyControlFieldExpression expectedField = new RubyControlFieldExpression(String.Empty, "listViewItem1", String.Empty, "listViewItem1");

            Assert.AreEqual(expectedField, field);
        }
コード例 #15
0
        public void LocalVariableInAssignment()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("listViewItem1.TooltipText = \"abc\"");
            RubyControlFieldExpression field      = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            RubyControlFieldExpression expectedField = new RubyControlFieldExpression("TooltipText", "listViewItem1", String.Empty, "listViewItem1.TooltipText");

            Assert.AreEqual(expectedField, field);
        }
コード例 #16
0
        public void RubyControlFieldExpressionEquals()
        {
            SimpleAssignmentExpression expression = RubyParserHelper.GetSimpleAssignmentExpression("self.textBox1.Name = \"abc\"");
            RubyControlFieldExpression field1     = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            expression = RubyParserHelper.GetSimpleAssignmentExpression("self.textBox1.Name = \"def\"");
            RubyControlFieldExpression field2 = RubyControlFieldExpression.Create(expression.Left as AttributeAccess);

            Assert.AreEqual(field1, field2);
        }
コード例 #17
0
        public static void Analyze(SyntaxNodeAnalysisContext context, LocalDeclarationStatementSyntax localDeclaration)
        {
            if (!localDeclaration.IsConst &&
                !localDeclaration.SpanOrTrailingTriviaContainsDirectives())
            {
                VariableDeclarationSyntax declaration = localDeclaration.Declaration;

                if (declaration != null)
                {
                    SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Variables;

                    if (variables.Any())
                    {
                        StatementSyntax nextStatement = localDeclaration.NextStatement();

                        if (nextStatement?.SpanOrLeadingTriviaContainsDirectives() == false)
                        {
                            SimpleAssignmentExpression assignment;
                            if (SimpleAssignmentExpression.TryCreate(nextStatement, out assignment) &&
                                assignment.Left.IsKind(SyntaxKind.IdentifierName))
                            {
                                SemanticModel     semanticModel     = context.SemanticModel;
                                CancellationToken cancellationToken = context.CancellationToken;

                                VariableDeclaratorSyntax declarator = FindInitializedVariable((IdentifierNameSyntax)assignment.Left, variables, semanticModel, cancellationToken);

                                if (declarator != null)
                                {
                                    EqualsValueClauseSyntax initializer = declarator.Initializer;
                                    ExpressionSyntax        value       = initializer?.Value;

                                    if (value == null ||
                                        IsDefaultValue(declaration.Type, value, semanticModel, cancellationToken))
                                    {
                                        context.ReportDiagnostic(DiagnosticDescriptors.MergeLocalDeclarationWithAssignment, declarator.Identifier);

                                        if (value != null)
                                        {
                                            context.ReportNode(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, initializer);
                                            context.ReportToken(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, assignment.Expression.OperatorToken);
                                        }

                                        context.ReportToken(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, localDeclaration.SemicolonToken);
                                        context.ReportNode(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, assignment.Left);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #18
0
        public void SetUpFixture()
        {
            componentCreator = new MockComponentCreator();

            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(GetRubyCode());

            rhsAssignmentExpression = assignment.Right;

            mockDesignerLoaderHost = new MockDesignerLoaderHost();
            typeResolutionService  = mockDesignerLoaderHost.TypeResolutionService;
            RubyCodeDeserializer deserializer = new RubyCodeDeserializer(componentCreator);

            deserializedObject = deserializer.Deserialize(rhsAssignmentExpression);
        }
コード例 #19
0
        public void EnumReturnedInArgumentsPassedToConstructor()
        {
            string rubyCode = "self.Font = System::Drawing::Font.new(\"Times New Roman\", System::Drawing::FontStyle.Regular, System::Drawing::GraphicsUnit.Point)";
            SimpleAssignmentExpression assignment = RubyParserHelper.GetSimpleAssignmentExpression(rubyCode);

            List <object> expectedArgs = new List <object>();

            expectedArgs.Add("Times New Roman");
            expectedArgs.Add(FontStyle.Regular);
            expectedArgs.Add(GraphicsUnit.Point);

            List <object> args = deserializer.GetArguments(assignment.Right as MethodCall);

            Assert.AreEqual(expectedArgs, args);
        }
コード例 #20
0
        public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsSimpleIf() &&
                !IsPartOfLazyInitialization(ifStatement))
            {
                EqualsToNullExpression equalsToNull;
                if (EqualsToNullExpression.TryCreate(ifStatement.Condition, out equalsToNull))
                {
                    SimpleAssignmentExpression assignment;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment) &&
                        assignment.Left.IsEquivalentTo(equalsToNull.Left, topLevel: false) &&
                        assignment.Right.IsSingleLine() &&
                        !ifStatement.SpanContainsDirectives())
                    {
                        StatementSyntax fixableStatement = ifStatement;

                        SyntaxList <StatementSyntax> statements;
                        if (ifStatement.TryGetContainingList(out statements))
                        {
                            int index = statements.IndexOf(ifStatement);

                            if (index > 0)
                            {
                                StatementSyntax previousStatement = statements[index - 1];

                                if (CanRefactor(previousStatement, ifStatement, equalsToNull.Left, ifStatement.Parent))
                                {
                                    fixableStatement = previousStatement;
                                }
                            }
                        }

                        context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, fixableStatement);
                    }
                }
            }
        }
コード例 #21
0
        void WalkSimpleAssignment(SimpleAssignmentExpression node)
        {
            AttributeAccess  attributeAccess = node.Left as AttributeAccess;
            InstanceVariable instance        = node.Left as InstanceVariable;
            LocalVariable    localVariable   = node.Left as LocalVariable;

            if (attributeAccess != null)
            {
                RubyControlFieldExpression field = RubyControlFieldExpression.Create(attributeAccess);
                object propertyValue             = deserializer.Deserialize(node.Right);
                if (propertyValue != null)
                {
                    field.SetPropertyValue(componentCreator, propertyValue);
                }
                else if (IsResource(node.Right))
                {
                    field.SetPropertyValue(componentCreator, GetResource(node.Right as MethodCall));
                }
                else
                {
                    ThrowCouldNotFindTypeException(node.Right);
                }
            }
            else if (instance != null)
            {
                string instanceName  = RubyControlFieldExpression.GetVariableName(instance.Name);
                object propertyValue = deserializer.Deserialize(instanceName, node.Right);
                AddComponent(instanceName, propertyValue);
            }
            else if (localVariable != null)
            {
                object propertyValue = deserializer.Deserialize(localVariable.Name, node.Right);
                if (propertyValue == null)
                {
                    ThrowCouldNotFindTypeException(node.Right);
                }
            }
        }
コード例 #22
0
        private static ImmutableArray <IfRefactoring> Analyze(
            LocalDeclarationStatementSyntax localDeclarationStatement,
            IfStatementSyntax ifStatement,
            IfAnalysisOptions options)
        {
            VariableDeclaratorSyntax declarator = localDeclarationStatement.Declaration?.SingleVariableOrDefault();

            if (declarator != null)
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false)
                {
                    SimpleAssignmentExpression assignment1;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1))
                    {
                        SimpleAssignmentExpression assignment2;
                        if (SimpleAssignmentExpression.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) &&
                            assignment1.Left.IsKind(SyntaxKind.IdentifierName) &&
                            assignment2.Left.IsKind(SyntaxKind.IdentifierName))
                        {
                            string identifier1 = ((IdentifierNameSyntax)assignment1.Left).Identifier.ValueText;
                            string identifier2 = ((IdentifierNameSyntax)assignment2.Left).Identifier.ValueText;

                            if (string.Equals(identifier1, identifier2, StringComparison.Ordinal) &&
                                string.Equals(identifier1, declarator.Identifier.ValueText, StringComparison.Ordinal) &&
                                options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(localDeclarationStatement.SpanStart, ifStatement.Span.End)))
                            {
                                return(new LocalDeclarationAndIfElseAssignmentWithConditionalExpression(localDeclarationStatement, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray());
                            }
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
コード例 #23
0
        public static IExpressionStatement CreateSimpleAssignmentExpressionStatement(IOperation target, IOperation value, SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit)
        {
            var expression = new SimpleAssignmentExpression(target, value, semanticModel, syntax, target.Type, default(Optional <object>), isImplicit);

            return(new ExpressionStatement(expression, semanticModel, syntax, type: null, constantValue: default(Optional <object>), isImplicit: isImplicit));
        }