Skip to content

Commit

Permalink
[DevTools] Support object destructuring pattern without runtime errors (
Browse files Browse the repository at this point in the history
facebook#22128)

## Summary

Before this commit, if a hook returned an object and we declared a variable using object destructuring on the returned value, we would produce a runtime 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 7ad4d08 commit 45441f3
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export class SourceMapMetadataConsumer {
return getHookNameForLocation({line, column}, hookMap);
}

hasHookMap(source: ?string) {
hasHookMap(source: ?string): boolean {
if (source == null) {
return null;
return false;
}
return this._getHookMapForSource(source) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
* @flow
*/

import React, {useEffect, useState} from 'react';
import React, {useDebugValue, useEffect, useState} from 'react';

export function Component() {
const [count, setCount] = useState(0);
const isDarkMode = useIsDarkMode();
const {foo} = useFoo();

useEffect(() => {
// ...
Expand All @@ -23,6 +24,7 @@ export function Component() {
<>
<div>Dark mode? {isDarkMode}</div>
<div>Count: {count}</div>
<div>Foo: {foo}</div>
<button onClick={handleClick}>Update count</button>
</>
);
Expand All @@ -37,3 +39,8 @@ function useIsDarkMode() {

return isDarkMode;
}

function useFoo() {
useDebugValue('foo');
return {foo: true};
}

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.

Loading

0 comments on commit 45441f3

Please sign in to comment.