public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode,
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
                Violation violation,
                CsElement csElement)
            {
                foreach (var method in from x in enumerate(new ElementTypeFilter(LanguageElementType.Method))
                                       let method = (DX.Method)x.ToLanguageElement()
                                       where method.RecoveredRange.Start.Line == violation.Line
                                          && method.IsStatic && method.IsConstructor
                                          && method.FirstChild == null
                                       select method)
                {
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, method.NameRange);
                }

                foreach (var statement in from x in enumerate(new ElementTypeFilter(deleteWhenEmpty))
                                          let statement = x.ToLanguageElement()
                                          where statement.RecoveredRange.Start.Line == violation.Line
                                             && (statement.ElementType == LanguageElementType.Try || statement.FirstChild == null)
                                          select statement)
                {
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, statement.GetKeywordRange());
                }
            }
            public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode, 
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
                Violation violation, 
                CsElement csElement)
            {
                CsToken startToken = null;
                foreach (var token in from token in csElement.ElementTokens
                                      where violation.Line == token.Location.StartPoint.LineNumber
                                        && (token.CsTokenType == CsTokenType.OpenCurlyBracket 
                                            || token.CsTokenType == CsTokenType.CloseCurlyBracket)
                                      select token)
                {
                    if (token.CsTokenType == CsTokenType.OpenCurlyBracket)
                    {
                        startToken = token;
                        continue;
                    }

                    if (token.CsTokenType == CsTokenType.CloseCurlyBracket)
                    {
                        var sourceRange = new SourceRange(startToken.Location.StartPoint.LineNumber, startToken.Location.StartPoint.IndexOnLine + 1, token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2);
                        yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                    }
                }
            }
            public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode, 
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
                Violation violation, 
                CsElement csElement)
            {
                SourcePoint? startPoint = null;
                SourcePoint? endPoint = null;
                foreach (var token in from token in csElement.ElementTokens
                                      where token.LineNumber == violation.Line && token.CsTokenType != CsTokenType.WhiteSpace
                                      select token)
                {
                    if (token.CsTokenType == CsTokenType.Namespace)
                    {
                        startPoint = endPoint = new SourcePoint(token.Location.StartPoint.LineNumber, token.Location.StartPoint.IndexOnLine + 1);
                    }
                    else if (token.CsTokenType == CsTokenType.OpenCurlyBracket || token.CsTokenType == CsTokenType.EndOfLine)
                    {
                        if (startPoint != null)
                        {
                            var sourceRange = new SourceRange(startPoint.Value, endPoint.Value);
                            yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                        }

                        yield break;
                    }
                    else if (startPoint != null)
                    {
                        endPoint = new SourcePoint(token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2);
                    }
                }
            }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     CsToken potentialViolation = null;
     bool whitespaceFound = false;
     foreach (var token in this.getTokens(csElement).Flatten().Where(x => x.LineNumber == violation.Line))
     {
         if (potentialViolation != null && token.CsTokenType == CsTokenType.WhiteSpace)
         {
             whitespaceFound = true;
         }
         else if (potentialViolation != null && whitespaceFound && isBannedFollower(token))
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(potentialViolation.Location.StartPoint.LineNumber, potentialViolation.Location.StartPoint.IndexOnLine + 1, potentialViolation.Location.EndPoint.LineNumber, potentialViolation.Location.EndPoint.IndexOnLine + 2));
             whitespaceFound = false;
             potentialViolation = null;
         }
         else if (this.reportIssueFor(token, violation))
         {
             potentialViolation = token;
             whitespaceFound = false;
         }
         else 
         {
             whitespaceFound = false;
             potentialViolation = null;
         }
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode, 
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
     Violation violation, 
     CsElement csElement)
 {
     bool predecessorFound = true;
     CodeLocation issueLocation = null;
     foreach (var token in this.getTokens(csElement).Flatten().Where(x => x.LineNumber == violation.Line))
     {
         if (!predecessorFound && !requiredFollowers.Contains(token.CsTokenType) && issueLocation != null)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(issueLocation.StartPoint.LineNumber, issueLocation.StartPoint.IndexOnLine + 1, issueLocation.EndPoint.LineNumber, issueLocation.EndPoint.IndexOnLine + 2));
             predecessorFound = false;
             issueLocation = null;
         }
         else if (reportIssueFor(token, violation))
         {
             issueLocation = token.Location;
         }
         else if (requiredPredecessors.Contains(token.CsTokenType))
         {
             predecessorFound = true;
         }
         else
         {
             predecessorFound = false;
             issueLocation = null;
         }
     }
 }
        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode, 
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
            Violation violation, 
            CsElement csElement)
        {
            CodePoint startPoint = null;
            CodePoint endPoint = null;
            bool emptyLine = true;
            foreach (var token in csElement.ElementTokens.Flatten().Where(x => x.LineNumber == violation.Line))
            {
                if (startPoint == null)
                {
                    startPoint = token.Location.StartPoint;
                    endPoint = token.Location.EndPoint;
                    emptyLine = token.CsTokenType == CsTokenType.WhiteSpace || token.CsTokenType == CsTokenType.EndOfLine;
                }
                
                if (token.CsTokenType != CsTokenType.WhiteSpace && token.CsTokenType != CsTokenType.EndOfLine)
                {
                    if (emptyLine)
                    {
                        startPoint = token.Location.StartPoint;
                        emptyLine = false;
                    }

                    endPoint = token.Location.EndPoint;
                }
                else if (token.CsTokenType == CsTokenType.EndOfLine)
                {
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, endPoint.LineNumber, endPoint.IndexOnLine + 2));
                }
            }
        }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     bool specialPredecessorFound = true;
     bool whitespaceFound = false;
     foreach (var token in this.getTokens(csElement).Flatten().Where(x => x.LineNumber == violation.Line))
     {
         if (!specialPredecessorFound && whitespaceFound && this.reportIssueFor(token, violation))
         {
             whitespaceFound = false;
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(token.Location.StartPoint.LineNumber, token.Location.StartPoint.IndexOnLine + 1, token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2));
         }
         else if (this.specialPredecessors.Contains(token.CsTokenType))
         {
             specialPredecessorFound = true;
             whitespaceFound = false;
         }
         else if (token.CsTokenType == CsTokenType.WhiteSpace)
         {
             whitespaceFound = true;
         }
         else
         {
             specialPredecessorFound = false;
             whitespaceFound = false;
         }
     }
 }
        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            CodePoint startPoint = null;
            CodePoint endPoint = null;
            foreach (var token in from token in csElement.ElementTokens
                                  where token.LineNumber >= violation.Line
                                  select token)
            {
                if (token.CsTokenType == CsTokenType.UsingDirective || token.CsTokenType == CsTokenType.Using)
                {
                    startPoint = token.Location.StartPoint;
                    continue;
                }

                if (token.CsTokenType == CsTokenType.Semicolon)
                {
                    endPoint = token.Location.EndPoint;
                }

                if (startPoint != null && endPoint != null)
                {
                    var sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, endPoint.LineNumber, endPoint.IndexOnLine + 2);
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                }
            }
        }
        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            var preprocessorToken = (from token in csElement.ElementTokens
                         where token.LineNumber == violation.Line && token.CsTokenType == CsTokenType.PreprocessorDirective
                         select token).FirstOrDefault();
            if (preprocessorToken != null)
            {
                int underlineLength = 1;
                while (preprocessorToken.Text[underlineLength] == ' ' || preprocessorToken.Text[underlineLength] == '\t')
                {
                    underlineLength++;
                }

                while (underlineLength < preprocessorToken.Text.Length && preprocessorToken.Text[underlineLength] != ' ' && preprocessorToken.Text[underlineLength] != '\t')
                {
                    underlineLength++;
                }

                var startPoint = preprocessorToken.Location.StartPoint;
                var sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, startPoint.LineNumber, startPoint.IndexOnLine + 1 + underlineLength);
                yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
            }
        }
            public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode, 
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
                Violation violation, 
                CsElement csElement)
            {
                int prefixLength = "The call to ".Length;
                var memberName = violation.Message.Substring(prefixLength, violation.Message.IndexOf(" must") - prefixLength);
                var thisFound = false;
                foreach (var token in from token in csElement.ElementTokens
                                      where token.LineNumber == violation.Line && token.CsTokenType != CsTokenType.WhiteSpace
                                      select token)
                {
                    if (token.CsTokenType == CsTokenType.This || (thisFound && token.Text == "."))
                    {
                        thisFound = true;
                    }
                    else
                    {
                        if (token.Text == memberName && !thisFound)
                        {
                            var sourceRange = new SourceRange(token.Location.StartPoint.LineNumber, token.Location.StartPoint.IndexOnLine + 1, token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2);
                            yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                        }

                        thisFound = false;
                    }
                }
            }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (BinaryOperatorExpression operation in from x in enumerate(new ElementTypeFilter(LanguageElementType.BinaryOperatorExpression))
                                                    let binaryOperation = (BinaryOperatorExpression)x.ToLanguageElement()
                                                    where binaryOperation.RecoveredRange.Start.Line == violation.Line
                                                       && (binaryOperation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression
                                                           || binaryOperation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression)
                                                    select binaryOperation)
     {
         var parentType = operators[operation.Name];
         var leftChildType = operation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operators[operation.LeftSide.Name] : parentType;
         var rightChildType = operation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operators[operation.RightSide.Name] : parentType;
         var isLeftModulo = operation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operation.LeftSide.Name == "%" : operation.Name == "%";
         var isRightModulo = operation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operation.RightSide.Name == "%" : operation.Name == "%";
         if (rightChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.RightSide.RecoveredRange);
         }
         else if (leftChildType > parentType || (leftChildType == rightChildType && isLeftModulo != isRightModulo))
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.LeftSide.RecoveredRange);
         }
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (var operation in from x in enumerate(new ElementTypeFilter(LanguageElementType.LogicalOperation))
                                            let logicalOperation = (LogicalOperation)x.ToLanguageElement()
                                            where logicalOperation.RecoveredRange.Start.Line == violation.Line
                                                 && (logicalOperation.LeftSide.ElementType == LanguageElementType.LogicalOperation
                                                      || logicalOperation.RightSide.ElementType == LanguageElementType.LogicalOperation)
                                            select logicalOperation)
     {
         var parentType = operators[operation.Name];
         var leftChildType = operation.LeftSide.ElementType == LanguageElementType.LogicalOperation ? operators[operation.LeftSide.Name] : parentType;
         var rightChildType = operation.RightSide.ElementType == LanguageElementType.LogicalOperation ? operators[operation.RightSide.Name] : parentType;
         if (rightChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.RightSide.RecoveredRange);
         }
         else if (leftChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.LeftSide.RecoveredRange);
         }
     }
 }
 private static void DebugWrite(Violation violation, params object[] args)
 {
     Debug.WriteLine(
         "{0} ({1}): {2}",
         violation.SourceCode.Path,
         violation.Line,
         string.Format(violation.Message, args));
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     return Enumerable.Empty<StyleCopCodeIssue>();
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     return this.issueLocators.SelectMany(locator => locator.GetCodeIssues(sourceCode, enumerate, violation, csElement));
 }
 private void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     if (e.SourceCode != null)
     {
         var violation = new Violation(e.Violation.Rule.CheckId, e.Message, e.LineNumber);
         this.violations.AddViolationToFile(e.SourceCode.Path, violation);
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     return (from token in csElement.ElementTokens
             where token.LineNumber == violation.Line && this.tokenTypes.Contains(token.CsTokenType)
             let location = token.Location
             select new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(location.StartPoint.LineNumber, location.StartPoint.IndexOnLine + 1, location.EndPoint.LineNumber, location.EndPoint.IndexOnLine + 2))).Take(1);
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     var startPoint = csElement.ElementTokens.First().Location.StartPoint;
     var endPoint = csElement.ElementTokens.Last().Location.EndPoint;
     var sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, endPoint.LineNumber, endPoint.IndexOnLine + 2);
     yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode, 
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
     Violation violation, 
     CsElement csElement)
 {
     return from x in enumerate(new ElementTypeFilter(LanguageElementType.Statement))
            let statement = (DX.Statement)x.ToLanguageElement()
            where statement.StartLine == violation.Line
                && statement.DetailNodeCount == 0
            select new StyleCopCodeIssue(CodeIssueType.CodeSmell, statement.Range);
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode, 
     Func<ElementTypeFilter, IEnumerable<IElement>> crEnumerable, 
     Violation violation, 
     CsElement csElement)
 {
     return from token in csElement.ElementTokens
            where token.Text.StartsWith("///") && !token.Text.StartsWith("////")
            let startPoint = token.Location.StartPoint
            let endPoint = token.Location.EndPoint
            let sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, endPoint.LineNumber, endPoint.IndexOnLine + 2)
            select new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     var token = this.getTokens(csElement).Flatten().LastOrDefault(x => x.LineNumber == violation.Line && this.reportIssueFor(x, violation));
     if (token != null)
     {
         var issueLocation = token.Location;
         yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(issueLocation.StartPoint.LineNumber, issueLocation.StartPoint.IndexOnLine + 1, issueLocation.EndPoint.LineNumber, issueLocation.EndPoint.IndexOnLine + 2));
     }
 }
Example #22
0
        /// <summary>
        /// Adds one violation to this element.
        /// </summary>
        /// <param name="violation">
        /// The violation to add.
        /// </param>
        /// <returns>
        /// Returns false if there is already an identical violation in the element.
        /// </returns>
        internal bool AddViolation(Violation violation)
        {
            Param.AssertNotNull(violation, "violation");
            int key = violation.Key;

            if (!this.violations.ContainsKey(key))
            {
                this.violations.Add(violation.Key, violation);
                return(true);
            }

            return(false);
        }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation, 
     CsElement csElement)
 {
     bool inTypeOfOrSizeOfOrDefault = false;
     bool openParenFound = false;
     bool typeFound = false;
     CsToken potentialViolation = null;
     foreach (var token in this.getTokens(csElement).Where(x => x.LineNumber == violation.Line))
     {
         if (token.CsTokenType == CsTokenType.Typeof || token.CsTokenType == CsTokenType.Sizeof || token.CsTokenType == CsTokenType.DefaultValue)
         {
             inTypeOfOrSizeOfOrDefault = true;
         }
         else if (inTypeOfOrSizeOfOrDefault && token.CsTokenType == CsTokenType.CloseParenthesis)
         {
             inTypeOfOrSizeOfOrDefault = false;
         }
         else if (!inTypeOfOrSizeOfOrDefault && token.CsTokenType == CsTokenType.OpenParenthesis)
         {
             openParenFound = true;
         }
         else if (!inTypeOfOrSizeOfOrDefault && openParenFound && potentialViolation == null && token.CsTokenType == CsTokenType.WhiteSpace)
         {
         }
         else if (!inTypeOfOrSizeOfOrDefault && openParenFound && potentialViolation == null && token is TypeToken)
         {
             typeFound = true;
         }
         else if (!inTypeOfOrSizeOfOrDefault && openParenFound && typeFound && potentialViolation == null && token.CsTokenType == CsTokenType.CloseParenthesis)
         {
             potentialViolation = token;
         }
         else if (!inTypeOfOrSizeOfOrDefault && potentialViolation != null && token.CsTokenType == CsTokenType.WhiteSpace)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(potentialViolation.Location.StartPoint.LineNumber, potentialViolation.Location.StartPoint.IndexOnLine + 1, potentialViolation.Location.EndPoint.LineNumber, potentialViolation.Location.EndPoint.IndexOnLine + 2));
             openParenFound = false;
             typeFound = false;
             potentialViolation = null;
         }
         else
         {
             openParenFound = false;
             typeFound = false;
             potentialViolation = null;
         }
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode, 
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
     Violation violation, 
     CsElement csElement)
 {
     foreach (var token in csElement.Header.ChildTokens.Where(childToken => childToken.CsTokenType == CsTokenType.XmlHeaderLine
         && ((childToken.Text.Length > 3 && childToken.Text[3] != ' ') 
             || (childToken.Text.Length > 4 && childToken.Text[4] == ' '))))
     {
         var location = token.Location;
         yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(location.StartPoint.LineNumber, location.StartPoint.IndexOnLine + 1, location.EndPoint.LineNumber, location.EndPoint.IndexOnLine + 2));
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (var element in from x in enumerate(new ElementTypeFilter(orderableElements))
                             where x.FirstNameRange.Start.Line == violation.Line
                             select x)
     {
         var nameRange = element.FirstNameRange;
         var sourceRange = new SourceRange(nameRange.Start.Line, nameRange.Start.Offset, nameRange.End.Line, nameRange.End.Offset);
         yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
     }
 }
        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            var token = csElement.Header.ChildTokens.Last();
            if (token == null)
            {
                return Enumerable.Empty<StyleCopCodeIssue>();
            }

            var location = token.Location;
            return new[] { new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(location.StartPoint.LineNumber, location.StartPoint.IndexOnLine + 1, location.EndPoint.LineNumber, location.EndPoint.IndexOnLine + 2)) };
        }
        public void AddViolationIssue(CheckCodeIssuesEventArgs ea, ISourceCode sourceCode, Violation violation)
        {
            var message = string.Format("{0}: {1}", violation.Rule.CheckId, violation.Message);
            var csElement = violation.Element as CsElement;
            if (csElement == null)
            {
                ea.AddSmell(new SourceRange(violation.Line, 1, violation.Line, sourceCode.LengthOfLine(violation.Line) + 1), message, violation, 10);
                return;
            }

            foreach (var styleCopCodeIssue in this.issueLocator.GetCodeIssues(sourceCode, filter => ea.GetEnumerable(ea.Scope, filter), violation, csElement))
            {
                ea.AddIssue(styleCopCodeIssue.IssueType, styleCopCodeIssue.Range, message, violation);
            }
        }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (IElement element in from x in enumerate(new ElementTypeFilter(LanguageElementType.Attribute))
                                  where x.FirstNameRange.Start.Line == violation.Line
                                  select x)
     {
         if (element.Name.Contains("SuppressMessage"))
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, element.FirstNameRange);
         }
     }
 }
Example #29
0
        /// <summary>
        /// Exports the contents of the given violation into the given xml node.
        /// </summary>
        /// <param name="violation">The violation to save.</param>
        /// <param name="violationsDocument">The xml document in which to store the violation information.</param>
        /// <param name="parentNode">The parent node within this xml document under which to store the violation information.</param>
        private static void ExportViolation(Violation violation, XmlDocument violationsDocument, XmlNode parentNode)
        {
            Param.AssertNotNull(violation, "violation");
            Param.AssertNotNull(violationsDocument, "violationsDocument");
            Param.AssertNotNull(parentNode, "parentNode");

            // Create the root element for this violation.
            XmlElement item = violationsDocument.CreateElement("violation");

            parentNode.AppendChild(item);

            // Create the namespace attribute.
            XmlAttribute nameSpace = violationsDocument.CreateAttribute("namespace");

            nameSpace.Value = violation.Rule.Namespace;
            item.Attributes.Append(nameSpace);

            // Create the rule name attribute.
            XmlAttribute name = violationsDocument.CreateAttribute("rule");

            name.Value = violation.Rule.Name;
            item.Attributes.Append(name);

            // Create the rule check-id attribute.
            XmlAttribute checkId = violationsDocument.CreateAttribute("ruleCheckId");

            checkId.Value = violation.Rule.CheckId;
            item.Attributes.Append(checkId);

            // Create a child node for the violation content string.
            XmlElement context = violationsDocument.CreateElement("context");

            context.InnerText = violation.Message;
            item.AppendChild(context);

            // Create a child node for the violation line number.
            XmlElement lineNumber = violationsDocument.CreateElement("line");

            lineNumber.InnerText = violation.Line.ToString(CultureInfo.InvariantCulture);
            item.AppendChild(lineNumber);

            // Create a child node for the warning indicator.
            XmlElement warning = violationsDocument.CreateElement("warning");

            warning.InnerText = violation.Rule.Warning.ToString(CultureInfo.InvariantCulture);
            item.AppendChild(warning);
        }
Example #30
0
        /// <summary>
        /// Adds the given violation.
        /// </summary>
        /// <param name="violation">The violation to add.</param>
        public void AddViolation(Violation violation)
        {
            Param.RequireNotNull(violation, "violation");

            if (violation.Element != null)
            {
                this.Core.AddViolation(violation.Element, violation);
            }
            else if (violation.SourceCode != null)
            {
                this.Core.AddViolation(violation.SourceCode, violation);
            }
            else
            {
                this.Core.AddViolation((ICodeElement)null, violation);
            }
        }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (var method in from x in enumerate(new ElementTypeFilter(LanguageElementType.AnonymousMethodExpression))
                            let method = (DX.AnonymousMethodExpression)x.ToLanguageElement()
                            where method.RecoveredRange.Start.Line == violation.Line
                               && method.ParameterCount == 0
                               && !method.ParamOpenRange.IsEmpty
                               && !method.ParamCloseRange.IsEmpty
                            select method)
     {
         yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(method.RecoveredRange.Start.Line, method.RecoveredRange.Start.Offset, method.ParamCloseRange.End.Line, method.ParamCloseRange.End.Offset));
     }
 }
 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (var element in from x in enumerate(new ElementTypeFilter(LanguageElementType.MethodCall))
                             where x.FirstNameRange.Start.Line == violation.Line && x.Name == this.methodName
                             select x)
     {
         MethodCall methodCall = (MethodCall)element.ToLanguageElement();
         if (this.qualifyParameters(methodCall))
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, methodCall.Range);
         }
     }
 }
        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            var locations = from token in csElement.ElementTokens
                   where token.LineNumber == violation.Line && this.tokenTypes.Contains(token.CsTokenType)
                   select token.Location;
            var firstToken = locations.FirstOrDefault();
            var lastToken = locations.LastOrDefault();

            if (firstToken != null && lastToken != null)
            {
                yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(firstToken.StartPoint.LineNumber, firstToken.StartPoint.IndexOnLine + 1, lastToken.EndPoint.LineNumber, lastToken.EndPoint.IndexOnLine + 2));
            }
        }
 /// <summary>
 /// Initializes a new instance of the ViolationEventArgs class.
 /// </summary>
 /// <param name="violation">
 /// The violation.
 /// </param>
 internal ViolationEventArgs(Violation violation)
 {
     Param.AssertNotNull(violation, "violation");
     this.violation = violation;
 }
Example #35
0
        /// <summary>
        /// Imports the cached violations under the given node.
        /// </summary>
        /// <param name="sourceCode">
        /// The source code containing the violations.
        /// </param>
        /// <param name="parentNode">
        /// The parent xml node containing the list of violations.
        /// </param>
        /// <returns>
        /// Returns true if all the data was loaded successfully from the file.
        /// </returns>
        internal bool ImportViolations(SourceCode sourceCode, XmlNode parentNode)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertNotNull(parentNode, "parentNode");

            bool success = true;

            try
            {
                XmlNodeList violations = parentNode.SelectNodes("violation");
                if (violations != null && violations.Count > 0)
                {
                    foreach (XmlNode violationNode in violations)
                    {
                        // Get the violation data from the xml node.
                        XmlNode nameSpace   = violationNode.SelectSingleNode("@namespace");
                        XmlNode ruleName    = violationNode.SelectSingleNode("@rule");
                        XmlNode ruleCheckId = violationNode.SelectSingleNode("@ruleCheckId");
                        XmlNode context     = violationNode.SelectSingleNode("context");
                        XmlNode lineNumber  = violationNode.SelectSingleNode("line");
                        XmlNode warning     = violationNode.SelectSingleNode("warning");

                        XmlNode index       = violationNode.SelectSingleNode("index");
                        XmlNode endIndex    = violationNode.SelectSingleNode("endIndex");
                        XmlNode startLine   = violationNode.SelectSingleNode("startLine");
                        XmlNode startColumn = violationNode.SelectSingleNode("startColumn");
                        XmlNode endLine     = violationNode.SelectSingleNode("endLine");
                        XmlNode endColumn   = violationNode.SelectSingleNode("endColumn");

                        // Create a Rule object representing this data.
                        Rule rule = new Rule(
                            ruleName.InnerText,
                            nameSpace.InnerText,
                            ruleCheckId.InnerText,
                            context.InnerText,
                            Convert.ToBoolean(warning.InnerText, CultureInfo.InvariantCulture));

                        Violation violation;

                        if (startLine != null && startColumn != null && endLine != null && endColumn != null)
                        {
                            CodeLocation location = new CodeLocation(
                                Convert.ToInt32(index.InnerText, null),
                                Convert.ToInt32(endIndex.InnerText, null),
                                Convert.ToInt32(startColumn.InnerText, null),
                                Convert.ToInt32(endColumn.InnerText, null),
                                Convert.ToInt32(startLine.InnerText, null),
                                Convert.ToInt32(endLine.InnerText, null));

                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, location, context.InnerText);
                        }
                        else
                        {
                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, Convert.ToInt32(lineNumber.InnerText, null), context.InnerText);
                        }

                        this.AddViolation(violation);
                    }
                }
            }
            catch (ArgumentException)
            {
                success = false;
            }
            catch (XmlException)
            {
                success = false;
            }
            catch (FormatException)
            {
                success = false;
            }
            catch (OverflowException)
            {
                success = false;
            }

            return(success);
        }