Exemple #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);
        }
Exemple #2
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 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);
        }