From 2093178ae6a94ff199825ecf2056cc39a1e3b7cc Mon Sep 17 00:00:00 2001 From: Andrew <30809111+acoates-ms@users.noreply.github.com> Date: Tue, 14 May 2024 11:03:11 -0700 Subject: [PATCH] Revert "C++20: use view() instead of str() to avoid a copy, use starts_with (#13218)" This reverts commit 71335b5af1f8ea0d2414185fa6082a7ea7730af4. --- ...-f53a2b65-d364-49a5-978c-5d64e08c22ea.json | 7 -- .../automation-commands/src/dumpVisualTree.ts | 96 +++++++++++-------- .../Utils/LocalBundleReader.cpp | 5 +- vnext/Shared/HermesSamplingProfiler.cpp | 3 +- vnext/Shared/OInstance.cpp | 3 +- 5 files changed, 65 insertions(+), 49 deletions(-) delete mode 100644 change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json diff --git a/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json b/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json deleted file mode 100644 index 845cc269c77..00000000000 --- a/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "patch", - "comment": "Use view() instead of str(), starts_with() instead of _Starts_with()", - "packageName": "react-native-windows", - "email": "nate@bracy.dev", - "dependentChangeType": "patch" -} diff --git a/packages/@react-native-windows/automation-commands/src/dumpVisualTree.ts b/packages/@react-native-windows/automation-commands/src/dumpVisualTree.ts index f57f8b51519..08a3067aaca 100644 --- a/packages/@react-native-windows/automation-commands/src/dumpVisualTree.ts +++ b/packages/@react-native-windows/automation-commands/src/dumpVisualTree.ts @@ -34,34 +34,34 @@ export type UIElement = { }; export type AutomationNode = { - AutomationId?: string, - ControlType?: number, - LocalizedControlType?: string, - __Children?: [AutomationNode], -} + AutomationId?: string; + ControlType?: number; + LocalizedControlType?: string; + __Children?: [AutomationNode]; +}; export type ComponentNode = { - Type: string, + Type: string; _Props?: { - TestId?: string, - Sources?: [{Uri?: string}], - }, - __Children?: [ComponentNode], -} + TestId?: string; + Sources?: [{Uri?: string}]; + }; + __Children?: [ComponentNode]; +}; export type VisualNode = { - Comment?: string, - Offset?: `${number} ${number} ${number}`, - Size?: `${number} ${number}`, - "Visual Type"?: string, - __Children?: [VisualNode] -} + Comment?: string; + Offset?: `${number} ${number} ${number}`; + Size?: `${number} ${number}`; + 'Visual Type'?: string; + __Children?: [VisualNode]; +}; export type VisualTree = { - "Automation Tree": AutomationNode, - "Component Tree": ComponentNode, - "Visual Tree": VisualNode, -} + 'Automation Tree': AutomationNode; + 'Component Tree': ComponentNode; + 'Visual Tree': VisualNode; +}; declare global { const automationClient: AutomationClient | undefined; @@ -95,63 +95,83 @@ export default async function dumpVisualTree( const element: UIElement | VisualTree = dumpResponse.result; - if ("XamlType" in element && opts?.pruneCollapsed !== false) { + if ('XamlType' in element && opts?.pruneCollapsed !== false) { pruneCollapsedElements(element); } - if ("XamlType" in element && opts?.deterministicOnly !== false) { + if ('XamlType' in element && opts?.deterministicOnly !== false) { removeNonDeterministicProps(element); } - if ("XamlType" in element && opts?.removeDefaultProps !== false) { + if ('XamlType' in element && opts?.removeDefaultProps !== false) { removeDefaultProps(element); } - if (!("XamlType" in element) && opts?.removeGuidsFromImageSources !== false) { + if (!('XamlType' in element) && opts?.removeGuidsFromImageSources !== false) { removeGuidsFromImageSources(element); } - + return element; } function removeGuidsFromImageSourcesHelper(node: ComponentNode) { if (node._Props && node._Props.Sources) { - node._Props.Sources.forEach((source : any) => { + node._Props.Sources.forEach((source: any) => { if (source.Uri) { if (source.Uri.startsWith('blob:')) { - source.Uri = source.Uri.replace(/blob:[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+/, 'blob:'); - } else if (source.Uri.startsWith('https://www.facebook.com/favicon.ico?r=1&t=')) { - source.Uri = 'https://www.facebook.com/favicon.ico?r=1&t='; - } else if (source.Uri.startsWith('https://www.facebook.com/ads/pics/successstories.png?hash=')) { - source.Uri = 'https://www.facebook.com/ads/pics/successstories.png?hash='; + source.Uri = source.Uri.replace( + /blob:[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+/, + 'blob:', + ); + } else if ( + source.Uri.startsWith('https://www.facebook.com/favicon.ico?r=1&t=') + ) { + source.Uri = + 'https://www.facebook.com/favicon.ico?r=1&t='; + } else if ( + source.Uri.startsWith( + 'https://www.facebook.com/ads/pics/successstories.png?hash=', + ) + ) { + source.Uri = + 'https://www.facebook.com/ads/pics/successstories.png?hash='; } else { - // When getting files from a prebuilt bundle the uri is going to include a local path, which would make snapshots inconsistent, // This logic replaces the local path so that we get consistent results. // file://E:\\repos\\react-native-windows\\packages\\e2e-test-app-fabric\\windows\\RNTesterApp-Fabric.Package\\bin\\x64\\Release\\AppX\\RNTesterApp-Fabric\\Bundle\\@react-native-windows/tester/js/assets/uie_thumb_normal@2x.png // becomes // @react-native-windows/tester/js/assets/uie_thumb_normal@2x.png - const packagesPath = require('path').resolve(__dirname, '../../..').replace(/\\/g, '\\\\'); - source.Uri = source.Uri.replace(new RegExp(`file://${packagesPath}.*\\\\Bundle\\\\assets/_+`), ''); + const packagesPath = require('path') + .resolve(__dirname, '../../..') + .replace(/\\/g, '\\\\'); + source.Uri = source.Uri.replace( + new RegExp(`file://${packagesPath}.*\\\\Bundle\\\\assets/_+`), + '', + ); // When loading the bundle from metro local paths will be replaced with paths to localhost, which will not align with snapshots made with prebuilt bundles. // This logic replaces the localhost uri, with the same uri that we would have gotten from a prebuild bundle. This makes it easier to debug without breaking snapshots // http://localhost:8081/assets/@@/@react-native-windows/tester/js/assets/uie_thumb_normal@2x.png?platform=windows&hash=c6f5aec4d9e0aa47c0887e4266796224 // becomes // "@react-native-windows/tester/js/assets/uie_thumb_normal@2x.png" - source.Uri = source.Uri.replace(/http:\/\/localhost:8081\/assets\/(@@\/)+/, ''); + source.Uri = source.Uri.replace( + /http:\/\/localhost:8081\/assets\/(@@\/)+/, + '', + ); source.Uri = source.Uri.replace(/\?platform=windows&hash=[^=]$/, ''); } } }); } if (node.__Children) { - node.__Children.forEach((child : any) => removeGuidsFromImageSourcesHelper(child)); + node.__Children.forEach((child: any) => + removeGuidsFromImageSourcesHelper(child), + ); } } function removeGuidsFromImageSources(visualTree: VisualTree) { - removeGuidsFromImageSourcesHelper(visualTree["Component Tree"]); + removeGuidsFromImageSourcesHelper(visualTree['Component Tree']); } /** diff --git a/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp b/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp index b45e48f87ea..08e5fe4d20b 100644 --- a/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp +++ b/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp @@ -59,10 +59,11 @@ std::future LocalBundleReader::LoadBundleAsync(const std::wstring b winrt::Windows::Storage::StorageFile file{nullptr}; // Supports "ms-appx://" or "ms-appdata://" - if (bundleUri.starts_with(L"ms-app")) { + // TODO: Replace call to private string function with C++ 20 `starts_with` + if (bundleUri._Starts_with(L"ms-app")) { winrt::Windows::Foundation::Uri uri(bundleUri); file = co_await winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri); - } else if (bundleUri.starts_with(L"resource://")) { + } else if (bundleUri._Starts_with(L"resource://")) { winrt::Windows::Foundation::Uri uri(bundleUri); co_return GetBundleFromEmbeddedResource(uri); } else { diff --git a/vnext/Shared/HermesSamplingProfiler.cpp b/vnext/Shared/HermesSamplingProfiler.cpp index d2fddb83334..442dba36887 100644 --- a/vnext/Shared/HermesSamplingProfiler.cpp +++ b/vnext/Shared/HermesSamplingProfiler.cpp @@ -54,7 +54,8 @@ IAsyncOperation getTraceFilePath() noexcept { .count(); os << hermesDataPath.c_str() << L"\\cpu_" << now << L".cpuprofile"; - co_return winrt::hstring(os.view()); + // TODO: Use C++ 20 `os.view()` to avoid a copy + co_return winrt::hstring(os.str()); } } // namespace diff --git a/vnext/Shared/OInstance.cpp b/vnext/Shared/OInstance.cpp index b72c6714570..ece2c869a9c 100644 --- a/vnext/Shared/OInstance.cpp +++ b/vnext/Shared/OInstance.cpp @@ -196,7 +196,8 @@ std::unique_ptr JsBigStringFromPath( return facebook::react::FileMappingBigString::fromPath(bundlePath); #else std::wstring bundlePath; - if (devSettings->bundleRootPath.starts_with("resource://")) { + // TODO: Replace call to private string function with C++ 20 `starts_with` + if (devSettings->bundleRootPath._Starts_with("resource://")) { auto uri = winrt::Windows::Foundation::Uri( winrt::to_hstring(devSettings->bundleRootPath), winrt::to_hstring(jsBundleRelativePath)); bundlePath = uri.ToString();