Ejemplo n.º 1
0
        /// <summary>
        /// Gets a setting for the add-in.
        /// </summary>
        /// <param name="addin">The addin being extended.</param>
        /// <param name="document">The document containing the settings.</param>
        /// <param name="propertyName">The name of the setting property.</param>
        /// <returns>Returns the setting or null if it does not exist.</returns>
        public static PropertyValue GetSetting(this StyleCopAddIn addin, CsDocument document, string propertyName)
        {
            Param.RequireNotNull(addin, "addin");
            Param.RequireNotNull(document, "document");
            Param.Ignore(propertyName);

            return(addin.GetSetting(CsDocumentWrapper.Wrapper(document).Settings, propertyName));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a value indicating whether the document is read-only.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns>Returns true if the document is readonly.</returns>
        public static bool IsReadOnly(this CsDocument document)
        {
            Param.RequireNotNull(document, "document");

            var wrapper = CsDocumentWrapper.Wrapper(document);

            Debug.Assert(wrapper != null, "Document has not been wrapped.");

            return(wrapper.ReadOnly);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds any suppressions for the given element by scanning the attributes on the element.
        /// </summary>
        private void AddRuleSuppressionsForElement()
        {
            if (this.element != null && this.element.Attributes != null && this.element.Attributes.Count > 0)
            {
                foreach (StyleCop.CSharp.CodeModel.Attribute attribute in this.element.Attributes)
                {
                    if (attribute.AttributeExpressions != null)
                    {
                        foreach (AttributeExpression attributeExpression in attribute.AttributeExpressions)
                        {
                            if (attributeExpression.Initialization != null)
                            {
                                MethodInvocationExpression methodInvocation = attributeExpression.Initialization as MethodInvocationExpression;
                                if (methodInvocation != null)
                                {
                                    if (IsCodeAnalysisSuppression(methodInvocation.Name))
                                    {
                                        // Crack open the expression and extract the rule checkID.
                                        string checkId;
                                        string ruleName;
                                        string ruleNamespace;

                                        if (TryCrackCodeAnalysisSuppression(methodInvocation, out checkId, out ruleName, out ruleNamespace))
                                        {
                                            Debug.Assert(!string.IsNullOrEmpty(checkId), "Rule ID should not be null");
                                            Debug.Assert(!string.IsNullOrEmpty(ruleName), "Rule Name should not be null");
                                            Debug.Assert(!string.IsNullOrEmpty(ruleNamespace), "Rule Namespace should not be null");

                                            CsParser parser = CsDocumentWrapper.Wrapper(this.element.Document).Parser;
                                            parser.AddRuleSuppression(this.element, checkId, ruleName, ruleNamespace);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Records or fixes an instance of a violation.
        /// </summary>
        /// <param name="addin">The addin being extended.</param>
        /// <param name="ruleName">The name of the rule that triggered the violation.</param>
        /// <param name="violationContext">Context for the violation.</param>
        /// <param name="correctionCallback">Callback which fixes the violation.</param>
        /// <param name="correctionContext">Optional callback context.</param>
        /// <typeparam name="T">The type of the callback context.</typeparam>
        public static void Violation <T>(this StyleCopAddIn addin, System.Enum ruleName, ViolationContext violationContext, CorrectViolationHandler <T> correctionCallback, T correctionContext)
        {
            Param.RequireNotNull(addin, "addin");
            Param.Ignore(ruleName);
            Param.RequireNotNull(violationContext, "violationContext");
            Param.RequireNotNull(correctionCallback, "correctViolationCallback");
            Param.Ignore(correctionContext, "correctViolationContext");

            if (addin.Core.RunContext.AutoFix)
            {
                Rule rule = addin.GetRule(ruleName.ToString());

                if (addin.IsRuleEnabled(CsDocumentWrapper.Wrapper(violationContext.Element.Document), rule.Name) &&
                    !addin.IsRuleSuppressed(ElementWrapper.Wrapper(violationContext.Element), rule))
                {
                    correctionCallback(violationContext, correctionContext);
                }
            }
            else
            {
                addin.AddViolation(violationContext.Element, violationContext.LineNumber, ruleName, violationContext.MessageValues);
            }
        }