diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs new file mode 100644 index 000000000..3d7204a51 --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs @@ -0,0 +1,282 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +using NUnit.Framework; + +using UnityEngine; + +using io.github.hatayama.UnityCliLoop.FirstPartyTools; +using io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event; + +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload +{ + /// + /// End-to-end coverage for hot reload of types declared in a namespace with a keyword-named + /// segment (@event): a method body, a property getter, a call to an added method and a nested + /// type's method are all patched, and RevertAll restores the compiled values. + /// + public class HotReloadKeywordNamespaceE2ETests + { + private const string FixtureFileName = "HotReloadKeywordNamespaceFixtures.cs"; + + private const string AddedMethodAnchor = " public int CallsAddedMethod()"; + + private const string AddedMethodSource = + " private int AddedKeywordNamespaceValue()\n" + + " {\n" + + " return 35998;\n" + + " }\n\n"; + + private HotReloadDomainTestScope _scope; + + [SetUp] + public void SetUp() + { + _scope = new HotReloadDomainTestScope(); + HotReloadAutoRefreshHold.SyncToActiveChanges(); + } + + [TearDown] + public void TearDown() + { + _scope.Dispose(); + HotReloadAutoRefreshHold.SyncToActiveChanges(); + } + + /// + /// What: an edited method body of a type in a keyword-named namespace is Patched and returns + /// the edited value; RevertAll restores the compiled value. + /// + [Test] + public async Task Run_MethodOfTypeInKeywordNamedNamespace_IsPatchedAndReverts() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.MethodValue(), Is.EqualTo(35990)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35990;", "return 35991;")); + + AssertKind(result, "MethodValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.MethodValue(), Is.EqualTo(35991)); + HotReloadCompositionRoot.Services.Patcher.RevertAll(); + Assert.That(host.MethodValue(), Is.EqualTo(35990)); + } + + /// + /// What: an edited property getter of a type in a keyword-named namespace is Patched and + /// returns the edited value, so the getter shim declared in that namespace compiles. + /// + [Test] + public async Task Run_PropertyGetterOfTypeInKeywordNamedNamespace_IsPatched() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.GetterValue, Is.EqualTo(35993)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35993;", "return 35994;")); + + AssertKind(result, "get_GetterValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.GetterValue, Is.EqualTo(35994)); + } + + /// + /// What: a method of a type in a keyword-named namespace that now calls a method this reload + /// adds is Patched and returns what the added method computes, so the call the rewrite + /// qualifies with the shim's namespace compiles. + /// + [Test] + public async Task Run_CallToAddedMethodOfTypeInKeywordNamedNamespace_IsPatched() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.CallsAddedMethod(), Is.EqualTo(35996)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk + .Replace(AddedMethodAnchor, AddedMethodSource + AddedMethodAnchor) + .Replace("return 35996;", "return AddedKeywordNamespaceValue();")); + + AssertKind(result, "CallsAddedMethod", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.CallsAddedMethod(), Is.EqualTo(35998)); + } + + /// + /// What: an edited method body of a type nested in a type of a keyword-named namespace is + /// Patched and returns the edited value; RevertAll restores the compiled value. + /// + [Test] + public async Task Run_MethodOfNestedTypeInKeywordNamedNamespace_IsPatchedAndReverts() + { + HotReloadKeywordNamespaceHost.Nested nested = new HotReloadKeywordNamespaceHost.Nested(); + Assert.That(nested.NestedValue(), Is.EqualTo(35999)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35999;", "return 36000;")); + + AssertKind(result, "NestedValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(nested.NestedValue(), Is.EqualTo(36000)); + HotReloadCompositionRoot.Services.Patcher.RevertAll(); + Assert.That(nested.NestedValue(), Is.EqualTo(35999)); + } + + /// + /// What: a changed const of a type in a keyword-named namespace produces the const drift + /// warning naming that const, so the compiled type is found by its metadata name. + /// + [Test] + public async Task Run_ConstChangedInTypeOfKeywordNamedNamespace_WarnsAboutTheDrift() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("KeywordTuning = 4;", "KeywordTuning = 6;")); + + Assert.That( + FindWarningContaining(result, "KeywordTuning"), + Does.Contain("is 6 in the edited source but 4 in the compiled assembly"), + string.Join("\n", result.Warnings)); + } + + /// + /// What: removing a compiled property of a type in a keyword-named namespace produces the + /// kind-change warning naming that property, and no outside-method-body drift warning, + /// because the removed declaration is matched to the kind-change key and left out of the + /// drift comparison. + /// + [Test] + public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutTheKindChangeOnly() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int KindChangeValue => 36002;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, "KindChangeValue"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + /// + /// What: removing a compiled property of a keyword-named type (@class) in a keyword-named + /// namespace produces the kind-change warning naming that property and no + /// outside-method-body drift warning. + /// + [Test] + public async Task Run_PropertyRemovedFromKeywordNamedType_WarnsAboutTheKindChangeOnly() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int ClassKindChangeValue => 36005;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, "ClassKindChangeValue"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + /// + /// What: removing a compiled keyword-named property (@checked) produces the kind-change + /// warning naming it and no outside-method-body drift warning. + /// + [Test] + public async Task Run_KeywordNamedPropertyRemoved_WarnsAboutTheKindChangeOnly() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int @checked => 36007;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, ".checked'"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + /// + /// What: a keyword-named field (@event) that the edit adds and a method now reads is Patched + /// with the field's initial value, and raises no outside-method-body drift warning. + /// + [Test] + public async Task Run_KeywordNamedFieldAdded_IsPatchedWithoutDriftWarning() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.ReadsAddedField(), Is.EqualTo(36008)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk + .Replace( + " public int ReadsAddedField()", + " private int @event = 36009;\n\n public int ReadsAddedField()") + .Replace("return 36008;", "return @event;")); + + AssertKind(result, "ReadsAddedField", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.ReadsAddedField(), Is.EqualTo(36009)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + private static void AssertNoOutsideMethodBodyDriftWarning(HotReloadOrchestratorResult result) + { + foreach (string warning in result.Warnings) + { + Assert.That( + warning, + Does.Not.StartWith("Edits outside method bodies"), + string.Join("\n", result.Warnings)); + } + } + + private static string FindWarningContaining(HotReloadOrchestratorResult result, string fragment) + { + foreach (string warning in result.Warnings) + { + if (warning.Contains(fragment)) + { + return warning; + } + } + + Assert.Fail("No warning names " + fragment + ".\n" + string.Join("\n", result.Warnings)); + return null; + } + + private static async Task RunEditAsync(Func edit) + { + string fixturePath = Path.GetFullPath( + Path.Combine(Application.dataPath, "Tests", "Editor", "HotReload", FixtureFileName)); + string onDisk = File.ReadAllText(fixturePath); + string edited = edit(onDisk); + Assert.That(edited, Is.Not.EqualTo(onDisk), "Precondition: the edit must change the fixture."); + string editedPath = HotReloadTestSourceWriter.WriteEditedSource("KeywordNamespaceE2E.cs", edited); + return await HotReloadCompositionRoot.Services.Orchestrator.RunAsync( + new[] { fixturePath }, + editedPath, + CancellationToken.None); + } + + private static void AssertKind( + HotReloadOrchestratorResult result, + string methodName, + HotReloadMethodOutcomeKind expectedKind) + { + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + if (outcome.Method.Contains("." + methodName + "(")) + { + Assert.That(outcome.Kind, Is.EqualTo(expectedKind), outcome.Method + ": " + outcome.Reason); + return; + } + } + + Assert.Fail("No row for " + methodName + ". Rows: " + DescribeRows(result)); + } + + private static string DescribeRows(HotReloadOrchestratorResult result) + { + string rows = string.Empty; + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + rows += outcome.Method + " = " + outcome.Kind + " (" + outcome.Reason + "); "; + } + + return rows; + } + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta new file mode 100644 index 000000000..2b2c7f50a --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 29e1f6c248584468c9b31e8534b8222c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs new file mode 100644 index 000000000..49e5ea7fd --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs @@ -0,0 +1,64 @@ +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event +{ + /// + /// Host types declared in a namespace whose last segment is a C# keyword. Tests edit their + /// bodies to check that hot reload patches them: the escaped spelling (@event) appears only in + /// source, while the compiled metadata names the namespace "event". + /// + public sealed class HotReloadKeywordNamespaceHost + { + public const int KeywordTuning = 4; + + public int MethodValue() + { + return 35990; + } + + public int GetterValue + { + get + { + return 35993; + } + } + + public int CallsAddedMethod() + { + return 35996; + } + + public int CallsIntroducedType() + { + return 36001; + } + + public int KindChangeValue => 36002; + + public int @checked => 36007; + + public int ReadsAddedField() + { + return 36008; + } + + /// + /// Nested host, so the conversion of a nested type name in that namespace is covered too. + /// + public sealed class Nested + { + public int NestedValue() + { + return 35999; + } + } + } + + /// + /// Host whose own name is a C# keyword, so a key built from its identifier must drop the @ + /// as well as the namespace's. + /// + public sealed class @class + { + public int ClassKindChangeValue => 36005; + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta new file mode 100644 index 000000000..04e6a79d2 --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6700df0513cd94d51848148484f7d12d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs new file mode 100644 index 000000000..568661e3a --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +using NUnit.Framework; + +using io.github.hatayama.UnityCliLoop.FirstPartyTools; + +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload +{ + /// + /// End-to-end coverage of a type an earlier reload introduced into a namespace with a + /// keyword-named segment (@event): a later body-only edit is patched on the retained artifact, + /// which is found by the type's metadata name. + /// + public class HotReloadKeywordNamespaceIntroducedTypeE2ETests : HotReloadIntroducedTypeE2ETestBase + { + private const string FixtureFileName = "HotReloadKeywordNamespaceFixtures.cs"; + private const string HostTypeAnchor = " public sealed class HotReloadKeywordNamespaceHost"; + private const string CallerBody = "return 36001;"; + private const string IntroducedTypeSimpleName = "HotReloadKeywordNamespaceIntroduced"; + + // Why "event" without the escape: this is the name the compiled metadata carries. + private const string IntroducedTypeMetadataName = + "io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.event." + IntroducedTypeSimpleName; + + /// + /// What: editing only the body of a type that an earlier reload introduced into a + /// keyword-named namespace patches it on the retained artifact without failures, and a call + /// into the artifact runs the edited body. + /// + [Test] + public async Task Run_IntroducedTypeInKeywordNamedNamespaceBodyEdited_PatchesTheRetainedArtifact() + { + string fixturePath = FixturePath(FixtureFileName); + + await RunInIntroducedTypeDomainAsync(async readArtifact => + { + HotReloadOrchestratorResult first = await RunAsync( + fixturePath, + "KeywordNamespaceIntroducing.cs", + "36003"); + Assert.That(CountFailures(first), Is.EqualTo(0), DescribeOutcomes(first)); + Assert.That(ReadComputedValue(readArtifact()), Is.EqualTo(36003)); + + HotReloadOrchestratorResult second = await RunAsync( + fixturePath, + "KeywordNamespaceIntroducedBodyEdited.cs", + "36004"); + + Assert.That(CountFailures(second), Is.EqualTo(0), DescribeOutcomes(second)); + Assert.That( + HasPatchedCompute(second), + Is.True, + "The edited body of the introduced type must be reported as patched.\n" + DescribeOutcomes(second)); + Assert.That(ReadComputedValue(readArtifact()), Is.EqualTo(36004)); + }); + } + + private static Task RunAsync( + string fixturePath, + string editedFileName, + string computedValue) + { + string onDisk = File.ReadAllText(fixturePath); + Assert.That(onDisk, Does.Contain(HostTypeAnchor), "Precondition: host type anchor must exist."); + Assert.That(onDisk, Does.Contain(CallerBody), "Precondition: caller body must exist."); + string introduced = + " public sealed class " + IntroducedTypeSimpleName + "\n" + + " {\n" + + " [System.Runtime.CompilerServices.MethodImpl(\n" + + " System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]\n" + + " public int Compute()\n" + + " {\n" + + " return " + computedValue + ";\n" + + " }\n" + + " }\n" + + "\n"; + string edited = onDisk + .Replace(HostTypeAnchor, introduced + HostTypeAnchor, StringComparison.Ordinal) + .Replace( + CallerBody, + "return new " + IntroducedTypeSimpleName + "().Compute();", + StringComparison.Ordinal); + return HotReloadCompositionRoot.Services.Orchestrator.RunAsync( + new[] { fixturePath }, + contentPathOverride: null, + CancellationToken.None, + new Dictionary + { + [fixturePath] = HotReloadTestSourceWriter.WriteEditedSource(editedFileName, edited) + }); + } + + private static bool HasPatchedCompute(HotReloadOrchestratorResult result) + { + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + if (outcome.Kind == HotReloadMethodOutcomeKind.Patched + && outcome.Method != null + && outcome.Method.Contains("." + IntroducedTypeSimpleName + ".Compute()", StringComparison.Ordinal)) + { + return true; + } + } + + return false; + } + + private static int ReadComputedValue(HotReloadIntroducedTypeArtifact artifact) + { + Assert.That(artifact, Is.Not.Null, "A reload had to introduce the type before this check."); + Type introducedType = artifact.Assembly.GetType(IntroducedTypeMetadataName, throwOnError: false); + Assert.That(introducedType, Is.Not.Null, "The artifact must hold " + IntroducedTypeMetadataName + "."); + MethodInfo compute = introducedType.GetMethod("Compute", BindingFlags.Instance | BindingFlags.Public); + return (int)compute.Invoke(Activator.CreateInstance(introducedType), Array.Empty()); + } + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta new file mode 100644 index 000000000..85ceda89b --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 48b39b36c15f141b9ad9b349cbb5477b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs index d508f66f5..6335e5fec 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs @@ -87,9 +87,9 @@ private static bool SnapshotDeclaresSameFieldType( Dictionary snapshotFieldMap = WorkerSyntaxMemberMaps.BuildSyntaxFieldMapOrNull(baseline.SnapshotRoot); - // Why the identifier text, not the symbol name: the snapshot map is keyed by the text as - // written, which keeps the @ of an escaped identifier such as @event. - string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeState.TypeMetadataNameFromSyntax, variable.Identifier.Text); + // Why ValueText: field keys drop the @ of a keyword-named field such as @event, so the + // added-field keys built from symbol names meet the snapshot map built from syntax. + string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeState.TypeMetadataNameFromSyntax, variable.Identifier.ValueText); if (snapshotFieldMap == null || !snapshotFieldMap.TryGetValue(syntaxKey, out VariableDeclaratorSyntax snapshotVariable) || !(snapshotVariable.Parent is VariableDeclarationSyntax snapshotDeclaration)) diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs index 586eebc71..9c6474bbf 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs @@ -29,7 +29,27 @@ public static string ToMetadataName(INamedTypeSymbol typeSymbol) return typeSymbol.MetadataName; } - return typeSymbol.ContainingNamespace.ToDisplayString() + "." + typeSymbol.MetadataName; + return ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; + } + + /// + /// Builds the dotted namespace name as metadata records it, for both metadata and reflection + /// type names. Returns an empty string for the global namespace. + /// + // Why the names and not ToDisplayString: the display escapes a keyword identifier (@event), + // which no metadata name carries, so a lookup by that key would miss the type. + internal static string ToNamespaceMetadataName(INamespaceSymbol namespaceSymbol) + { + List names = new List(); + for (INamespaceSymbol current = namespaceSymbol; + current != null && !current.IsGlobalNamespace; + current = current.ContainingNamespace) + { + names.Add(current.Name); + } + + names.Reverse(); + return string.Join(".", names); } public static string ToParameterTypeFullName(IParameterSymbol parameterSymbol) diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs index 94c99b644..0d264a818 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs @@ -174,23 +174,7 @@ internal static string ToReflectionMetadataName(INamedTypeSymbol typeSymbol) return typeSymbol.MetadataName; } - return ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; - } - - // Why the names and not ToDisplayString: the display escapes a keyword identifier (@event), - // which no metadata name carries, so a lookup by that key would miss the type. - private static string ToNamespaceMetadataName(INamespaceSymbol namespaceSymbol) - { - List names = new List(); - for (INamespaceSymbol current = namespaceSymbol; - current != null && !current.IsGlobalNamespace; - current = current.ContainingNamespace) - { - names.Add(current.Name); - } - - names.Reverse(); - return string.Join(".", names); + return CecilTypeNames.ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; } /// diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs index 55e1d4f38..1bd9c738f 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs @@ -240,7 +240,7 @@ public override SyntaxNode VisitEventFieldDeclaration(EventFieldDeclarationSynta { string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey( typeMetadataName, - variable.Identifier.Text); + variable.Identifier.ValueText); if (!_syntaxKeysToStrip.Contains(syntaxKey)) { remaining.Add(variable); @@ -273,7 +273,7 @@ public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node) List remaining = new List(); foreach (VariableDeclaratorSyntax variable in node.Declaration.Variables) { - string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (!_syntaxKeysToStrip.Contains(syntaxKey)) { remaining.Add(variable); diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs index 1126e9a5b..c051e023b 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs @@ -74,7 +74,9 @@ internal static string BuildTypeMetadataNameFromSyntax(TypeDeclarationSyntax typ TypeDeclarationSyntax current = typeDeclaration; while (current != null) { - string simpleName = current.Identifier.Text; + // Why ValueText: the key stands for a metadata name, and kind-change keys built from + // symbols meet these keys; Text keeps the @ of a keyword-named type such as @class. + string simpleName = current.Identifier.ValueText; if (current.TypeParameterList != null && current.TypeParameterList.Parameters.Count > 0) { simpleName += "`" + current.TypeParameterList.Parameters.Count.ToString(CultureInfo.InvariantCulture); @@ -102,7 +104,7 @@ internal static string BuildTypeMetadataNameFromSyntax(TypeDeclarationSyntax typ internal static string BuildEnumMetadataNameFromSyntax(EnumDeclarationSyntax enumDeclaration) { - string simpleName = enumDeclaration.Identifier.Text; + string simpleName = enumDeclaration.Identifier.ValueText; if (enumDeclaration.Parent is TypeDeclarationSyntax containingType) { return BuildTypeMetadataNameFromSyntax(containingType) + "+" + simpleName; @@ -125,14 +127,9 @@ internal static string GetContainingNamespaceName(SyntaxNode node) SyntaxNode current = node.Parent; while (current != null) { - // Why NormalizeWhitespace: trivia in nested namespace names must not invent distinct keys. - if (current is NamespaceDeclarationSyntax namespaceDeclaration) + if (current is BaseNamespaceDeclarationSyntax namespaceDeclaration) { - parts.Add(namespaceDeclaration.Name.NormalizeWhitespace().ToString()); - } - else if (current is FileScopedNamespaceDeclarationSyntax fileScopedNamespace) - { - parts.Add(fileScopedNamespace.Name.NormalizeWhitespace().ToString()); + parts.Add(ToUnescapedName(namespaceDeclaration.Name)); } current = current.Parent; @@ -147,11 +144,25 @@ internal static string GetContainingNamespaceName(SyntaxNode node) return string.Join(".", parts); } + // Why segment ValueTexts rather than the name text: the text keeps trivia and the @ of a + // keyword-named segment such as @event, which no metadata name carries, so a key built from a + // symbol would never meet it. + private static string ToUnescapedName(NameSyntax name) + { + return string.Join( + ".", + name.DescendantNodesAndSelf() + .OfType() + .Select(simpleName => simpleName.Identifier.ValueText)); + } + internal static string BuildSyntaxPropertyKey( string typeMetadataName, PropertyDeclarationSyntax propertyDeclaration) { - string name = propertyDeclaration.Identifier.Text; + // Why ValueText: kind-change keys name the compiled property by its symbol name, which + // drops the @ of a keyword-named property such as @checked. + string name = propertyDeclaration.Identifier.ValueText; if (propertyDeclaration.ExplicitInterfaceSpecifier != null) { // Why NormalizeWhitespace: keep property keys symmetric with BuildSyntaxMethodKey so diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs index 570aefe02..4f07ca825 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs @@ -43,7 +43,7 @@ internal static Dictionary BuildSyntaxFieldMap { foreach (VariableDeclaratorSyntax variable in fieldDeclaration.Declaration.Variables) { - string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (map.ContainsKey(key)) { return null; @@ -215,7 +215,7 @@ internal static Dictionary BuildSyntaxEventFie { // Why field key format: kind-change identity is type metadata + "::" + name, // the same shape BuildSyntaxFieldKey already uses. - string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (map.ContainsKey(key)) { return null;