Пример #1
0
        /// <summary>
        /// Given a list of attribute asts, return a list of rule suppression
        /// with startoffset at start and endoffset at end
        /// </summary>
        /// <param name="attrAsts"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> attrAsts, int start, int end, Ast scopeAst)
        {
            List<RuleSuppression> result = new List<RuleSuppression>();

            if (attrAsts == null || scopeAst == null)
            {
                return result;
            }

            IEnumerable<AttributeAst> suppressionAttribute = attrAsts.Where(
                item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));

            foreach (var attributeAst in suppressionAttribute)
            {
                RuleSuppression ruleSupp = new RuleSuppression(attributeAst, start, end);

                // If there is no error and scope is not null
                if (String.IsNullOrWhiteSpace(ruleSupp.Error) && !String.IsNullOrWhiteSpace(ruleSupp.Scope))
                {
                    if (String.IsNullOrWhiteSpace(ruleSupp.Target))
                    {
                        ruleSupp.Target = "*";
                    }

                    // regex for wild card *
                    Regex reg = new Regex(String.Format("^{0}$", Regex.Escape(ruleSupp.Target).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
                    IEnumerable<Ast> targetAsts = null;

                    switch (ruleSupp.Scope.ToLower())
                    {
                        case "function":
                            targetAsts = scopeAst.FindAll(item => item is FunctionDefinitionAst && reg.IsMatch((item as FunctionDefinitionAst).Name), true);
                            goto default;

                        case "class":
                            targetAsts = scopeAst.FindAll(item => item is TypeDefinitionAst && reg.IsMatch((item as TypeDefinitionAst).Name), true);
                            goto default;

                        default:
                            break;
                    }

                    if (targetAsts != null)
                    {
                        if (targetAsts.Count() == 0)
                        {
                            ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartAttributeLine,
                                System.IO.Path.GetFileName(scopeAst.Extent.File), String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            result.Add(ruleSupp);
                            continue;
                        }

                        foreach (Ast targetAst in targetAsts)
                        {
                            result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset,
                                targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber, ruleSupp.Justification));
                        }
                    }

                }
                else
                {
                    // this may add rule suppression that contains error but we will check for this in the engine to throw out error
                    result.Add(ruleSupp);
                }
            }

            return result;
        }
Пример #2
0
        /// <summary>
        /// Given a list of attribute asts, return a list of rule suppression
        /// with startoffset at start and endoffset at end
        /// </summary>
        /// <param name="attrAsts"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static List <RuleSuppression> GetSuppressions(IEnumerable <AttributeAst> attrAsts, int start, int end, Ast scopeAst)
        {
            List <RuleSuppression> result = new List <RuleSuppression>();

            if (attrAsts == null || scopeAst == null)
            {
                return(result);
            }

            IEnumerable <AttributeAst> suppressionAttribute = attrAsts.Where(
                item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));

            foreach (var attributeAst in suppressionAttribute)
            {
                RuleSuppression ruleSupp = new RuleSuppression(attributeAst, start, end);

                // If there is no error and scope is not null
                if (String.IsNullOrWhiteSpace(ruleSupp.Error) && !String.IsNullOrWhiteSpace(ruleSupp.Scope))
                {
                    if (String.IsNullOrWhiteSpace(ruleSupp.Target))
                    {
                        ruleSupp.Target = "*";
                    }

                    // According to documentation 'target' supports regular expression. But to maintain compatibility with
                    // previous implementation we interpret '*' as a glob and therefore replace '*' with '.*'
                    // regex for wild card *
                    Regex             reg        = new Regex(String.Format("^{0}$", ruleSupp.Target.Replace(@"*", ".*")), RegexOptions.IgnoreCase);
                    IEnumerable <Ast> targetAsts = null;

                    string scope = ruleSupp.Scope;
                    if (scope.Equals("function", StringComparison.OrdinalIgnoreCase))
                    {
                        targetAsts = scopeAst.FindAll(ast => ast is FunctionDefinitionAst && reg.IsMatch((ast as FunctionDefinitionAst).Name), true);
                    }
                    #if !(PSV3 || PSV4)
                    else if (scope.Equals("class", StringComparison.OrdinalIgnoreCase))
                    {
                        targetAsts = scopeAst.FindAll(ast => ast is TypeDefinitionAst && reg.IsMatch((ast as TypeDefinitionAst).Name), true);
                    }
                    #endif

                    if (targetAsts != null)
                    {
                        if (targetAsts.Count() == 0)
                        {
                            if (String.IsNullOrWhiteSpace(scopeAst.Extent.File))
                            {
                                ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormatScriptDefinition, ruleSupp.StartAttributeLine,
                                                               String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            }
                            else
                            {
                                ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartAttributeLine,
                                                               System.IO.Path.GetFileName(scopeAst.Extent.File), String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            }

                            result.Add(ruleSupp);
                            continue;
                        }

                        foreach (Ast targetAst in targetAsts)
                        {
                            result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset,
                                                           targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber, ruleSupp.Justification));
                        }
                    }
                }
                else
                {
                    // this may add rule suppression that contains error but we will check for this in the engine to throw out error
                    result.Add(ruleSupp);
                }
            }

            return(result);
        }
        /// <summary>
        /// Given a list of attribute asts, return a list of rule suppression
        /// with startoffset at start and endoffset at end
        /// </summary>
        /// <param name="attrAsts"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> attrAsts, int start, int end, Ast scopeAst)
        {
            List<RuleSuppression> result = new List<RuleSuppression>();

            if (attrAsts == null || scopeAst == null)
            {
                return result;
            }

            IEnumerable<AttributeAst> suppressionAttribute = attrAsts.Where(
                item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));

            foreach (var attributeAst in suppressionAttribute)
            {
                RuleSuppression ruleSupp = new RuleSuppression(attributeAst, start, end);

                // If there is no error and scope is not null
                if (String.IsNullOrWhiteSpace(ruleSupp.Error) && !String.IsNullOrWhiteSpace(ruleSupp.Scope))
                {
                    if (String.IsNullOrWhiteSpace(ruleSupp.Target))
                    {
                        ruleSupp.Target = "*";
                    }

                    // regex for wild card *
                    Regex reg = new Regex(String.Format("^{0}$", Regex.Escape(ruleSupp.Target).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
                    IEnumerable<Ast> targetAsts = null;

                    switch (ruleSupp.Scope.ToLower())
                    {
                        case "function":
                            targetAsts = scopeAst.FindAll(item => item is FunctionDefinitionAst && reg.IsMatch((item as FunctionDefinitionAst).Name), true);
                            goto default;

                        case "class":
                            targetAsts = scopeAst.FindAll(item => item is TypeDefinitionAst && reg.IsMatch((item as TypeDefinitionAst).Name), true);
                            goto default;

                        default:
                            break;
                    }

                    if (targetAsts != null)
                    {
                        if (targetAsts.Count() == 0)
                        {
                            ruleSupp.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSupp.StartAttributeLine,
                                System.IO.Path.GetFileName(scopeAst.Extent.File), String.Format(Strings.TargetCannotBeFoundError, ruleSupp.Target, ruleSupp.Scope));
                            result.Add(ruleSupp);
                            continue;
                        }

                        foreach (Ast targetAst in targetAsts)
                        {
                            result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset,
                                targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber, ruleSupp.Justification));
                        }
                    }

                }
                else
                {
                    // this may add rule suppression that contains error but we will check for this in the engine to throw out error
                    result.Add(ruleSupp);
                }
            }

            return result;
        }