Skip to content

Commit

Permalink
[DevTools] Clean Up DevTools Code (#24782)
Browse files Browse the repository at this point in the history
This PR cleans up the DevTools codebase by:
* Consolidating `normalizeCodeLocInfo` into one place
* Remove unused source argument in the DevTools component stacks code
  • Loading branch information
lunaruan authored Jun 23, 2022
1 parent 9abe745 commit 955cad9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@

'use strict';

function normalizeCodeLocInfo(str) {
return (
typeof str === 'string' &&
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
})
);
}
import {normalizeCodeLocInfo} from './utils';

describe('Timeline profiler', () => {
let React;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@
* @flow
*/

function normalizeCodeLocInfo(str) {
if (typeof str !== 'string') {
return str;
}
// This special case exists only for the special source location in
// ReactElementValidator. That will go away if we remove source locations.
str = str.replace(/Check your code at .+?:\d+/g, 'Check your code at **');
// V8 format:
// at Component (/path/filename.js:123:45)
// React format:
// in Component (at filename.js:123)
return str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
});
}
import {normalizeCodeLocInfo} from './utils';

describe('component stack', () => {
let React;
Expand Down
12 changes: 3 additions & 9 deletions packages/react-devtools-shared/src/__tests__/console-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*
* @flow
*/

import {normalizeCodeLocInfo} from './utils';

let React;
let ReactDOMClient;
let act;
Expand Down Expand Up @@ -60,15 +63,6 @@ describe('console', () => {
legacyRender = utils.legacyRender;
});

function normalizeCodeLocInfo(str) {
return (
str &&
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
})
);
}

// @reactVersion >=18.0
it('should not patch console methods that are not explicitly overridden', () => {
expect(fakeConsole.error).not.toBe(mockError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@

'use strict';

function normalizeCodeLocInfo(str) {
return (
typeof str === 'string' &&
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
})
);
}
import {normalizeCodeLocInfo} from './utils';

describe('Timeline profiler', () => {
let React;
Expand Down
16 changes: 16 additions & 0 deletions packages/react-devtools-shared/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,19 @@ export function overrideFeatureFlags(overrideFlags) {
};
});
}

export function normalizeCodeLocInfo(str) {
if (typeof str !== 'string') {
return str;
}
// This special case exists only for the special source location in
// ReactElementValidator. That will go away if we remove source locations.
str = str.replace(/Check your code at .+?:\d+/g, 'Check your code at **');
// V8 format:
// at Component (/path/filename.js:123:45)
// React format:
// in Component (at filename.js:123)
return str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// while still maintaining support for multiple renderer versions
// (which use different values for ReactTypeOfWork).

import type {Source} from 'shared/ReactElementType';
import type {LazyComponent} from 'react/src/ReactLazy';
import type {CurrentDispatcherRef} from './types';

Expand All @@ -36,7 +35,6 @@ import {disableLogs, reenableLogs} from './DevToolsConsolePatching';
let prefix;
export function describeBuiltInComponentFrame(
name: string,
source: void | null | Source,
ownerFn: void | null | Function,
): string {
if (prefix === undefined) {
Expand Down Expand Up @@ -204,7 +202,6 @@ export function describeNativeComponentFrame(

export function describeClassComponentFrame(
ctor: Function,
source: void | null | Source,
ownerFn: void | null | Function,
currentDispatcherRef: CurrentDispatcherRef,
): string {
Expand All @@ -213,7 +210,6 @@ export function describeClassComponentFrame(

export function describeFunctionComponentFrame(
fn: Function,
source: void | null | Source,
ownerFn: void | null | Function,
currentDispatcherRef: CurrentDispatcherRef,
): string {
Expand All @@ -227,7 +223,6 @@ function shouldConstruct(Component: Function) {

export function describeUnknownElementTypeFrameInDEV(
type: any,
source: void | null | Source,
ownerFn: void | null | Function,
currentDispatcherRef: CurrentDispatcherRef,
): string {
Expand All @@ -245,23 +240,22 @@ export function describeUnknownElementTypeFrameInDEV(
);
}
if (typeof type === 'string') {
return describeBuiltInComponentFrame(type, source, ownerFn);
return describeBuiltInComponentFrame(type, ownerFn);
}
switch (type) {
case SUSPENSE_NUMBER:
case SUSPENSE_SYMBOL_STRING:
return describeBuiltInComponentFrame('Suspense', source, ownerFn);
return describeBuiltInComponentFrame('Suspense', ownerFn);
case SUSPENSE_LIST_NUMBER:
case SUSPENSE_LIST_SYMBOL_STRING:
return describeBuiltInComponentFrame('SuspenseList', source, ownerFn);
return describeBuiltInComponentFrame('SuspenseList', ownerFn);
}
if (typeof type === 'object') {
switch (type.$$typeof) {
case FORWARD_REF_NUMBER:
case FORWARD_REF_SYMBOL_STRING:
return describeFunctionComponentFrame(
type.render,
source,
ownerFn,
currentDispatcherRef,
);
Expand All @@ -270,7 +264,6 @@ export function describeUnknownElementTypeFrameInDEV(
// Memo may contain any component type so we recursively resolve it.
return describeUnknownElementTypeFrameInDEV(
type.type,
source,
ownerFn,
currentDispatcherRef,
);
Expand All @@ -283,7 +276,6 @@ export function describeUnknownElementTypeFrameInDEV(
// Lazy may contain any component type so we recursively resolve it.
return describeUnknownElementTypeFrameInDEV(
init(payload),
source,
ownerFn,
currentDispatcherRef,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,32 @@ export function describeFiber(
? workInProgress._debugOwner.type
: null
: null;
const source = __DEV__ ? workInProgress._debugSource : null;
switch (workInProgress.tag) {
case HostComponent:
return describeBuiltInComponentFrame(workInProgress.type, source, owner);
return describeBuiltInComponentFrame(workInProgress.type, owner);
case LazyComponent:
return describeBuiltInComponentFrame('Lazy', source, owner);
return describeBuiltInComponentFrame('Lazy', owner);
case SuspenseComponent:
return describeBuiltInComponentFrame('Suspense', source, owner);
return describeBuiltInComponentFrame('Suspense', owner);
case SuspenseListComponent:
return describeBuiltInComponentFrame('SuspenseList', source, owner);
return describeBuiltInComponentFrame('SuspenseList', owner);
case FunctionComponent:
case IndeterminateComponent:
case SimpleMemoComponent:
return describeFunctionComponentFrame(
workInProgress.type,
source,
owner,
currentDispatcherRef,
);
case ForwardRef:
return describeFunctionComponentFrame(
workInProgress.type.render,
source,
owner,
currentDispatcherRef,
);
case ClassComponent:
return describeClassComponentFrame(
workInProgress.type,
source,
owner,
currentDispatcherRef,
);
Expand Down

0 comments on commit 955cad9

Please sign in to comment.