Skip to content

Commit

Permalink
Revert "C++20: use view() instead of str() to avoid a copy, use start…
Browse files Browse the repository at this point in the history
…s_with (microsoft#13218)"

This reverts commit 71335b5.
  • Loading branch information
acoates-ms committed May 14, 2024
1 parent b187f60 commit 2093178
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:<some_guid_here>');
} 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=<some_hash_here>';
} 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=<some_hash_here>';
source.Uri = source.Uri.replace(
/blob:[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+-[a-f0-9]+/,
'blob:<some_guid_here>',
);
} 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=<some_hash_here>';
} 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=<some_hash_here>';
} 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
// <localOrBundlerUri>@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/_+`), '<localOrBundlerUri>');
const packagesPath = require('path')
.resolve(__dirname, '../../..')
.replace(/\\/g, '\\\\');
source.Uri = source.Uri.replace(
new RegExp(`file://${packagesPath}.*\\\\Bundle\\\\assets/_+`),
'<localOrBundlerUri>',
);

// 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
// "<localOrBundlerUri>@react-native-windows/tester/js/assets/uie_thumb_normal@2x.png"
source.Uri = source.Uri.replace(/http:\/\/localhost:8081\/assets\/(@@\/)+/, '<localOrBundlerUri>');
source.Uri = source.Uri.replace(
/http:\/\/localhost:8081\/assets\/(@@\/)+/,
'<localOrBundlerUri>',
);
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']);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ std::future<std::string> 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 {
Expand Down
3 changes: 2 additions & 1 deletion vnext/Shared/HermesSamplingProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ IAsyncOperation<winrt::hstring> 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
Expand Down
3 changes: 2 additions & 1 deletion vnext/Shared/OInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ std::unique_ptr<const facebook::react::JSBigString> 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();
Expand Down

0 comments on commit 2093178

Please sign in to comment.