Skip to content

Commit

Permalink
Update CodeStyleOption<T> parsing to ignore trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Aug 16, 2024
1 parent d797c32 commit bf03a75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public class EditorConfigCodeStyleParserTests
[InlineData("false : warning", false, ReportDiagnostic.Warn)]
[InlineData("true : error", true, ReportDiagnostic.Error)]
[InlineData("false : error", false, ReportDiagnostic.Error)]

[WorkItem("https://github.com/dotnet/roslyn/issues/44596")]
[InlineData("true:warning # comment", true, ReportDiagnostic.Warn)]
[InlineData("false:warning # comment", false, ReportDiagnostic.Warn)]
[InlineData("true:error # comment", true, ReportDiagnostic.Error)]
[InlineData("false:error # comment", false, ReportDiagnostic.Error)]
[InlineData("true:warning ; comment", true, ReportDiagnostic.Warn)]
[InlineData("false:warning ; comment", false, ReportDiagnostic.Warn)]
[InlineData("true:error ; comment", true, ReportDiagnostic.Error)]
[InlineData("false:error ; comment", false, ReportDiagnostic.Error)]
public void TestParseEditorConfigCodeStyleOption(string args, bool isEnabled, ReportDiagnostic severity)
{
CodeStyleHelpers.TryParseBoolEditorConfigCodeStyleOption(args, defaultValue: CodeStyleOption2<bool>.Default, out var result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ namespace Microsoft.CodeAnalysis.CodeStyle
{
internal static class CodeStyleHelpers
{
/// <summary>
/// Delimiters which the compiler previously treated as embedded comment delimiters for parsing EditorConfig
/// files. For code style options maintained by Roslyn, we continue to detect and remove these "comments" for
/// backwards compatibility.
/// </summary>
private static readonly char[] s_commentDelimiters = ['#', ';'];

public static bool TryParseStringEditorConfigCodeStyleOption(string arg, CodeStyleOption2<string> defaultValue, [NotNullWhen(true)] out CodeStyleOption2<string>? option)
{
if (TryGetCodeStyleValueAndOptionalNotification(
Expand Down Expand Up @@ -62,6 +69,16 @@ public static bool TryGetCodeStyleValue(
public static bool TryGetCodeStyleValueAndOptionalNotification(
string arg, NotificationOption2 defaultNotification, [NotNullWhen(true)] out string? value, [NotNullWhen(true)] out NotificationOption2 notification)
{
var embeddedCommentTextIndex = arg.IndexOfAny(s_commentDelimiters);
if (embeddedCommentTextIndex >= 0)
{
// For backwards compatibility, remove the embedded comment before continuing. In following the
// EditorConfig specification, the compiler no longer treats text after a '#' or ';' as an embedded
// comment.
// https://github.com/dotnet/roslyn/issues/44596
arg = arg[..embeddedCommentTextIndex];
}

var firstColonIndex = arg.IndexOf(':');

// We allow a single value to be provided without an explicit notification.
Expand Down

0 comments on commit bf03a75

Please sign in to comment.