Skip to content

Commit d897c89

Browse files
committed
tuple → record
1 parent 5c39fe7 commit d897c89

4 files changed

Lines changed: 27 additions & 10 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace System.Runtime.CompilerServices;
2+
3+
internal class IsExternalInit { }

src/StringLiteralGenerator/TypeInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace StringLiteralGenerator;
44

55
public partial class Utf8StringLiteralGenerator
66
{
7+
private record Utf8LiteralMethod(TypeInfo Type, MethodInfo Method)
8+
{
9+
public Utf8LiteralMethod(IMethodSymbol m, string text)
10+
: this(new TypeInfo(m.ContainingType), new MethodInfo(m, text))
11+
{ }
12+
}
13+
714
private record struct TypeInfo(string? Namespace, string Name)
815
{
916
public TypeInfo(INamedTypeSymbol t)
@@ -24,3 +31,4 @@ public MethodInfo(IMethodSymbol m, string text)
2431
{ }
2532
}
2633
}
34+

src/StringLiteralGenerator/Utf8StringLiteralGenerator.Parser.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
23
using Microsoft.CodeAnalysis.CSharp.Syntax;
34

45
namespace StringLiteralGenerator;
@@ -10,6 +11,17 @@ public partial class Utf8StringLiteralGenerator : ISourceGenerator
1011
private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
1112
node is MethodDeclarationSyntax { AttributeLists.Count: > 0 };
1213

14+
private static Utf8LiteralMethod? GetSemanticTargetForGeneration(SemanticModel semanticModel, MethodDeclarationSyntax m)
15+
{
16+
if (m.ParameterList.Parameters.Count != 0) return null;
17+
if (semanticModel.GetDeclaredSymbol(m) is not { } methodSymbol) return null;
18+
if (!methodSymbol.IsPartialDefinition || !methodSymbol.IsStatic) return null;
19+
if (!ReturnsString(methodSymbol)) return null;
20+
if (GetUtf8Attribute(methodSymbol) is not { } value) return null;
21+
22+
return new(methodSymbol, value);
23+
}
24+
1325
static bool ReturnsString(IMethodSymbol methodSymbol)
1426
{
1527
return methodSymbol.ReturnType is INamedTypeSymbol s

src/StringLiteralGenerator/Utf8StringLiteralGenerator.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.CodeAnalysis;
2-
using Microsoft.CodeAnalysis.CSharp;
32
using Microsoft.CodeAnalysis.CSharp.Syntax;
43
using Microsoft.CodeAnalysis.Text;
54
using System.Collections.Generic;
@@ -19,7 +18,7 @@ public void Execute(GeneratorExecutionContext context)
1918

2019
var buffer = new StringBuilder();
2120

22-
var group = enumerate().GroupBy(x => x.type, x => x.method);
21+
var group = enumerate().GroupBy(x => x.Type, x => x.Method);
2322

2423
foreach (var g in group)
2524
{
@@ -29,19 +28,14 @@ public void Execute(GeneratorExecutionContext context)
2928
context.AddSource(filename, SourceText.From(generatedSource, Encoding.UTF8));
3029
}
3130

32-
IEnumerable<(TypeInfo type, MethodInfo method)> enumerate()
31+
IEnumerable<Utf8LiteralMethod> enumerate()
3332
{
3433
foreach (var m in receiver.CandidateMethods)
3534
{
3635
var model = compilation.GetSemanticModel(m.SyntaxTree);
3736

38-
if (m.ParameterList.Parameters.Count != 0) continue;
39-
if (model.GetDeclaredSymbol(m) is not { } methodSymbol) continue;
40-
if (!methodSymbol.IsPartialDefinition || !methodSymbol.IsStatic) continue;
41-
if (!ReturnsString(methodSymbol)) continue;
42-
if (GetUtf8Attribute(methodSymbol) is not { } value) continue;
43-
44-
yield return (new(methodSymbol.ContainingType), new(methodSymbol, value));
37+
if (GetSemanticTargetForGeneration(model, m) is { } t)
38+
yield return t;
4539
}
4640
}
4741
}

0 commit comments

Comments
 (0)