-
Notifications
You must be signed in to change notification settings - Fork 48.8k
DevTools: Hook names optimizations #22403
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
241 changes: 241 additions & 0 deletions
241
packages/react-devtools-shared/src/hooks/SourceMapConsumer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/** | ||
* 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 {withSyncPerfMeasurements} from 'react-devtools-shared/src/PerformanceLoggingUtils'; | ||
import {decode} from 'sourcemap-codec'; | ||
|
||
import type { | ||
IndexSourceMap, | ||
BasicSourceMap, | ||
MixedSourceMap, | ||
} from './SourceMapTypes'; | ||
|
||
type SearchPosition = {| | ||
columnNumber: number, | ||
lineNumber: number, | ||
|}; | ||
|
||
type ResultPosition = {| | ||
column: number, | ||
line: number, | ||
sourceContent: string, | ||
sourceURL: string, | ||
|}; | ||
|
||
export type SourceMapConsumerType = {| | ||
originalPositionFor: SearchPosition => ResultPosition, | ||
|}; | ||
|
||
type Mappings = Array<Array<Array<number>>>; | ||
|
||
export default function SourceMapConsumer( | ||
sourceMapJSON: MixedSourceMap, | ||
): SourceMapConsumerType { | ||
if (sourceMapJSON.sections != null) { | ||
return IndexedSourceMapConsumer(((sourceMapJSON: any): IndexSourceMap)); | ||
} else { | ||
return BasicSourceMapConsumer(((sourceMapJSON: any): BasicSourceMap)); | ||
} | ||
} | ||
|
||
function BasicSourceMapConsumer(sourceMapJSON: BasicSourceMap) { | ||
const decodedMappings: Mappings = withSyncPerfMeasurements( | ||
'Decoding source map mappings with sourcemap-codec', | ||
() => decode(sourceMapJSON.mappings), | ||
); | ||
|
||
function originalPositionFor({ | ||
columnNumber, | ||
lineNumber, | ||
}: SearchPosition): ResultPosition { | ||
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based. | ||
const targetColumnNumber = columnNumber - 1; | ||
|
||
const lineMappings = decodedMappings[lineNumber - 1]; | ||
|
||
let nearestEntry = null; | ||
|
||
let startIndex = 0; | ||
let stopIndex = lineMappings.length - 1; | ||
let index = -1; | ||
while (startIndex <= stopIndex) { | ||
index = Math.floor((stopIndex + startIndex) / 2); | ||
nearestEntry = lineMappings[index]; | ||
|
||
const currentColumn = nearestEntry[0]; | ||
if (currentColumn === targetColumnNumber) { | ||
break; | ||
} else { | ||
if (currentColumn > targetColumnNumber) { | ||
if (stopIndex - index > 0) { | ||
stopIndex = index; | ||
} else { | ||
index = stopIndex; | ||
break; | ||
} | ||
} else { | ||
if (index - startIndex > 0) { | ||
startIndex = index; | ||
} else { | ||
index = startIndex; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// We have found either the exact element, or the next-closest element. | ||
// However there may be more than one such element. | ||
// Make sure we always return the smallest of these. | ||
while (index > 0) { | ||
const previousEntry = lineMappings[index - 1]; | ||
const currentColumn = previousEntry[0]; | ||
if (currentColumn !== targetColumnNumber) { | ||
break; | ||
} | ||
index--; | ||
} | ||
|
||
if (nearestEntry == null) { | ||
// TODO maybe fall back to the runtime source instead of throwing? | ||
throw Error( | ||
`Could not find runtime location for line:${lineNumber} and column:${columnNumber}`, | ||
); | ||
} | ||
|
||
const sourceIndex = nearestEntry[1]; | ||
const sourceContent = | ||
sourceMapJSON.sourcesContent != null | ||
? sourceMapJSON.sourcesContent[sourceIndex] | ||
: null; | ||
const sourceURL = sourceMapJSON.sources[sourceIndex] ?? null; | ||
const line = nearestEntry[2] + 1; | ||
const column = nearestEntry[3]; | ||
|
||
if (sourceContent === null || sourceURL === null) { | ||
// TODO maybe fall back to the runtime source instead of throwing? | ||
throw Error( | ||
`Could not find original source for line:${lineNumber} and column:${columnNumber}`, | ||
); | ||
} | ||
|
||
return { | ||
column, | ||
line, | ||
sourceContent: ((sourceContent: any): string), | ||
sourceURL: ((sourceURL: any): string), | ||
}; | ||
} | ||
|
||
return (({ | ||
originalPositionFor, | ||
}: any): SourceMapConsumerType); | ||
} | ||
|
||
function IndexedSourceMapConsumer(sourceMapJSON: IndexSourceMap) { | ||
let lastOffset = { | ||
line: -1, | ||
column: 0, | ||
}; | ||
|
||
const sections = sourceMapJSON.sections.map(section => { | ||
const offset = section.offset; | ||
const offsetLine = offset.line; | ||
const offsetColumn = offset.column; | ||
|
||
if ( | ||
offsetLine < lastOffset.line || | ||
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column) | ||
) { | ||
throw new Error('Section offsets must be ordered and non-overlapping.'); | ||
} | ||
|
||
lastOffset = offset; | ||
|
||
return { | ||
// 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), | ||
}; | ||
}); | ||
|
||
function originalPositionFor({ | ||
columnNumber, | ||
lineNumber, | ||
}: SearchPosition): ResultPosition { | ||
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based. | ||
const targetColumnNumber = columnNumber - 1; | ||
|
||
let section = null; | ||
|
||
let startIndex = 0; | ||
let stopIndex = sections.length - 1; | ||
let index = -1; | ||
while (startIndex <= stopIndex) { | ||
index = Math.floor((stopIndex + startIndex) / 2); | ||
section = sections[index]; | ||
|
||
const currentLine = section.generatedLine; | ||
if (currentLine === lineNumber) { | ||
const currentColumn = section.generatedColumn; | ||
if (currentColumn === lineNumber) { | ||
break; | ||
} else { | ||
if (currentColumn > targetColumnNumber) { | ||
if (stopIndex - index > 0) { | ||
stopIndex = index; | ||
} else { | ||
index = stopIndex; | ||
break; | ||
} | ||
} else { | ||
if (index - startIndex > 0) { | ||
startIndex = index; | ||
} else { | ||
index = startIndex; | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
if (currentLine > lineNumber) { | ||
if (stopIndex - index > 0) { | ||
stopIndex = index; | ||
} else { | ||
index = stopIndex; | ||
break; | ||
} | ||
} else { | ||
if (index - startIndex > 0) { | ||
startIndex = index; | ||
} else { | ||
index = startIndex; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (section == null) { | ||
// TODO maybe fall back to the runtime source instead of throwing? | ||
throw Error( | ||
`Could not find matching section for line:${lineNumber} and column:${columnNumber}`, | ||
); | ||
} | ||
|
||
return section.sourceMapConsumer.originalPositionFor({ | ||
columnNumber, | ||
lineNumber, | ||
}); | ||
} | ||
|
||
return (({ | ||
originalPositionFor, | ||
}: any): SourceMapConsumerType); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.