Skip to content

Commit 649c29a

Browse files
author
Brian Vaughn
committed
Deleted old source-map-js integration branch in parseSourceAndMetadata
1 parent aacf091 commit 649c29a

File tree

2 files changed

+15
-299
lines changed

2 files changed

+15
-299
lines changed

packages/react-devtools-shared/src/hooks/astUtils.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export type Position = {|
1818
column: number,
1919
|};
2020

21-
export type SourceConsumer = any;
22-
2321
export type SourceFileASTWithHookDetails = {
2422
sourceFileAST: File,
2523
line: number,

packages/react-devtools-shared/src/hooks/parseHookNames/parseSourceAndMetadata.js

+15-297
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import {parse} from '@babel/parser';
1414
import LRU from 'lru-cache';
15-
import {SourceMapConsumer} from 'source-map-js';
1615
import {getHookName} from '../astUtils';
1716
import {areSourceMapsAppliedToErrors} from '../ErrorTester';
1817
import {__DEBUG__} from 'react-devtools-shared/src/constants';
@@ -31,9 +30,6 @@ import type {
3130
} from './loadSourceAndMetadata';
3231
import type {HookSource} from 'react-debug-tools/src/ReactDebugHooks';
3332
import type {HookNames, LRUCache} from 'react-devtools-shared/src/types';
34-
import type {SourceConsumer} from '../astUtils';
35-
36-
const USE_ALTERNATE_SOURCE_MAP = true;
3733

3834
type AST = mixed;
3935

@@ -57,9 +53,6 @@ type HookParsedMetadata = {|
5753
// Column number in original source code.
5854
originalSourceColumnNumber: number | null,
5955

60-
// APIs from source-map for parsing source maps (if detected).
61-
sourceConsumer: SourceConsumer | null,
62-
6356
// Alternate APIs from source-map for parsing source maps (if detected).
6457
sourceMapConsumer: SourceMapConsumerType | null,
6558
|};
@@ -68,29 +61,13 @@ type LocationKeyToHookParsedMetadata = Map<string, HookParsedMetadata>;
6861

6962
type CachedRuntimeCodeMetadata = {|
7063
metadataConsumer: SourceMapMetadataConsumer | null,
71-
sourceConsumer: SourceConsumer | null,
7264
sourceMapConsumer: SourceMapConsumerType | null,
7365
|};
7466

7567
const runtimeURLToMetadataCache: LRUCache<
7668
string,
7769
CachedRuntimeCodeMetadata,
78-
> = new LRU({
79-
max: 50,
80-
dispose: (runtimeSourceURL: string, metadata: CachedRuntimeCodeMetadata) => {
81-
if (__DEBUG__) {
82-
console.log(
83-
`runtimeURLToMetadataCache.dispose() Evicting cached metadata for "${runtimeSourceURL}"`,
84-
);
85-
}
86-
87-
console.log(`runtimeURLToMetadataCache() dispose of "${runtimeSourceURL}"`);
88-
const sourceConsumer = metadata.sourceConsumer;
89-
if (sourceConsumer !== null) {
90-
sourceConsumer.destroy();
91-
}
92-
},
93-
});
70+
> = new LRU({max: 50});
9471

9572
type CachedSourceCodeMetadata = {|
9673
originalSourceAST: AST,
@@ -121,35 +98,19 @@ export async function parseSourceAndMetadata(
12198
() => initializeHookParsedMetadata(locationKeyToHookSourceAndMetadata),
12299
);
123100

124-
if (USE_ALTERNATE_SOURCE_MAP) {
125-
withSyncPerfMeasurements('parseSourceMapsAlternate', () =>
126-
parseSourceMapsAlternate(
127-
locationKeyToHookSourceAndMetadata,
128-
locationKeyToHookParsedMetadata,
129-
),
130-
);
131-
132-
withSyncPerfMeasurements('parseSourceASTAlternate()', () =>
133-
parseSourceASTAlternate(
134-
locationKeyToHookSourceAndMetadata,
135-
locationKeyToHookParsedMetadata,
136-
),
137-
);
138-
} else {
139-
withSyncPerfMeasurements('parseSourceMaps', () =>
140-
parseSourceMaps(
141-
locationKeyToHookSourceAndMetadata,
142-
locationKeyToHookParsedMetadata,
143-
),
144-
);
101+
withSyncPerfMeasurements('parseSourceMaps', () =>
102+
parseSourceMaps(
103+
locationKeyToHookSourceAndMetadata,
104+
locationKeyToHookParsedMetadata,
105+
),
106+
);
145107

146-
withSyncPerfMeasurements('parseSourceAST()', () =>
147-
parseSourceAST(
148-
locationKeyToHookSourceAndMetadata,
149-
locationKeyToHookParsedMetadata,
150-
),
151-
);
152-
}
108+
withSyncPerfMeasurements('parseSourceAST()', () =>
109+
parseSourceAST(
110+
locationKeyToHookSourceAndMetadata,
111+
locationKeyToHookParsedMetadata,
112+
),
113+
);
153114

154115
return withSyncPerfMeasurements('findHookNames()', () =>
155116
findHookNames(hooksList, locationKeyToHookParsedMetadata),
@@ -245,7 +206,6 @@ function initializeHookParsedMetadata(
245206
originalSourceURL: null,
246207
originalSourceLineNumber: null,
247208
originalSourceColumnNumber: null,
248-
sourceConsumer: null,
249209
sourceMapConsumer: null,
250210
};
251211

@@ -289,194 +249,6 @@ function parseSourceAST(
289249
throw Error('Hook source code location not found.');
290250
}
291251

292-
const {metadataConsumer, sourceConsumer} = hookParsedMetadata;
293-
const runtimeSourceCode = ((hookSourceAndMetadata.runtimeSourceCode: any): string);
294-
295-
let hasHookMap = false;
296-
let originalSourceURL;
297-
let originalSourceCode;
298-
let originalSourceColumnNumber;
299-
let originalSourceLineNumber;
300-
if (areSourceMapsAppliedToErrors() || sourceConsumer == null) {
301-
// Either the current environment automatically applies source maps to errors,
302-
// or the current code had no source map to begin with.
303-
// Either way, we don't need to convert the Error stack frame locations.
304-
originalSourceColumnNumber = columnNumber;
305-
originalSourceLineNumber = lineNumber;
306-
// There's no source map to parse here so we can just parse the original source itself.
307-
originalSourceCode = runtimeSourceCode;
308-
// TODO (named hooks) This mixes runtimeSourceURLs with source mapped URLs in the same cache key space.
309-
// Namespace them?
310-
originalSourceURL = hookSourceAndMetadata.runtimeSourceURL;
311-
} else {
312-
// Parse and extract the AST from the source map.
313-
// Now that the source map has been loaded,
314-
// extract the original source for later.
315-
// TODO (named hooks) Refactor this read, github.com/facebook/react/pull/22181
316-
const {column, line, source} = withSyncPerfMeasurements(
317-
'sourceConsumer.originalPositionFor()',
318-
() =>
319-
sourceConsumer.originalPositionFor({
320-
line: lineNumber,
321-
322-
// Column numbers are represented differently between tools/engines.
323-
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-based.
324-
// For more info see https://github.com/facebook/react/issues/21792#issuecomment-873171991
325-
column: columnNumber - 1,
326-
}),
327-
);
328-
329-
if (source == null) {
330-
// TODO (named hooks) maybe fall back to the runtime source instead of throwing?
331-
throw new Error(
332-
'Could not map hook runtime location to original source location',
333-
);
334-
}
335-
336-
originalSourceColumnNumber = column;
337-
originalSourceLineNumber = line;
338-
// TODO (named hooks) maybe canonicalize this URL somehow?
339-
// It can be relative if the source map specifies it that way,
340-
// but we use it as a cache key across different source maps and there can be collisions.
341-
originalSourceURL = (source: string);
342-
originalSourceCode = withSyncPerfMeasurements(
343-
'sourceConsumer.sourceContentFor()',
344-
() => (sourceConsumer.sourceContentFor(source, true): string),
345-
);
346-
347-
if (__DEBUG__) {
348-
console.groupCollapsed(
349-
`parseSourceAST() Extracted source code from source map for "${originalSourceURL}"`,
350-
);
351-
console.log(originalSourceCode);
352-
console.groupEnd();
353-
}
354-
355-
if (
356-
metadataConsumer != null &&
357-
metadataConsumer.hasHookMap(originalSourceURL)
358-
) {
359-
hasHookMap = true;
360-
}
361-
}
362-
363-
if (__DEBUG__) {
364-
console.log(
365-
`parseSourceAST() mapped line ${lineNumber}->${originalSourceLineNumber} and column ${columnNumber}->${originalSourceColumnNumber}`,
366-
);
367-
}
368-
369-
hookParsedMetadata.originalSourceCode = originalSourceCode;
370-
hookParsedMetadata.originalSourceURL = originalSourceURL;
371-
hookParsedMetadata.originalSourceLineNumber = originalSourceLineNumber;
372-
hookParsedMetadata.originalSourceColumnNumber = originalSourceColumnNumber;
373-
374-
if (hasHookMap) {
375-
if (__DEBUG__) {
376-
console.log(
377-
`parseSourceAST() Found hookMap and skipping parsing for "${originalSourceURL}"`,
378-
);
379-
}
380-
// If there's a hook map present from an extended sourcemap then
381-
// we don't need to parse the source files and instead can use the
382-
// hook map to extract hook names.
383-
return;
384-
}
385-
386-
if (__DEBUG__) {
387-
console.log(
388-
`parseSourceAST() Did not find hook map for "${originalSourceURL}"`,
389-
);
390-
}
391-
392-
// The cache also serves to deduplicate parsing by URL in our loop over location keys.
393-
// This may need to change if we switch to async parsing.
394-
const sourceMetadata = originalURLToMetadataCache.get(originalSourceURL);
395-
if (sourceMetadata != null) {
396-
if (__DEBUG__) {
397-
console.groupCollapsed(
398-
`parseSourceAST() Found cached source metadata for "${originalSourceURL}"`,
399-
);
400-
console.log(sourceMetadata);
401-
console.groupEnd();
402-
}
403-
hookParsedMetadata.originalSourceAST = sourceMetadata.originalSourceAST;
404-
hookParsedMetadata.originalSourceCode =
405-
sourceMetadata.originalSourceCode;
406-
} else {
407-
try {
408-
// TypeScript is the most commonly used typed JS variant so let's default to it
409-
// unless we detect explicit Flow usage via the "@flow" pragma.
410-
const plugin =
411-
originalSourceCode.indexOf('@flow') > 0 ? 'flow' : 'typescript';
412-
413-
// TODO (named hooks) This is probably where we should check max source length,
414-
// rather than in loadSourceAndMetatada -> loadSourceFiles().
415-
// TODO(#22319): Support source files that are html files with inline script tags.
416-
const originalSourceAST = withSyncPerfMeasurements(
417-
'[@babel/parser] parse(originalSourceCode)',
418-
() =>
419-
parse(originalSourceCode, {
420-
sourceType: 'unambiguous',
421-
plugins: ['jsx', plugin],
422-
}),
423-
);
424-
hookParsedMetadata.originalSourceAST = originalSourceAST;
425-
426-
if (__DEBUG__) {
427-
console.log(
428-
`parseSourceAST() Caching source metadata for "${originalSourceURL}"`,
429-
);
430-
}
431-
432-
originalURLToMetadataCache.set(originalSourceURL, {
433-
originalSourceAST,
434-
originalSourceCode,
435-
});
436-
} catch (error) {
437-
throw new Error(
438-
`Failed to parse source file: ${originalSourceURL}\n\n` +
439-
`Original error: ${error}`,
440-
);
441-
}
442-
}
443-
},
444-
);
445-
}
446-
447-
function parseSourceASTAlternate(
448-
locationKeyToHookSourceAndMetadata: LocationKeyToHookSourceAndMetadata,
449-
locationKeyToHookParsedMetadata: LocationKeyToHookParsedMetadata,
450-
): void {
451-
locationKeyToHookSourceAndMetadata.forEach(
452-
(hookSourceAndMetadata, locationKey) => {
453-
const hookParsedMetadata = locationKeyToHookParsedMetadata.get(
454-
locationKey,
455-
);
456-
if (hookParsedMetadata == null) {
457-
throw Error(`Expected to find HookParsedMetadata for "${locationKey}"`);
458-
}
459-
460-
if (hookParsedMetadata.originalSourceAST !== null) {
461-
// Use cached metadata.
462-
return;
463-
}
464-
465-
if (
466-
hookParsedMetadata.originalSourceURL != null &&
467-
hookParsedMetadata.originalSourceCode != null &&
468-
hookParsedMetadata.originalSourceColumnNumber != null &&
469-
hookParsedMetadata.originalSourceLineNumber != null
470-
) {
471-
// Use cached metadata.
472-
return;
473-
}
474-
475-
const {lineNumber, columnNumber} = hookSourceAndMetadata.hookSource;
476-
if (lineNumber == null || columnNumber == null) {
477-
throw Error('Hook source code location not found.');
478-
}
479-
480252
const {metadataConsumer, sourceMapConsumer} = hookParsedMetadata;
481253
const runtimeSourceCode = ((hookSourceAndMetadata.runtimeSourceCode: any): string);
482254
let hasHookMap = false;
@@ -616,59 +388,6 @@ function parseSourceMaps(
616388
throw Error(`Expected to find HookParsedMetadata for "${locationKey}"`);
617389
}
618390

619-
const sourceMapJSON = hookSourceAndMetadata.sourceMapJSON;
620-
621-
if (hookParsedMetadata.sourceConsumer === null) {
622-
if (sourceMapJSON != null) {
623-
hookParsedMetadata.sourceConsumer = withSyncPerfMeasurements(
624-
'new SourceMapConsumer(sourceMapJSON)',
625-
() => new SourceMapConsumer(sourceMapJSON),
626-
);
627-
}
628-
}
629-
630-
if (hookParsedMetadata.metadataConsumer === null) {
631-
if (sourceMapJSON != null) {
632-
hookParsedMetadata.metadataConsumer = withSyncPerfMeasurements(
633-
'new SourceMapMetadataConsumer(sourceMapJSON)',
634-
() => new SourceMapMetadataConsumer(sourceMapJSON),
635-
);
636-
}
637-
}
638-
639-
const runtimeSourceURL = hookSourceAndMetadata.runtimeSourceURL;
640-
641-
// Only set once to avoid triggering eviction/cleanup code.
642-
if (!runtimeURLToMetadataCache.has(runtimeSourceURL)) {
643-
if (__DEBUG__) {
644-
console.log(
645-
`parseSourceMaps() Caching runtime metadata for "${runtimeSourceURL}"`,
646-
);
647-
}
648-
649-
runtimeURLToMetadataCache.set(runtimeSourceURL, {
650-
metadataConsumer: hookParsedMetadata.metadataConsumer,
651-
sourceConsumer: hookParsedMetadata.sourceConsumer,
652-
sourceMapConsumer: null,
653-
});
654-
}
655-
},
656-
);
657-
}
658-
659-
function parseSourceMapsAlternate(
660-
locationKeyToHookSourceAndMetadata: LocationKeyToHookSourceAndMetadata,
661-
locationKeyToHookParsedMetadata: LocationKeyToHookParsedMetadata,
662-
) {
663-
locationKeyToHookSourceAndMetadata.forEach(
664-
(hookSourceAndMetadata, locationKey) => {
665-
const hookParsedMetadata = locationKeyToHookParsedMetadata.get(
666-
locationKey,
667-
);
668-
if (hookParsedMetadata == null) {
669-
throw Error(`Expected to find HookParsedMetadata for "${locationKey}"`);
670-
}
671-
672391
const {runtimeSourceURL, sourceMapJSON} = hookSourceAndMetadata;
673392

674393
// If we've already loaded the source map info for this file,
@@ -684,8 +403,8 @@ function parseSourceMapsAlternate(
684403
}
685404

686405
hookParsedMetadata.metadataConsumer = runtimeMetadata.metadataConsumer;
687-
hookParsedMetadata.sourceConsumer = runtimeMetadata.sourceConsumer;
688-
hookParsedMetadata.sourceMapConsumer = runtimeMetadata.sourceMapConsumer;
406+
hookParsedMetadata.sourceMapConsumer =
407+
runtimeMetadata.sourceMapConsumer;
689408
} else {
690409
if (sourceMapJSON != null) {
691410
const sourceMapConsumer = withSyncPerfMeasurements(
@@ -704,7 +423,6 @@ function parseSourceMapsAlternate(
704423
// Only set once to avoid triggering eviction/cleanup code.
705424
runtimeURLToMetadataCache.set(runtimeSourceURL, {
706425
metadataConsumer: metadataConsumer,
707-
sourceConsumer: null,
708426
sourceMapConsumer: sourceMapConsumer,
709427
});
710428
}

0 commit comments

Comments
 (0)