From 94f63631db714edbc48119d4124968d5cda70e1b Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Mon, 14 Dec 2020 17:36:46 +0200 Subject: [PATCH] Use C#9 features --- .../Internal/AnalyzerAssertions.cs | 2 +- CliFx.Analyzers/DiagnosticDescriptors.cs | 36 +++++++------- CliFx.Demo/Commands/BookAddCommand.cs | 6 +-- CliFx.Demo/Models/Library.cs | 2 +- .../Commands/SupportedArgumentTypesCommand.cs | 6 +-- CliFx/ArgumentValueValidator.cs | 4 +- CliFx/CliApplicationBuilder.cs | 4 +- CliFx/Domain/CommandInput.cs | 2 +- CliFx/Domain/CommandOptionSchema.cs | 4 +- CliFx/Internal/Extensions/StringExtensions.cs | 2 +- CliFx/Internal/Polyfills.cs | 48 ++++++++----------- CliFx/Internal/StackFrame.cs | 2 +- CliFx/Utilities/ProgressTicker.cs | 2 +- 13 files changed, 55 insertions(+), 65 deletions(-) diff --git a/CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs b/CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs index 1eab305..b7d97cc 100644 --- a/CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs +++ b/CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs @@ -102,6 +102,6 @@ Produced: {string.Join(", ", producedIds)} internal static class AnalyzerAssertionsExtensions { - public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new AnalyzerAssertions(analyzer); + public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new(analyzer); } } \ No newline at end of file diff --git a/CliFx.Analyzers/DiagnosticDescriptors.cs b/CliFx.Analyzers/DiagnosticDescriptors.cs index bec96a5..1d4cb43 100644 --- a/CliFx.Analyzers/DiagnosticDescriptors.cs +++ b/CliFx.Analyzers/DiagnosticDescriptors.cs @@ -5,126 +5,126 @@ namespace CliFx.Analyzers public static class DiagnosticDescriptors { public static readonly DiagnosticDescriptor CliFx0001 = - new DiagnosticDescriptor(nameof(CliFx0001), + new(nameof(CliFx0001), "Type must implement the 'CliFx.ICommand' interface in order to be a valid command", "Type must implement the 'CliFx.ICommand' interface in order to be a valid command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0002 = - new DiagnosticDescriptor(nameof(CliFx0002), + new(nameof(CliFx0002), "Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command", "Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0021 = - new DiagnosticDescriptor(nameof(CliFx0021), + new(nameof(CliFx0021), "Parameter order must be unique within its command", "Parameter order must be unique within its command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0022 = - new DiagnosticDescriptor(nameof(CliFx0022), + new(nameof(CliFx0022), "Parameter order must have unique name within its command", "Parameter order must have unique name within its command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0023 = - new DiagnosticDescriptor(nameof(CliFx0023), + new(nameof(CliFx0023), "Only one non-scalar parameter per command is allowed", "Only one non-scalar parameter per command is allowed", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0024 = - new DiagnosticDescriptor(nameof(CliFx0024), + new(nameof(CliFx0024), "Non-scalar parameter must be last in order", "Non-scalar parameter must be last in order", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0025 = - new DiagnosticDescriptor(nameof(CliFx0025), + new(nameof(CliFx0025), "Parameter converter must implement 'CliFx.IArgumentValueConverter'", "Parameter converter must implement 'CliFx.IArgumentValueConverter'", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0026 = - new DiagnosticDescriptor(nameof(CliFx0026), + new(nameof(CliFx0026), "Parameter validator must implement 'CliFx.ArgumentValueValidator'", "Parameter validator must implement 'CliFx.ArgumentValueValidator'", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0041 = - new DiagnosticDescriptor(nameof(CliFx0041), + new(nameof(CliFx0041), "Option must have a name or short name specified", "Option must have a name or short name specified", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0042 = - new DiagnosticDescriptor(nameof(CliFx0042), + new(nameof(CliFx0042), "Option name must be at least 2 characters long", "Option name must be at least 2 characters long", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0043 = - new DiagnosticDescriptor(nameof(CliFx0043), + new(nameof(CliFx0043), "Option name must be unique within its command", "Option name must be unique within its command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0044 = - new DiagnosticDescriptor(nameof(CliFx0044), + new(nameof(CliFx0044), "Option short name must be unique within its command", "Option short name must be unique within its command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0045 = - new DiagnosticDescriptor(nameof(CliFx0045), + new(nameof(CliFx0045), "Option environment variable name must be unique within its command", "Option environment variable name must be unique within its command", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0046 = - new DiagnosticDescriptor(nameof(CliFx0046), + new(nameof(CliFx0046), "Option converter must implement 'CliFx.IArgumentValueConverter'", "Option converter must implement 'CliFx.IArgumentValueConverter'", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0047 = - new DiagnosticDescriptor(nameof(CliFx0047), + new(nameof(CliFx0047), "Option validator must implement 'CliFx.ArgumentValueValidator'", "Option validator must implement 'CliFx.ArgumentValueValidator'", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0048 = - new DiagnosticDescriptor(nameof(CliFx0048), + new(nameof(CliFx0048), "Option name must begin with a letter character.", "Option name must begin with a letter character.", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0049 = - new DiagnosticDescriptor(nameof(CliFx0049), + new(nameof(CliFx0049), "Option short name must be a letter character.", "Option short name must be a letter character.", "Usage", DiagnosticSeverity.Error, true ); public static readonly DiagnosticDescriptor CliFx0100 = - new DiagnosticDescriptor(nameof(CliFx0100), + new(nameof(CliFx0100), "Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation", "Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation", "Usage", DiagnosticSeverity.Warning, true diff --git a/CliFx.Demo/Commands/BookAddCommand.cs b/CliFx.Demo/Commands/BookAddCommand.cs index a7a397b..de4522d 100644 --- a/CliFx.Demo/Commands/BookAddCommand.cs +++ b/CliFx.Demo/Commands/BookAddCommand.cs @@ -47,9 +47,9 @@ namespace CliFx.Demo.Commands public partial class BookAddCommand { - private static readonly Random Random = new Random(); + private static readonly Random Random = new(); - private static DateTimeOffset CreateRandomDate() => new DateTimeOffset( + private static DateTimeOffset CreateRandomDate() => new( Random.Next(1800, 2020), Random.Next(1, 12), Random.Next(1, 28), @@ -58,7 +58,7 @@ namespace CliFx.Demo.Commands Random.Next(1, 59), TimeSpan.Zero); - private static Isbn CreateRandomIsbn() => new Isbn( + private static Isbn CreateRandomIsbn() => new( Random.Next(0, 999), Random.Next(0, 99), Random.Next(0, 99999), diff --git a/CliFx.Demo/Models/Library.cs b/CliFx.Demo/Models/Library.cs index 1e23f99..28b325d 100644 --- a/CliFx.Demo/Models/Library.cs +++ b/CliFx.Demo/Models/Library.cs @@ -15,6 +15,6 @@ namespace CliFx.Demo.Models public partial class Library { - public static Library Empty { get; } = new Library(Array.Empty()); + public static Library Empty { get; } = new(Array.Empty()); } } \ No newline at end of file diff --git a/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs b/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs index 71233b3..55906ef 100644 --- a/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs +++ b/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs @@ -144,7 +144,7 @@ namespace CliFx.Tests.Commands [JsonConstructor] private CustomStringParseable(string value) => Value = value; - public static CustomStringParseable Parse(string value) => new CustomStringParseable(value); + public static CustomStringParseable Parse(string value) => new(value); } public class CustomStringParseableWithFormatProvider @@ -155,7 +155,7 @@ namespace CliFx.Tests.Commands private CustomStringParseableWithFormatProvider(string value) => Value = value; public static CustomStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) => - new CustomStringParseableWithFormatProvider(value + " " + formatProvider); + new(value + " " + formatProvider); } public class CustomConvertible @@ -168,7 +168,7 @@ namespace CliFx.Tests.Commands public class CustomConvertibleConverter : ArgumentValueConverter { public override CustomConvertible ConvertFrom(string value) => - new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture)); + new(int.Parse(value, CultureInfo.InvariantCulture)); } } } \ No newline at end of file diff --git a/CliFx/ArgumentValueValidator.cs b/CliFx/ArgumentValueValidator.cs index d0cc3eb..9134b9c 100644 --- a/CliFx/ArgumentValueValidator.cs +++ b/CliFx/ArgumentValueValidator.cs @@ -27,12 +27,12 @@ /// /// Creates successful result, meaning that the validation has passed. /// - public static ValidationResult Ok() => new ValidationResult(); + public static ValidationResult Ok() => new(); /// /// Creates an error result, meaning that the validation has failed. /// - public static ValidationResult Error(string message) => new ValidationResult(message); + public static ValidationResult Error(string message) => new(message); } internal interface IArgumentValueValidator diff --git a/CliFx/CliApplicationBuilder.cs b/CliFx/CliApplicationBuilder.cs index 65e0e59..92fd1a8 100644 --- a/CliFx/CliApplicationBuilder.cs +++ b/CliFx/CliApplicationBuilder.cs @@ -13,7 +13,7 @@ namespace CliFx /// public partial class CliApplicationBuilder { - private readonly HashSet _commandTypes = new HashSet(); + private readonly HashSet _commandTypes = new(); private bool _isDebugModeAllowed = true; private bool _isPreviewModeAllowed = true; @@ -180,7 +180,7 @@ namespace CliFx public partial class CliApplicationBuilder { - private static readonly Lazy LazyEntryAssembly = new Lazy(Assembly.GetEntryAssembly); + private static readonly Lazy LazyEntryAssembly = new(Assembly.GetEntryAssembly); // Entry assembly is null in tests private static Assembly? EntryAssembly => LazyEntryAssembly.Value; diff --git a/CliFx/Domain/CommandInput.cs b/CliFx/Domain/CommandInput.cs index 989a36d..32cbf0b 100644 --- a/CliFx/Domain/CommandInput.cs +++ b/CliFx/Domain/CommandInput.cs @@ -243,7 +243,7 @@ namespace CliFx.Domain internal partial class CommandInput { - public static CommandInput Empty { get; } = new CommandInput( + public static CommandInput Empty { get; } = new( Array.Empty(), null, Array.Empty(), diff --git a/CliFx/Domain/CommandOptionSchema.cs b/CliFx/Domain/CommandOptionSchema.cs index 18a2e9d..f35e4a6 100644 --- a/CliFx/Domain/CommandOptionSchema.cs +++ b/CliFx/Domain/CommandOptionSchema.cs @@ -108,7 +108,7 @@ namespace CliFx.Domain internal partial class CommandOptionSchema { - public static CommandOptionSchema HelpOption { get; } = new CommandOptionSchema( + public static CommandOptionSchema HelpOption { get; } = new( null, "help", 'h', @@ -119,7 +119,7 @@ namespace CliFx.Domain Array.Empty() ); - public static CommandOptionSchema VersionOption { get; } = new CommandOptionSchema( + public static CommandOptionSchema VersionOption { get; } = new( null, "version", null, diff --git a/CliFx/Internal/Extensions/StringExtensions.cs b/CliFx/Internal/Extensions/StringExtensions.cs index dd7f8d4..ce5cb8a 100644 --- a/CliFx/Internal/Extensions/StringExtensions.cs +++ b/CliFx/Internal/Extensions/StringExtensions.cs @@ -11,7 +11,7 @@ namespace CliFx.Internal.Extensions ? str : null; - public static string Repeat(this char c, int count) => new string(c, count); + public static string Repeat(this char c, int count) => new(c, count); public static string AsString(this char c) => c.Repeat(1); diff --git a/CliFx/Internal/Polyfills.cs b/CliFx/Internal/Polyfills.cs index daa3965..6596369 100644 --- a/CliFx/Internal/Polyfills.cs +++ b/CliFx/Internal/Polyfills.cs @@ -2,50 +2,40 @@ // Polyfills to bridge the missing APIs in older versions of the framework/standard. +using System; +using System.Collections.Generic; + #if NETSTANDARD2_0 -namespace System +internal static partial class PolyfillExtensions { - using Linq; + public static bool StartsWith(this string str, char c) => + str.Length > 0 && str[0] == c; - internal static class Extensions - { - public static bool Contains(this string str, char c) => - str.Any(i => i == c); + public static bool EndsWith(this string str, char c) => + str.Length > 0 && str[str.Length - 1] == c; - public static bool StartsWith(this string str, char c) => - str.Length > 0 && str[0] == c; - - public static bool EndsWith(this string str, char c) => - str.Length > 0 && str[str.Length - 1] == c; - - public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) => - str.Split(new[] {separator}, splitOptions); - } + public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) => + str.Split(new[] {separator}, splitOptions); } -namespace System.Collections.Generic +internal static partial class PolyfillExtensions { - internal static class Extensions + public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) { - public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) - { - key = pair.Key; - value = pair.Value; - } - - public static TValue GetValueOrDefault(this IReadOnlyDictionary dic, TKey key) => - dic.TryGetValue(key!, out var result) ? result! : default!; + key = pair.Key; + value = pair.Value; } + + public static TValue GetValueOrDefault(this IReadOnlyDictionary dic, TKey key) => + dic.TryGetValue(key!, out var result) ? result! : default!; } namespace System.Linq { - using Collections.Generic; - - internal static class Extensions + internal static class PolyfillExtensions { public static HashSet ToHashSet(this IEnumerable source, IEqualityComparer comparer) => - new HashSet(source, comparer); + new(source, comparer); } } #endif \ No newline at end of file diff --git a/CliFx/Internal/StackFrame.cs b/CliFx/Internal/StackFrame.cs index 3163db9..0ee05b4 100644 --- a/CliFx/Internal/StackFrame.cs +++ b/CliFx/Internal/StackFrame.cs @@ -52,7 +52,7 @@ namespace CliFx.Internal private const string NotSpace = @"[^\x20\t]"; // Taken from https://github.com/atifaziz/StackTraceParser - private static readonly Regex Pattern = new Regex(@" + private static readonly Regex Pattern = new(@" ^ " + Space + @"* \w+ " + Space + @"+ diff --git a/CliFx/Utilities/ProgressTicker.cs b/CliFx/Utilities/ProgressTicker.cs index da944f0..7d8e667 100644 --- a/CliFx/Utilities/ProgressTicker.cs +++ b/CliFx/Utilities/ProgressTicker.cs @@ -57,6 +57,6 @@ namespace CliFx.Utilities /// /// Creates a bound to this console. /// - public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console); + public static ProgressTicker CreateProgressTicker(this IConsole console) => new(console); } } \ No newline at end of file