/// <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));
        }
Example #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);
        }
Example #3
0
        /// <summary>
        /// Coverts the given document into a <see cref="CsDocument" /> if possible.
        /// </summary>
        /// <param name="document">The document to convert.</param>
        /// <returns>Returns the converted document or null.</returns>
        public static CsDocument AsCsDocument(this ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocumentWrapper wrapper = document as CsDocumentWrapper;

            if (wrapper == null)
            {
                return(null);
            }

            return(wrapper.CsDocument);
        }
Example #4
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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Parses the given file.
        /// </summary>
        /// <param name="sourceCode">The source code to parse.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <param name="document">The parsed representation of the file.</param>
        /// <returns>Returns false if no further analysis should be done on this file, or
        /// true if the file should be parsed again during the next pass.</returns>
        public override bool ParseFile(SourceCode sourceCode, int passNumber, ref ICodeDocument document)
        {
            Param.RequireNotNull(sourceCode, "sourceCode");
            Param.RequireGreaterThanOrEqualToZero(passNumber, "passNumber");
            Param.Ignore(document);

            // The document is parsed on the first pass. On any subsequent passes, we do not do anything.
            if (passNumber == 0)
            {
                try
                {
                    using (TextReader reader = sourceCode.Read())
                    {
                        // Create the document.
                        if (reader == null)
                        {
                            this.AddViolation(sourceCode, 1, Rules.FileMustBeReadable);
                        }
                        else
                        {
                            CsLanguageService languageService = new CsLanguageService();

                            document = new CsDocumentWrapper(
                                this,
                                sourceCode,
                                languageService.CreateCodeModel(reader, sourceCode.Name, sourceCode.Path));
                        }
                    }
                }
                catch (SyntaxException syntaxex)
                {
                    this.AddViolation(sourceCode, syntaxex.LineNumber, Rules.SyntaxException, syntaxex.Message);
                    document = null;
                }
            }

            return(false);
        }
        /// <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);
            }
        }
Example #7
0
        /// <summary>
        /// Parses the given file.
        /// </summary>
        /// <param name="sourceCode">The source code to parse.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <param name="document">The parsed representation of the file.</param>
        /// <returns>Returns false if no further analysis should be done on this file, or
        /// true if the file should be parsed again during the next pass.</returns>
        public override bool ParseFile(SourceCode sourceCode, int passNumber, ref ICodeDocument document)
        {
            Param.RequireNotNull(sourceCode, "sourceCode");
            Param.RequireGreaterThanOrEqualToZero(passNumber, "passNumber");
            Param.Ignore(document);

            // The document is parsed on the first pass. On any subsequent passes, we do not do anything.
            if (passNumber == 0)
            {
                try
                {
                    using (TextReader reader = sourceCode.Read())
                    {
                        // Create the document.
                        if (reader == null)
                        {
                            this.AddViolation(sourceCode, 1, Rules.FileMustBeReadable);
                        }
                        else
                        {
                            CsLanguageService languageService = new CsLanguageService();
                            
                            document = new CsDocumentWrapper(
                                this, 
                                sourceCode, 
                                languageService.CreateCodeModel(reader, sourceCode.Name, sourceCode.Path));
                        }
                    }
                }
                catch (SyntaxException syntaxex)
                {
                    this.AddViolation(sourceCode, syntaxex.LineNumber, Rules.SyntaxException, syntaxex.Message);
                    document = null;
                }
            }

            return false;
        }