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: Lazily parse indexed map sections #22415

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Changes from all commits
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
22 changes: 19 additions & 3 deletions packages/react-devtools-shared/src/hooks/SourceMapConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {decode} from 'sourcemap-codec';

import type {
IndexSourceMap,
IndexSourceMapSection,
BasicSourceMap,
MixedSourceMap,
} from './SourceMapTypes';
Expand All @@ -34,7 +35,7 @@ export type SourceMapConsumerType = {|
type Mappings = Array<Array<Array<number>>>;

export default function SourceMapConsumer(
sourceMapJSON: MixedSourceMap,
sourceMapJSON: MixedSourceMap | IndexSourceMapSection,
): SourceMapConsumerType {
if (sourceMapJSON.sections != null) {
return IndexedSourceMapConsumer(((sourceMapJSON: any): IndexSourceMap));
Expand Down Expand Up @@ -137,13 +138,22 @@ function BasicSourceMapConsumer(sourceMapJSON: BasicSourceMap) {
}: any): SourceMapConsumerType);
}

type Section = {|
+generatedColumn: number,
+generatedLine: number,
+map: MixedSourceMap,

// Lazily parsed only when/as the section is needed.
sourceMapConsumer: SourceMapConsumerType | null,
|};

function IndexedSourceMapConsumer(sourceMapJSON: IndexSourceMap) {
let lastOffset = {
line: -1,
column: 0,
};

const sections = sourceMapJSON.sections.map(section => {
const sections: Array<Section> = sourceMapJSON.sections.map(section => {
const offset = section.offset;
const offsetLine = offset.line;
const offsetColumn = offset.column;
Expand All @@ -161,7 +171,8 @@ function IndexedSourceMapConsumer(sourceMapJSON: IndexSourceMap) {
// The offset fields are 0-based, but we use 1-based indices when encoding/decoding from VLQ.
generatedLine: offsetLine + 1,
generatedColumn: offsetColumn + 1,
sourceMapConsumer: new SourceMapConsumer(section.map),
map: section.map,
sourceMapConsumer: null,
};
});

Expand Down Expand Up @@ -229,6 +240,11 @@ function IndexedSourceMapConsumer(sourceMapJSON: IndexSourceMap) {
);
}

if (section.sourceMapConsumer === 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: use != instead of === ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, what do you mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean == instead of ===?

In this case, it's always ether null or a SourceMapConsumer instance. (Flow typings ensure this too.)

// Lazily parse the section only when it's needed.
section.sourceMapConsumer = new SourceMapConsumer(section.map);
}

return section.sourceMapConsumer.originalPositionFor({
columnNumber,
lineNumber,
Expand Down