Files
react/packages/react-devtools-extensions/src/SourceMapUtils.js
Juan 6abda7f4f2 [DevTools] Access metadata in source maps correctly accounting for different formats (#22096)
## Summary

Follow up from https://github.com/facebook/react/pull/22010.

The initial implementation of named hooks and for looking up hook name metadata in an extended source map both assumed that the source maps would always have a `sources` field available, and didn't account for the source maps in the [Index Map](https://sourcemaps.info/spec.html#h.535es3xeprgt) format, which contain a list of `sections` and don't have the `source` field available directly. 

In order to properly access metadata in extended source maps, this commit:

-  Adds a new `SourceMapMetadataConsumer` api, which is a fork / very similar in structure to the corresponding [consumer in Metro](2b44ec39b4/packages/metro-symbolicate/src/SourceMetadataMapConsumer.js (L56)) (as specified by @motiz88 in https://github.com/facebook/react/issues/21782.
- Updates `parseHookNames` to use this new api

## Test Plan

- yarn flow
- yarn test
- yarn test-build-devtools
- added new regression tests covering the index map format
- named hooks still work on manual test of browser extension on a few different apps (code sandbox, create-react-app, internally).
2021-08-18 14:30:31 -04:00

35 lines
824 B
JavaScript

/*
* 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 strict-local
*/
import type {
BasicSourceMap,
MixedSourceMap,
IndexSourceMap,
} from './SourceMapTypes';
export function sourceMapIncludesSource(
sourcemap: MixedSourceMap,
source: ?string,
): boolean {
if (source == null) {
return false;
}
if (sourcemap.mappings === undefined) {
const indexSourceMap: IndexSourceMap = sourcemap;
return indexSourceMap.sections.some(section => {
return sourceMapIncludesSource(section.map, source);
});
}
const basicMap: BasicSourceMap = sourcemap;
return basicMap.sources.some(
s => s === 'Inline Babel script' || source.endsWith(s),
);
}