Skip to content

Commit

Permalink
chsarpier
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSauer committed Jan 25, 2024
1 parent b01a565 commit abc40d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 62 deletions.
21 changes: 9 additions & 12 deletions AutomaticInterface/AutomaticInterface/AutomaticInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ public void Execute(GeneratorExecutionContext context)

string GetLogPath()
{
var mainSyntaxTree = context
.Compilation
.SyntaxTrees
.First(x => x.HasCompilationUnitRoot);
var mainSyntaxTree = context.Compilation.SyntaxTrees.First(x =>
x.HasCompilationUnitRoot
);

var logDir =
Path.GetDirectoryName(mainSyntaxTree.FilePath) ?? Environment.CurrentDirectory;
Expand Down Expand Up @@ -200,14 +199,12 @@ SemanticModel classSemanticModel

var paramResult = new HashSet<string>();
method
.Parameters
.Select(GetMethodSignature)
.Parameters.Select(GetMethodSignature)
.ToList()
.ForEach(x => paramResult.Add(x));

var typedArgs = method
.TypeParameters
.Select(arg => (arg.ToDisplayString(), arg.GetWhereStatement()))
.TypeParameters.Select(arg => (arg.ToDisplayString(), arg.GetWhereStatement()))
.ToList();
codeGenerator.AddMethodToInterface(
name,
Expand Down Expand Up @@ -356,8 +353,8 @@ ClassDeclarationSyntax classSyntax
var match = classSyntax
.DescendantNodes()
.OfType<EventFieldDeclarationSyntax>()
.SingleOrDefault(
x => x.Declaration.Variables.Any(y => y.Identifier.ValueText == method.Name)
.SingleOrDefault(x =>
x.Declaration.Variables.Any(y => y.Identifier.ValueText == method.Name)
);

if (match is null)
Expand Down Expand Up @@ -420,15 +417,15 @@ NamespaceDeclarationSyntax ndSyntax
);
}

return [ ..allUsings.Select(x => x.ToString()) ];
return [..allUsings.Select(x => x.ToString())];
}

private static List<ClassDeclarationSyntax> GetClassesToAddInterfaceFor(
SyntaxReceiver receiver,
Compilation compilation
)
{
List<ClassDeclarationSyntax> classSymbols = [ ];
List<ClassDeclarationSyntax> classSymbols = [];
foreach (var cls in receiver.CandidateClasses)
{
var model = compilation.GetSemanticModel(cls.SyntaxTree);
Expand Down
27 changes: 13 additions & 14 deletions AutomaticInterface/AutomaticInterface/InterfaceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ bool hasNullableContext
)
{
private readonly string autogenerated = """
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
""";
""";

private readonly HashSet<string> usings = [ "using System.CodeDom.Compiler;" ];
private readonly List<PropertyInfo> propertyInfos = [ ];
private readonly List<MethodInfo> methodInfos = [ ];
private readonly List<EventInfo> events = [ ];
private readonly HashSet<string> usings = ["using System.CodeDom.Compiler;"];
private readonly List<PropertyInfo> propertyInfos = [];
private readonly List<MethodInfo> methodInfos = [];
private readonly List<EventInfo> events = [];
private string classDocumentation = string.Empty;
private string genericType = string.Empty;

Expand Down Expand Up @@ -170,8 +170,7 @@ private static void BuildMethod(CodeBuilder cb, MethodInfo method)
if (method.GenericArgs.Count != 0)
{
var constraints = method
.GenericArgs
.Where(a => !string.IsNullOrWhiteSpace(a.WhereConstraint))
.GenericArgs.Where(a => !string.IsNullOrWhiteSpace(a.WhereConstraint))
.Select(a => a.WhereConstraint);
cb.Append($" {string.Join(" ", constraints)}");
}
Expand Down
17 changes: 7 additions & 10 deletions AutomaticInterface/AutomaticInterface/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ string attributeName
{
return classSyntax.AttributeLists.Count > 0
&& classSyntax
.AttributeLists
.SelectMany(
al =>
al.Attributes.Where(a =>
{
return (a?.Name as IdentifierNameSyntax)?.Identifier.Text
== attributeName;
})
.AttributeLists.SelectMany(al =>
al.Attributes.Where(a =>
{
return (a?.Name as IdentifierNameSyntax)?.Identifier.Text
== attributeName;
})
)
.Any();
}
Expand All @@ -66,8 +64,7 @@ public static string GetNamespace(this CompilationUnitSyntax root)
return root.ChildNodes()
.OfType<BaseNamespaceDeclarationSyntax>()
.First()
.Name
.ToString();
.Name.ToString();
}

public static List<string> GetUsings(this CompilationUnitSyntax root)
Expand Down
18 changes: 8 additions & 10 deletions AutomaticInterface/Tests/CSharpSourceGeneratorVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected override CompilationOptions CreateCompilationOptions()
);

return compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions
.SpecificDiagnosticOptions
.SetItems(GetNullableWarningsFromCompiler())
compilationOptions.SpecificDiagnosticOptions.SetItems(
GetNullableWarningsFromCompiler()
)
);
}

Expand All @@ -37,13 +37,11 @@ private static ImmutableDictionary<
> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
var commandLineArguments = CSharpCommandLineParser
.Default
.Parse(
args,
baseDirectory: Environment.CurrentDirectory,
sdkDirectory: Environment.CurrentDirectory
);
var commandLineArguments = CSharpCommandLineParser.Default.Parse(
args,
baseDirectory: Environment.CurrentDirectory,
sdkDirectory: Environment.CurrentDirectory
);
var nullableWarnings = commandLineArguments
.CompilationOptions
.SpecificDiagnosticOptions;
Expand Down
28 changes: 12 additions & 16 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace Tests
public class GeneratorTests
{
private readonly ImmutableArray<string> references = AppDomain
.CurrentDomain
.GetAssemblies()
.CurrentDomain.GetAssemblies()
.Where(assembly => !assembly.IsDynamic)
.Select(assembly => assembly.Location)
.ToImmutableArray();
Expand Down Expand Up @@ -48,20 +47,17 @@ private async Task RunTestAsync(
};

tester.ReferenceAssemblies.AddAssemblies(references);
tester
.TestState
.AdditionalReferences
.Add(typeof(GenerateAutomaticInterfaceAttribute).Assembly);

tester
.ExpectedDiagnostics
.AddRange(
new List<DiagnosticResult>()
{
new("AutomaticInterface", DiagnosticSeverity.Info),
new("AutomaticInterface", DiagnosticSeverity.Info)
}
);
tester.TestState.AdditionalReferences.Add(
typeof(GenerateAutomaticInterfaceAttribute).Assembly
);

tester.ExpectedDiagnostics.AddRange(
new List<DiagnosticResult>()
{
new("AutomaticInterface", DiagnosticSeverity.Info),
new("AutomaticInterface", DiagnosticSeverity.Info)
}
);

await tester.RunAsync();
}
Expand Down

0 comments on commit abc40d5

Please sign in to comment.