Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LayoutSettings"/> class.
        /// </summary>
        /// <param name="layoutSettingsObject">The JSON object containing the settings.</param>
        /// <param name="analyzerConfigOptions">The <strong>.editorconfig</strong> options to use if
        /// <strong>stylecop.json</strong> does not provide values.</param>
        protected internal LayoutSettings(JsonObject layoutSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            OptionSetting?newlineAtEndOfFile     = null;
            bool?         allowConsecutiveUsings = null;

            foreach (var kvp in layoutSettingsObject)
            {
                switch (kvp.Key)
                {
                case "newlineAtEndOfFile":
                    newlineAtEndOfFile = kvp.ToEnumValue <OptionSetting>();
                    break;

                case "allowConsecutiveUsings":
                    allowConsecutiveUsings = kvp.ToBooleanValue();
                    break;

                default:
                    break;
                }
            }

            newlineAtEndOfFile ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "insert_final_newline") switch
            {
                true => OptionSetting.Require,
                false => OptionSetting.Omit,
                _ => null,
            };

            allowConsecutiveUsings ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.layout.allowConsecutiveUsings");

            this.newlineAtEndOfFile     = newlineAtEndOfFile.GetValueOrDefault(OptionSetting.Allow);
            this.allowConsecutiveUsings = allowConsecutiveUsings.GetValueOrDefault(true);
        }
Beispiel #2
0
        internal static ImmutableArray <string>?TryGetStringListValue(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key, bool allowExplicitUnset = true)
        {
            if (analyzerConfigOptions.TryGetValue(key, out var value))
            {
                if (allowExplicitUnset && value == "unset")
                {
                    return(null);
                }

                return(value.Split(',').Select(static x => x.Trim()).ToImmutableArray());
Beispiel #3
0
        internal static int?TryGetInt32Value(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key)
        {
            if (analyzerConfigOptions.TryGetValue(key, out var value) &&
                value != "unset" &&
                int.TryParse(value, out var intValue))
            {
                return(intValue);
            }

            return(null);
        }
Beispiel #4
0
        internal static bool?TryGetBooleanValue(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key)
        {
            if (analyzerConfigOptions.TryGetValue(key, out var value) &&
                value != "unset" &&
                bool.TryParse(value, out var boolValue))
            {
                return(boolValue);
            }

            return(null);
        }
Beispiel #5
0
        internal static string TryGetStringValue(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key, bool allowExplicitUnset = true)
        {
            if (analyzerConfigOptions.TryGetValue(key, out var value))
            {
                if (allowExplicitUnset && value == "unset")
                {
                    return(null);
                }

                return(value);
            }

            return(null);
        }
Beispiel #6
0
        protected internal OrderingSettings(JsonObject orderingSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            ImmutableArray <OrderingTrait> .Builder elementOrder = null;
            bool?systemUsingDirectivesFirst = null;
            UsingDirectivesPlacement?usingDirectivesPlacement     = null;
            OptionSetting?           blankLinesBetweenUsingGroups = null;

            foreach (var kvp in orderingSettingsObject)
            {
                switch (kvp.Key)
                {
                case "elementOrder":
                    kvp.AssertIsArray();
                    elementOrder = ImmutableArray.CreateBuilder <OrderingTrait>();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        elementOrder.Add(value.ToEnumValue <OrderingTrait>(kvp.Key));
                    }

                    break;

                case "systemUsingDirectivesFirst":
                    systemUsingDirectivesFirst = kvp.ToBooleanValue();
                    break;

                case "usingDirectivesPlacement":
                    usingDirectivesPlacement = kvp.ToEnumValue <UsingDirectivesPlacement>();
                    break;

                case "blankLinesBetweenUsingGroups":
                    blankLinesBetweenUsingGroups = kvp.ToEnumValue <OptionSetting>();
                    break;

                default:
                    break;
                }
            }

            systemUsingDirectivesFirst ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "dotnet_sort_system_directives_first");
            usingDirectivesPlacement ??= AnalyzerConfigHelper.TryGetStringValueAndNotification(analyzerConfigOptions, "csharp_using_directive_placement") switch
            {
                ("inside_namespace", _) => UsingDirectivesPlacement.InsideNamespace,
                ("outside_namespace", _) => UsingDirectivesPlacement.OutsideNamespace,
                _ => null,
            };
        protected internal ReadabilitySettings(JsonObject readabilitySettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            bool?allowBuiltInTypeAliases = null;

            foreach (var kvp in readabilitySettingsObject)
            {
                switch (kvp.Key)
                {
                case "allowBuiltInTypeAliases":
                    allowBuiltInTypeAliases = kvp.ToBooleanValue();
                    break;

                default:
                    break;
                }
            }

            allowBuiltInTypeAliases ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.readability.allowBuiltInTypeAliases");

            this.allowBuiltInTypeAliases = allowBuiltInTypeAliases.GetValueOrDefault(false);
        }
        protected internal IndentationSettings(JsonObject indentationSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            int? indentationSize = null;
            int? tabSize         = null;
            bool?useTabs         = null;

            foreach (var kvp in indentationSettingsObject)
            {
                switch (kvp.Key)
                {
                case "indentationSize":
                    indentationSize = kvp.ToInt32Value();
                    break;

                case "tabSize":
                    tabSize = kvp.ToInt32Value();
                    break;

                case "useTabs":
                    useTabs = kvp.ToBooleanValue();
                    break;

                default:
                    break;
                }
            }

            indentationSize ??= AnalyzerConfigHelper.TryGetInt32Value(analyzerConfigOptions, "indent_size");
            tabSize ??= AnalyzerConfigHelper.TryGetInt32Value(analyzerConfigOptions, "tab_width");
            useTabs ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "indent_style") switch
            {
                "tab" => true,
                "space" => false,
                _ => null,
            };

            this.indentationSize = indentationSize.GetValueOrDefault(4);
            this.tabSize         = tabSize.GetValueOrDefault(4);
            this.useTabs         = useTabs.GetValueOrDefault(false);
        }
        protected internal DocumentationSettings(JsonObject documentationSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            bool?  documentExposedElements  = null;
            bool?  documentInternalElements = null;
            bool?  documentPrivateElements  = null;
            bool?  documentInterfaces       = null;
            bool?  documentPrivateFields    = null;
            string companyName      = null;
            string copyrightText    = null;
            string headerDecoration = null;

            ImmutableDictionary <string, string> .Builder variables = null;
            bool?xmlHeader = null;
            FileNamingConvention?fileNamingConvention = null;
            string documentationCulture = null;

            ImmutableArray <string> .Builder excludeFromPunctuationCheck = null;

            foreach (var kvp in documentationSettingsObject)
            {
                switch (kvp.Key)
                {
                case "documentExposedElements":
                    documentExposedElements = kvp.ToBooleanValue();
                    break;

                case "documentInternalElements":
                    documentInternalElements = kvp.ToBooleanValue();
                    break;

                case "documentPrivateElements":
                    documentPrivateElements = kvp.ToBooleanValue();
                    break;

                case "documentInterfaces":
                    documentInterfaces = kvp.ToBooleanValue();
                    break;

                case "documentPrivateFields":
                    documentPrivateFields = kvp.ToBooleanValue();
                    break;

                case "companyName":
                    companyName = kvp.ToStringValue();
                    break;

                case "copyrightText":
                    copyrightText = kvp.ToStringValue();
                    break;

                case "headerDecoration":
                    headerDecoration = kvp.ToStringValue();
                    break;

                case "variables":
                    kvp.AssertIsObject();
                    variables = ImmutableDictionary.CreateBuilder <string, string>();
                    foreach (var child in kvp.Value.AsJsonObject)
                    {
                        string name = child.Key;

                        if (!Regex.IsMatch(name, "^[a-zA-Z0-9]+$"))
                        {
                            continue;
                        }

                        string value = child.ToStringValue();

                        variables.Add(name, value);
                    }

                    break;

                case "xmlHeader":
                    xmlHeader = kvp.ToBooleanValue();
                    break;

                case "fileNamingConvention":
                    fileNamingConvention = kvp.ToEnumValue <FileNamingConvention>();
                    break;

                case "documentationCulture":
                    documentationCulture = kvp.ToStringValue();
                    break;

                case "excludeFromPunctuationCheck":
                    kvp.AssertIsArray();
                    excludeFromPunctuationCheck = ImmutableArray.CreateBuilder <string>();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        excludeFromPunctuationCheck.Add(value.AsString);
                    }

                    break;

                default:
                    break;
                }
            }

            documentExposedElements ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.documentExposedElements");
            documentInternalElements ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.documentInternalElements");
            documentPrivateElements ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.documentPrivateElements");
            documentInterfaces ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.documentInterfaces");
            documentPrivateFields ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.documentPrivateFields");

            companyName ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.companyName");
            copyrightText ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.copyrightText")
            ?? AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "file_header_template");
            headerDecoration ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.headerDecoration");

            xmlHeader ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.xmlHeader");
            fileNamingConvention ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.fileNamingConvention") switch
            {
                "stylecop" => FileNamingConvention.StyleCop,
                "metadata" => FileNamingConvention.Metadata,
                _ => null,
            };

            documentationCulture ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.documentationCulture");
            excludeFromPunctuationCheck ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.documentation.excludeFromPunctuationCheck")?.ToBuilder();

            this.documentExposedElements  = documentExposedElements.GetValueOrDefault(true);
            this.documentInternalElements = documentInternalElements.GetValueOrDefault(true);
            this.documentPrivateElements  = documentPrivateElements.GetValueOrDefault(false);
            this.documentInterfaces       = documentInterfaces.GetValueOrDefault(true);
            this.documentPrivateFields    = documentPrivateFields.GetValueOrDefault(false);
            this.companyName                 = companyName ?? DefaultCompanyName;
            this.copyrightText               = copyrightText ?? DefaultCopyrightText;
            this.headerDecoration            = headerDecoration;
            this.variables                   = variables?.ToImmutable() ?? ImmutableDictionary <string, string> .Empty;
            this.xmlHeader                   = xmlHeader.GetValueOrDefault(true);
            this.fileNamingConvention        = fileNamingConvention.GetValueOrDefault(FileNamingConvention.StyleCop);
            this.documentationCulture        = documentationCulture ?? DefaultDocumentationCulture;
            this.documentationCultureInfo    = this.documentationCulture == DefaultDocumentationCulture ? CultureInfo.InvariantCulture : new CultureInfo(this.documentationCulture);
            this.excludeFromPunctuationCheck = excludeFromPunctuationCheck?.ToImmutable() ?? DefaultExcludeFromPunctuationCheck;
        }
        protected internal StyleCopSettings(JsonObject settingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            foreach (var kvp in settingsObject)
            {
                var childSettingsObject = kvp.Value.AsJsonObject;
                switch (kvp.Key)
                {
                case "indentation":
                    kvp.AssertIsObject();
                    this.indentation = new IndentationSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "spacingRules":
                    kvp.AssertIsObject();
                    this.spacingRules = new SpacingSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "readabilityRules":
                    kvp.AssertIsObject();
                    this.readabilityRules = new ReadabilitySettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "orderingRules":
                    kvp.AssertIsObject();
                    this.orderingRules = new OrderingSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "namingRules":
                    kvp.AssertIsObject();
                    this.namingRules = new NamingSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "maintainabilityRules":
                    kvp.AssertIsObject();
                    this.maintainabilityRules = new MaintainabilitySettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "layoutRules":
                    kvp.AssertIsObject();
                    this.layoutRules = new LayoutSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                case "documentationRules":
                    kvp.AssertIsObject();
                    this.documentationRules = new DocumentationSettings(childSettingsObject, analyzerConfigOptions);
                    break;

                default:
                    break;
                }
            }

            this.indentation ??= new IndentationSettings(new JsonObject(), analyzerConfigOptions);

            this.spacingRules ??= new SpacingSettings(new JsonObject(), analyzerConfigOptions);
            this.readabilityRules ??= new ReadabilitySettings(new JsonObject(), analyzerConfigOptions);
            this.orderingRules ??= new OrderingSettings(new JsonObject(), analyzerConfigOptions);
            this.namingRules ??= new NamingSettings(new JsonObject(), analyzerConfigOptions);
            this.maintainabilityRules ??= new MaintainabilitySettings(new JsonObject(), analyzerConfigOptions);
            this.layoutRules ??= new LayoutSettings(new JsonObject(), analyzerConfigOptions);
            this.documentationRules ??= new DocumentationSettings(new JsonObject(), analyzerConfigOptions);
        }
Beispiel #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MaintainabilitySettings"/> class.
        /// </summary>
        /// <param name="maintainabilitySettingsObject">The JSON object containing the settings.</param>
        /// <param name="analyzerConfigOptions">The <strong>.editorconfig</strong> options to use if
        /// <strong>stylecop.json</strong> does not provide values.</param>
        protected internal MaintainabilitySettings(JsonObject maintainabilitySettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            ImmutableArray <TopLevelType> .Builder topLevelTypes = null;

            foreach (var kvp in maintainabilitySettingsObject)
            {
                switch (kvp.Key)
                {
                case "topLevelTypes":
                    kvp.AssertIsArray();
                    topLevelTypes = ImmutableArray.CreateBuilder <TopLevelType>();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        var typeKind = value.ToEnumValue <TopLevelType>(kvp.Key);
                        topLevelTypes.Add(typeKind);
                    }

                    break;

                default:
                    break;
                }
            }

            this.topLevelTypes = topLevelTypes?.ToImmutable() ?? ImmutableArray <TopLevelType> .Empty;
        }
        protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            bool?allowCommonHungarianPrefixes = null;

            ImmutableArray <string> .Builder allowedHungarianPrefixes   = null;
            ImmutableArray <string> .Builder allowedNamespaceComponents = null;
            bool?includeInferredTupleElementNames       = null;
            TupleElementNameCase?tupleElementNameCasing = null;

            foreach (var kvp in namingSettingsObject)
            {
                switch (kvp.Key)
                {
                case "allowCommonHungarianPrefixes":
                    allowCommonHungarianPrefixes = kvp.ToBooleanValue();
                    break;

                case "allowedHungarianPrefixes":
                    kvp.AssertIsArray();
                    allowedHungarianPrefixes = ImmutableArray.CreateBuilder <string>();
                    foreach (var prefixJsonValue in kvp.Value.AsJsonArray)
                    {
                        var prefix = prefixJsonValue.ToStringValue(kvp.Key);

                        if (!Regex.IsMatch(prefix, "^[a-z]{1,2}$"))
                        {
                            continue;
                        }

                        allowedHungarianPrefixes.Add(prefix);
                    }

                    break;

                case "allowedNamespaceComponents":
                    kvp.AssertIsArray();
                    allowedNamespaceComponents = ImmutableArray.CreateBuilder <string>();
                    allowedNamespaceComponents.AddRange(kvp.Value.AsJsonArray.Select(x => x.ToStringValue(kvp.Key)));
                    break;

                case "includeInferredTupleElementNames":
                    includeInferredTupleElementNames = kvp.ToBooleanValue();
                    break;

                case "tupleElementNameCasing":
                    tupleElementNameCasing = kvp.ToEnumValue <TupleElementNameCase>();
                    break;

                default:
                    break;
                }
            }

            allowCommonHungarianPrefixes ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.allowCommonHungarianPrefixes");
            allowedHungarianPrefixes ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedHungarianPrefixes")
            ?.Where(value => Regex.IsMatch(value, "^[a-z]{1,2}$"))
            .ToImmutableArray()
            .ToBuilder();
            allowedNamespaceComponents ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedNamespaceComponents")?.ToBuilder();
            includeInferredTupleElementNames ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.includeInferredTupleElementNames");
            tupleElementNameCasing ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.naming.tupleElementNameCasing") switch
            {
                "camelCase" => TupleElementNameCase.CamelCase,
                "pascalCase" => TupleElementNameCase.PascalCase,
                _ => null,
            };

            this.AllowCommonHungarianPrefixes = allowCommonHungarianPrefixes.GetValueOrDefault(true);
            this.AllowedHungarianPrefixes     = allowedHungarianPrefixes?.ToImmutable() ?? ImmutableArray <string> .Empty;
            this.AllowedNamespaceComponents   = allowedNamespaceComponents?.ToImmutable() ?? ImmutableArray <string> .Empty;

            this.IncludeInferredTupleElementNames = includeInferredTupleElementNames.GetValueOrDefault(false);
            this.TupleElementNameCasing           = tupleElementNameCasing.GetValueOrDefault(TupleElementNameCase.PascalCase);
        }
Beispiel #13
0
        internal static KeyValuePair <string, string>?TryGetStringValueAndNotification(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key, bool allowExplicitUnset = true)
        {
            if (analyzerConfigOptions.TryGetValue(key, out var value))
            {
                if (allowExplicitUnset && value == "unset")
                {
                    return(null);
                }

                var colonIndex = value.IndexOf(':');
                if (colonIndex >= 0)
                {
                    return(new KeyValuePair <string, string>(value.Substring(0, colonIndex), value.Substring(colonIndex + 1)));
                }
            }

            return(null);
        }
Beispiel #14
0
 protected internal SpacingSettings(JsonObject spacingSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
 {
     // Currently unused
     _ = spacingSettingsObject;
     _ = analyzerConfigOptions;
 }