Skip to content

Commit

Permalink
Refactor with C# 12 features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Dec 10, 2023
1 parent 5854f36 commit 490398f
Show file tree
Hide file tree
Showing 68 changed files with 370 additions and 621 deletions.
2 changes: 1 addition & 1 deletion CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.5" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
Expand Down
8 changes: 3 additions & 5 deletions CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@

namespace CliFx.Analyzers.Tests.Utils;

internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>
internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer)
: ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>(analyzer)
{
protected override string Identifier { get; } = "analyzer";

public AnalyzerAssertions(DiagnosticAnalyzer analyzer)
: base(analyzer) { }
protected override string Identifier => "analyzer";

private Compilation Compile(string sourceCode)
{
Expand Down
2 changes: 1 addition & 1 deletion CliFx.Analyzers/CliFx.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.26.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<!-- Make sure to target the lowest possible version of the compiler for wider support -->
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" PrivateAssets="all" />
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/CommandMustBeAnnotatedAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CommandMustBeAnnotatedAnalyzer : AnalyzerBase
public class CommandMustBeAnnotatedAnalyzer()
: AnalyzerBase(
$"Commands must be annotated with `{SymbolNames.CliFxCommandAttribute}`",
$"This type must be annotated with `{SymbolNames.CliFxCommandAttribute}` in order to be a valid command."
)
{
public CommandMustBeAnnotatedAnalyzer()
: base(
$"Commands must be annotated with `{SymbolNames.CliFxCommandAttribute}`",
$"This type must be annotated with `{SymbolNames.CliFxCommandAttribute}` in order to be a valid command."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
ClassDeclarationSyntax classDeclaration,
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/CommandMustImplementInterfaceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CommandMustImplementInterfaceAnalyzer : AnalyzerBase
public class CommandMustImplementInterfaceAnalyzer()
: AnalyzerBase(
$"Commands must implement `{SymbolNames.CliFxCommandInterface}` interface",
$"This type must implement `{SymbolNames.CliFxCommandInterface}` interface in order to be a valid command."
)
{
public CommandMustImplementInterfaceAnalyzer()
: base(
$"Commands must implement `{SymbolNames.CliFxCommandInterface}` interface",
$"This type must implement `{SymbolNames.CliFxCommandInterface}` interface in order to be a valid command."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
ClassDeclarationSyntax classDeclaration,
Expand Down
38 changes: 14 additions & 24 deletions CliFx.Analyzers/ObjectModel/CommandOptionSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,26 @@

namespace CliFx.Analyzers.ObjectModel;

internal partial class CommandOptionSymbol : ICommandMemberSymbol
internal partial class CommandOptionSymbol(
IPropertySymbol property,
string? name,
char? shortName,
bool? isRequired,
ITypeSymbol? converterType,
IReadOnlyList<ITypeSymbol> validatorTypes
) : ICommandMemberSymbol
{
public IPropertySymbol Property { get; }
public IPropertySymbol Property { get; } = property;

public string? Name { get; }
public string? Name { get; } = name;

public char? ShortName { get; }
public char? ShortName { get; } = shortName;

public bool? IsRequired { get; }
public bool? IsRequired { get; } = isRequired;

public ITypeSymbol? ConverterType { get; }
public ITypeSymbol? ConverterType { get; } = converterType;

public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; }

public CommandOptionSymbol(
IPropertySymbol property,
string? name,
char? shortName,
bool? isRequired,
ITypeSymbol? converterType,
IReadOnlyList<ITypeSymbol> validatorTypes
)
{
Property = property;
Name = name;
ShortName = shortName;
IsRequired = isRequired;
ConverterType = converterType;
ValidatorTypes = validatorTypes;
}
public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; } = validatorTypes;
}

internal partial class CommandOptionSymbol
Expand Down
38 changes: 14 additions & 24 deletions CliFx.Analyzers/ObjectModel/CommandParameterSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,26 @@

namespace CliFx.Analyzers.ObjectModel;

internal partial class CommandParameterSymbol : ICommandMemberSymbol
internal partial class CommandParameterSymbol(
IPropertySymbol property,
int order,
string? name,
bool? isRequired,
ITypeSymbol? converterType,
IReadOnlyList<ITypeSymbol> validatorTypes
) : ICommandMemberSymbol
{
public IPropertySymbol Property { get; }
public IPropertySymbol Property { get; } = property;

public int Order { get; }
public int Order { get; } = order;

public string? Name { get; }
public string? Name { get; } = name;

public bool? IsRequired { get; }
public bool? IsRequired { get; } = isRequired;

public ITypeSymbol? ConverterType { get; }
public ITypeSymbol? ConverterType { get; } = converterType;

public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; }

public CommandParameterSymbol(
IPropertySymbol property,
int order,
string? name,
bool? isRequired,
ITypeSymbol? converterType,
IReadOnlyList<ITypeSymbol> validatorTypes
)
{
Property = property;
Order = order;
Name = name;
IsRequired = isRequired;
ConverterType = converterType;
ValidatorTypes = validatorTypes;
}
public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; } = validatorTypes;
}

internal partial class CommandParameterSymbol
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/OptionMustBeInsideCommandAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustBeInsideCommandAnalyzer : AnalyzerBase
public class OptionMustBeInsideCommandAnalyzer()
: AnalyzerBase(
"Options must be defined inside commands",
$"This option must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
)
{
public OptionMustBeInsideCommandAnalyzer()
: base(
"Options must be defined inside commands",
$"This option must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustBeRequiredIfPropertyRequiredAnalyzer : AnalyzerBase
public class OptionMustBeRequiredIfPropertyRequiredAnalyzer()
: AnalyzerBase(
"Options bound to required properties cannot be marked as non-required",
"This option cannot be marked as non-required because it's bound to a required property."
)
{
public OptionMustBeRequiredIfPropertyRequiredAnalyzer()
: base(
"Options bound to required properties cannot be marked as non-required",
"This option cannot be marked as non-required because it's bound to a required property."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/OptionMustHaveNameOrShortNameAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveNameOrShortNameAnalyzer : AnalyzerBase
public class OptionMustHaveNameOrShortNameAnalyzer()
: AnalyzerBase(
"Options must have either a name or short name specified",
"This option must have either a name or short name specified."
)
{
public OptionMustHaveNameOrShortNameAnalyzer()
: base(
"Options must have either a name or short name specified",
"This option must have either a name or short name specified."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
16 changes: 7 additions & 9 deletions CliFx.Analyzers/OptionMustHaveUniqueNameAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveUniqueNameAnalyzer : AnalyzerBase
public class OptionMustHaveUniqueNameAnalyzer()
: AnalyzerBase(
"Options must have unique names",
"This option's name must be unique within the command (comparison IS NOT case sensitive). "
+ "Specified name: `{0}`. "
+ "Property bound to another option with the same name: `{1}`."
)
{
public OptionMustHaveUniqueNameAnalyzer()
: base(
"Options must have unique names",
"This option's name must be unique within the command (comparison IS NOT case sensitive). "
+ "Specified name: `{0}`. "
+ "Property bound to another option with the same name: `{1}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
16 changes: 7 additions & 9 deletions CliFx.Analyzers/OptionMustHaveUniqueShortNameAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveUniqueShortNameAnalyzer : AnalyzerBase
public class OptionMustHaveUniqueShortNameAnalyzer()
: AnalyzerBase(
"Options must have unique short names",
"This option's short name must be unique within the command (comparison IS case sensitive). "
+ "Specified short name: `{0}` "
+ "Property bound to another option with the same short name: `{1}`."
)
{
public OptionMustHaveUniqueShortNameAnalyzer()
: base(
"Options must have unique short names",
"This option's short name must be unique within the command (comparison IS case sensitive). "
+ "Specified short name: `{0}` "
+ "Property bound to another option with the same short name: `{1}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/OptionMustHaveValidConverterAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveValidConverterAnalyzer : AnalyzerBase
public class OptionMustHaveValidConverterAnalyzer()
: AnalyzerBase(
$"Option converters must derive from `{SymbolNames.CliFxBindingConverterClass}`",
$"Converter specified for this option must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`."
)
{
public OptionMustHaveValidConverterAnalyzer()
: base(
$"Option converters must derive from `{SymbolNames.CliFxBindingConverterClass}`",
$"Converter specified for this option must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
14 changes: 6 additions & 8 deletions CliFx.Analyzers/OptionMustHaveValidNameAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveValidNameAnalyzer : AnalyzerBase
public class OptionMustHaveValidNameAnalyzer()
: AnalyzerBase(
"Options must have valid names",
"This option's name must be at least 2 characters long and must start with a letter. "
+ "Specified name: `{0}`."
)
{
public OptionMustHaveValidNameAnalyzer()
: base(
"Options must have valid names",
"This option's name must be at least 2 characters long and must start with a letter. "
+ "Specified name: `{0}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
14 changes: 6 additions & 8 deletions CliFx.Analyzers/OptionMustHaveValidShortNameAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveValidShortNameAnalyzer : AnalyzerBase
public class OptionMustHaveValidShortNameAnalyzer()
: AnalyzerBase(
"Option short names must be letter characters",
"This option's short name must be a single letter character. "
+ "Specified short name: `{0}`."
)
{
public OptionMustHaveValidShortNameAnalyzer()
: base(
"Option short names must be letter characters",
"This option's short name must be a single letter character. "
+ "Specified short name: `{0}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/OptionMustHaveValidValidatorsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class OptionMustHaveValidValidatorsAnalyzer : AnalyzerBase
public class OptionMustHaveValidValidatorsAnalyzer()
: AnalyzerBase(
$"Option validators must derive from `{SymbolNames.CliFxBindingValidatorClass}`",
$"Each validator specified for this option must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`."
)
{
public OptionMustHaveValidValidatorsAnalyzer()
: base(
$"Option validators must derive from `{SymbolNames.CliFxBindingValidatorClass}`",
$"Each validator specified for this option must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
12 changes: 5 additions & 7 deletions CliFx.Analyzers/ParameterMustBeInsideCommandAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
namespace CliFx.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ParameterMustBeInsideCommandAnalyzer : AnalyzerBase
public class ParameterMustBeInsideCommandAnalyzer()
: AnalyzerBase(
"Parameters must be defined inside commands",
$"This parameter must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
)
{
public ParameterMustBeInsideCommandAnalyzer()
: base(
"Parameters must be defined inside commands",
$"This parameter must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
) { }

private void Analyze(
SyntaxNodeAnalysisContext context,
PropertyDeclarationSyntax propertyDeclaration,
Expand Down
Loading

0 comments on commit 490398f

Please sign in to comment.