Skip to content

Commit

Permalink
[DevTools] Using array destructuring without assigning first variable…
Browse files Browse the repository at this point in the history
… does not error (facebook#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).
  • Loading branch information
Juan authored and zhengjitf committed Apr 15, 2022
1 parent 45441f3 commit 97a2fc1
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function Component(props) {
const [foo] = useState(true);
const bar = useState(true);
const [baz] = React.useState(true);
const [, forceUpdate] = useState();
return `${foo}-${bar}-${baz}`;
}

module.exports = {Component};
module.exports = {Component};
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('parseHookNames', () => {
const Component = require('./__source__/__untransformed__/ComponentWithUseState')
.Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz']);
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz', null]);
});

it('should parse names for useReducer()', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/src/astUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function getHookVariableName(
const nodeType = hook.node.id.type;
switch (nodeType) {
case AST_NODE_TYPES.ARRAY_PATTERN:
return !isCustomHook ? hook.node.id.elements[0].name : null;
return !isCustomHook ? hook.node.id.elements[0]?.name ?? null : null;

case AST_NODE_TYPES.IDENTIFIER:
return hook.node.id.name;
Expand Down

0 comments on commit 97a2fc1

Please sign in to comment.