-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code so that isInternalModule function could be unit tested.
- Loading branch information
Brian Vaughn
committed
Oct 20, 2021
1 parent
a6f6461
commit 79f6f34
Showing
8 changed files
with
245 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...-devtools-scheduling-profiler/src/content-views/utils/__tests__/__modules__/module-one.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export const outerErrorA = new Error(); | ||
|
||
export const moduleStartError = new Error(); | ||
export const innerError = new Error(); | ||
export const moduleStopError = new Error(); | ||
|
||
export const outerErrorB = new Error(); |
18 changes: 18 additions & 0 deletions
18
...-devtools-scheduling-profiler/src/content-views/utils/__tests__/__modules__/module-two.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export const moduleAStartError = new Error(); | ||
export const innerErrorA = new Error(); | ||
export const moduleAStopError = new Error(); | ||
|
||
export const outerError = new Error(); | ||
|
||
export const moduleBStartError = new Error(); | ||
export const innerErrorB = new Error(); | ||
export const moduleBStopError = new Error(); |
112 changes: 112 additions & 0 deletions
112
...eact-devtools-scheduling-profiler/src/content-views/utils/__tests__/moduleFilters-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import ErrorStackParser from 'error-stack-parser'; | ||
import {isInternalModule} from '../moduleFilters'; | ||
|
||
import * as moduleOne from './__modules__/module-one'; | ||
import * as moduleTwo from './__modules__/module-two'; | ||
|
||
describe('isInternalModule', () => { | ||
let moduleSourceToRanges; | ||
|
||
function parseAndGetStackFrame(error, index = 0) { | ||
const stackFrame = ErrorStackParser.parse(error)[index]; | ||
|
||
// Strip out the portion of the file path that includes "react-devtools-scheduling-profiler" | ||
// or every frame will be flagged as internal. | ||
stackFrame.fileName = stackFrame.fileName.replace( | ||
/.+__tests__/, | ||
'../__tests__', | ||
); | ||
|
||
return stackFrame; | ||
} | ||
|
||
function stackFrameToFlamechartStackFrame(stackFrame) { | ||
return { | ||
name: 'test', | ||
timestamp: 0, | ||
duration: 1, | ||
scriptUrl: stackFrame.fileName, | ||
locationLine: stackFrame.lineNumber, | ||
locationColumn: stackFrame.columnNumber, | ||
}; | ||
} | ||
|
||
beforeEach(() => { | ||
moduleSourceToRanges = new Map(); | ||
|
||
let startErrorStackFrame = parseAndGetStackFrame( | ||
moduleOne.moduleStartError, | ||
); | ||
let stopErrorStackFrame = parseAndGetStackFrame(moduleOne.moduleStopError); | ||
moduleSourceToRanges.set(startErrorStackFrame.fileName, [ | ||
[startErrorStackFrame, stopErrorStackFrame], | ||
]); | ||
|
||
const moduleTwoRanges = []; | ||
|
||
startErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleAStartError); | ||
stopErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleAStopError); | ||
moduleTwoRanges.push([startErrorStackFrame, stopErrorStackFrame]); | ||
|
||
startErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleBStartError); | ||
stopErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleBStopError); | ||
moduleTwoRanges.push([startErrorStackFrame, stopErrorStackFrame]); | ||
|
||
moduleSourceToRanges.set(startErrorStackFrame.fileName, moduleTwoRanges); | ||
}); | ||
|
||
it('should properly identify stack frames within the provided module ranges', () => { | ||
let flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleOne.innerError), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
true, | ||
); | ||
|
||
flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleTwo.innerErrorA), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
true, | ||
); | ||
|
||
flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleTwo.innerErrorB), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
true, | ||
); | ||
}); | ||
|
||
it('should properly identify stack frames outside of the provided module ranges', () => { | ||
let flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleOne.outerErrorA), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
false, | ||
); | ||
|
||
flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleOne.outerErrorB), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
false, | ||
); | ||
|
||
flamechartStackFrame = stackFrameToFlamechartStackFrame( | ||
parseAndGetStackFrame(moduleTwo.outerError), | ||
); | ||
expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( | ||
false, | ||
); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
packages/react-devtools-scheduling-profiler/src/content-views/utils/moduleFilters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type { | ||
FlamechartStackFrame, | ||
InternalModuleSourceToRanges, | ||
} from '../../types'; | ||
|
||
import { | ||
CHROME_WEBSTORE_EXTENSION_ID, | ||
INTERNAL_EXTENSION_ID, | ||
LOCAL_EXTENSION_ID, | ||
} from 'react-devtools-shared/src/constants'; | ||
|
||
export function isInternalModule( | ||
internalModuleSourceToRanges: InternalModuleSourceToRanges, | ||
flamechartStackFrame: FlamechartStackFrame, | ||
): boolean { | ||
const {locationColumn, locationLine, scriptUrl} = flamechartStackFrame; | ||
|
||
if (scriptUrl == null || locationColumn == null || locationLine == null) { | ||
// This could indicate a browser-internal API like performance.mark(). | ||
return false; | ||
} | ||
|
||
// Internal modules are only registered if DevTools was running when the profile was captured, | ||
// but DevTools should also hide its own frames to avoid over-emphasizing them. | ||
if ( | ||
// Handle webpack-internal:// sources | ||
scriptUrl.includes('/react-devtools') || | ||
scriptUrl.includes('/react_devtools') || | ||
// Filter out known extension IDs | ||
scriptUrl.includes(CHROME_WEBSTORE_EXTENSION_ID) || | ||
scriptUrl.includes(INTERNAL_EXTENSION_ID) || | ||
scriptUrl.includes(LOCAL_EXTENSION_ID) | ||
// Unfortunately this won't get everything, like relatively loaded chunks or Web Worker files. | ||
) { | ||
return true; | ||
} | ||
|
||
// Filter out React internal packages. | ||
const ranges = internalModuleSourceToRanges.get(scriptUrl); | ||
if (ranges != null) { | ||
for (let i = 0; i < ranges.length; i++) { | ||
const [startStackFrame, stopStackFrame] = ranges[i]; | ||
|
||
const isAfterStart = | ||
locationLine > startStackFrame.lineNumber || | ||
(locationLine === startStackFrame.lineNumber && | ||
locationColumn >= startStackFrame.columnNumber); | ||
const isBeforeStop = | ||
locationLine < stopStackFrame.lineNumber || | ||
(locationLine === stopStackFrame.lineNumber && | ||
locationColumn <= stopStackFrame.columnNumber); | ||
|
||
if (isAfterStart && isBeforeStop) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters