Esempio n. 1
0
        public static PropertyContractInfo TryCreate(
            [NotNull] IIndexerDeclaration declaration,
            TreeTextRange selectedTreeRange,
            [NotNull] Func <IType, bool> isAvailableForType)
        {
            if (declaration.GetNameRange().Contains(selectedTreeRange) &&
                declaration.ArrowClause == null &&
                declaration.AccessorDeclarations.Any(accessorDeclaration => accessorDeclaration.AssertNotNull().ArrowClause == null))
            {
                var property = declaration.DeclaredElement;

                Debug.Assert(property != null);

                if (CanAcceptContracts(property) && isAvailableForType(property.Type))
                {
                    var contractKind = property.IsReadable
                        ? (property.IsWritable ? ContractKind.RequiresAndEnsures : ContractKind.Ensures)
                        : (property.IsWritable ? (ContractKind?)ContractKind.Requires : null);
                    if (contractKind != null)
                    {
                        return(new PropertyContractInfo((ContractKind)contractKind, declaration, property.Type));
                    }
                }
            }

            return(null);
        }
        /// <summary>Inspects the specified indexer.</summary>
        /// <param name="indexer">The indexer.</param>
        public void Inspect([NotNull] IIndexerDeclaration indexer)
        {
            this.CodeAnnotation = new CodeAnnotation(indexer);
            if (!this.CodeAnnotation.IsValid)
            {
                return;
            }

            if (indexer.IsAbstract || indexer.IsExtern)
            {
                return;
            }

            this.TypeMember    = indexer;
            this.AppliedReturn = this.CodeAnnotation.GetAnnotation(indexer);

            var getter = indexer.AccessorDeclarations.FirstOrDefault(a => a.Kind == AccessorKind.GETTER);
            var setter = indexer.AccessorDeclarations.FirstOrDefault(a => a.Kind == AccessorKind.SETTER);

            if (this.AppliedReturn != CodeAnnotationAttribute.Undefined && this.AppliedReturn != CodeAnnotationAttribute.NotSet)
            {
                this.ExpectedReturn = this.AppliedReturn;
            }
            else if (getter != null)
            {
                var attribute = this.CodeAnnotation.InspectControlGraf(getter);
                if (attribute != CodeAnnotationAttribute.Undefined && attribute != CodeAnnotationAttribute.NotSet)
                {
                    this.ExpectedReturn = attribute;
                }
            }

            if (getter != null)
            {
                this.BuildValueParameter(this.TypeMember, getter, setter);
            }

            if (setter == null)
            {
                return;
            }

            var body = setter.Body;

            if (body == null)
            {
                return;
            }

            this.Body = body;

            // this.BuildParameters(setter, false);
            this.BuildAssertions(body);
        }
        /// <summary>Gets the annotation.</summary>
        /// <param name="indexer">The indexer.</param>
        /// <returns>Returns the annotation.</returns>
        public CodeAnnotationAttribute GetAnnotation([NotNull] IIndexerDeclaration indexer)
        {
            if (indexer == null)
            {
                throw new ArgumentNullException("indexer");
            }

            var attributesOwner = indexer.DeclaredElement as IAttributesOwner;

            return(attributesOwner != null?this.GetAnnotation(attributesOwner) : CodeAnnotationAttribute.Undefined);
        }
Esempio n. 4
0
        public override void VisitIndexerDeclaration(IIndexerDeclaration decl, SST sst)
        {
            var name = decl.DeclaredElement.GetName <IPropertyName>();

            var propDecl = new PropertyDeclaration {
                Name = name
            };

            sst.Properties.Add(propDecl);

            AddAccessorDecl(decl, propDecl);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns an xml string of the documentation for an element.
        /// </summary>
        /// <param name="owner">
        /// The owner of the doc comment block.
        /// </param>
        /// <param name="docConfig">
        /// The config for the current ProjectFile.
        /// </param>
        /// <returns>
        /// A string of the declarations summary text.
        /// </returns>
        private static string CreateDocumentationForElement(IDocCommentBlockOwnerNode owner, DocumentationRulesConfiguration docConfig)
        {
            ITreeNode        element         = owner;
            IDeclaredElement declaredElement = (element is IDeclaration) ? ((IDeclaration)element).DeclaredElement : null;
            StringBuilder    text            = new StringBuilder();

            text.AppendLine("<summary>");
            string summaryText = string.Empty;

            if (element is IConstructorDeclaration)
            {
                summaryText = Utils.CreateSummaryForConstructorDeclaration((IConstructorDeclaration)element);
            }

            if (element is IDestructorDeclaration)
            {
                summaryText = Utils.CreateSummaryForDestructorDeclaration((IDestructorDeclaration)element);
            }

            if (element is IPropertyDeclaration)
            {
                summaryText = Utils.CreateSummaryDocumentationForProperty((IPropertyDeclaration)element);
            }

            text.AppendLine(summaryText);
            text.AppendLine("</summary>");

            ICSharpParametersOwnerDeclaration declarationWithParameters = element as ICSharpParametersOwnerDeclaration;

            if (declarationWithParameters != null)
            {
                foreach (IRegularParameterDeclaration parameterDeclaration in declarationWithParameters.ParameterDeclarations)
                {
                    text.AppendLine(Utils.CreateDocumentationForParameter(parameterDeclaration));
                }
            }

            ICSharpTypeDeclaration typeDeclaration = element as ICSharpTypeDeclaration;

            if (typeDeclaration != null && (typeDeclaration.TypeParameters.Count > 0))
            {
                foreach (ITypeParameterOfTypeDeclaration typeParameter in typeDeclaration.TypeParameters)
                {
                    text.AppendLine(Utils.CreateDocumentationForParameter(typeParameter));
                }
            }

            ITypeParametersOwner typeParametersOwner = element as ITypeParametersOwner;

            if (typeParametersOwner != null && (typeParametersOwner.TypeParameters.Count > 0))
            {
                foreach (ITypeParameter typeParameter in typeParametersOwner.TypeParameters)
                {
                    text.AppendLine(Utils.CreateDocumentationForTypeParameterDeclaration((ITypeParameterDeclaration)typeParameter));
                }
            }

            IMethodDeclaration methodDeclaration = element as IMethodDeclaration;

            if (methodDeclaration != null && (methodDeclaration.TypeParameterDeclarations.Count > 0))
            {
                foreach (ITypeParameterOfMethodDeclaration typeParameter in methodDeclaration.TypeParameterDeclarations)
                {
                    text.AppendLine(Utils.CreateDocumentationForParameter(typeParameter));
                }
            }

            IParametersOwner parametersOwner = declaredElement as IParametersOwner;

            if ((parametersOwner != null && ((parametersOwner is IMethod) || (parametersOwner is IOperator))) &&
                !parametersOwner.ReturnType.Equals(parametersOwner.Module.GetPredefinedType().Void))
            {
                text.AppendLine("<returns></returns>");
            }

            bool ruleIsEnabled = docConfig.GetStyleCopRuleEnabled("PropertyDocumentationMustHaveValue");

            if (element is IPropertyDeclaration && ruleIsEnabled)
            {
                text.AppendLine(Utils.CreateValueDocumentationForProperty((IPropertyDeclaration)element));
            }

            List <IType> exceptions = new List <IType>();
            ICSharpFunctionDeclaration functionDeclaration = element as ICSharpFunctionDeclaration;

            if (functionDeclaration != null && functionDeclaration.Body != null)
            {
                CollectExceptions(functionDeclaration.Body, exceptions);
            }

            IPropertyDeclaration propertyDeclaration = element as IPropertyDeclaration;

            if (propertyDeclaration != null)
            {
                CollectExceptions(propertyDeclaration.AccessorDeclarations, exceptions);
            }

            IIndexerDeclaration indexerDeclaration = element as IIndexerDeclaration;

            if (indexerDeclaration != null)
            {
                CollectExceptions(indexerDeclaration.AccessorDeclarations, exceptions);
            }

            IEventDeclaration eventDeclaration = element as IEventDeclaration;

            if (eventDeclaration != null)
            {
                CollectExceptions(eventDeclaration.AccessorDeclarations, exceptions);
            }

            foreach (IType exception in exceptions)
            {
                string presentableName = exception.GetPresentableName(CSharpLanguage.Instance);

                string a = Utils.StripClassName(presentableName);
                string b = exception.ToString();
                text.AppendLine("<exception cref=\"" + Utils.SwapGenericTypeToDocumentation(a) + "\"></exception>");
            }

            return(text.ToString());
        }
 /// <summary>
 /// Visits the indexer declaration.
 /// </summary>
 /// <param name="indexerDeclarationParam">The indexer declaration param.</param>
 /// <param name="consumer">The consumer.</param>
 /// <returns></returns>
 public override object VisitIndexerDeclaration(IIndexerDeclaration indexerDeclarationParam, IHighlightingConsumer consumer)
 {
     this.VisitTypeMember(indexerDeclarationParam, consumer);
       return base.VisitIndexerDeclaration(indexerDeclarationParam, consumer);
 }
 public IndexerDeclarationCompiler(IIndexerDeclaration indexerDeclaration, AbstractILCompilerParams myParams) : base(myParams)
 {
     myPropertyDeclaration = indexerDeclaration;
     myParams.LocalVariableIndexer.InitForNewMethod(indexerDeclaration.DeclaredElement);
 }