Skip to content

Commit e7c6555

Browse files
committed
use constants in source gen projects
1 parent f499b3e commit e7c6555

12 files changed

Lines changed: 65 additions & 54 deletions

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
462462

463463
_sb.Append(@"
464464
[global::System.Diagnostics.DebuggerStepThroughAttribute]
465-
public void ").Append(isPartially ? "SwitchPartially" : "Switch");
465+
public void ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
466466

467467
if (withState)
468468
{
@@ -631,7 +631,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
631631

632632
_sb.Append(@"
633633
[global::System.Diagnostics.DebuggerStepThroughAttribute]
634-
public TResult ").Append(isPartially ? "SwitchPartially" : "Switch");
634+
public TResult ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
635635

636636
if (withState)
637637
{
@@ -795,7 +795,7 @@ private void GenerateMap(bool isPartially)
795795

796796
_sb.Append(@"
797797
[global::System.Diagnostics.DebuggerStepThroughAttribute]
798-
public TResult ").Append(isPartially ? "MapPartially" : "Map").Append("<TResult>(");
798+
public TResult ").Append(isPartially ? Constants.Methods.MAP_PARTIALLY : Constants.Methods.MAP).Append("<TResult>(");
799799

800800
if (isPartially)
801801
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/CodeFixes/ThinktectureRuntimeExtensionsCodeFixProvider.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ private static async Task<Document> GenerateValidateMethodAsync(
497497

498498
var methodBody = SyntaxFactory.Block(BuildThrowNotImplementedException(false));
499499

500-
var validateMethod = SyntaxFactory.MethodDeclaration(returnType, "Validate")
500+
var validateMethod = SyntaxFactory.MethodDeclaration(returnType, Constants.Methods.VALIDATE)
501501
.WithModifiers(SyntaxFactory.TokenList(
502502
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
503503
SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
@@ -553,7 +553,7 @@ private static async Task<Document> GenerateToValueMethodAsync(
553553

554554
var methodBody = SyntaxFactory.Block(BuildThrowNotImplementedException(false));
555555

556-
var toValueMethod = SyntaxFactory.MethodDeclaration(valueTypeName, "ToValue")
556+
var toValueMethod = SyntaxFactory.MethodDeclaration(valueTypeName, Constants.Methods.TO_VALUE)
557557
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)))
558558
.WithParameterList(SyntaxFactory.ParameterList())
559559
.WithBody(methodBody)
@@ -848,7 +848,7 @@ private static async Task<Document> UseStateOverloadAsync(
848848

849849
// State argument (first)
850850
var stateArg = SyntaxFactory.Argument(
851-
SyntaxFactory.NameColon("state"),
851+
SyntaxFactory.NameColon(Constants.Parameters.STATE),
852852
default,
853853
stateExpression);
854854
newArguments.Add(stateArg);
@@ -904,13 +904,13 @@ private static LambdaExpressionSyntax TransformLambdaForStateOverload(
904904

905905
if (captures.Count == 1)
906906
{
907-
replacement = SyntaxFactory.IdentifierName("state").WithTriviaFrom(identifier);
907+
replacement = SyntaxFactory.IdentifierName(Constants.Parameters.STATE).WithTriviaFrom(identifier);
908908
}
909909
else
910910
{
911911
replacement = SyntaxFactory.MemberAccessExpression(
912912
SyntaxKind.SimpleMemberAccessExpression,
913-
SyntaxFactory.IdentifierName("state"),
913+
SyntaxFactory.IdentifierName(Constants.Parameters.STATE),
914914
SyntaxFactory.IdentifierName(identifier.Identifier.Text)).WithTriviaFrom(identifier);
915915
}
916916

@@ -932,7 +932,7 @@ private static LambdaExpressionSyntax TransformLambdaForStateOverload(
932932
}
933933

934934
// Step 3: Add state parameter
935-
var stateParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier("state"));
935+
var stateParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier(Constants.Parameters.STATE));
936936

937937
if (transformedLambda is SimpleLambdaExpressionSyntax simple)
938938
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Constants.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,28 @@ public static class ComparerAccessor
3636

3737
public static class Methods
3838
{
39-
public const string VALIDATE_FACTORY_ARGUMENTS = "ValidateFactoryArguments";
39+
public const string CREATE = "Create";
40+
public const string FACTORY_POST_INIT = "FactoryPostInit";
4041
public const string GET = "Get";
42+
public const string MAP = "Map";
43+
public const string MAP_PARTIALLY = "MapPartially";
44+
public const string SWITCH = "Switch";
45+
public const string SWITCH_PARTIALLY = "SwitchPartially";
46+
public const string TO_STRING = "ToString";
47+
public const string TO_VALUE = "ToValue";
48+
public const string TRY_CREATE = "TryCreate";
49+
public const string VALIDATE = "Validate";
50+
public const string VALIDATE_FACTORY_ARGUMENTS = "ValidateFactoryArguments";
51+
}
52+
53+
public static class Parameters
54+
{
55+
public const string STATE = "state";
56+
}
57+
58+
public static class Variables
59+
{
60+
public const string FACTORY_ARGUMENTS_VALIDATION_ERROR = "factoryArgumentsValidationError";
4161
}
4262

4363
public static class Configuration

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Diagnostics/ThinktectureRuntimeExtensionsAnalyzer.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Thinktecture.CodeAnalysis.Diagnostics;
88
[DiagnosticAnalyzer(LanguageNames.CSharp)]
99
public sealed class ThinktectureRuntimeExtensionsAnalyzer : DiagnosticAnalyzer
1010
{
11-
private const string _SWITCH_PARTIALLY = "SwitchPartially";
12-
private const string _MAP_PARTIALLY = "MapPartially";
13-
1411
/// <inheritdoc />
1512
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
1613
[
@@ -414,10 +411,10 @@ private static void AnalyzeMethodCall(OperationAnalysisContext context)
414411
if (operation.Instance is null
415412
|| operation.Arguments.IsDefaultOrEmpty
416413
|| operation.TargetMethod.IsStatic
417-
|| (operation.TargetMethod.Name != "Switch"
418-
&& operation.TargetMethod.Name != _SWITCH_PARTIALLY
419-
&& operation.TargetMethod.Name != "Map"
420-
&& operation.TargetMethod.Name != _MAP_PARTIALLY))
414+
|| (operation.TargetMethod.Name != Constants.Methods.SWITCH
415+
&& operation.TargetMethod.Name != Constants.Methods.SWITCH_PARTIALLY
416+
&& operation.TargetMethod.Name != Constants.Methods.MAP
417+
&& operation.TargetMethod.Name != Constants.Methods.MAP_PARTIALLY))
421418
{
422419
return;
423420
}
@@ -445,7 +442,7 @@ private static void AnalyzeMethodCall(OperationAnalysisContext context)
445442
return;
446443
}
447444

448-
if (operation.TargetMethod.Name is "Switch" or _SWITCH_PARTIALLY)
445+
if (operation.TargetMethod.Name is Constants.Methods.SWITCH or Constants.Methods.SWITCH_PARTIALLY)
449446
{
450447
AnalyzeSwitchMapLambdas(context, operation);
451448
}
@@ -458,7 +455,7 @@ private static void AnalyzeEnumSwitchMap(
458455
IInvocationOperation operation)
459456
{
460457
var numberOfCallbacks = (items.IsDefaultOrEmpty ? 0 : items.Length)
461-
+ (operation.TargetMethod.Name is _SWITCH_PARTIALLY or _MAP_PARTIALLY ? 1 : 0);
458+
+ (operation.TargetMethod.Name is Constants.Methods.SWITCH_PARTIALLY or Constants.Methods.MAP_PARTIALLY ? 1 : 0);
462459

463460
AnalyzeSwitchMap(context, args, operation, numberOfCallbacks);
464461
}
@@ -471,7 +468,7 @@ private static void AnalyzeAnyUnionSwitchMap(
471468
var numberOfCallbacks = operation.TargetMethod.Parameters.IsDefaultOrEmpty ? 0 : operation.TargetMethod.Parameters.Length;
472469

473470
if (numberOfCallbacks > 0
474-
&& operation.TargetMethod.Parameters[0].Name == "state")
471+
&& operation.TargetMethod.Parameters[0].Name == Constants.Parameters.STATE)
475472
{
476473
numberOfCallbacks--;
477474
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionCodeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially, IReadOnly
256256

257257
_sb.Append(@"
258258
[global::System.Diagnostics.DebuggerStepThroughAttribute]
259-
public void ").Append(isPartially ? "SwitchPartially" : "Switch");
259+
public void ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
260260

261261
if (withState)
262262
{
@@ -417,7 +417,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially, IReadOnlyLi
417417

418418
_sb.Append(@"
419419
[global::System.Diagnostics.DebuggerStepThroughAttribute]
420-
public TResult ").Append(isPartially ? "SwitchPartially" : "Switch");
420+
public TResult ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
421421

422422
if (withState)
423423
{
@@ -572,7 +572,7 @@ private void GenerateMap(bool isPartially, IReadOnlyList<TypeMember> typeMembers
572572

573573
_sb.Append(@"
574574
[global::System.Diagnostics.DebuggerStepThroughAttribute]
575-
public TResult ").Append(isPartially ? "MapPartially" : "Map").Append("<TResult>(");
575+
public TResult ").Append(isPartially ? Constants.Methods.MAP_PARTIALLY : Constants.Methods.MAP).Append("<TResult>(");
576576

577577
if (isPartially)
578578
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
445445

446446
_sb.Append(@"
447447
[global::System.Diagnostics.DebuggerStepThroughAttribute]
448-
public void ").Append(isPartially ? "SwitchPartially" : "Switch");
448+
public void ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
449449

450450
if (withState)
451451
{
@@ -588,7 +588,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
588588

589589
_sb.Append(@"
590590
[global::System.Diagnostics.DebuggerStepThroughAttribute]
591-
public TResult ").Append(isPartially ? "SwitchPartially" : "Switch");
591+
public TResult ").Append(isPartially ? Constants.Methods.SWITCH_PARTIALLY : Constants.Methods.SWITCH);
592592

593593
if (withState)
594594
{
@@ -726,7 +726,7 @@ private void GenerateMap(bool isPartially)
726726

727727
_sb.Append(@"
728728
[global::System.Diagnostics.DebuggerStepThroughAttribute]
729-
public TResult ").Append(isPartially ? "MapPartially" : "Map").Append("<TResult>(");
729+
public TResult ").Append(isPartially ? Constants.Methods.MAP_PARTIALLY : Constants.Methods.MAP).Append("<TResult>(");
730730

731731
if (isPartially)
732732
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/TypedMemberState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public TypedMemberState(ITypeSymbol type)
152152

153153
private static bool IsToStringNullable(ITypeSymbol type)
154154
{
155-
var array = type.GetMembers("ToString");
155+
var array = type.GetMembers(Constants.Methods.TO_STRING);
156156

157157
for (var i = 0; i < array.Length; i++)
158158
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/AllValueObjectSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public AllValueObjectSettings(
4242
SkipKeyMember = valueObjectAttribute.FindSkipKeyMember() ?? false;
4343
SkipFactoryMethods = valueObjectAttribute.FindSkipFactoryMethods() ?? false;
4444
ConstructorAccessModifier = valueObjectAttribute.FindConstructorAccessModifier() ?? Constants.ValueObject.DEFAULT_CONSTRUCTOR_ACCESS_MODIFIER;
45-
CreateFactoryMethodName = valueObjectAttribute.FindCreateFactoryMethodName() ?? "Create";
46-
TryCreateFactoryMethodName = valueObjectAttribute.FindTryCreateFactoryMethodName() ?? "TryCreate";
45+
CreateFactoryMethodName = valueObjectAttribute.FindCreateFactoryMethodName() ?? Constants.Methods.CREATE;
46+
TryCreateFactoryMethodName = valueObjectAttribute.FindTryCreateFactoryMethodName() ?? Constants.Methods.TRY_CREATE;
4747
EmptyStringInFactoryMethodsYieldsNull = hasStringKey && (valueObjectAttribute.FindEmptyStringInFactoryMethodsYieldsNull() ?? false);
4848
NullInFactoryMethodsYieldsNull = EmptyStringInFactoryMethodsYieldsNull || (valueObjectAttribute.FindNullInFactoryMethodsYieldsNull() ?? false);
4949
SkipIComparable = valueObjectAttribute.FindSkipIComparable() ?? false;

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ComplexValueObjectCodeGenerator.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ namespace Thinktecture.CodeAnalysis.ValueObjects;
44

55
public sealed class ComplexValueObjectCodeGenerator : SmartEnumAndValueObjectCodeGeneratorBase
66
{
7-
private const string _FACTORY_ARGUMENTS_VALIDATION_ERROR = "factoryArgumentsValidationError";
8-
private const string _FACTORY_POST_INIT = "FactoryPostInit";
9-
107
private readonly ComplexValueObjectSourceGeneratorState _state;
118
private readonly StringBuilder _sb;
129

@@ -56,7 +53,7 @@ private void GenerateValueObject(CancellationToken cancellationToken)
5653

5754
if (!_state.Settings.SkipEqualityComparison)
5855
{
59-
_sb.Append(@"
56+
_sb.Append(@"
6057
global::System.IEquatable<").AppendTypeFullyQualifiedNullAnnotated(_state).Append(@">,
6158
global::System.Numerics.IEqualityOperators<").AppendTypeFullyQualified(_state).Append(", ").AppendTypeFullyQualified(_state).Append(", bool>,");
6259
}
@@ -114,9 +111,9 @@ private void GenerateValueObject(CancellationToken cancellationToken)
114111

115112
if (!_state.Settings.SkipEqualityComparison)
116113
{
117-
GenerateEqualityOperators();
118-
GenerateEquals();
119-
GenerateGetHashCode();
114+
GenerateEqualityOperators();
115+
GenerateEquals();
116+
GenerateGetHashCode();
120117
}
121118

122119
if (!_state.Settings.SkipToString)
@@ -319,7 +316,7 @@ private void GenerateValidateMethod()
319316
");
320317

321318
if (_state.FactoryValidationReturnType is not null)
322-
_sb.Append("var ").Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR).Append(" = ");
319+
_sb.Append("var ").Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR).Append(" = ");
323320

324321
_sb.Append(Constants.Methods.VALIDATE_FACTORY_ARGUMENTS).Append(@"(
325322
ref validationError").RenderArguments(fieldsAndProperties, @"
@@ -332,10 +329,10 @@ private void GenerateValidateMethod()
332329
GenerateConstructCall();
333330

334331
_sb.Append(@";
335-
obj.").Append(_FACTORY_POST_INIT).Append("(");
332+
obj.").Append(Constants.Methods.FACTORY_POST_INIT).Append("(");
336333

337334
if (_state.FactoryValidationReturnType is not null)
338-
_sb.Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR);
335+
_sb.Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR);
339336

340337
_sb.Append(@");
341338
}
@@ -372,10 +369,10 @@ private void GenerateFactoryPostInit()
372369
{
373370
_sb.Append(@"
374371
375-
partial void ").Append(_FACTORY_POST_INIT).Append("(");
372+
partial void ").Append(Constants.Methods.FACTORY_POST_INIT).Append("(");
376373

377374
if (_state.FactoryValidationReturnType is not null)
378-
_sb.Append(_state.FactoryValidationReturnType).Append(" ").Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR);
375+
_sb.Append(_state.FactoryValidationReturnType).Append(" ").Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR);
379376

380377
_sb.Append(");");
381378
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/KeyedValueObjectCodeGenerator.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ namespace Thinktecture.CodeAnalysis.ValueObjects;
44

55
public sealed class KeyedValueObjectCodeGenerator : SmartEnumAndValueObjectCodeGeneratorBase
66
{
7-
private const string _FACTORY_ARGUMENTS_VALIDATION_ERROR = "factoryArgumentsValidationError";
8-
private const string _FACTORY_POST_INIT = "FactoryPostInit";
9-
107
private readonly KeyedValueObjectSourceGeneratorState _state;
118
private readonly StringBuilder _sb;
129

@@ -414,7 +411,7 @@ private void GenerateValidateMethod(bool allowNullKeyMemberInput, bool allowNull
414411
");
415412

416413
if (_state.FactoryValidationReturnType is not null)
417-
_sb.Append("var ").Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR).Append(" = ");
414+
_sb.Append("var ").Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR).Append(" = ");
418415

419416
_sb.Append(Constants.Methods.VALIDATE_FACTORY_ARGUMENTS).Append("(ref validationError, ").RenderArgument(_state.KeyMember, "ref ").Append(@");
420417
@@ -425,10 +422,10 @@ private void GenerateValidateMethod(bool allowNullKeyMemberInput, bool allowNull
425422
GenerateConstructCall();
426423

427424
_sb.Append(@";
428-
obj.").Append(_FACTORY_POST_INIT).Append("(");
425+
obj.").Append(Constants.Methods.FACTORY_POST_INIT).Append("(");
429426

430427
if (_state.FactoryValidationReturnType is not null)
431-
_sb.Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR);
428+
_sb.Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR);
432429

433430
_sb.Append(@");
434431
}
@@ -457,10 +454,10 @@ private void GenerateFactoryPostInit()
457454
{
458455
_sb.Append(@"
459456
460-
partial void ").Append(_FACTORY_POST_INIT).Append("(");
457+
partial void ").Append(Constants.Methods.FACTORY_POST_INIT).Append("(");
461458

462459
if (_state.FactoryValidationReturnType is not null)
463-
_sb.Append(_state.FactoryValidationReturnType).Append(" ").Append(_FACTORY_ARGUMENTS_VALIDATION_ERROR);
460+
_sb.Append(_state.FactoryValidationReturnType).Append(" ").Append(Constants.Variables.FACTORY_ARGUMENTS_VALIDATION_ERROR);
464461

465462
_sb.Append(");");
466463
}

0 commit comments

Comments
 (0)