Skip to content

Commit

Permalink
fix: ignore non declarative primitive hooks during variable name inje…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
vibhorgupta-gh committed Nov 30, 2020
1 parent 03e8796 commit dd932fe
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,17 @@ export function modifyHooksToAddVariableNames(hookLog: HooksTree, sourceMaps: Do
.find(node => checkNodeLocation(node, line) && isConfirmedHookDeclaration(node));
if (!potentialReactHookASTNode) {
if (!isCustomHook) {
// Custom hooks and primitive hooks that aren't assigned any variables, don't have any corresponding AST nodes
if (!isCustomHook && !isNonDeclarativePrimitiveHook(hook)) {
throw new Error(`No Potential React Hook found at line ${line}`);
}
// If the customHook is not assigned to any variable, its variable declarator AST node also cannot be found.
// For such cases, we inject variable names for subhooks and return the original hook
injectSubHooksWithVariableNames(hook, sourceMaps, sourceMapUrls, sourceFileUrls);
if (isCustomHook) {
// If the customHook is not assigned to any variable, its variable declarator AST node also cannot be found.
// For such cases, we inject variable names for subhooks.
injectSubHooksWithVariableNames(hook, sourceMaps, sourceMapUrls, sourceFileUrls);
}
// Return original hook object for primitive and custom hooks that are not assigned to any variables.
// eg. useEffect, useLayoutEffect etc.
return hook;
}
Expand Down Expand Up @@ -1173,6 +1178,16 @@ function isPotentialHookDeclaration(path: NodePath): boolean {
return false;
}
/**
* Determines whether incoming hook is a primitive hook that gets assigned to variables.
*
* @param {HooksNode} hook - Original hook object
* @return {boolean} - Returns true for primitive hooks that are not assigned to variables.
*/
function isNonDeclarativePrimitiveHook(hook: HooksNode) {
return ['Effect', 'ImperativeHandle', 'LayoutEffect', 'DebugValue'].includes(hook.name);
}
/**
* Check whether hookNode of a declaration contains obvious variable name
*
Expand Down

0 comments on commit dd932fe

Please sign in to comment.