示例#1
0
        /// <summary>
        /// Checks the order of the elements within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null)
            {
                // Get the value of the GeneratedCodeElementOrder property.
                bool checkGeneratedCode = OrderingRules.GeneratedCodeElementOrderDefaultValueProperty;

                if (document.Settings != null)
                {
                    BooleanProperty setting = this.GetSetting(document.Settings, OrderingRules.GeneratedCodeElementOrderProperty) as BooleanProperty;
                    if (setting != null)
                    {
                        checkGeneratedCode = setting.Value;
                    }

                    // Check the rest of the elements.
                    this.ProcessElements(csdocument, checkGeneratedCode);
                }

                this.CheckUsingDirectiveOrder(csdocument);
            }
        }
示例#2
0
        /// <summary>
        /// Checks the placement of brackets within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            XmlDocument contents = new XmlDocument();
            XmlNode     root     = contents.CreateElement("StyleCopCsParserObjectModel");

            contents.AppendChild(root);

            CsDocument csdocument = document.AsCsDocument();

            this.ProcessCodeUnit(csdocument, root);

            // Get the location where the output file should be stored.
            string testOutputDirectory = (string)this.Core.HostTag;

            if (string.IsNullOrEmpty(testOutputDirectory))
            {
                throw new InvalidOperationException("The HostTag has not been properly set in StyleCopCore.");
            }

            // Save the output to the file.
            string outputFileLocation = Path.Combine(
                testOutputDirectory,
                Path.GetFileNameWithoutExtension(document.SourceCode.Path) + "ObjectModelResults.xml");

            contents.Save(outputFileLocation);
        }
示例#3
0
        /// <summary>
        /// Checks the order of the elements within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null)
            {
                // Get the value of the GeneratedCodeElementOrder property.
                bool checkGeneratedCode = OrderingRules.GeneratedCodeElementOrderDefaultValueProperty;

                if (document.Settings != null)
                {
                    BooleanProperty setting = this.GetSetting(document.Settings, OrderingRules.GeneratedCodeElementOrderProperty) as BooleanProperty;
                    if (setting != null)
                    {
                        checkGeneratedCode = setting.Value;
                    }

                    // Check the rest of the elements.
                    this.ProcessElements(csdocument, checkGeneratedCode);
                }

                this.CheckUsingDirectiveOrder(csdocument);
            }
        }
示例#4
0
        /// <summary>
        /// Checks the placement of brackets within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            XmlDocument contents = new XmlDocument();
            XmlNode root = contents.CreateElement("StyleCopCsParserObjectModel");
            contents.AppendChild(root);

            CsDocument csdocument = document.AsCsDocument();
            this.ProcessCodeUnit(csdocument, root);

            // Get the location where the output file should be stored.
            string testOutputDirectory = (string)this.Core.HostTag;
            if (string.IsNullOrEmpty(testOutputDirectory))
            {
                throw new InvalidOperationException("The HostTag has not been properly set in StyleCopCore.");
            }

            // Save the output to the file.
            string outputFileLocation = Path.Combine(
                testOutputDirectory,
                Path.GetFileNameWithoutExtension(document.SourceCode.Path) + "ObjectModelResults.xml");

            contents.Save(outputFileLocation);
        }
示例#5
0
        /// <summary>
        /// Checks the spacing of items within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null && !csdocument.Generated)
            {
                this.CheckSpacing(csdocument, false);
            }
        }
示例#6
0
        /// <summary>
        /// Checks the case of element names within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (!csdocument.Generated)
            {
                Dictionary<string, string> validPrefixes = this.GetPrefixes(document.Settings);
                this.ProcessElement(csdocument, validPrefixes, false);
            }
        }
示例#7
0
        /// <summary>
        /// Checks the case of element names within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (!csdocument.Generated)
            {
                Dictionary <string, string> validPrefixes = this.GetPrefixes(document.Settings);
                this.ProcessElement(csdocument, validPrefixes, false);
            }
        }
示例#8
0
        /// <summary>
        /// Checks the placement of brackets within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null && !csdocument.Generated)
            {
                csdocument.WalkCodeModel(this.VisitCodeUnit);

                // Check line spacing rules.
                this.CheckLineSpacing(csdocument);
            }
        }
示例#9
0
        /// <summary>
        /// Checks the placement of brackets within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null && !csdocument.Generated)
            {
                csdocument.WalkCodeModel(this.VisitCodeUnit);

                // Check line spacing rules.
                this.CheckLineSpacing(csdocument);
            }
        }
        /// <summary>
        /// Returns a value indicating whether to delay analysis of this document until the next pass.
        /// </summary>
        /// <param name="document">The document to analyze.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <returns>Returns true if analysis should be delayed.</returns>
        public override bool DelayAnalysis(ICodeDocument document, int passNumber)
        {
            Param.RequireNotNull(document, "document");
            Param.Ignore(passNumber);

            bool delay = false;

            // We sometimes delay pass zero, but never pass one.
            if (passNumber == 0)
            {
                // Get the root element.
                CsDocument csdocument = document.AsCsDocument();
                if (csdocument != null)
                {
                    // If the element has any partial classes, structs, or interfaces, delay. This is due
                    // to the fact that the class members rules need knowledge about all parts of the class
                    // in order to find all class members.
                    delay = this.ContainsPartialMembers(csdocument);
                }
            }

            return(delay);
        }
        /// <summary>
        /// Returns a value indicating whether to delay analysis of this document until the next pass.
        /// </summary>
        /// <param name="document">The document to analyze.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <returns>Returns true if analysis should be delayed.</returns>
        public override bool DelayAnalysis(ICodeDocument document, int passNumber)
        {
            Param.RequireNotNull(document, "document");
            Param.Ignore(passNumber);

            bool delay = false;

            // We sometimes delay pass zero, but never pass one.
            if (passNumber == 0)
            {
                // Get the root element.
                CsDocument csdocument = document.AsCsDocument();
                if (csdocument != null)
                {
                    // If the element has any partial classes, structs, or interfaces, delay. This is due
                    // to the fact that the class members rules need knowledge about all parts of the class
                    // in order to find all class members.
                    delay = this.ContainsPartialMembers(csdocument);
                }
            }

            return delay;
        }
示例#12
0
        /// <summary>
        /// Analyzes or fixes the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        private void ProcessDocument(ICodeDocument document)
        {
            Param.AssertNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            Settings settings = new Settings();

            settings.DoNotUseRegions = this.IsRuleEnabled(document, Rules.DoNotUseRegions.ToString());
            settings.DoNotPlaceRegionsWithinElements = this.IsRuleEnabled(document, Rules.DoNotPlaceRegionsWithinElements.ToString());

            if (csdocument != null && !csdocument.Generated)
            {
                // Checks various formatting rules.
                csdocument.WalkCodeModel <Settings>(this.VisitCodeUnit, settings);

                // Check statement formatting rules.
                this.CheckStatementFormattingRulesForElement(csdocument);

                // Check the class member rules.
                this.CheckClassMemberRulesForElements(csdocument, null, null);
            }
        }
示例#13
0
        /// <summary>
        /// Checks the methods within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null && !csdocument.Generated)
            {
                // Check the access modifier rules.
                TopLevelElements topLevelElements = new TopLevelElements();

                csdocument.WalkCodeModel <TopLevelElements>(this.VisitCodeUnit, topLevelElements);

                // If there is more than one top-level class in the file, make sure they are all
                // partial classes and are all of the same type.
                if (topLevelElements.Classes.Count > 1)
                {
                    string name = string.Empty;
                    foreach (Class classElement in topLevelElements.Classes)
                    {
                        if (!classElement.ContainsModifier(TokenType.Partial) ||
                            (!string.IsNullOrEmpty(name) && string.Compare(name, classElement.FullyQualifiedName, StringComparison.Ordinal) != 0))
                        {
                            // Set the violation line number to the second class in the file.
                            int count = 0;
                            foreach (Class c in topLevelElements.Classes)
                            {
                                if (count == 1)
                                {
                                    this.AddViolation(c, c.LineNumber, Rules.FileMayOnlyContainASingleClass);
                                    break;
                                }

                                ++count;
                            }

                            break;
                        }

                        name = classElement.FullyQualifiedName;
                    }
                }

                // If there is more than one namespace in the file, this is a violation.
                if (topLevelElements.Namespaces.Count > 1)
                {
                    // Set the violation line number to the second namespace in the file.
                    int count = 0;
                    foreach (Namespace n in topLevelElements.Namespaces)
                    {
                        if (count == 1)
                        {
                            this.AddViolation(n, n.LineNumber, Rules.FileMayOnlyContainASingleNamespace);
                            break;
                        }

                        ++count;
                    }
                }
            }
        }
示例#14
0
        /// <summary>
        /// Analyzes or fixes the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        private void ProcessDocument(ICodeDocument document)
        {
            Param.AssertNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            Settings settings = new Settings();
            settings.DoNotUseRegions = this.IsRuleEnabled(document, Rules.DoNotUseRegions.ToString());
            settings.DoNotPlaceRegionsWithinElements = this.IsRuleEnabled(document, Rules.DoNotPlaceRegionsWithinElements.ToString());

            if (csdocument != null && !csdocument.Generated)
            {
                // Checks various formatting rules.
                csdocument.WalkCodeModel<Settings>(this.VisitCodeUnit, settings);

                // Check statement formatting rules.
                this.CheckStatementFormattingRulesForElement(csdocument);

                // Check the class member rules.
                this.CheckClassMemberRulesForElements(csdocument, null, null);
            }
        }
示例#15
0
        /// <summary>
        /// Checks the element headers within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (!csdocument.Generated)
            {
                this.CheckElementDocumentation(csdocument);
                this.CheckFileHeader(csdocument);
                this.CheckSingleLineComments(csdocument);
            }
        }
示例#16
0
        /// <summary>
        /// Automatically fixes rule violations within a code document.
        /// </summary>
        /// <param name="document">The document to fix.</param>
        public override void AutoFixDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (!csdocument.Generated)
            {
                this.CheckFileHeader(csdocument);
            }
        }
        /// <summary>
        /// Checks the methods within the given document.
        /// </summary>
        /// <param name="document">The document to check.</param>
        public override void AnalyzeDocument(ICodeDocument document)
        {
            Param.RequireNotNull(document, "document");

            CsDocument csdocument = document.AsCsDocument();

            if (csdocument != null && !csdocument.Generated)
            {
                // Check the access modifier rules.
                TopLevelElements topLevelElements = new TopLevelElements();

                csdocument.WalkCodeModel<TopLevelElements>(this.VisitCodeUnit, topLevelElements);

                // If there is more than one top-level class in the file, make sure they are all
                // partial classes and are all of the same type.
                if (topLevelElements.Classes.Count > 1)
                {
                    string name = string.Empty;
                    foreach (Class classElement in topLevelElements.Classes)
                    {
                        if (!classElement.ContainsModifier(TokenType.Partial) ||
                            (!string.IsNullOrEmpty(name) && string.Compare(name, classElement.FullyQualifiedName, StringComparison.Ordinal) != 0))
                        {
                            // Set the violation line number to the second class in the file.
                            int count = 0;
                            foreach (Class c in topLevelElements.Classes)
                            {
                                if (count == 1)
                                {
                                    this.AddViolation(c, c.LineNumber, Rules.FileMayOnlyContainASingleClass);
                                    break;
                                }

                                ++count;
                            }

                            break;
                        }

                        name = classElement.FullyQualifiedName;
                    }
                }

                // If there is more than one namespace in the file, this is a violation.
                if (topLevelElements.Namespaces.Count > 1)
                {
                    // Set the violation line number to the second namespace in the file.
                    int count = 0;
                    foreach (Namespace n in topLevelElements.Namespaces)
                    {
                        if (count == 1)
                        {
                            this.AddViolation(n, n.LineNumber, Rules.FileMayOnlyContainASingleNamespace);
                            break;
                        }

                        ++count;
                    }
                }
            }
        }