Skip to content

Commit

Permalink
Fixing some issues. Applying Metalama code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfraiteur committed Sep 24, 2024
1 parent cb4bca9 commit b1d6229
Show file tree
Hide file tree
Showing 27 changed files with 224 additions and 129 deletions.
4 changes: 2 additions & 2 deletions CodeQualityTalk.Analyzers/CodeQualityTalk.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Polyfill" Version="5.5.3"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"/>
<PackageReference Include="Polyfill" />
</ItemGroup>

</Project>
63 changes: 33 additions & 30 deletions CodeQualityTalk.Analyzers/FactoryNameAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Diagnostics;
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -9,71 +10,73 @@

namespace CodeQualityTalk.Analyzers
{

[DiagnosticAnalyzer( LanguageNames.CSharp )]
public class FactoryNameAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor _diagnosticDescriptor = new DiagnosticDescriptor(
private static readonly DiagnosticDescriptor _diagnosticDescriptor = new(
"MY001",
"Types implementing IDocumentFactory must be named *Factory",
"(Analyzer) The type {0} must have the Factory suffix because it implements IDocumentFactory",
"Naming", DiagnosticSeverity.Warning, true);
"Naming",
DiagnosticSeverity.Warning,
true );

public override void Initialize(AnalysisContext context)
public override void Initialize( AnalysisContext context )
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSemanticModelAction(AnalyzeSemanticModel);
context.ConfigureGeneratedCodeAnalysis( GeneratedCodeAnalysisFlags.None );
context.RegisterSemanticModelAction( this.AnalyzeSemanticModel );
}

private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
private void AnalyzeSemanticModel( SemanticModelAnalysisContext context )
{
new Walker(context).Visit(context.FilterTree.GetRoot());
new Walker( context ).Visit( context.FilterTree.GetRoot() );
}

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
= [_diagnosticDescriptor];

class Walker : CSharpSyntaxWalker
private class Walker : CSharpSyntaxWalker
{
private readonly SemanticModelAnalysisContext _context;

public Walker(SemanticModelAnalysisContext context)
public Walker( SemanticModelAnalysisContext context )
{
_context = context;
this._context = context;
}


public override void VisitClassDeclaration(ClassDeclarationSyntax node)
public override void VisitClassDeclaration( ClassDeclarationSyntax node )
{
this.InspectType(node);
base.VisitClassDeclaration(node);
this.InspectType( node );
base.VisitClassDeclaration( node );
}

public override void VisitRecordDeclaration(RecordDeclarationSyntax node)
public override void VisitRecordDeclaration( RecordDeclarationSyntax node )
{
this.InspectType(node);
base.VisitRecordDeclaration(node);
this.InspectType( node );
base.VisitRecordDeclaration( node );
}

public override void VisitStructDeclaration(StructDeclarationSyntax node)
public override void VisitStructDeclaration( StructDeclarationSyntax node )
{
this.InspectType(node);
base.VisitStructDeclaration(node);
this.InspectType( node );
base.VisitStructDeclaration( node );
}

private void InspectType(TypeDeclarationSyntax type)
private void InspectType( TypeDeclarationSyntax type )
{
var typeSymbol = this._context.SemanticModel.GetDeclaredSymbol(type);
var typeSymbol = this._context.SemanticModel.GetDeclaredSymbol( type );

if ( typeSymbol != null &&
!type.Identifier.Text.EndsWith("Factory") &&
typeSymbol.AllInterfaces.Any(i => i.Name == "IDocumentFactory"))
!type.Identifier.Text.EndsWith( "Factory" ) &&
typeSymbol.AllInterfaces.Any( i => i.Name == "IDocumentFactory" ) )
{
_context.ReportDiagnostic(Diagnostic.Create(_diagnosticDescriptor, type.Identifier.GetLocation(),
type.Identifier.Text));
this._context.ReportDiagnostic(
Diagnostic.Create(
_diagnosticDescriptor,
type.Identifier.GetLocation(),
type.Identifier.Text ) );
}

}
}
}
Expand Down
22 changes: 12 additions & 10 deletions CodeQualityTalk.ArchUnitTests/ArchitectureTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using ArchUnitNET.Domain;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
Expand All @@ -10,38 +12,38 @@ namespace CodeQualityTalk.Tests;
public class ArchitectureTests
{
private readonly Architecture _testedProjects =
new ArchLoader().LoadAssembly(typeof(IDocumentFactory).Assembly).Build();
new ArchLoader().LoadAssembly( typeof(IDocumentFactory).Assembly ).Build();

[Fact]
public void TypesDerivedFromIFactory_ShouldBe_NamedFactory()
{
Types()
.That()
.ImplementInterface(typeof(IDocumentFactory))
.ImplementInterface( typeof(IDocumentFactory) )
.Should()
.HaveName("^.*Factory$", true)
.Check(_testedProjects);
.HaveName( "^.*Factory$", true )
.Check( this._testedProjects );
}

[Fact]
public void TypesDerivedFromIDocument_ShouldBe_InDocumentsNamespace()
{
Types()
.That()
.ImplementInterface(typeof(IDocument))
.ImplementInterface( typeof(IDocument) )
.Should()
.HaveFullName(@"^CodeQualityTalk\.Documents\..*", true)
.Check(_testedProjects);
.HaveFullName( @"^CodeQualityTalk\.Documents\..*", true )
.Check( this._testedProjects );
}

[Fact]
public void Abstractions_CannotUse_DocumentsNamespace()
{
Types()
.That()
.ResideInNamespace("CodeQualityTalk.Abstractions")
.ResideInNamespace( "CodeQualityTalk.Abstractions" )
.Should()
.NotDependOnAny(Types().That().DoNotResideInNamespace("CodeQualityTalk.Abstractions"))
.Check(_testedProjects);
.NotDependOnAny( Types().That().DoNotResideInNamespace( "CodeQualityTalk.Abstractions" ) )
.Check( this._testedProjects );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="TngTech.ArchUnitNET.xUnit" Version="0.10.6"/>
<PackageReference Include="xunit" Version="2.8.1"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="TngTech.ArchUnitNET.xUnit"/>
<PackageReference Include="xunit" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace CodeQualityTalk.Abstractions;

public record DocumentCreationContext( string OwnerId );
6 changes: 4 additions & 2 deletions CodeQualityTalk.Metalama/Abstractions/DocumentHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using CodeQualityTalk.Documents;
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Documents;

namespace CodeQualityTalk.Abstractions;

public static class DocumentHelper
{
public static bool IsInvoice(IDocument document) => document is Invoice;
public static bool IsInvoice( IDocument document ) => document is Invoice;
}
3 changes: 3 additions & 0 deletions CodeQualityTalk.Metalama/Abstractions/IDocument.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace CodeQualityTalk.Abstractions;

public interface IDocument
{
string OwnerId { get; }

string Name { get; }
}
8 changes: 5 additions & 3 deletions CodeQualityTalk.Metalama/Abstractions/IDocumentFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Metalama.Extensions.Architecture.Aspects;
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using Metalama.Extensions.Architecture.Aspects;

namespace CodeQualityTalk.Abstractions;

[DerivedTypesMustRespectNamingConvention("*Factory")]
[DerivedTypesMustRespectNamingConvention( "*Factory" )]
public interface IDocumentFactory
{
IDocument CreateDocument(string name, DocumentCreationContext context);
IDocument CreateDocument( string name, DocumentCreationContext context );
}
22 changes: 13 additions & 9 deletions CodeQualityTalk.Metalama/Architecture/AdvancedFabric.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CodeQualityTalk.Abstractions;
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Abstractions;
using Metalama.Framework.Aspects;
using Metalama.Framework.Code;
using Metalama.Framework.Diagnostics;
Expand All @@ -11,21 +13,23 @@ public class AdvancedFabric : ProjectFabric
{
private const string _documentNamespace = "CodeQualityTalk.Documents";

private static DiagnosticDefinition<(INamedType Type, string Namespace)> _warning = new("MY001", Severity.Warning,
"(Metalama) The '{0}' type must be in the '{1}' namespace.");
private static readonly DiagnosticDefinition<(INamedType Type, string Namespace)> _warning = new(
"MY001",
Severity.Warning,
"(Metalama) The '{0}' type must be in the '{1}' namespace." );

public override void AmendProject(IProjectAmender amender)
public override void AmendProject( IProjectAmender amender )
{
// Validate namespace.
amender.SelectReflectionType(typeof(IDocument))
.ValidateInboundReferences(ValidateNamespace, ReferenceGranularity.Type, ReferenceKinds.BaseType);
amender.SelectReflectionType( typeof(IDocument) )
.ValidateInboundReferences( ValidateNamespace, ReferenceGranularity.Type, ReferenceKinds.BaseType );
}

private static void ValidateNamespace(ReferenceValidationContext context)
private static void ValidateNamespace( ReferenceValidationContext context )
{
if (context.Origin.Namespace.FullName != _documentNamespace)
if ( context.Origin.Namespace.FullName != _documentNamespace )
{
context.Diagnostics.Report(_warning.WithArguments((context.Origin.Type, _documentNamespace)));
context.Diagnostics.Report( _warning.WithArguments( (context.Origin.Type, _documentNamespace) ) );
}
}
}
10 changes: 6 additions & 4 deletions CodeQualityTalk.Metalama/Architecture/Fabric.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Metalama.Extensions.Architecture;
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using Metalama.Extensions.Architecture;
using Metalama.Extensions.Architecture.Predicates;
using Metalama.Framework.Fabrics;

namespace CodeQualityTalk.Architecture;

public class Fabric : ProjectFabric
{
public override void AmendProject(IProjectAmender amender)
public override void AmendProject( IProjectAmender amender )
{
// Validate dependencies.
amender
.Select(c=>c.GlobalNamespace.GetDescendant("CodeQualityTalk.Documents")!)
.CannotBeUsedFrom( x => x.Namespace("CodeQualityTalk.Abstractions"));
.Select( c => c.GlobalNamespace.GetDescendant( "CodeQualityTalk.Documents" )! )
.CannotBeUsedFrom( x => x.Namespace( "CodeQualityTalk.Abstractions" ) );
}
}
4 changes: 1 addition & 3 deletions CodeQualityTalk.Metalama/CodeQualityTalk.Metalama.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
<RootNamespace>CodeQualityTalk</RootNamespace>
</PropertyGroup>

<Import Project="c:\src\Metalama.Extensions\Metalama.Extensions.Import.props"/>

<ItemGroup>

<PackageReference Include="Metalama.Extensions.Architecture" Version="$(MetalamaExtensionsVersion)"/>
<PackageReference Include="Metalama.Extensions.Architecture" />
</ItemGroup>

</Project>
13 changes: 8 additions & 5 deletions CodeQualityTalk.Metalama/CreditNote.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Abstractions;

namespace CodeQualityTalk;

internal class CreditNote : IDocument
{
public string OwnerId { get; }
public string Name { get; }
public string OwnerId { get; }

public string Name { get; }

public CreditNote(string ownerId, string name)
public CreditNote( string ownerId, string name )
{
OwnerId = ownerId;
Name = name;
this.OwnerId = ownerId;
this.Name = name;
}
}
13 changes: 8 additions & 5 deletions CodeQualityTalk.Metalama/Documents/Invoice.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Abstractions;

namespace CodeQualityTalk.Documents;

internal class Invoice : IDocument
{
public string OwnerId { get; }
public string Name { get; }
public string OwnerId { get; }

public string Name { get; }

public Invoice(string ownerId, string name)
public Invoice( string ownerId, string name )
{
OwnerId = ownerId;
Name = name;
this.OwnerId = ownerId;
this.Name = name;
}
}
5 changes: 3 additions & 2 deletions CodeQualityTalk.Metalama/Factories/CreditNoteCreator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Abstractions;

namespace CodeQualityTalk.Factories;

public class CreditNoteCreator : IDocumentFactory
{
public IDocument CreateDocument(string name, DocumentCreationContext context)
=> new CreditNote(context.OwnerId, name);
public IDocument CreateDocument( string name, DocumentCreationContext context ) => new CreditNote( context.OwnerId, name );
}
5 changes: 3 additions & 2 deletions CodeQualityTalk.Metalama/Factories/InvoiceFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using CodeQualityTalk.Abstractions;
using CodeQualityTalk.Documents;

namespace CodeQualityTalk.Factories;

public class InvoiceFactory : IDocumentFactory
{
public IDocument CreateDocument(string name, DocumentCreationContext context)
=> new Invoice(context.OwnerId, name);
public IDocument CreateDocument( string name, DocumentCreationContext context ) => new Invoice( context.OwnerId, name );
}
Loading

0 comments on commit b1d6229

Please sign in to comment.