Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: facebook/react
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 10e15af724b6d6dd5df6590237ca4601552052e1
Choose a base ref
..
head repository: facebook/react
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 21a345a8869116e80ee47a2e0155acd047dbabe7
Choose a head ref
78 changes: 28 additions & 50 deletions packages/react-devtools-extensions/src/astUtils.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
* @flow
*/

import {__PERFORMANCE_PROFILE__} from 'react-devtools-shared/src/constants';
import {withSyncPerformanceMark} from 'react-devtools-shared/src/PerformanceMarks';
import traverse, {NodePath, Node} from '@babel/traverse';
import {File} from '@babel/types';

@@ -28,15 +28,6 @@ export type SourceFileASTWithHookDetails = {

export const NO_HOOK_NAME = '<no-hook>';

function mark(markName: string): void {
performance.mark(markName + '-start');
}

function measure(markName: string): void {
performance.mark(markName + '-end');
performance.measure(markName, markName + '-start', markName + '-end');
}

const AST_NODE_TYPES = Object.freeze({
PROGRAM: 'Program',
CALL_EXPRESSION: 'CallExpression',
@@ -141,13 +132,10 @@ export function getHookName(
originalSourceLineNumber: number,
originalSourceColumnNumber: number,
): string | null {
if (__PERFORMANCE_PROFILE__) {
mark('getPotentialHookDeclarationsFromAST(originalSourceAST)');
}
const hooksFromAST = getPotentialHookDeclarationsFromAST(originalSourceAST);
if (__PERFORMANCE_PROFILE__) {
measure('getPotentialHookDeclarationsFromAST(originalSourceAST)');
}
const hooksFromAST = withSyncPerformanceMark(
'getPotentialHookDeclarationsFromAST(originalSourceAST)',
() => getPotentialHookDeclarationsFromAST(originalSourceAST),
);

let potentialReactHookASTNode = null;
if (originalSourceColumnNumber === 0) {
@@ -188,29 +176,23 @@ export function getHookName(
// nodesAssociatedWithReactHookASTNode could directly be used to obtain the hook variable name
// depending on the type of potentialReactHookASTNode
try {
if (__PERFORMANCE_PROFILE__) {
mark('getFilteredHookASTNodes()');
}
const nodesAssociatedWithReactHookASTNode = getFilteredHookASTNodes(
potentialReactHookASTNode,
hooksFromAST,
originalSourceCode,
const nodesAssociatedWithReactHookASTNode = withSyncPerformanceMark(
'getFilteredHookASTNodes()',
() =>
getFilteredHookASTNodes(
potentialReactHookASTNode,
hooksFromAST,
originalSourceCode,
),
);
if (__PERFORMANCE_PROFILE__) {
measure('getFilteredHookASTNodes()');
}

if (__PERFORMANCE_PROFILE__) {
mark('getHookNameFromNode()');
}
const name = getHookNameFromNode(
hook,
nodesAssociatedWithReactHookASTNode,
potentialReactHookASTNode,
const name = withSyncPerformanceMark('getHookNameFromNode()', () =>
getHookNameFromNode(
hook,
nodesAssociatedWithReactHookASTNode,
potentialReactHookASTNode,
),
);
if (__PERFORMANCE_PROFILE__) {
measure('getHookNameFromNode()');
}

return name;
} catch (error) {
@@ -315,19 +297,15 @@ function getHookVariableName(

function getPotentialHookDeclarationsFromAST(sourceAST: File): NodePath[] {
const potentialHooksFound: NodePath[] = [];
if (__PERFORMANCE_PROFILE__) {
mark('traverse(sourceAST)');
}
traverse(sourceAST, {
enter(path) {
if (path.isVariableDeclarator() && isPotentialHookDeclaration(path)) {
potentialHooksFound.push(path);
}
},
});
if (__PERFORMANCE_PROFILE__) {
measure('traverse(sourceAST)');
}
withSyncPerformanceMark('traverse(sourceAST)', () =>
traverse(sourceAST, {
enter(path) {
if (path.isVariableDeclarator() && isPotentialHookDeclaration(path)) {
potentialHooksFound.push(path);
}
},
}),
);
return potentialHooksFound;
}

Loading