Skip to content

[DevTools] Hook names are correctly extracted when parsing nested hook calls #22037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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
*/
const {useMemo, useState} = require('react');

function Component(props) {
const InnerComponent = useMemo(() => () => {
const [state] = useState(0);

return state;
});
props.callback(InnerComponent);

return null;
}

module.exports = {Component};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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
*/
const {useMemo, useState} = require('react');

function Component(props) {
const InnerComponent = useMemo(() => () => {
const [state] = useState(0);

return state;
});
props.callback(InnerComponent);

return null;
};

module.exports = {Component};
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ export {Component as ComponentUsingHooksIndirectly} from './ComponentUsingHooksI
export {Component as ComponentWithCustomHook} from './ComponentWithCustomHook';
export {Component as ComponentWithExternalCustomHooks} from './ComponentWithExternalCustomHooks';
export {Component as ComponentWithMultipleHooksPerLine} from './ComponentWithMultipleHooksPerLine';
export {Component as ComponentWithNestedHooks} from './ComponentWithNestedHooks';
export {Component as ContainingStringSourceMappingURL} from './ContainingStringSourceMappingURL';
export {Component as Example} from './Example';
export {Component as InlineRequire} from './InlineRequire';
Original file line number Diff line number Diff line change
@@ -152,6 +152,20 @@ describe('parseHookNames', () => {
expectHookNamesToEqual(hookNames, ['count', 'darkMode', 'isDarkMode']);
});

it('should parse names for code using nested hooks', async () => {
const Component = require('./__source__/__untransformed__/ComponentWithNestedHooks')
.Component;
let InnerComponent;
const hookNames = await getHookNamesForComponent(Component, {
callback: innerComponent => {
InnerComponent = innerComponent;
},
});
const innerHookNames = await getHookNamesForComponent(InnerComponent);
expectHookNamesToEqual(hookNames, ['InnerComponent']);
expectHookNamesToEqual(innerHookNames, ['state']);
});

it('should return null for custom hooks without explicit names', async () => {
const Component = require('./__source__/__untransformed__/ComponentWithUnnamedCustomHooks')
.Component;
@@ -261,6 +275,35 @@ describe('parseHookNames', () => {
); // simulated Webpack 'cheap-module-source-map'
});

it('should work when code is using nested hooks', async () => {
async function test(path, name = 'Component') {
const Component = require(path)[name];
let InnerComponent;
const hookNames = await getHookNamesForComponent(Component, {
callback: innerComponent => {
InnerComponent = innerComponent;
},
});
const innerHookNames = await getHookNamesForComponent(InnerComponent);
expectHookNamesToEqual(hookNames, [
'InnerComponent', // useMemo()
]);
expectHookNamesToEqual(innerHookNames, [
'state', // useState()
]);
}

await test('./__source__/__compiled__/inline/ComponentWithNestedHooks'); // inline source map
await test('./__source__/__compiled__/external/ComponentWithNestedHooks'); // external source map
await test(
'./__source__/__compiled__/bundle',
'ComponentWithNestedHooks',
); // bundle source map
await test(
'./__source__/__compiled__/no-columns/ComponentWithNestedHooks',
); // simulated Webpack 'cheap-module-source-map'
});

it('should work for external hooks', async () => {
async function test(path, name = 'Component') {
const Component = require(path)[name];
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/src/astUtils.js
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ function checkNodeLocation(
): boolean {
const {start, end} = path.node.loc;

if (line < start.line || line > end.line) {
if (line !== start.line) {
return false;
}