From c569611abe184f6ca10c1bc9fcca8c05f03dee9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 18 Mar 2023 15:07:18 +0100 Subject: [PATCH] Update reading of file_header_template and stylecop.documentation.copyrightText to allow multiple lines #3605 --- .../Settings/SettingsCSharp8UnitTests.cs | 30 +++++++++++++++++++ .../ObjectModel/AnalyzerConfigHelper.cs | 6 ++++ .../ObjectModel/DocumentationSettings.cs | 4 +-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs index 8e8b75f1d..421aa621b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Settings/SettingsCSharp8UnitTests.cs @@ -69,6 +69,36 @@ public async Task VerifyEditorConfigSettingsAreReadCorrectlyAsync() Assert.Equal(OptionSetting.Allow, styleCopSettings.OrderingRules.BlankLinesBetweenUsingGroups); } + [Fact] + public async Task VerifyFileHeaderTemplateFromEditorConfigAsync() + { + var settings = @"root = true + +[*] +file_header_template = Line 1\nLine 2. +"; + var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); + + var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + + Assert.Equal("Line 1\nLine 2.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); + } + + [Fact] + public async Task VerifyStyleCopDocumentationCopyrightTextFromEditorConfigAsync() + { + var settings = @"root = true + +[*] +stylecop.documentation.copyrightText = Line 1\nLine 2. +"; + var context = await this.CreateAnalysisContextFromEditorConfigAsync(settings).ConfigureAwait(false); + + var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None); + + Assert.Equal("Line 1\nLine 2.", styleCopSettings.DocumentationRules.GetCopyrightText("unused")); + } + [Theory] [CombinatorialData] public async Task VerifyBooleanDocumentationSettingsFromEditorConfigAsync(bool value) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/AnalyzerConfigHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/AnalyzerConfigHelper.cs index b2875c5ce..0cfc4a307 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/AnalyzerConfigHelper.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/AnalyzerConfigHelper.cs @@ -51,6 +51,12 @@ internal static string TryGetStringValue(AnalyzerConfigOptionsWrapper analyzerCo return null; } + internal static string TryGetMultiLineStringValue(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key, bool allowExplicitUnset = true) + { + var orgValue = TryGetStringValue(analyzerConfigOptions, key, allowExplicitUnset); + return orgValue?.Replace("\\r", "\r").Replace("\\n", "\n"); + } + internal static KeyValuePair? TryGetStringValueAndNotification(AnalyzerConfigOptionsWrapper analyzerConfigOptions, string key, bool allowExplicitUnset = true) { if (analyzerConfigOptions.TryGetValue(key, out var value)) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs index 73bdac5d2..82698721b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs @@ -245,8 +245,8 @@ protected internal DocumentationSettings(JsonObject documentationSettingsObject, 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"); + copyrightText ??= AnalyzerConfigHelper.TryGetMultiLineStringValue(analyzerConfigOptions, "stylecop.documentation.copyrightText") + ?? AnalyzerConfigHelper.TryGetMultiLineStringValue(analyzerConfigOptions, "file_header_template"); headerDecoration ??= AnalyzerConfigHelper.TryGetStringValue(analyzerConfigOptions, "stylecop.documentation.headerDecoration"); xmlHeader ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.documentation.xmlHeader");