Use C#9 features

This commit is contained in:
Tyrrrz
2020-12-14 17:36:46 +02:00
parent 90d1b11430
commit 94f63631db
13 changed files with 55 additions and 65 deletions

View File

@@ -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);
}
}

View File

@@ -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<T>'",
"Parameter validator must implement 'CliFx.ArgumentValueValidator<T>'",
"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<T>'",
"Option validator must implement 'CliFx.ArgumentValueValidator<T>'",
"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

View File

@@ -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),

View File

@@ -15,6 +15,6 @@ namespace CliFx.Demo.Models
public partial class Library
{
public static Library Empty { get; } = new Library(Array.Empty<Book>());
public static Library Empty { get; } = new(Array.Empty<Book>());
}
}

View File

@@ -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<CustomConvertible>
{
public override CustomConvertible ConvertFrom(string value) =>
new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture));
new(int.Parse(value, CultureInfo.InvariantCulture));
}
}
}

View File

@@ -27,12 +27,12 @@
/// <summary>
/// Creates successful result, meaning that the validation has passed.
/// </summary>
public static ValidationResult Ok() => new ValidationResult();
public static ValidationResult Ok() => new();
/// <summary>
/// Creates an error result, meaning that the validation has failed.
/// </summary>
public static ValidationResult Error(string message) => new ValidationResult(message);
public static ValidationResult Error(string message) => new(message);
}
internal interface IArgumentValueValidator

View File

@@ -13,7 +13,7 @@ namespace CliFx
/// </summary>
public partial class CliApplicationBuilder
{
private readonly HashSet<Type> _commandTypes = new HashSet<Type>();
private readonly HashSet<Type> _commandTypes = new();
private bool _isDebugModeAllowed = true;
private bool _isPreviewModeAllowed = true;
@@ -180,7 +180,7 @@ namespace CliFx
public partial class CliApplicationBuilder
{
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
private static readonly Lazy<Assembly?> LazyEntryAssembly = new(Assembly.GetEntryAssembly);
// Entry assembly is null in tests
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;

View File

@@ -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<CommandDirectiveInput>(),
null,
Array.Empty<CommandParameterInput>(),

View File

@@ -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<Type>()
);
public static CommandOptionSchema VersionOption { get; } = new CommandOptionSchema(
public static CommandOptionSchema VersionOption { get; } = new(
null,
"version",
null,

View File

@@ -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);

View File

@@ -2,16 +2,12 @@
// 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;
internal static class Extensions
{
public static bool Contains(this string str, char c) =>
str.Any(i => i == c);
public static bool StartsWith(this string str, char c) =>
str.Length > 0 && str[0] == c;
@@ -21,11 +17,8 @@ namespace System
public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) =>
str.Split(new[] {separator}, splitOptions);
}
}
namespace System.Collections.Generic
{
internal static class Extensions
internal static partial class PolyfillExtensions
{
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
{
@@ -36,16 +29,13 @@ namespace System.Collections.Generic
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> 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<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer) =>
new HashSet<T>(source, comparer);
new(source, comparer);
}
}
#endif

View File

@@ -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 + @"+

View File

@@ -57,6 +57,6 @@ namespace CliFx.Utilities
/// <summary>
/// Creates a <see cref="ProgressTicker"/> bound to this console.
/// </summary>
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
public static ProgressTicker CreateProgressTicker(this IConsole console) => new(console);
}
}