Example #1
0
        /// <summary>
        /// Analyzes given line of code
        /// </summary>
        /// <param name="text">Source code</param>
        /// <param name="languages">List of languages</param>
        /// <returns>Array of matches</returns>
        public Issue[] Analyze(string text, string[] languages, int lineNumber = -1)
        {
            // Get rules for the given content type
            IEnumerable <Rule> rules         = GetRulesForLanguages(languages);
            List <Issue>       resultsList   = new List <Issue>();
            TextContainer      textContainer = new TextContainer(text, (languages.Length > 0) ? languages[0] : string.Empty);
            TextContainer      line          = (lineNumber > 0)?new TextContainer(textContainer.GetLineContent(lineNumber), (languages.Length > 0) ? languages[0] : string.Empty):textContainer;

            // Go through each rule
            foreach (Rule rule in rules)
            {
                List <Issue> matchList = new List <Issue>();

                // Skip rules that don't apply based on settings
                if (rule.Disabled || !SeverityLevel.HasFlag(rule.Severity))
                {
                    continue;
                }

                // Go through each matching pattern of the rule
                foreach (SearchPattern pattern in rule.Patterns)
                {
                    // Get all matches for the pattern
                    List <Boundary> matches = line.MatchPattern(pattern);

                    if (matches.Count > 0)
                    {
                        foreach (Boundary match in matches)
                        {
                            bool passedConditions   = true;
                            var  translatedBoundary = match;
                            if (lineNumber >= 0)
                            {
                                translatedBoundary = new Boundary()
                                {
                                    Length = match.Length,
                                    Index  = textContainer.GetBoundaryFromLine(lineNumber).Index + match.Index
                                };
                            }

                            if (!textContainer.ScopeMatch(pattern, translatedBoundary))
                            {
                                passedConditions = false;
                            }
                            else
                            {
                                foreach (SearchCondition condition in rule.Conditions)
                                {
                                    bool res = line.MatchPattern(condition.Pattern, match, condition);
                                    if (res && condition.NegateFinding)
                                    {
                                        passedConditions = false;
                                        break;
                                    }
                                    if (!res && condition.NegateFinding)
                                    {
                                        passedConditions = true;
                                        break;
                                    }
                                    if (!res)
                                    {
                                        passedConditions = false;
                                        break;
                                    }
                                }
                            }

                            if (passedConditions)
                            {
                                Issue issue = new Issue()
                                {
                                    Boundary      = match,
                                    StartLocation = line.GetLocation(match.Index),
                                    EndLocation   = line.GetLocation(match.Index + match.Length),
                                    Rule          = rule
                                };

                                matchList.Add(issue);
                            }
                        }
                    }
                }

                // We got matching rule and suppression are enabled,
                // let's see if we have a supression on the line
                if (EnableSuppressions && matchList.Count > 0)
                {
                    Suppression supp;
                    foreach (Issue result in matchList)
                    {
                        supp = new Suppression(textContainer, (lineNumber > 0)?lineNumber:result.StartLocation.Line);
                        // If rule is NOT being suppressed then report it
                        SuppressedIssue supissue = supp.GetSuppressedIssue(result.Rule.Id);
                        if (supissue == null)
                        {
                            resultsList.Add(result);
                        }
                        // Otherwise add the suppression info instead
                        else
                        {
                            result.IsSuppressionInfo = true;

                            if (!resultsList.Any(x => x.Rule.Id == result.Rule.Id && x.Boundary.Index == result.Boundary.Index))
                            {
                                resultsList.Add(result);
                            }
                        }
                    }
                }
                // Otherwise put matchlist to resultlist
                else
                {
                    resultsList.AddRange(matchList);
                }
            }

            // Deal with overrides
            List <Issue> removes = new List <Issue>();

            foreach (Issue m in resultsList)
            {
                if (m.Rule.Overrides != null && m.Rule.Overrides.Length > 0)
                {
                    foreach (string ovrd in m.Rule.Overrides)
                    {
                        // Find all overriden rules and mark them for removal from issues list
                        foreach (Issue om in resultsList.FindAll(x => x.Rule.Id == ovrd))
                        {
                            if (om.Boundary.Index >= m.Boundary.Index &&
                                om.Boundary.Index <= m.Boundary.Index + m.Boundary.Length)
                            {
                                removes.Add(om);
                            }
                        }
                    }
                }
            }

            // Remove overriden rules
            resultsList.RemoveAll(x => removes.Contains(x));

            return(resultsList.ToArray());
        }