Ejemplo n.º 1
0
        private static void WriteObjectInitializer(CodeTextBuilder code, ObjectInitializerExpression expression)
        {
            if (expression.PropertyValues == null || expression.PropertyValues.Count == 0)
            {
                code.Write("{ }");
                return;
            }

            code.WriteListStart(opener: "{", closer: "}", separator: ",", newLine: true);

            foreach (var keyValue in expression.PropertyValues)
            {
                code.WriteListItem();
                code.Write(keyValue.Name.GetSealedOrCased(CasingStyle.Camel, sealLanguage: LanguageInfo.Entries.JavaScript()));

                if (keyValue.Value is IAssignable assignable && assignable.Name == keyValue.Name)
                {
                    continue;
                }

                code.Write(": ");
                WriteExpression(code, keyValue.Value);
            }

            code.WriteListEnd();
        }
        /// <summary>
        /// Determines whether the given expression is the left-hand-side literal in any of the assignment expressions
        /// within an object initialize expression.
        /// </summary>
        /// <param name="expression">The expression to check.</param>
        /// <returns>Returns true if the expression is the left-hand-side literal in any of the assignment expressions
        /// within an object initializer expression.</returns>
        /// <remarks>This method checks for the following situation:
        /// class MyClass
        /// {
        ///     public bool Member { get { return true; } }
        ///     public void SomeMethod()
        ///     {
        ///         MyObjectType someObject = new MyObjectType { Member = this.Member };
        ///     }
        /// }
        /// In this case, StyleCop will raise a violation since it looks like the Member token should be prefixed by 'this.', however,
        /// it is actually referring to a property on the MyObjectType type.
        /// </remarks>
        private static bool IsObjectInitializerLeftHandSideExpression(Expression expression)
        {
            Param.AssertNotNull(expression, "expression");

            // The expression should be a literal expression if it represents the keyword being checked.
            if (expression.ExpressionType == ExpressionType.Literal)
            {
                // The literal should be a child-expression of an assignment expression.
                AssignmentExpression assignmentExpression = expression.Parent as AssignmentExpression;
                if (assignmentExpression != null)
                {
                    // The left-hand-side of the assignment expression should be the literal expression.
                    if (assignmentExpression.LeftHandSide == expression)
                    {
                        // The assignment expression should be the child of an object initializer expression.
                        ObjectInitializerExpression objectInitializeExpression = assignmentExpression.Parent as ObjectInitializerExpression;
                        if (objectInitializeExpression != null)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        public override void VisitNamespace <TUsingDirective, TNamespace, TClassCollection, TDelegate, TEnum, TInterface, TStructCollection>(
            INamespace <TUsingDirective, TNamespace, TClassCollection, TDelegate, TEnum, TInterface, TStructCollection> @namespace)
        {
            Namespace properties;
            ObjectInitializerExpression expression = CreateExpression(typeof(Namespace));

            expression.CreateObjectExpression.NewExpression.Parameters.Add(CreateArgument(@namespace.Name));
            expression.Members.AddIfNotNull(nameof(properties.Classes), @namespace.Classes);
            UseExpression(expression);
        }
        public override void VisitClassMethod <TAttributeGroup, TDeclaringType, TGenericParameter, TTypeReference, TParameter, TMethodBody>(
            IClassMethod <TAttributeGroup, TDeclaringType, TGenericParameter, TTypeReference, TParameter, TMethodBody> method)
        {
            ClassMethod properties;
            ObjectInitializerExpression expression = CreateExpression(typeof(ClassMethod));

            expression.CreateObjectExpression.NewExpression.Parameters.Add(CreateArgument(method.Name));
            expression.Members.AddIfNotNull(nameof(properties.Visibility), method.Visibility);
            expression.Members.AddIfNotNull(nameof(properties.InheritanceModifier), method.InheritanceModifier);
            expression.Members.AddIfNotNull(nameof(properties.IsAsync), method.IsAsync);
            expression.Members.AddIfNotNull(nameof(properties.ReturnType), method.ReturnType);
            expression.Members.AddIfNotNull(nameof(properties.GenericParameters), method.GenericParameters);
            expression.Members.AddIfNotNull(nameof(properties.Parameters), method.Parameters);
            UseExpression(expression);
        }
        public override void VisitClassType <TAttributeGroup, TGenericParameter, TClassReference, TInterfaceReference, TEventCollection, TPropertyCollection, TIndexerCollection, TMethodCollection, TFieldCollection, TConstructor, TOperatorOverload, TConversionOperator, TNestedClassCollection, TNestedDelegate, TNestedEnum, TNestedInterface, TNestedStructCollection, TStaticConstructor, TDestructor>(
            IClassType <TAttributeGroup, TGenericParameter, TClassReference, TInterfaceReference, TEventCollection, TPropertyCollection, TIndexerCollection, TMethodCollection, TFieldCollection, TConstructor, TOperatorOverload, TConversionOperator, TNestedClassCollection, TNestedDelegate, TNestedEnum, TNestedInterface, TNestedStructCollection, TStaticConstructor, TDestructor> @class)
        {
            ClassBody properties;
            ObjectInitializerExpression expression = CreateExpression(typeof(ClassBody));

            expression.Members.AddIfNotNull(nameof(properties.Fields), @class.Fields);
            expression.Members.AddIfNotNull(nameof(properties.Events), @class.Events);
            expression.Members.AddIfNotNull(nameof(properties.Properties), @class.Properties);
            expression.Members.AddIfNotNull(
                nameof(properties.ExplicitInterfaceProperties),
                WrapperFactory.CreateClassPropertyCollectionWrapper(@class.Properties).ExplicitInterfaceProperties);
            expression.Members.AddIfNotNull(nameof(properties.Indexers), @class.Indexers);
            //
            expression.Members.AddIfNotNull(nameof(properties.Methods), @class.Methods);
            UseExpression(expression);
        }
        public override void VisitClass <TNamespace, TDocument, TProject, TSolution, TAttributeGroup, TGenericParameter, TClassReference, TInterfaceReference, TEventCollection, TPropertyCollection, TIndexerCollection, TMethodCollection, TFieldCollection, TConstructor, TOperatorOverload, TConversionOperator, TNestedClassCollection, TNestedDelegate, TNestedEnum, TNestedInterface, TNestedStructCollection, TStaticConstructor, TDestructor>(
            IClass <TNamespace, TDocument, TProject, TSolution, TAttributeGroup, TGenericParameter, TClassReference, TInterfaceReference, TEventCollection, TPropertyCollection, TIndexerCollection, TMethodCollection, TFieldCollection, TConstructor, TOperatorOverload, TConversionOperator, TNestedClassCollection, TNestedDelegate, TNestedEnum, TNestedInterface, TNestedStructCollection, TStaticConstructor, TDestructor> @class)
        {
            Class properties;
            ObjectInitializerExpression expression = CreateExpression(typeof(Class));

            expression.CreateObjectExpression.NewExpression.Parameters.Add(CreateArgument(@class.Name));
            expression.Members.AddIfNotNull(nameof(properties.Visibility), @class.Visibility);
            expression.Members.AddIfNotNull(nameof(properties.GenericParameters), @class.GenericParameters);
            expression.Members.AddIfNotNull(nameof(properties.BaseClass), @class.BaseClass);
            VisitClassType(@class);
            if (Expression.ObjectInitializerExpression != null)
            {
                expression.Members.Add(nameof(properties.Body), Expression);
            }

            UseExpression(expression);
        }
        private static bool IsInsideInitializerToTypeWithSource(CheckContentAvailabilityEventArgs ea)
        {
            _TypeElement = null;
            _ObjectCreationExpression = ea.Element as ObjectCreationExpression;
            if (_ObjectCreationExpression == null)
            {
                _ObjectCreationExpression = ea.Element.GetParent(LanguageElementType.ObjectCreationExpression) as ObjectCreationExpression;
                if (_ObjectCreationExpression == null)
                    return false;
            }
            _InitializerExpression = _ObjectCreationExpression.ObjectInitializer;
            if (_InitializerExpression == null)
                return false;
            _TypeElement = _ObjectCreationExpression.ObjectType.Resolve(ParserServices.SourceTreeResolver) as ITypeElement;
            if (_TypeElement == null)
                return false;

            return !_TypeElement.InReferencedAssembly;
        }
 private void UseExpression(ObjectInitializerExpression expression)
 {
     if (expression.Elements.Count == 0 &&
         expression.Initializers.Count == 0 &&
         expression.Members.Count == 0)
     {
         Expression = new Expression()
         {
             NewExpression = expression.CreateObjectExpression.NewExpression
         };
     }
     else
     {
         Expression = new Expression()
         {
             ObjectInitializerExpression = expression
         };
     }
 }
Ejemplo n.º 9
0
        private static AbstractExpression ReadObjectInitializer(IObjectOrCollectionInitializerOperation op)
        {
            var context = CodeReaderContext.GetContextOrThrow();
            var result  = new ObjectInitializerExpression {
                PropertyValues = op.Initializers.Select(readInitializer).ToList()
            };

            return(result);

            NamedPropertyValue readInitializer(IOperation initOp)
            {
                if (initOp is IAssignmentOperation assignment &&
                    assignment.Target is IMemberReferenceOperation memberRef)
                {
                    var property = context.FindMemberOrThrow <AbstractMember>(memberRef.Member);
                    var value    = ReadAssignmentValue(assignment);

                    return(new NamedPropertyValue(property.Name, value));
                }

                throw new CodeReadErrorException($"Unrecognized object initializer: {initOp.Syntax}");
            }
        }
 private static bool HasMatchingConstructors(ObjectInitializerExpression initializer, ITypeElement typeElement)
 {
     ExpressionCollection parameters = initializer.Initializers;
     IElementCollection foundConstructors = typeElement.FindConstructors(parameters);
     return foundConstructors != null && foundConstructors.Count > 0;
 }
Ejemplo n.º 11
0
 public virtual ObjectInitializerExpression RewriteObjectInitializerExpression(ObjectInitializerExpression expression)
 {
     return(expression);
 }
Ejemplo n.º 12
0
 public virtual void VisitObjectInitializerExpression(ObjectInitializerExpression expression)
 {
     VisitAbstractExpression(expression);
 }