[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).
This commit is contained in:
Juan 2021-08-18 18:26:57 -04:00 committed by GitHub
parent f1db9c30cc
commit 42ef45b129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 3 deletions

View File

@ -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};

View File

@ -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 () => {

View File

@ -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;