Ejemplo n.º 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));
                }
            }
Ejemplo n.º 2
0
        public RelatedUnit WithPrefixedUnits(string unitShortName, string fieldName, CommonPrefixes kilo)
        {
            void Add(string unitPrefix, string fieldPrefix, CommonPrefixes flag, decimal factor)
            {
                if ((kilo & flag) != 0)
                {
                    WithPrefixedUnit(unitPrefix + unitShortName, fieldPrefix + fieldName, factor);
                }
            }

            Add("k", "Kilo", CommonPrefixes.Kilo, 1_000);
            Add("M", "Mega", CommonPrefixes.Mega, 1_000_000);
            Add("G", "Giga", CommonPrefixes.Giga, 1_000_000_000);
            Add("m", "Mili", CommonPrefixes.Mili, 0.001m);
            Add("µ", "Micro", CommonPrefixes.Micro, 0.000_001m);
            return(this);
        }
Ejemplo n.º 3
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));
            }