Skip to content

Commit 42ef45b

Browse files
author
Juan
authored
[DevTools] Using array destructuring without assigning first variable does not error (#22129)
## Summary Before this commit, if a hook returned an array the was destructured, but without assigning a variable to the first element in the array, this would produce an error. This was detected via internal testing. This commit fixes that and adds regression tests. ## Test Plan - yarn flow - yarn test - yarn test-build-devtools - added new regression tests - named hooks still work on manual test of browser extension on a few different apps (code sandbox, create-react-app, internally).
1 parent f1db9c3 commit 42ef45b

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

packages/react-devtools-extensions/src/__tests__/__source__/__untransformed__/ComponentWithUseState.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function Component(props) {
1414
const [foo] = useState(true);
1515
const bar = useState(true);
1616
const [baz] = React.useState(true);
17+
const [, forceUpdate] = useState();
1718
return `${foo}-${bar}-${baz}`;
1819
}
1920

20-
module.exports = {Component};
21+
module.exports = {Component};

packages/react-devtools-extensions/src/__tests__/parseHookNames-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('parseHookNames', () => {
9595
const Component = require('./__source__/__untransformed__/ComponentWithUseState')
9696
.Component;
9797
const hookNames = await getHookNamesForComponent(Component);
98-
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz']);
98+
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz', null]);
9999
});
100100

101101
it('should parse names for useReducer()', async () => {

packages/react-devtools-extensions/src/astUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function getHookVariableName(
271271
const nodeType = hook.node.id.type;
272272
switch (nodeType) {
273273
case AST_NODE_TYPES.ARRAY_PATTERN:
274-
return !isCustomHook ? hook.node.id.elements[0].name : null;
274+
return !isCustomHook ? hook.node.id.elements[0]?.name ?? null : null;
275275

276276
case AST_NODE_TYPES.IDENTIFIER:
277277
return hook.node.id.name;

0 commit comments

Comments
 (0)