Skip to content

Commit

Permalink
chore: csharpier format
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Nov 15, 2023
1 parent c5098d6 commit 6ab75e8
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ protected MembersMappingBuilderContext(MappingBuilderContext builderContext, T m
_unmappedSourceMemberNames = GetSourceMemberNames();
TargetMembers = GetTargetMembers();

IgnoredSourceMemberNames = builderContext.Configuration.Properties.IgnoredSources
IgnoredSourceMemberNames = builderContext
.Configuration
.Properties
.IgnoredSources
.Concat(GetIgnoredObsoleteSourceMembers())
.ToHashSet();
var ignoredTargetMemberNames = builderContext.Configuration.Properties.IgnoredTargets
var ignoredTargetMemberNames = builderContext
.Configuration
.Properties
.IgnoredTargets
.Concat(GetIgnoredObsoleteTargetMembers())
.ToHashSet();

Expand All @@ -47,7 +53,8 @@ protected MembersMappingBuilderContext(MappingBuilderContext builderContext, T m
MemberConfigsByRootTargetName = GetMemberConfigurations();

// source and target properties may have been ignored and mapped explicitly
_mappedAndIgnoredSourceMemberNames = MemberConfigsByRootTargetName.Values
_mappedAndIgnoredSourceMemberNames = MemberConfigsByRootTargetName
.Values
.SelectMany(v => v.Select(s => s.Source.Path.First()))
.ToHashSet();
_mappedAndIgnoredSourceMemberNames.IntersectWith(IgnoredSourceMemberNames);
Expand Down Expand Up @@ -98,7 +105,8 @@ private IEnumerable<string> GetIgnoredObsoleteTargetMembers()
if (!obsoleteStrategy.HasFlag(IgnoreObsoleteMembersStrategy.Target))
return Enumerable.Empty<string>();

return BuilderContext.SymbolAccessor
return BuilderContext
.SymbolAccessor
.GetAllAccessibleMappableMembers(Mapping.TargetType)
.Where(x => BuilderContext.SymbolAccessor.HasAttribute<ObsoleteAttribute>(x.MemberSymbol))
.Select(x => x.Name);
Expand All @@ -111,7 +119,8 @@ private IEnumerable<string> GetIgnoredObsoleteSourceMembers()
if (!obsoleteStrategy.HasFlag(IgnoreObsoleteMembersStrategy.Source))
return Enumerable.Empty<string>();

return BuilderContext.SymbolAccessor
return BuilderContext
.SymbolAccessor
.GetAllAccessibleMappableMembers(Mapping.SourceType)
.Where(x => BuilderContext.SymbolAccessor.HasAttribute<ObsoleteAttribute>(x.MemberSymbol))
.Select(x => x.Name);
Expand All @@ -124,14 +133,18 @@ private HashSet<string> GetSourceMemberNames()

private Dictionary<string, IMappableMember> GetTargetMembers()
{
return BuilderContext.SymbolAccessor
return BuilderContext
.SymbolAccessor
.GetAllAccessibleMappableMembers(Mapping.TargetType)
.ToDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);
}

private Dictionary<string, List<PropertyMappingConfiguration>> GetMemberConfigurations()
{
return BuilderContext.Configuration.Properties.ExplicitMappings
return BuilderContext
.Configuration
.Properties
.ExplicitMappings
.GroupBy(x => x.Target.Path.First())
.ToDictionary(x => x.Key, x => x.ToList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ private static void BuildInitOnlyMemberMappings(INewInstanceBuilderContext<IMapp
}

if (
!ctx.BuilderContext.SymbolAccessor.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(targetMember.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out var sourceMemberPath
)
!ctx.BuilderContext
.SymbolAccessor
.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(targetMember.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out var sourceMemberPath
)
)
{
if (targetMember.IsRequired)
Expand Down Expand Up @@ -205,7 +207,8 @@ private static void BuildConstructorMapping(INewInstanceBuilderContext<IMapping>
// parameterless ctor is prio 2
// then by descending parameter count
// ctors annotated with [Obsolete] are considered last unless they have a MapperConstructor attribute set
var ctorCandidates = namedTargetType.InstanceConstructors
var ctorCandidates = namedTargetType
.InstanceConstructors
.Where(ctor => ctx.BuilderContext.SymbolAccessor.IsDirectlyAccessible(ctor))
.OrderByDescending(x => ctx.BuilderContext.SymbolAccessor.HasAttribute<MapperConstructorAttribute>(x))
.ThenBy(x => ctx.BuilderContext.SymbolAccessor.HasAttribute<ObsoleteAttribute>(x))
Expand Down Expand Up @@ -315,13 +318,15 @@ private static bool TryFindConstructorParameterSourcePath(

if (!ctx.MemberConfigsByRootTargetName.TryGetValue(parameter.Name, out var memberConfigs))
{
return ctx.BuilderContext.SymbolAccessor.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(parameter.Name),
ctx.IgnoredSourceMemberNames,
true,
out sourcePath
);
return ctx.BuilderContext
.SymbolAccessor
.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(parameter.Name),
ctx.IgnoredSourceMemberNames,
true,
out sourcePath
);
}

if (memberConfigs.Count > 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ out MemberPath? sourcePath
var ignoreCase = ctx.BuilderContext.MapperConfiguration.PropertyNameMappingStrategy == PropertyNameMappingStrategy.CaseInsensitive;

if (
ctx.BuilderContext.SymbolAccessor.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(field.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out sourcePath
)
ctx.BuilderContext
.SymbolAccessor
.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(field.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out sourcePath
)
)
{
return true;
Expand All @@ -195,12 +197,14 @@ out sourcePath
if (!ctx.Mapping.SourceType.IsTupleType || ctx.Mapping.SourceType is not INamedTypeSymbol namedType)
return false;

var mappableField = namedType.TupleElements.FirstOrDefault(
x =>
x.CorrespondingTupleField != default
&& !ctx.IgnoredSourceMemberNames.Contains(x.Name)
&& string.Equals(field.CorrespondingTupleField!.Name, x.CorrespondingTupleField!.Name)
);
var mappableField = namedType
.TupleElements
.FirstOrDefault(
x =>
x.CorrespondingTupleField != default
&& !ctx.IgnoredSourceMemberNames.Contains(x.Name)
&& string.Equals(field.CorrespondingTupleField!.Name, x.CorrespondingTupleField!.Name)
);

if (mappableField == default)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public static void BuildMappingBody(IMembersContainerBuilderContext<IMemberAssig
}

if (
ctx.BuilderContext.SymbolAccessor.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(targetMember.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out var sourceMemberPath
)
ctx.BuilderContext
.SymbolAccessor
.TryFindMemberPath(
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(targetMember.Name),
ctx.IgnoredSourceMemberNames,
ignoreCase,
out var sourceMemberPath
)
)
{
BuildMemberAssignmentMapping(ctx, sourceMemberPath, new MemberPath(new[] { targetMember }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ public static void BuildMappingBody(MappingBuilderContext ctx, UserDefinedNewIns
var mappings = GetUserMappingCandidates(ctx)
.Where(
x =>
mapping.TypeParameters.DoesTypesSatisfyTypeParameterConstraints(
ctx.SymbolAccessor,
x.SourceType.NonNullable(),
x.TargetType
)
mapping
.TypeParameters
.DoesTypesSatisfyTypeParameterConstraints(ctx.SymbolAccessor, x.SourceType.NonNullable(), x.TargetType)
);

BuildMappingBody(ctx, mapping, mappings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static class CtorMappingBuilder
return null;

// resolve ctors which have the source as single argument
var ctorMethod = namedTarget.InstanceConstructors
var ctorMethod = namedTarget
.InstanceConstructors
.Where(ctx.SymbolAccessor.IsDirectlyAccessible)
.FirstOrDefault(
m =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ INewInstanceMapping valueMapping
var enumerableType = ctx.Types.Get(typeof(IEnumerable<>));
var dictionaryType = ctx.Types.Get(typeof(Dictionary<,>));

var fromEnumerableCtor = dictionaryType.Constructors.FirstOrDefault(
x =>
x.Parameters.Length == 1
&& SymbolEqualityComparer.Default.Equals(((INamedTypeSymbol)x.Parameters[0].Type).ConstructedFrom, enumerableType)
);
var fromEnumerableCtor = dictionaryType
.Constructors
.FirstOrDefault(
x =>
x.Parameters.Length == 1
&& SymbolEqualityComparer.Default.Equals(((INamedTypeSymbol)x.Parameters[0].Type).ConstructedFrom, enumerableType)
);

if (fromEnumerableCtor != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ _ when ctx.SymbolAccessor.HasAttribute<FlagsAttribute>(ctx.Target) => EnumCastMa
enumMemberMappings.TargetMembers,
fallbackMapping
);
var differentValueExplicitEnumMappings = enumMemberMappings.ExplicitMemberMappings
var differentValueExplicitEnumMappings = enumMemberMappings
.ExplicitMemberMappings
.Where(x => x.Key.ConstantValue?.Equals(x.Value.ConstantValue) != true)
.ToDictionary(x => x.Key, x => x.Value, (IEqualityComparer<IFieldSymbol>)SymbolEqualityComparer.Default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ private static LinqEnumerableMapping BuildLinqMapping(
return null;

var typedEnumerable = ctx.Types.Get(typeof(IEnumerable<>)).Construct(typeSymbol);
var hasCtor = namedType.Constructors.Any(
m => m.Parameters.Length == 1 && SymbolEqualityComparer.Default.Equals(m.Parameters[0].Type, typedEnumerable)
);
var hasCtor = namedType
.Constructors
.Any(m => m.Parameters.Length == 1 && SymbolEqualityComparer.Default.Equals(m.Parameters[0].Type, typedEnumerable));
if (hasCtor)
return namedType;

Expand Down Expand Up @@ -294,7 +294,8 @@ INewInstanceMapping elementMapping
// and has an implicit .Add() method
// the implicit check is an easy way to exclude for example immutable types.
if (
ctx.CollectionInfos!.Target.CollectionType == CollectionType.Array || !ctx.CollectionInfos.Target.HasImplicitCollectionAddMethod
ctx.CollectionInfos!.Target.CollectionType == CollectionType.Array
|| !ctx.CollectionInfos.Target.HasImplicitCollectionAddMethod
)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static class NewInstanceObjectPropertyMappingBuilder
);

if (
ctx.Target is not INamedTypeSymbol namedTarget || namedTarget.Constructors.All(x => !ctx.SymbolAccessor.IsDirectlyAccessible(x))
ctx.Target is not INamedTypeSymbol namedTarget
|| namedTarget.Constructors.All(x => !ctx.SymbolAccessor.IsDirectlyAccessible(x))
)
return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper;

Expand Down
12 changes: 6 additions & 6 deletions src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ private static bool BuildGenericTypeParameters(

var targetType = parameters.Target?.Type ?? methodSymbol.ReturnType.UpgradeNullable();
var targetTypeParameter = methodSymbol.TypeParameters.FirstOrDefault(x => SymbolEqualityComparer.Default.Equals(x, targetType));
var sourceTypeParameter = methodSymbol.TypeParameters.FirstOrDefault(
x => SymbolEqualityComparer.Default.Equals(x, parameters.Source.Type)
);
var sourceTypeParameter = methodSymbol
.TypeParameters
.FirstOrDefault(x => SymbolEqualityComparer.Default.Equals(x, parameters.Source.Type));

var expectedTypeParametersCount = 0;
if (targetTypeParameter != null)
Expand Down Expand Up @@ -324,9 +324,9 @@ private static bool BuildParameters(

private static MethodParameter? BuildReferenceHandlerParameter(SimpleMappingBuilderContext ctx, IMethodSymbol method)
{
var refHandlerParameterSymbol = method.Parameters.FirstOrDefault(
p => ctx.SymbolAccessor.HasAttribute<ReferenceHandlerAttribute>(p)
);
var refHandlerParameterSymbol = method
.Parameters
.FirstOrDefault(p => ctx.SymbolAccessor.HasAttribute<ReferenceHandlerAttribute>(p));
if (refHandlerParameterSymbol == null)
return null;

Expand Down
7 changes: 3 additions & 4 deletions src/Riok.Mapperly/Helpers/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ namespace Riok.Mapperly.Helpers;

internal static class SymbolExtensions
{
private static readonly SymbolDisplayFormat _fullyQualifiedNullableFormat =
SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(
SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier
);
private static readonly SymbolDisplayFormat _fullyQualifiedNullableFormat = SymbolDisplayFormat
.FullyQualifiedFormat
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);

private static readonly ImmutableHashSet<string> _wellKnownImmutableTypes = ImmutableHashSet.Create(
typeof(Uri).FullName!,
Expand Down
9 changes: 5 additions & 4 deletions src/Riok.Mapperly/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var assemblyName = context.CompilationProvider.Select((x, _) => x.Assembly.Name);

// report compilation diagnostics
var compilationDiagnostics = context.CompilationProvider.SelectMany(
static (compilation, _) => BuildCompilationDiagnostics(compilation)
);
var compilationDiagnostics = context
.CompilationProvider
.SelectMany(static (compilation, _) => BuildCompilationDiagnostics(compilation));
context.ReportDiagnostics(compilationDiagnostics);

// build the compilation context
var compilationContext = context.CompilationProvider
var compilationContext = context
.CompilationProvider
.Select(static (c, _) => new CompilationContext(c, new WellKnownTypes(c), new FileNameBuilder()))
.WithTrackingName(MapperGeneratorStepNames.BuildCompilationContext);

Expand Down
11 changes: 7 additions & 4 deletions src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ namespace Riok.Mapperly;

internal static class SyntaxProvider
{
private static readonly SymbolDisplayFormat _fullyQualifiedFormatWithoutGlobal =
SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining);
private static readonly SymbolDisplayFormat _fullyQualifiedFormatWithoutGlobal = SymbolDisplayFormat
.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining);

public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations(IncrementalGeneratorInitializationContext context)
{
return context.SyntaxProvider
return context
.SyntaxProvider
.CreateSyntaxProvider(
static (s, _) => s is ClassDeclarationSyntax { AttributeLists.Count: > 0 },
static (ctx, ct) => GetMapperDeclaration(ctx, ct)
Expand All @@ -24,7 +26,8 @@ public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations

public static IncrementalValueProvider<IAssemblySymbol?> GetMapperDefaultDeclarations(IncrementalGeneratorInitializationContext context)
{
return context.SyntaxProvider
return context
.SyntaxProvider
.CreateSyntaxProvider(
static (s, _) => s is CompilationUnitSyntax { AttributeLists.Count: > 0 },
static (ctx, ct) => GetMapperDefaultDeclarations(ctx, ct)
Expand Down
6 changes: 4 additions & 2 deletions src/Riok.Mapperly/SyntaxProvider.Roslyn4.4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal static class SyntaxProvider
{
public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations(IncrementalGeneratorInitializationContext context)
{
return context.SyntaxProvider
return context
.SyntaxProvider
.ForAttributeWithMetadataName(
MapperGenerator.MapperAttributeName,
static (s, _) => s is ClassDeclarationSyntax,
Expand All @@ -22,7 +23,8 @@ public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations

public static IncrementalValueProvider<IAssemblySymbol?> GetMapperDefaultDeclarations(IncrementalGeneratorInitializationContext context)
{
return context.SyntaxProvider
return context
.SyntaxProvider
.ForAttributeWithMetadataName(
MapperGenerator.MapperDefaultsAttributeName,
static (s, _) => s is CompilationUnitSyntax,
Expand Down
Loading

0 comments on commit 6ab75e8

Please sign in to comment.