예제 #1
0
        private static (object result, bool succeeded) ParseDictionary(
            IReadOnlyDictionary <string, string?> allRawConventions,
            Type type
            )
        {
            if (type == typeof(NamingStylePreferences))
            {
                var editorconfigNamingStylePreferences =
                    EditorConfigNamingStyleParser.GetNamingStylesFromDictionary(allRawConventions);

                if (
                    !editorconfigNamingStylePreferences.NamingRules.Any() &&
                    !editorconfigNamingStylePreferences.NamingStyles.Any() &&
                    !editorconfigNamingStylePreferences.SymbolSpecifications.Any()
                    )
                {
                    // We were not able to parse any rules from editorconfig, tell the caller that the parse failed
                    return(result : editorconfigNamingStylePreferences, succeeded : false);
                }

                // no existing naming styles were passed so just return the set of styles that were parsed from editorconfig
                return(result : editorconfigNamingStylePreferences, succeeded : true);
            }

            throw ExceptionUtilities.UnexpectedValue(type);
        }
        private static (object result, bool succeeded) ParseDictionary(
            object underlyingOption, IReadOnlyDictionary <string, object> allRawConventions, Type type)
        {
            if (type == typeof(NamingStylePreferences))
            {
                var editorconfigNamingStylePreferences = EditorConfigNamingStyleParser.GetNamingStylesFromDictionary(allRawConventions);

                if (!editorconfigNamingStylePreferences.NamingRules.Any() &&
                    !editorconfigNamingStylePreferences.NamingStyles.Any() &&
                    !editorconfigNamingStylePreferences.SymbolSpecifications.Any())
                {
                    // We were not able to parse any rules from editorconfig, tell the caller that the parse failed
                    return(result : editorconfigNamingStylePreferences, succeeded : false);
                }

                if (underlyingOption is NamingStylePreferences workspaceNamingStylePreferences)
                {
                    // We parsed naming styles from editorconfig, append them to our existing styles
                    var combinedNamingStylePreferences = workspaceNamingStylePreferences.PrependNamingStylePreferences(editorconfigNamingStylePreferences);
                    return(result : combinedNamingStylePreferences, succeeded : true);
                }

                // no existing naming styles were passed so just return the set of styles that were parsed from editorconfig
                return(result : editorconfigNamingStylePreferences, succeeded : true);
            }
            else
            {
                return(Contract.FailWithReturn <(object, bool)>(
                           $"{nameof(NamingStylePreferenceEditorConfigStorageLocation)} can only be called with {nameof(PerLanguageOption<NamingStylePreferences>)}<{nameof(NamingStylePreferences)}>."));
            }
        }
예제 #3
0
        public static void TestEditorConfigParseForApplicableSymbolKinds()
        {
            var symbolSpecifications = CreateDefaultSymbolSpecification();

            foreach (var applicableSymbolKind in symbolSpecifications.ApplicableSymbolKindList)
            {
                var editorConfigString = EditorConfigNamingStyleParser.ToEditorConfigString(ImmutableArray.Create(applicableSymbolKind));
                Assert.True(!string.IsNullOrEmpty(editorConfigString));
            }
        }
예제 #4
0
 public EditorConfigStorageLocation()
 {
     // If the user didn't pass a keyName assume we need to parse the entire dictionary
     _parseDictionary = (dictionary, type) =>
     {
         if (type == typeof(NamingStylePreferences))
         {
             return(EditorConfigNamingStyleParser.GetNamingStylesFromDictionary(dictionary));
         }
         else
         {
             throw new NotSupportedException(WorkspacesResources.Option_0_has_an_unsupported_type_to_use_with_1_You_should_specify_a_parsing_function);
         }
     };
 }
 public Implementation(AnalyzerConfigOptions options)
 {
     _options = options;
     _lazyNamingStylePreferences = new Lazy <NamingStylePreferences>(() => EditorConfigNamingStyleParser.ParseDictionary(_options));
 }
        private void ValidateNamingStyles(Section section)
        {
            NamingStylePreferences namingStyle = EditorConfigNamingStyleParser.GetNamingStyles(section.Properties);

            IOrderedEnumerable <NamingRule> orderedRules = namingStyle.Rules.NamingRules
                                                           .OrderBy(rule => rule, NamingRuleModifierListComparer.Instance)
                                                           .ThenBy(rule => rule, NamingRuleAccessibilityListComparer.Instance)
                                                           .ThenBy(rule => rule, NamingRuleSymbolListComparer.Instance)
                                                           .ThenBy(rule => rule.Name, StringComparer.OrdinalIgnoreCase)
                                                           .ThenBy(rule => rule.Name, StringComparer.Ordinal);

            var orderedRulePreferences = new NamingStylePreferences(
                namingStyle.SymbolSpecifications,
                namingStyle.NamingStyles,
                orderedRules.Select(
                    rule => new SerializableNamingRule
            {
                Name = rule.Name,
                SymbolSpecificationID = rule.SymbolSpecification.ID,
                NamingStyleID         = rule.NamingStyle.ID,
                EnforcementLevel      = rule.EnforcementLevel,
            }).ToImmutableArray());

            var reorderedOverlappingRules = new List <(string firstRule, string secondRule)>();
            ImmutableArray <NamingRule> declaredNamingRules = namingStyle.Rules.NamingRules;
            ImmutableArray <NamingRule> orderedNamingRules  = orderedRulePreferences.Rules.NamingRules;

            for (int i = 0; i < declaredNamingRules.Length; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    NamingRule firstRule  = declaredNamingRules[j];
                    NamingRule secondRule = declaredNamingRules[i];

                    // If the reordered rules are in the same relative order, no need to check the overlap
                    bool reordered = false;
                    foreach (NamingRule rule in orderedNamingRules)
                    {
                        if (rule.Name == firstRule.Name)
                        {
                            break;
                        }
                        else if (rule.Name == secondRule.Name)
                        {
                            reordered = true;
                            break;
                        }
                    }

                    if (!reordered)
                    {
                        continue;
                    }

                    // If the rules don't overlap, reordering is not a problem
                    if (!Overlaps(firstRule, secondRule))
                    {
                        continue;
                    }

                    reorderedOverlappingRules.Add((firstRule.Name, secondRule.Name));
                }
            }

            var reportedRules = new HashSet <string>();

            foreach (Property property in section.Properties)
            {
                string name = property.Keyword.Text.Trim();
                if (!name.StartsWith("dotnet_naming_rule."))
                {
                    continue;
                }

                string[] nameSplit = name.Split('.');
                if (nameSplit.Length != 3)
                {
                    continue;
                }

                string ruleName = nameSplit[1];
                if (!reportedRules.Add(ruleName))
                {
                    continue;
                }

                foreach ((string firstRule, string secondRule) in reorderedOverlappingRules)
                {
                    if (secondRule != ruleName)
                    {
                        continue;
                    }

                    ErrorCatalog.NamingRuleReordered.Run(property.Keyword, e =>
                    {
                        e.Register(secondRule, firstRule);
                    });
                }
            }
        }
예제 #7
0
 private static NamingStylePreferences ParseDictionary(Dictionary <string, string> options)
 => EditorConfigNamingStyleParser.ParseDictionary(new DictionaryAnalyzerConfigOptions(options.ToImmutableDictionary()));