Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions src/Dunet.Generator/Dunet.Generator.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<IsPackable>True</IsPackable>
<NoWarn>$(NoWarn);NU5128</NoWarn>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<RootNamespace>Dunet.Generator</RootNamespace>
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

<ItemGroup>
<None Include="$(OutputPath)/$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<IsPackable>True</IsPackable>
<NoWarn>$(NoWarn);NU5128</NoWarn>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<RootNamespace>Dunet.Generator</RootNamespace>
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>
<ItemGroup>
<None
Include="$(OutputPath)/$(AssemblyName).dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false"
/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
3 changes: 1 addition & 2 deletions src/Dunet.Generator/IdentifierExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var s when s.StartsWith("@") => identifier,
// - Prepend '@' to prevent keyword conflicts.
// - Lowercase the first character to abide by C# style rules for method parameter
// casing and prevent collision with its type.
[var firstCharacter, .. var rest]
=> $"@{char.ToLowerInvariant(firstCharacter)}{rest}",
[var firstCharacter, .. var rest] => $"@{char.ToLowerInvariant(firstCharacter)}{rest}",
// If anything else came in, we just return it back and let the caller handle it.
_ => identifier,
};
Expand Down
10 changes: 4 additions & 6 deletions src/Dunet.Generator/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public static string ToKeyword(this Accessibility accessibility) =>
accessibility switch
{
Accessibility.Public => "public",
Accessibility.ProtectedOrInternal
or Accessibility.ProtectedOrFriend
=> "protected internal",
Accessibility.ProtectedOrInternal or Accessibility.ProtectedOrFriend =>
"protected internal",
Accessibility.Internal or Accessibility.Friend => "internal",
Accessibility.Protected => "protected",
Accessibility.ProtectedAndInternal
or Accessibility.ProtectedAndFriend
=> "private protected",
Accessibility.ProtectedAndInternal or Accessibility.ProtectedAndFriend =>
"private protected",
Accessibility.Private => "private",
Accessibility.NotApplicable => "",
_ => "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal static class RecordDeclarationSyntaxParser
public static IEnumerable<TypeParameter> GetTypeParameters(
this RecordDeclarationSyntax record
) =>
record
.TypeParameterList?.Parameters
.Select(static typeParam => new TypeParameter(typeParam.Identifier.ToString())) ?? [];
record.TypeParameterList?.Parameters.Select(static typeParam => new TypeParameter(
typeParam.Identifier.ToString()
)) ?? [];

/// <summary>
/// Gets the type parameter constraints of this record declaration.
Expand All @@ -43,15 +43,13 @@ public static IEnumerable<Parameter> GetParameters(
this RecordDeclarationSyntax record,
SemanticModel semanticModel
) =>
record
.ParameterList?.Parameters
.Select(parameter => new Parameter(
Type: new ParameterType(
Identifier: parameter.Type?.ToString() ?? "",
IsInterface: parameter.Type.IsInterfaceType(semanticModel)
),
Identifier: parameter.Identifier.ToString()
)) ?? [];
record.ParameterList?.Parameters.Select(parameter => new Parameter(
Type: new ParameterType(
Identifier: parameter.Type?.ToString() ?? "",
IsInterface: parameter.Type.IsInterfaceType(semanticModel)
),
Identifier: parameter.Identifier.ToString()
)) ?? [];

/// <summary>
/// Gets the properties declared in this record.
Expand Down Expand Up @@ -94,7 +92,7 @@ SemanticModel semanticModel
{
Identifier = nestedRecord.Identifier.ToString(),
TypeParameters = nestedRecord.GetTypeParameters().ToImmutableEquatableArray(),
Parameters = nestedRecord.GetParameters(semanticModel).ToImmutableEquatableArray()
Parameters = nestedRecord.GetParameters(semanticModel).ToImmutableEquatableArray(),
});

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Dunet.Generator/UnionGeneration/UnionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ private static void Emit(SourceProductionContext context, UnionDeclaration union
var union = UnionSourceBuilder.Build(unionRecord);
context.AddSource(
unionRecord.TypeParameters.Count == 0
? $"{unionRecord.Namespace}.{unionRecord.Name}.g.cs"
: $"{unionRecord.Namespace}.{unionRecord.Name}.{unionRecord.TypeParameters.Count}.g.cs",
? $"{unionRecord.Namespace}.{unionRecord.Name}.g.cs"
: $"{unionRecord.Namespace}.{unionRecord.Name}.{unionRecord.TypeParameters.Count}.g.cs",
SourceText.From(union, Encoding.UTF8)
);

Expand Down
4 changes: 3 additions & 1 deletion src/Dunet.Generator/UnionGeneration/UnionSourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ VariantDeclaration variant
);
builder.Append($" TState state");
builder.AppendLine(union.Variants.Count > 0 ? "," : string.Empty);
builder.Append($" global::System.Action<TState, {specificVariant.Identifier}");
builder.Append(
$" global::System.Action<TState, {specificVariant.Identifier}"
);
builder.AppendTypeParams(specificVariant.TypeParameters);
builder.AppendLine($"> {specificVariant.Identifier.ToMethodParameterCase()},");
builder.AppendLine($" global::System.Action<TState> @else");
Expand Down
24 changes: 13 additions & 11 deletions src/Dunet/Dunet.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<IsPackable>True</IsPackable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<IsPackable>True</IsPackable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Dunet.Generator\Dunet.Generator.csproj" PackAsAnalyzer="true" PrivateAssets="all" />
<ProjectReference
Include="..\Dunet.Generator\Dunet.Generator.csproj"
PackAsAnalyzer="true"
PrivateAssets="all"
/>
</ItemGroup>
</Project>
53 changes: 33 additions & 20 deletions test/Compilation/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Dunet.Generator.UnionGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -39,29 +42,39 @@ public static CompilationResult Compile(params string[] sources)
);
}

private static CSharpCompilation CreateCompilation(params string[] sources) =>
CSharpCompilation.Create(
private static CSharpCompilation CreateCompilation(params string[] sources)
{
// Include metadata references for all currently loaded assemblies that have a physical location.
// This keeps the test compilation environment in sync with the test host and avoids
// manually listing specific runtime assemblies.
var loadedAssemblyPaths = AppDomain
.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
.Select(a => a.Location)
.Distinct()
.ToList();

var references = loadedAssemblyPaths
.Select(path => MetadataReference.CreateFromFile(path))
.ToList();

// Ensure the assembly containing UnionAttribute is referenced in case it's not already loaded.
var unionAssemblyLocation = typeof(UnionAttribute).GetTypeInfo().Assembly.Location;
if (
!string.IsNullOrEmpty(unionAssemblyLocation)
&& !loadedAssemblyPaths.Contains(unionAssemblyLocation)
)
{
references.Add(MetadataReference.CreateFromFile(unionAssemblyLocation));
}

return CSharpCompilation.Create(
"compilation",
sources.Select(static source => CSharpSyntaxTree.ParseText(source)),
[
// Resolves to System.Private.CoreLib.dll
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
// Resolves to System.Runtime.dll, which is needed for the Attribute type
// Can't use typeof(Attribute).GetTypeInfo().Assembly.Location because it resolves to System.Private.CoreLib.dll
MetadataReference.CreateFromFile(
AppDomain
.CurrentDomain.GetAssemblies()
.First(static assembly =>
assembly.FullName?.Contains("System.Runtime") is true
)
.Location
),
MetadataReference.CreateFromFile(
typeof(UnionAttribute).GetTypeInfo().Assembly.Location
)
],
references,
new CSharpCompilationOptions(OutputKind.ConsoleApplication)
);
}

private static GenerationResult RunGenerator(Microsoft.CodeAnalysis.Compilation compilation)
{
Expand Down