GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [react-devtools-shared] Added string type check for object name prop … · react/react@d1c2555

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/react-devtools-shared/src
    • __tests__
      • utils-test.js
    • utils.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-devtools-shared/src/__tests__/utils-test.js‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {getDisplayName} from 'react-devtools-shared/src/utils';
11+
12+
describe('utils', () => {
13+
describe('getDisplayName', () => {
14+
it('should return a function name', () => {
15+
function FauxComponent() {}
16+
expect(getDisplayName(FauxComponent)).toEqual('FauxComponent');
17+
});
18+
19+
it('should return a displayName name if specified', () => {
20+
function FauxComponent() {}
21+
FauxComponent.displayName = 'OverrideDisplayName';
22+
expect(getDisplayName(FauxComponent)).toEqual('OverrideDisplayName');
23+
});
24+
25+
it('should return the fallback for anonymous functions', () => {
26+
expect(getDisplayName(() => {}, 'Fallback')).toEqual('Fallback');
27+
});
28+
29+
it('should return Anonymous for anonymous functions without a fallback', () => {
30+
expect(getDisplayName(() => {})).toEqual('Anonymous');
31+
});
32+
33+
// Simulate a reported bug:
34+
// https://github.com/facebook/react/issues/16685
35+
it('should return a fallback when the name prop is not a string', () => {
36+
const FauxComponent = {name: {}};
37+
expect(getDisplayName(FauxComponent, 'Fallback')).toEqual('Fallback');
38+
});
39+
});
40+
});
Collapse file

‎packages/react-devtools-shared/src/utils.js‎

Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/utils.js
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ export function getDisplayName(
4646
return nameFromCache;
4747
}
4848

49-
let displayName;
49+
let displayName = fallbackName;
5050

5151
// The displayName property is not guaranteed to be a string.
5252
// It's only safe to use for our purposes if it's a string.
5353
// github.com/facebook/react-devtools/issues/803
5454
if (typeof type.displayName === 'string') {
5555
displayName = type.displayName;
56-
}
57-
58-
if (!displayName) {
59-
displayName = type.name || fallbackName;
56+
} else if (typeof type.name === 'string' && type.name !== '') {
57+
displayName = type.name;
6058
}
6159

6260
cachedDisplayNames.set(type, displayName);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.