/// <summary>
        /// Check for prefixes.
        /// </summary>
        /// <param name="prefix">Expected prefix.</param>
        /// <param name="token">Token to check.</param>
        /// <param name="rule">Rule descriptor.</param>
        /// <param name="predicate">Naming rule for the remaining bits.</param>
        /// <param name="report">Reporting function.</param>
        private static void CheckPrefix(
            string prefix,
            SyntaxToken token,
            RuleDescription rule,
            Func <string, bool> predicate,
            Action <Diagnostic> report)
        {
            // Get the name.
            var name = token.ToString();

            // Chekc for prefix rules.
            if (name.StartsWith(prefix, StringComparison.Ordinal) &&
                name.Length > prefix.Length &&
                predicate(name.Substring(prefix.Length, 1)))
            {
                // Prefix in order. Stop processing.
                return;
            }

            // Prefix faulty. Report warning.
            var diagnostic = Diagnostic.Create(
                rule.Rule,
                token.GetLocation(), name);

            report(diagnostic);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check naming for syntax token.
        /// </summary>
        /// <param name="identifier">Token identifier.</param>
        /// <param name="name">Name to check.</param>
        /// <param name="ruleDescription">Rule to check for.</param>
        /// <param name="predicate">Name condition.</param>
        /// <param name="report">Fucntion used to report the diagnostic findings.</param>
        /// <param name="args">Format args.</param>
        private static void CheckName(
            SyntaxToken identifier,
            string name,
            RuleDescription ruleDescription,
            Func <string, bool> predicate,
            Action <Diagnostic> report,
            params object[] args)
        {
            // If the name is empty the checks don't apply.
            //
            // Either the name really is empty or this should have been caught somewhere
            // else. This is usually a case of prefixed names after prefix stripping.
            if (name == "")
            {
                return;
            }

            // Check the name.
            if (!predicate(name))
            {
                // Name is faulty. Report.
                var formatParams = new List <object>(args)
                {
                    name
                };
                var diagnostic = Diagnostic.Create(
                    ruleDescription.Rule,
                    identifier.GetLocation(),
                    formatParams.ToArray());
                report(diagnostic);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Check naming rules.
 /// </summary>
 /// <param name="context">Analysis context.</param>
 /// <param name="ruleDescription">Rule to check for.</param>
 /// <param name="predicate">Name condition.</param>
 /// <param name="args">Format args.</param>
 private static void CheckName(
     SymbolAnalysisContext context,
     RuleDescription ruleDescription,
     Func <string, bool> predicate,
     params object[] args)
 {
     // Delegate.
     CheckName(context, context.Symbol.Name, ruleDescription, predicate, args);
 }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="rule">Diagnostic rule the fix is responsible for.</param>
        public FixDescription(RuleDescription rule)
        {
            // Store the rule.
            this.Rule = rule;

            // Load the resources.
            this.Title = new LocalizableResourceString(
                rule.Name + "_Fix", Resources.ResourceManager, typeof(Resources)).ToString();
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Check naming for syntax token.
 /// </summary>
 /// <param name="identifier">Token identifier.</param>
 /// <param name="ruleDescription">Rule to check for.</param>
 /// <param name="predicate">Name condition.</param>
 /// <param name="report">Fucntion used to report the diagnostic findings.</param>
 /// <param name="args">Format args.</param>
 private static void CheckName(
     SyntaxToken identifier,
     RuleDescription ruleDescription,
     Func <string, bool> predicate,
     Action <Diagnostic> report,
     params object[] args)
 {
     // Delegate.
     CheckName(identifier, identifier.ToString(), ruleDescription, predicate, report);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Check naming rules.
        /// </summary>
        /// <param name="context">Analysis context.</param>
        /// <param name="name">Name to check.</param>
        /// <param name="ruleDescription">Rule to check for.</param>
        /// <param name="predicate">Name condition.</param>
        /// <param name="args">Format args.</param>
        private static void CheckName(
            SymbolAnalysisContext context,
            string name,
            RuleDescription ruleDescription,
            Func <string, bool> predicate,
            params object[] args)
        {
            // If this is implicit symbol, skip it for naming checks.
            if (context.Symbol.IsImplicitlyDeclared)
            {
                return;
            }

            // If this can't be referenced by name (property getters/setters)
            // we don't check for naming rules.
            if (!context.Symbol.CanBeReferencedByName)
            {
                return;
            }

            // If the name is empty the checks don't apply.
            //
            // Either the name really is empty or this should have been caught somewhere
            // else. This is usually a case of prefixed names after prefix stripping.
            if (name == "")
            {
                return;
            }

            // Check for predicate.
            if (!predicate(name))
            {
                // Naming rules not in order.

                // Report each location.
                foreach (var location in context.Symbol.Locations)
                {
                    // Create diagnostic and report.
                    var formatParams = new List <object>(args)
                    {
                        context.Symbol.Name
                    };
                    var diagnostic = Diagnostic.Create(
                        ruleDescription.Rule,
                        location,
                        formatParams.ToArray());
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Check for prefixes.
        /// </summary>
        /// <param name="prefix">Expected prefix.</param>
        /// <param name="token">Token to check.</param>
        /// <param name="rule">Rule descriptor.</param>
        /// <param name="predicate">Naming rule for the remaining bits.</param>
        /// <param name="report">Reporting function.</param>
        /// <returns>Name after the prefix.</returns>
        private static string CheckPrefix(
            string prefix,
            SyntaxToken token,
            RuleDescription rule,
            Func <string, bool> predicate,
            Action <Diagnostic> report
            )
        {
            // Get the name.
            var name = token.ToString();

            // Check for prefix rules.
            String remainingName = name;

            if (name.StartsWith(prefix, StringComparison.Ordinal))
            {
                // Prefix exists.
                // Strip the prefix from the remaining checks.
                remainingName = name.Substring(prefix.Length);
            }
            else
            {
                // Prefix faulty. Report warning.
                var diagnostic = Diagnostic.Create(
                    rule.Rule,
                    token.GetLocation(), name);
                report(diagnostic);
            }

            // Check that the remaining name exists.
            if (remainingName.Length == 0)
            {
                // There was no remaining name. Report the error.
                var diagnostic = Diagnostic.Create(
                    NamePrefixedNamesWithDescriptiveName.Rule,
                    token.GetLocation(), name);
                report(diagnostic);
            }

            // Return the remaining name for further checks.
            return(remainingName);
        }
        /// <summary>
        /// Check naming rules.
        /// </summary>
        /// <param name="context">Analysis context.</param>
        /// <param name="ruleDescription">Rule to check for.</param>
        /// <param name="predicate">Name condition.</param>
        /// <param name="args">Format args.</param>
        private static void CheckName(
            SymbolAnalysisContext context,
            RuleDescription ruleDescription,
            Func <string, bool> predicate,
            params object[] args)
        {
            // If this is implicit symbol, skip it for naming checks.
            if (context.Symbol.IsImplicitlyDeclared)
            {
                return;
            }

            // If this can't be referenced by name (property getters/setters)
            // we don't check for naming rules.
            if (!context.Symbol.CanBeReferencedByName)
            {
                return;
            }

            // Check for predicate.
            if (!predicate(context.Symbol.Name))
            {
                // Naming rules not in order.

                // Report each location.
                foreach (var location in context.Symbol.Locations)
                {
                    // Create diagnostic and report.
                    var formatParams = new List <object>(args)
                    {
                        context.Symbol.Name
                    };
                    var diagnostic = Diagnostic.Create(
                        ruleDescription.Rule,
                        location,
                        formatParams.ToArray());
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
        /// <summary>
        /// Check naming for syntax token.
        /// </summary>
        /// <param name="context">Analysis context.</param>
        /// <param name="identifier">Token identifier.</param>
        /// <param name="ruleDescription">Rule to check for.</param>
        /// <param name="predicate">Name condition.</param>
        /// <param name="args">Format args.</param>
        private static void CheckName(
            SyntaxNodeAnalysisContext context,
            SyntaxToken identifier,
            RuleDescription ruleDescription,
            Func <string, bool> predicate,
            params object[] args)
        {
            // Check the name.
            var name = identifier.ToString();

            if (!predicate(name))
            {
                // Name is faulty. Report.
                var formatParams = new List <object>(args)
                {
                    name
                };
                var diagnostic = Diagnostic.Create(
                    ruleDescription.Rule,
                    identifier.GetLocation(),
                    formatParams.ToArray());
                context.ReportDiagnostic(diagnostic);
            }
        }