This commit is contained in:
Tyrrrz
2022-01-10 23:41:28 +02:00
parent 8e96d2701d
commit 4ff1e1d3e1
30 changed files with 184 additions and 165 deletions

View File

@@ -9,7 +9,7 @@ public class ParameterMustBeLastIfNonScalarAnalyzerSpecs
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeLastIfNonScalarAnalyzer();
[Fact]
public void Analyzer_reports_an_error_if_a_non_scalar_parameter_is_not_last_in_order()
public void Analyzer_reports_an_error_if_a_non_scalar_parameter_is_not_the_last_in_order()
{
// Arrange
// language=cs
@@ -31,7 +31,7 @@ public class MyCommand : ICommand
}
[Fact]
public void Analyzer_does_not_report_an_error_if_a_non_scalar_parameter_is_last_in_order()
public void Analyzer_does_not_report_an_error_if_a_non_scalar_parameter_is_the_last_in_order()
{
// Arrange
// language=cs

View File

@@ -9,7 +9,7 @@ public class ParameterMustBeLastIfNotRequiredAnalyzerSpecs
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeLastIfNotRequiredAnalyzer();
[Fact]
public void Analyzer_reports_an_error_if_a_parameter_is_not_required_and_is_not_last_parameter()
public void Analyzer_reports_an_error_if_a_non_required_parameter_is_not_the_last_in_order()
{
// Arrange
// language=cs
@@ -31,7 +31,7 @@ public class MyCommand : ICommand
}
[Fact]
public void Analyzer_does_not_report_an_error_if_isRequired_is_false_on_last_parameter()
public void Analyzer_does_not_report_an_error_if_a_non_required_parameter_is_the_last_in_order()
{
// Arrange
// language=cs
@@ -53,7 +53,7 @@ public class MyCommand : ICommand
}
[Fact]
public void Analyzer_does_not_report_an_error_on_a_property_when_isRequired_is_not_set()
public void Analyzer_does_not_report_an_error_if_no_non_required_parameters_are_defined()
{
// Arrange
// language=cs
@@ -64,7 +64,7 @@ public class MyCommand : ICommand
[CommandParameter(0, Name = ""foo"")]
public string Foo { get; set; }
[CommandParameter(1, Name = ""bar"")]
[CommandParameter(1, Name = ""bar"", IsRequired = true)]
public string Bar { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
@@ -73,4 +73,22 @@ public class MyCommand : ICommand
// Act & assert
Analyzer.Should().NotProduceDiagnostics(code);
}
[Fact]
public void Analyzer_does_not_report_an_error_on_a_property_that_is_not_a_parameter()
{
// Arrange
// language=cs
const string code = @"
[Command]
public class MyCommand : ICommand
{
public string Foo { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}";
// Act & assert
Analyzer.Should().NotProduceDiagnostics(code);
}
}

View File

@@ -71,10 +71,9 @@ internal partial class CommandOptionSymbol
{
var attribute = TryGetOptionAttribute(property);
if (attribute is null)
return null;
return FromAttribute(attribute);
return attribute is not null
? FromAttribute(attribute)
: null;
}
public static bool IsOptionProperty(IPropertySymbol property) =>

View File

@@ -41,7 +41,7 @@ internal partial class CommandParameterSymbol
private static CommandParameterSymbol FromAttribute(AttributeData attribute)
{
var order = (int) attribute
var order = (int)attribute
.ConstructorArguments
.Select(a => a.Value)
.First()!;

View File

@@ -12,8 +12,8 @@ public class ParameterMustBeLastIfNonScalarAnalyzer : AnalyzerBase
{
public ParameterMustBeLastIfNonScalarAnalyzer()
: base(
"Parameters of non-scalar types must be last in order",
"This parameter has a non-scalar type so it must be last in order (its order must be highest within the command).")
"Parameters of non-scalar types must be the last in order",
"This parameter has a non-scalar type so it must be the last in order (its order must be highest within the command).")
{
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using CliFx.Analyzers.ObjectModel;
using CliFx.Analyzers.Utils.Extensions;
using Microsoft.CodeAnalysis;
@@ -13,8 +12,8 @@ public class ParameterMustBeLastIfNotRequiredAnalyzer : AnalyzerBase
{
public ParameterMustBeLastIfNotRequiredAnalyzer()
: base(
"Only last parameter can be optional",
"IsRequired can only be set to false on the last parameter.")
"Parameters marked as non-required must be the last in order",
"This parameter is non-required so it must be the last in order (its order must be highest within the command).")
{
}
@@ -27,8 +26,10 @@ public class ParameterMustBeLastIfNotRequiredAnalyzer : AnalyzerBase
return;
var parameter = CommandParameterSymbol.TryResolve(property);
if (parameter is null)
return;
if (parameter is null || parameter.IsRequired != false)
if (parameter.IsRequired != false)
return;
var otherProperties = property
@@ -44,7 +45,7 @@ public class ParameterMustBeLastIfNotRequiredAnalyzer : AnalyzerBase
if (otherParameter is null)
continue;
if (parameter.IsRequired is false && parameter.Order < otherParameter.Order)
if (otherParameter.Order > parameter.Order)
{
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.GetLocation()));
}

View File

@@ -29,7 +29,7 @@ public sealed class CommandOptionAttribute : Attribute
public char? ShortName { get; }
/// <summary>
/// Whether this option is required.
/// Whether this option is required (default: <c>false</c>).
/// If an option is required, the user will get an error if they don't set it.
/// </summary>
public bool IsRequired { get; set; }

View File

@@ -11,23 +11,24 @@ public sealed class CommandParameterAttribute : Attribute
{
/// <summary>
/// Parameter order.
/// </summary>
/// <remarks>
/// Higher order means the parameter appears later, lower order means
/// it appears earlier.
///
/// </summary>
/// <remarks>
/// All parameters in a command must have unique order.
///
/// Parameter whose type is a non-scalar (e.g. array), must always be the last in order.
/// Only one non-scalar parameter is allowed in a command.
/// </remarks>
public int Order { get; }
/// <summary>
/// Whether this parameter is required. (default: true)
/// Whether this parameter is required (default: <c>true</c>).
/// If a parameter is required, the user will get an error if they don't set it.
/// Only the last parameter in order can be optional.
/// </summary>
/// <remarks>
/// Parameter marked as non-required must always be the last in order.
/// Only one non-required parameter is allowed in a command.
/// </remarks>
public bool IsRequired { get; set; } = true;
/// <summary>

View File

@@ -13,27 +13,28 @@ internal partial class ParameterSchema : IMemberSchema
public string Name { get; }
public string? Description { get; }
public bool IsRequired { get; }
public string? Description { get; }
public Type? ConverterType { get; }
public IReadOnlyList<Type> ValidatorTypes { get; }
public ParameterSchema(IPropertyDescriptor property,
public ParameterSchema(
IPropertyDescriptor property,
int order,
string name,
string? description,
bool isRequired,
string? description,
Type? converterType,
IReadOnlyList<Type> validatorTypes)
{
Property = property;
Order = order;
Name = name;
Description = description;
IsRequired = isRequired;
Description = description;
ConverterType = converterType;
ValidatorTypes = validatorTypes;
}
@@ -58,8 +59,8 @@ internal partial class ParameterSchema
new BindablePropertyDescriptor(property),
attribute.Order,
name,
description,
attribute.IsRequired,
description,
attribute.Converter,
attribute.Validators
);

View File

@@ -32,7 +32,6 @@ internal static partial class PolyfillExtensions
stream.Write(buffer, 0, buffer.Length);
}
namespace System.Linq
{
internal static class PolyfillExtensions

View File

@@ -226,7 +226,7 @@ Similarly, unseparated arguments in the form of `myapp -ofile` will be treated a
Because of these rules, order of arguments is semantically important and must always follow this pattern:
```ini
```txt
[directives] [command name] [parameters] [options]
```