From bb6f7d05ae889d58152b08d22f2bb967649c239b Mon Sep 17 00:00:00 2001
From: Benjamin Michaelis <git@relay.benjamin.michaelis.net>
Date: Tue, 15 Oct 2024 14:36:16 -0700
Subject: [PATCH] cleanup

---
 .../Parsing/CliDiagnostic.cs                  | 64 ++++---------------
 .../Parsing/CliDiagnosticDescriptor.cs        | 47 ++++++++++++++
 .../Parsing/CliDiagnosticSeverity.cs          | 31 +++++++++
 .../Parsing/ParseDiagnostics.cs               | 17 +++++
 .../System.CommandLine.csproj                 |  5 +-
 5 files changed, 111 insertions(+), 53 deletions(-)
 create mode 100644 src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs
 create mode 100644 src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs
 create mode 100644 src/System.CommandLine/Parsing/ParseDiagnostics.cs

diff --git a/src/System.CommandLine/Parsing/CliDiagnostic.cs b/src/System.CommandLine/Parsing/CliDiagnostic.cs
index f8605ee78..d291eae12 100644
--- a/src/System.CommandLine/Parsing/CliDiagnostic.cs
+++ b/src/System.CommandLine/Parsing/CliDiagnostic.cs
@@ -2,66 +2,14 @@
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
 using System.Collections.Immutable;
-using System.Diagnostics.CodeAnalysis;
 
 namespace System.CommandLine.Parsing;
 
-/*
- * Pattern based on: 
- * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnostic.cs
- * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnosticDescriptor.cs
- * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs
- * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/Diagnostic.cs
- * https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141791086
- */
-internal static class ParseDiagnostics
-{
-    public const string DirectiveIsNotDefinedId = "CMD0001";
-    public static readonly CliDiagnosticDescriptor DirectiveIsNotDefined =
-        new(
-            DirectiveIsNotDefinedId,
-            //TODO: use localized strings
-            "Directive is not defined",
-            "The directive '{0}' is not defined.",
-            CliDiagnosticSeverity.Error,
-            null);
-}
-
-public sealed class CliDiagnosticDescriptor
-{
-    public CliDiagnosticDescriptor(string id, string title, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string messageFormat, CliDiagnosticSeverity severity, string? helpUri)
-    {
-        Id = id;
-        Title = title;
-        MessageFormat = messageFormat;
-        Severity = severity;
-        HelpUri = helpUri;
-    }
-
-    public string Id { get; }
-    public string Title { get; }
-    [StringSyntax(StringSyntaxAttribute.CompositeFormat)]
-    public string MessageFormat { get; }
-    public CliDiagnosticSeverity Severity { get; }
-    public string? HelpUri { get; }
-}
-
-public enum CliDiagnosticSeverity
-{
-    Hidden = 0,
-    Info,
-    Warning,
-    Error
-}
-
 /// <summary>
 /// Describes an error that occurs while parsing command line input.
 /// </summary>
 public sealed class CliDiagnostic
 {
-    // TODO: reevaluate whether we should be exposing a SymbolResult here
-    // TODO: Rename to CliError
-
     /// <summary>
     /// Initializes a new instance of the <see cref="CliDiagnostic"/> class.
     /// </summary>
@@ -70,6 +18,14 @@ public sealed class CliDiagnostic
     /// <param name="properties">Properties to be associated with the diagnostic.</param>
     /// <param name="cliSymbolResult">Contains information about a single value entered.</param>
     /// <param name="location">The location of the error.</param>
+    /*
+     * Pattern based on: 
+     * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnostic.cs
+     * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnosticDescriptor.cs
+     * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs
+     * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/Diagnostic.cs
+     * https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141791086
+     */
     public CliDiagnostic(
         CliDiagnosticDescriptor descriptor,
         object?[]? messageArgs,
@@ -80,6 +36,8 @@ public CliDiagnostic(
         Descriptor = descriptor;
         MessageArgs = messageArgs;
         Properties = properties;
+        CliSymbolResult = cliSymbolResult;
+        Location = location;
     }
 
     /// <summary>
@@ -105,6 +63,8 @@ public string Message
 
     public CliSymbolResult? CliSymbolResult { get; }
 
+    public Location? Location { get; }
+
     /// <inheritdoc />
     public override string ToString() => Message;
 }
diff --git a/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs b/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs
new file mode 100644
index 000000000..2e3243ac8
--- /dev/null
+++ b/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs
@@ -0,0 +1,47 @@
+// Copyright (c) .NET Foundation and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using System.Diagnostics.CodeAnalysis;
+
+namespace System.CommandLine.Parsing;
+
+/// <summary>
+/// Provides a description of a <see cref="CliDiagnostic"/>.
+/// </summary>
+public sealed class CliDiagnosticDescriptor
+{
+    public CliDiagnosticDescriptor(string id, string title, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string messageFormat, CliDiagnosticSeverity severity, string? helpUri)
+    {
+        Id = id;
+        Title = title;
+        MessageFormat = messageFormat;
+        Severity = severity;
+        HelpUri = helpUri;
+    }
+
+    /// <summary>
+    /// A unique identifier for the diagnostic.
+    /// </summary>
+    public string Id { get; }
+
+    /// <summary>
+    /// A short title describing the diagnostic.
+    /// </summary>
+    public string Title { get; }
+
+    /// <summary>
+    /// A composite format string, which can be passed to <see cref="string.Format(string, object[])"/> to create a message.
+    /// </summary>
+    [StringSyntax(StringSyntaxAttribute.CompositeFormat)]
+    public string MessageFormat { get; }
+
+    /// <summary>
+    /// The severity of the diagnostic.
+    /// </summary>
+    public CliDiagnosticSeverity Severity { get; }
+
+    /// <summary>
+    /// An optional hyperlink that provides more information about the diagnostic.
+    /// </summary>
+    public string? HelpUri { get; }
+}
diff --git a/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs b/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs
new file mode 100644
index 000000000..956892fb9
--- /dev/null
+++ b/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs
@@ -0,0 +1,31 @@
+// Copyright (c) .NET Foundation and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+namespace System.CommandLine.Parsing;
+
+/// <summary>
+/// Describes how severe a <see cref="CliDiagnostic"/> is."/>
+/// </summary>
+// Pattern based on: https://github.com/dotnet/roslyn/blob/1cca63b5d8ea170f8d8e88e1574aa3ebe354c23b/src/Compilers/Core/Portable/Diagnostic/DiagnosticSeverity.cs.
+public enum CliDiagnosticSeverity
+{
+    /// <summary>
+    /// Something that is not surfaced through normal means.
+    /// </summary>
+    Hidden = 0,
+
+    /// <summary>
+    /// Information that does not indicate a problem (i.e. not prescriptive).
+    /// </summary>
+    Info,
+
+    /// <summary>
+    /// Something suspicious but allowed.
+    /// </summary>
+    Warning,
+
+    /// <summary>
+    /// Something that is definitely wrong and needs fixing.
+    /// </summary>
+    Error
+}
diff --git a/src/System.CommandLine/Parsing/ParseDiagnostics.cs b/src/System.CommandLine/Parsing/ParseDiagnostics.cs
new file mode 100644
index 000000000..b814c441e
--- /dev/null
+++ b/src/System.CommandLine/Parsing/ParseDiagnostics.cs
@@ -0,0 +1,17 @@
+// Copyright (c) .NET Foundation and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+namespace System.CommandLine.Parsing;
+
+internal static class ParseDiagnostics
+{
+    public const string DirectiveIsNotDefinedId = "CMD0001";
+    public static readonly CliDiagnosticDescriptor DirectiveIsNotDefined =
+        new(
+            DirectiveIsNotDefinedId,
+            //TODO: use localized strings
+            "Directive is not defined",
+            "The directive '{0}' is not defined.",
+            CliDiagnosticSeverity.Error,
+            null);
+}
diff --git a/src/System.CommandLine/System.CommandLine.csproj b/src/System.CommandLine/System.CommandLine.csproj
index 5c73ca87b..ea33049a7 100644
--- a/src/System.CommandLine/System.CommandLine.csproj
+++ b/src/System.CommandLine/System.CommandLine.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <IsPackable>true</IsPackable>
@@ -29,7 +29,10 @@
     <Compile Include="CliValueSymbol.cs" />
     <Compile Include="ICliValueSymbol.cs" />
     <Compile Include="Parsing\CliCommandResult.cs" />
+    <Compile Include="Parsing\CliDiagnosticDescriptor.cs" />
+    <Compile Include="Parsing\CliDiagnosticSeverity.cs" />
     <Compile Include="Parsing\CliSymbolResult.cs" />
+    <Compile Include="Parsing\ParseDiagnostics.cs" />
     <Compile Include="Parsing\SymbolLookupByName.cs" />
     <Compile Include="Parsing\ValueResultOutcome.cs" />
     <Compile Include="Binding\ArgumentConversionResultType.cs" />