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] Updated source map extension format + more precise types #22073

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function compile(fileName) {
// When using the x_fb_sources extension field, the first item
// for a given source is reserved for the Function Map, and the
// Hook Map is added as the second item.
x_fb_sources: [[null, encodedHookMap]],
x_fb_sources: [[null, [encodedHookMap]]],
};
const reactSourcesExtendedSourceMap = {
...sourceMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {getHookSourceLocationKey} from 'react-devtools-shared/src/hookNamesCache
import {decodeHookMap} from '../generateHookMap';
import {getHookNameForLocation} from '../getHookNameForLocation';

import type {MixedSourceMap} from '../SourceMapTypes';
import type {HookMap} from '../generateHookMap';
import type {
HooksNode,
Expand All @@ -30,7 +31,7 @@ import type {SourceConsumer} from '../astUtils';
const SOURCE_MAP_REGEX = / ?sourceMappingURL=([^\s'"]+)/gm;
const MAX_SOURCE_LENGTH = 100_000_000;
const REACT_SOURCES_EXTENSION_KEY = 'x_react_sources';
const FB_SOURCES_EXTENSION_KEY = 'x_fb_sources';
const FB_SOURCES_EXTENSION_KEY = 'x_facebook_sources';

type AST = mixed;

Expand Down Expand Up @@ -662,21 +663,33 @@ function updateLruCache(
}

function extractHookMapFromSourceMap(
sourcemap,
sourcemap: MixedSourceMap,
sourceIndex: number,
): HookMap | null {
let hookMap;
if (sourcemap.hasOwnProperty(REACT_SOURCES_EXTENSION_KEY)) {
if (
sourcemap.hasOwnProperty(REACT_SOURCES_EXTENSION_KEY) &&
sourcemap[REACT_SOURCES_EXTENSION_KEY] != null
) {
// When using the x_react_sources extension field, the first item
// for a given source is reserved for the Hook Map, which is why
// we look up the index at position 0.
hookMap = sourcemap[REACT_SOURCES_EXTENSION_KEY][sourceIndex][0];
} else if (sourcemap.hasOwnProperty(FB_SOURCES_EXTENSION_KEY)) {
// When using the x_fb_sources extension field, the first item
const reactMetadataForSource =
sourcemap[REACT_SOURCES_EXTENSION_KEY][sourceIndex];
hookMap = reactMetadataForSource != null ? reactMetadataForSource[0] : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would probably unify this with the later identical line.

} else if (
sourcemap.hasOwnProperty(FB_SOURCES_EXTENSION_KEY) &&
sourcemap[FB_SOURCES_EXTENSION_KEY] != null
) {
// When using the x_facebook_sources extension field, the first item
// for a given source is reserved for the Function Map, and the
// Hook Map is added as the second item, which is why we look up
// the index at position 1.
hookMap = sourcemap[FB_SOURCES_EXTENSION_KEY][sourceIndex][1];
const fbMetadataForSource =
sourcemap[FB_SOURCES_EXTENSION_KEY][sourceIndex];
const reactMetadataForSource =
fbMetadataForSource != null ? fbMetadataForSource[1] : null;
hookMap = reactMetadataForSource != null ? reactMetadataForSource[0] : null;
}
if (hookMap != null) {
return decodeHookMap(hookMap);
Expand Down