From 32c3cd3e8addb924f904de19c6e311a0ddb4e963 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 11 Jun 2024 15:16:13 -0700 Subject: [PATCH] Fix junk in React warnings in Logbox (#44812) Summary: Before all React errors showed junk like this: ![Screenshot 2024-06-06 at 06 24 38](https://github.com/facebook/react-native/assets/810438/40be3133-e31d-43e8-b04d-ffbc5b462027) This is because `isComponentStack` detected a component stack but `parseComponentStack` couldn't actually parse it (it doesn't deal with React's current format like `in Foo (created by FeedItemInner)`) so `componentStack` was an empty array, resulting in the next block of code pushing stuff into `argsWithoutComponentStack` _again_, thus repeating its args. The fix is not to do that. Result on my local copy: ![Screenshot 2024-06-06 at 06 24 24](https://github.com/facebook/react-native/assets/810438/8f3d32d9-6f28-472c-be34-c802a0e2f161) Ofc this doesn't actually show the component stack but that was broken before too. I edited in-place in my `node_modules` so I haven't verified this 100% works on main. Hope this is useful! ## Changelog: [General] [Fixed] - Remove accidental duplication in React warnings in Logbox Pull Request resolved: https://github.com/facebook/react-native/pull/44812 Reviewed By: cortinico Differential Revision: D58240357 Pulled By: rickhanlonii fbshipit-source-id: b6ecb659d3b393e497caf5e7b2087a8e529f1b28 --- .../Data/__tests__/parseLogBoxLog-test.js | 30 +++++++++++++++++++ .../Libraries/LogBox/Data/parseLogBoxLog.js | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/LogBox/Data/__tests__/parseLogBoxLog-test.js b/packages/react-native/Libraries/LogBox/Data/__tests__/parseLogBoxLog-test.js index 895ccf57ec5..8de9a84f326 100644 --- a/packages/react-native/Libraries/LogBox/Data/__tests__/parseLogBoxLog-test.js +++ b/packages/react-native/Libraries/LogBox/Data/__tests__/parseLogBoxLog-test.js @@ -112,6 +112,36 @@ describe('parseLogBoxLog', () => { }); }); + it('does not duplicate message if component stack found but not parsed', () => { + expect( + parseLogBoxLog([ + 'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s', + '\n\nCheck the render method of `MyOtherComponent`.', + '', + '\n in\n in\n in', + ]), + ).toEqual({ + componentStackType: 'legacy', + componentStack: [], + category: + 'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.', + message: { + content: + 'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.', + substitutions: [ + { + length: 48, + offset: 62, + }, + { + length: 0, + offset: 110, + }, + ], + }, + }); + }); + it('detects a component stack in an interpolated warning', () => { expect( parseLogBoxLog([ diff --git a/packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js b/packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js index f4519b3eb57..af0bb5c2010 100644 --- a/packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js +++ b/packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js @@ -462,7 +462,7 @@ export function parseLogBoxLog(args: $ReadOnlyArray): {| } } - if (componentStack.length === 0) { + if (componentStack.length === 0 && argsWithoutComponentStack.length === 0) { // Try finding the component stack elsewhere. for (const arg of args) { if (typeof arg === 'string' && isComponentStack(arg)) {