Example #1
0
            public void HandleVariableDeclarationSyntax(SyntaxNodeAnalysisContext context)
            {
                var syntax = (VariableDeclarationSyntax)context.Node;

                if (syntax.Parent.IsKind(SyntaxKind.EventFieldDeclaration))
                {
                    return;
                }

                if (NamedTypeHelpers.IsContainedInNativeMethodsClass(syntax))
                {
                    return;
                }

                var fieldDeclaration = syntax.Parent.IsKind(SyntaxKind.FieldDeclaration);

                foreach (var variableDeclarator in syntax.Variables)
                {
                    if (variableDeclarator == null)
                    {
                        continue;
                    }

                    var identifier = variableDeclarator.Identifier;
                    if (identifier.IsMissing)
                    {
                        continue;
                    }

                    string name = identifier.ValueText;
                    if (string.IsNullOrEmpty(name))
                    {
                        continue;
                    }

                    var match = HungarianRegex.Match(name);
                    if (!match.Success)
                    {
                        continue;
                    }

                    var notationValue = match.Groups["notation"].Value;
                    if (this.namingSettings.AllowCommonHungarianPrefixes && CommonPrefixes.Contains(notationValue))
                    {
                        continue;
                    }

                    if (this.namingSettings.AllowedHungarianPrefixes.Contains(notationValue))
                    {
                        continue;
                    }

                    // Variable names must begin with lower-case letter
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, identifier.GetLocation(), fieldDeclaration ? "field" : "variable", name));
                }
            }
Example #2
0
            private static void CheckIdentifier(SyntaxNodeAnalysisContext context, SyntaxToken identifier, StyleCopSettings settings, string declarationType = "variable")
            {
                if (identifier.IsMissing)
                {
                    return;
                }

                string name = identifier.ValueText;

                if (string.IsNullOrEmpty(name))
                {
                    return;
                }

                var match = HungarianRegex.Match(name);

                if (!match.Success)
                {
                    return;
                }

                var notationValue = match.Groups["notation"].Value;

                if (settings.NamingRules.AllowCommonHungarianPrefixes && CommonPrefixes.Contains(notationValue))
                {
                    return;
                }

                if (settings.NamingRules.AllowedHungarianPrefixes.Contains(notationValue))
                {
                    return;
                }

                // Variable names should begin with lower-case letter
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, identifier.GetLocation(), declarationType, name));
            }