Skip to content

Commit 47012ca

Browse files
authored
Merge pull request #35 from nuskey8/refactoring-sourcegenerator
refactor: CreateFunctionGeneartor
2 parents dac38e2 + e6422bf commit 47012ca

File tree

2 files changed

+8
-52
lines changed

2 files changed

+8
-52
lines changed

src/Luau.SourceGenerator/CreateFunctionGenerator.cs

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,22 @@
55

66
namespace Luau.SourceGenerator;
77

8-
// TODO: refactoring
9-
10-
internal class CreateFunctionContext : IEquatable<CreateFunctionContext>
8+
internal record CreateFunctionContext
119
{
1210
public CreateFunctionMethod? Method { get; set; }
13-
public required DiagnosticReporter DiagnosticReporter { get; init; }
14-
public required SemanticModel Model { get; init; }
15-
16-
public bool Equals(CreateFunctionContext other)
17-
{
18-
return Method == other.Method;
19-
}
20-
21-
public override bool Equals(object obj)
22-
{
23-
return obj is CreateFunctionContext ctx && Equals(ctx);
24-
}
25-
26-
public override int GetHashCode()
27-
{
28-
return Method == null ? 0 : Method.GetHashCode();
29-
}
11+
public required IgnoreEquality<DiagnosticReporter> DiagnosticReporter { get; init; }
12+
public required IgnoreEquality<SemanticModel> Model { get; init; }
3013
}
3114

32-
internal class CreateFunctionMethod : IEquatable<CreateFunctionMethod>
15+
internal record CreateFunctionMethod
3316
{
34-
public required CreateFunctionMethodParameter[] Parameters { get; init; }
17+
public required EquatableArray<CreateFunctionMethodParameter> Parameters { get; init; }
3518
public required string ReturnTypeName { get; init; }
3619
public required bool HasReturnValue { get; init; }
3720
public required bool IsAsync { get; init; }
3821
public required string FilePath { get; init; }
3922
public required int LineNumber { get; init; }
4023

41-
public bool Equals(CreateFunctionMethod other)
42-
{
43-
return Parameters.SequenceEqual(other.Parameters) && ReturnTypeName == other.ReturnTypeName;
44-
}
45-
46-
public override bool Equals(object obj)
47-
{
48-
return obj is CreateFunctionMethod other && Equals(other);
49-
}
50-
51-
public override int GetHashCode()
52-
{
53-
if (Parameters.Length == 0) return 0;
54-
var hashCode = Parameters[0].GetHashCode();
55-
for (int i = 1; i < Parameters.Length; i++)
56-
{
57-
hashCode ^= Parameters[i].GetHashCode();
58-
}
59-
hashCode ^= ReturnTypeName.GetHashCode();
60-
return hashCode;
61-
}
62-
6324
public static CreateFunctionMethod Create(Location location, bool isAsync, ITypeSymbol? returnType, CreateFunctionMethodParameter[] parameters)
6425
{
6526
var returnTypeName = returnType == null ? "void" : returnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
@@ -212,12 +173,7 @@ internal static partial class GeneratedLuauStateExtensions
212173
.ToArray();
213174

214175
var returnType = methodSymbol.ReturnsVoid ? null : methodSymbol.ReturnType;
215-
var isAsync = returnType != null && (
216-
returnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.Threading.Tasks.Task" ||
217-
returnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.Threading.Tasks.ValueTask" ||
218-
returnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::Cysharp.Threading.Tasks.UniTask" ||
219-
returnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::UnityEngine.Awaitable"
220-
);
176+
var isAsync = returnType.IsTaskType();
221177

222178
result.Method = CreateFunctionMethod.Create(actionExpression.GetLocation(), isAsync, returnType, parameters);
223179
}
@@ -244,9 +200,9 @@ void EmitRegisterFunctionMethod(SourceProductionContext context, ImmutableArray<
244200
foreach (var methodContext in methodContexts)
245201
{
246202
// check compilation errors
247-
if (methodContext.DiagnosticReporter.HasDiagnostics)
203+
if (methodContext.DiagnosticReporter.Value.HasDiagnostics)
248204
{
249-
methodContext.DiagnosticReporter.ReportToContext(context);
205+
methodContext.DiagnosticReporter.Value.ReportToContext(context);
250206
continue;
251207
}
252208

Binary file not shown.

0 commit comments

Comments
 (0)