Skip to content
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

DevTools: Show hook names based on variable usage #21641

Merged
merged 25 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d75814c
Initial work to support hook variable names in devtools extension.
Jun 24, 2021
3dd1247
Moved named hooks feature before enableHookNameParsing flag
Jun 24, 2021
48632b7
Moved hook names parsing behind user setting
Jun 24, 2021
6a1eecd
Moved feature flag override logic into test helper
Jun 24, 2021
f502611
Added compiled test sources to Git repo
Jun 24, 2021
cb80fba
Test script auto-compiles all source files
Jun 24, 2021
81770cd
Added Jest serializer for Hooks (to sanitize source location)
Jun 25, 2021
d508c43
Removed injectHookVariableNames-test in favor of less hard-coded pars…
Jun 25, 2021
75d2256
Don't generate hook source unless feature flag is enabled
Jun 25, 2021
8075462
Remove hard-coded mappings.wasm file from source
Jun 25, 2021
7f3af1c
Reorganized tests slightly
Jun 25, 2021
49dd494
Fixed stack trace errors. Added additional tests
Jun 26, 2021
f91e8d8
Prevent Jest from configuring Error source-maps
Jun 26, 2021
43bd1b2
Account for Error stack frames having source maps already applied
Jun 28, 2021
475ef1b
Refactored AST utils slightly
Jun 29, 2021
9b7d709
Fixed timeout logic error in hook names cache
Jun 30, 2021
57f15ac
Map hook to hook name (rather than array) for sub hooks
Jun 30, 2021
064da9e
Added DEBUG logging to parseHookNames util
Jun 30, 2021
6222d7f
Updated hook names tests to account for map vs array
Jun 30, 2021
615d8e4
Added option to lazily parse hook names for only the currently inspec…
Jun 30, 2021
9f7a22f
Added an LRU cache and eviction strategy for source code/ASTs
Jun 30, 2021
73fafbe
Enabled external hooks test
Jul 1, 2021
8c4fcb9
Error source map stack frame check is now lazy instead of during modu…
Jul 1, 2021
3bf7311
Styled custom hook names too
Jul 1, 2021
401b94e
Reverted files that were accidentally modified from main branch
Jul 1, 2021
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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ packages/react-devtools-core/dist
packages/react-devtools-extensions/chrome/build
packages/react-devtools-extensions/firefox/build
packages/react-devtools-extensions/shared/build
packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/
packages/react-devtools-extensions/src/ErrorTesterCompiled.js
packages/react-devtools-inline/dist
packages/react-devtools-shell/dist
packages/react-devtools-scheduling-profiler/dist
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ packages/react-devtools-core/dist
packages/react-devtools-extensions/chrome/build
packages/react-devtools-extensions/firefox/build
packages/react-devtools-extensions/shared/build
packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/
packages/react-devtools-extensions/src/ErrorTesterCompiled.js
packages/react-devtools-inline/dist
packages/react-devtools-shell/dist
packages/react-devtools-scheduling-profiler/dist
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"scripts": {
"build": "node ./scripts/rollup/build.js",
"build-combined": "node ./scripts/rollup/build-all-release-channels.js",
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build react/index,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer,react-refresh --type=NODE && rm -rf build2 && mkdir build2 && cp -r ./build/node_modules build2/oss-experimental/",
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build react/index,react/jsx,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer,react-refresh --type=NODE && rm -rf build2 && mkdir build2 && cp -r ./build/node_modules build2/oss-experimental/",
"build-for-devtools-dev": "yarn build-for-devtools --type=NODE_DEV",
"build-for-devtools-prod": "yarn build-for-devtools --type=NODE_PROD",
"linc": "node ./scripts/tasks/linc.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/react-devtools-core/webpack.backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ module.exports = {
scheduler: resolve(builtModulesDir, 'scheduler'),
},
},
node: {
fs: 'empty',
},
plugins: [
new DefinePlugin({
__DEV__,
Expand Down
6 changes: 6 additions & 0 deletions packages/react-devtools-extensions/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const build = async (tempPath, manifestPath) => {
STATIC_FILES.map(file => copy(join(__dirname, file), join(zipPath, file))),
);

// The "source-map" library requires this chunk of WASM to be bundled at runtime.
await copy(
join(__dirname, 'node_modules', 'source-map', 'lib', 'mappings.wasm'),
join(zipPath, 'mappings.wasm'),
);

const commit = getGitCommit();
const dateString = new Date().toLocaleDateString();
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"main.html",
"panel.html",
"build/react_devtools_backend.js",
"build/renderer.js"
"build/renderer.js",
"mappings.wasm"
],

"background": {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"main.html",
"panel.html",
"build/react_devtools_backend.js",
"build/renderer.js"
"build/renderer.js",
"mappings.wasm"
],

"background": {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"main.html",
"panel.html",
"build/react_devtools_backend.js",
"build/renderer.js"
"build/renderer.js",
"mappings.wasm"
],

"background": {
Expand Down
8 changes: 7 additions & 1 deletion packages/react-devtools-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"build:edge:dev": "cross-env NODE_ENV=development node ./edge/build",
"test:chrome": "node ./chrome/test",
"test:firefox": "node ./firefox/test",
"test:edge": "node ./edge/test"
"test:edge": "node ./edge/test",
"update-mock-source-maps": "node ./src/__tests__/updateMockSourceMaps.js"
},
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-transform-flow-strip-types": "^7.10.4",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@babel/plugin-transform-react-jsx-source": "^7.10.5",
"@babel/preset-env": "^7.11.0",
"@babel/preset-flow": "^7.10.4",
Expand All @@ -29,16 +31,20 @@
"babel-core": "^7.0.0-bridge",
"babel-eslint": "^9.0.0",
"babel-loader": "^8.0.4",
"babel-preset-minify": "^0.5.1",
"child-process-promise": "^2.2.1",
"chrome-launch": "^1.1.4",
"crx": "^5.0.0",
"css-loader": "^1.0.1",
"firefox-profile": "^1.0.2",
"fs-extra": "^4.0.2",
"jest-fetch-mock": "^3.0.3",
"node-libs-browser": "0.5.3",
"nullthrows": "^1.0.0",
"open": "^7.0.2",
"os-name": "^3.1.0",
"raw-loader": "^3.1.0",
"source-map": "^0.8.0-beta.0",
"style-loader": "^0.23.1",
"web-ext": "^3.0.0",
"webpack": "^4.43.0",
Expand Down
34 changes: 34 additions & 0 deletions packages/react-devtools-extensions/src/ErrorTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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
*/

import ErrorStackParser from 'error-stack-parser';
import testErrorStack, {
SOURCE_STACK_FRAME_LINE_NUMBER,
} from './ErrorTesterCompiled';

let sourceMapsAreAppliedToErrors: boolean | null = null;

// Source maps are automatically applied to Error stack frames.
export function areSourceMapsAppliedToErrors(): boolean {
if (sourceMapsAreAppliedToErrors === null) {
try {
testErrorStack();
sourceMapsAreAppliedToErrors = false;
} catch (error) {
const parsed = ErrorStackParser.parse(error);
const topStackFrame = parsed[0];
const lineNumber = topStackFrame.lineNumber;
if (lineNumber === SOURCE_STACK_FRAME_LINE_NUMBER) {
sourceMapsAreAppliedToErrors = true;
}
}
}

return sourceMapsAreAppliedToErrors === true;
}
25 changes: 25 additions & 0 deletions packages/react-devtools-extensions/src/ErrorTesterCompiled.js

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

1 change: 1 addition & 0 deletions packages/react-devtools-extensions/src/__tests__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The JavaScript files and source maps in this directory are used to test DevTools hook name parsing code. The files here use source maps in different formats to mirror real world applications. The source for the files is located in the parnet `__tests__` folder. To regenerate these compiled files, run `yarn update-mock-source-maps` in the `react-devtools-extensions` directory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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
*/

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

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

useEffect(() => {
// ...
}, []);

const handleClick = () => setCount(count + 1);

return (
<>
<div>Dark mode? {isDarkMode}</div>
<div>Count: {count}</div>
<button onClick={handleClick}>Update count</button>
</>
);
}

function useIsDarkMode() {
const [isDarkMode] = useState(false);

useEffect(function useEffectCreate() {
// Here is where we may listen to a "theme" event...
}, []);

return isDarkMode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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
*/

import React from 'react';
import useTheme from './useTheme';

export function Component() {
const theme = useTheme();

return <div>theme: {theme}</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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
*/

import React, {useState} from 'react';

export function Component() {
const [count, setCount] = useState(0);

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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
*/

export function Component() {
const [count] = require('react').useState(0);

return count;
}
Loading