Skip to content

Commit 47ba8a5

Browse files
yhparkrobhogan
andauthored
metro-source-map: Look up source content in indexed maps without querying every section (#2250)
* SectionsConsumer: Look up source content without querying every section * Test that the first section with content for a source wins --------- Co-authored-by: Rob Hogan <2590098+robhogan@users.noreply.github.com>
1 parent ae7428c commit 47ba8a5

2 files changed

Lines changed: 224 additions & 1 deletion

File tree

‎packages/metro-source-map/src/Consumer/SectionsConsumer.js‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @oncall react_native
1010
*/
1111

12-
import type {IndexMap} from '../source-map';
12+
import type {BasicSourceMap, IndexMap} from '../source-map';
1313
import type {
1414
GeneratedOffset,
1515
GeneratedPositionLookup,
@@ -21,6 +21,7 @@ import type {
2121
import AbstractConsumer from './AbstractConsumer';
2222
import {EMPTY_POSITION, FIRST_COLUMN, FIRST_LINE} from './constants';
2323
import createConsumer from './createConsumer';
24+
import normalizeSourcePath from './normalizeSourcePath';
2425
import {subtractOffsetFromPosition} from './positionMath';
2526
import {greatestLowerBound} from './search';
2627
import {add, add0, get0, get1, sub, sub1} from 'ob1';
@@ -34,9 +35,13 @@ export default class SectionsConsumer
3435
implements IConsumer
3536
{
3637
_consumers: ReadonlyArray<[GeneratedOffset, IConsumer]>;
38+
_basicMaps: ?ReadonlyArray<BasicSourceMap>;
39+
_contentBySource: ?ReadonlyMap<string, string>;
3740

3841
constructor(sourceMap: IndexMap) {
3942
super(sourceMap);
43+
this._basicMaps = getBasicMaps(sourceMap);
44+
this._contentBySource = null;
4045
this._consumers = sourceMap.sections.map((section, index) => {
4146
const generatedOffset = {
4247
lines: add0(section.offset.line),
@@ -113,6 +118,10 @@ export default class SectionsConsumer
113118
}
114119

115120
sourceContentFor(source: string, nullOnMissing: true): ?string {
121+
const contentBySource = this._buildAndCacheContentBySource();
122+
if (contentBySource) {
123+
return contentBySource.get(normalizeSourcePath(source, {})) ?? null;
124+
}
116125
for (const [_, consumer] of this._consumers) {
117126
const content = consumer.sourceContentFor(source, nullOnMissing);
118127
if (content != null) {
@@ -121,4 +130,55 @@ export default class SectionsConsumer
121130
}
122131
return null;
123132
}
133+
134+
_buildAndCacheContentBySource(): ?ReadonlyMap<string, string> {
135+
if (!this._contentBySource && this._basicMaps) {
136+
this._contentBySource = buildContentBySource(this._basicMaps);
137+
}
138+
return this._contentBySource;
139+
}
140+
}
141+
142+
function getBasicMaps(
143+
sourceMap: IndexMap,
144+
basicMaps: Array<BasicSourceMap> = [],
145+
): ?Array<BasicSourceMap> {
146+
for (const {map} of sourceMap.sections) {
147+
// eslint-disable-next-line lint/strictly-null
148+
if (map.mappings === undefined) {
149+
if (getBasicMaps(map, basicMaps) == null) {
150+
return null;
151+
}
152+
} else if (map.sourceRoot != null) {
153+
return null;
154+
} else {
155+
basicMaps.push(map);
156+
}
157+
}
158+
return basicMaps;
159+
}
160+
161+
function buildContentBySource(
162+
basicMaps: ReadonlyArray<BasicSourceMap>,
163+
): Map<string, string> {
164+
const contentBySource = new Map<string, string>();
165+
for (const map of basicMaps) {
166+
const {sources, sourcesContent} = map;
167+
if (sourcesContent == null) {
168+
continue;
169+
}
170+
const seen = new Set<string>();
171+
for (const [i, source] of sources.entries()) {
172+
const normalized = normalizeSourcePath(source, map);
173+
if (seen.has(normalized)) {
174+
continue;
175+
}
176+
seen.add(normalized);
177+
const content = sourcesContent[i];
178+
if (content != null && !contentBySource.has(normalized)) {
179+
contentBySource.set(normalized, content);
180+
}
181+
}
182+
}
183+
return contentBySource;
124184
}

‎packages/metro-source-map/src/__tests__/Consumer-test.js‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,169 @@ describe('indexed (sectioned) maps', () => {
710710
});
711711
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
712712
});
713+
714+
test('uses the first section with content', () => {
715+
const consumer = new Consumer({
716+
version: 3,
717+
sections: [
718+
{
719+
offset: {line: 0, column: 0},
720+
map: {
721+
version: 3,
722+
mappings: '',
723+
names: [],
724+
sources: ['a.js'],
725+
sourcesContent: ['content in section 0'],
726+
},
727+
},
728+
{
729+
offset: {line: 1, column: 0},
730+
map: {
731+
version: 3,
732+
mappings: '',
733+
names: [],
734+
sources: ['./a.js'],
735+
sourcesContent: ['content in section 1'],
736+
},
737+
},
738+
],
739+
});
740+
expect(consumer.sourceContentFor('a.js', true)).toBe(
741+
'content in section 0',
742+
);
743+
});
744+
745+
test('skips sections without sourcesContent', () => {
746+
const consumer = new Consumer({
747+
version: 3,
748+
sections: [
749+
{
750+
offset: {line: 0, column: 0},
751+
map: {version: 3, mappings: '', names: [], sources: ['a.js']},
752+
},
753+
{
754+
offset: {line: 1, column: 0},
755+
map: {
756+
version: 3,
757+
mappings: '',
758+
names: [],
759+
sources: ['a.js'],
760+
sourcesContent: ['content of a.js'],
761+
},
762+
},
763+
],
764+
});
765+
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
766+
expect(consumer.sourceContentFor('missing.js', true)).toBeNull();
767+
});
768+
769+
test('uses the first entry for a source repeated within a section', () => {
770+
const consumer = new Consumer({
771+
version: 3,
772+
sections: [
773+
{
774+
offset: {line: 0, column: 0},
775+
map: {
776+
version: 3,
777+
mappings: '',
778+
names: [],
779+
sources: ['a.js', './a.js'],
780+
sourcesContent: [null, 'second entry in section 1'],
781+
},
782+
},
783+
{
784+
offset: {line: 1, column: 0},
785+
map: {
786+
version: 3,
787+
mappings: '',
788+
names: [],
789+
sources: ['a.js'],
790+
sourcesContent: ['content of a.js'],
791+
},
792+
},
793+
],
794+
});
795+
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
796+
});
797+
798+
test('normalizes the requested source', () => {
799+
const consumer = new Consumer({
800+
version: 3,
801+
sections: [
802+
{
803+
offset: {line: 0, column: 0},
804+
map: {
805+
version: 3,
806+
mappings: '',
807+
names: [],
808+
sources: ['a.js'],
809+
sourcesContent: ['content of a.js'],
810+
},
811+
},
812+
],
813+
});
814+
expect(consumer.sourceContentFor('./a.js', true)).toBe('content of a.js');
815+
});
816+
817+
test('found in a map with a sourceRoot section', () => {
818+
const consumer = new Consumer({
819+
version: 3,
820+
sections: [
821+
{
822+
offset: {line: 0, column: 0},
823+
map: {
824+
version: 3,
825+
mappings: '',
826+
names: [],
827+
sources: ['b.js'],
828+
sourcesContent: ['content of b.js'],
829+
},
830+
},
831+
{
832+
offset: {line: 1, column: 0},
833+
map: {
834+
version: 3,
835+
mappings: '',
836+
names: [],
837+
sourceRoot: '/root',
838+
sources: ['/root/a.js'],
839+
sourcesContent: ['content of a.js'],
840+
},
841+
},
842+
],
843+
});
844+
expect(consumer.sourceContentFor('/root/a.js', true)).toBe(
845+
'content of a.js',
846+
);
847+
expect(consumer.sourceContentFor('b.js', true)).toBe('content of b.js');
848+
});
849+
850+
test('found in a nested section', () => {
851+
const consumer = new Consumer({
852+
version: 3,
853+
sections: [
854+
{
855+
offset: {line: 0, column: 0},
856+
map: {
857+
version: 3,
858+
sections: [
859+
{
860+
offset: {line: 0, column: 0},
861+
map: {
862+
version: 3,
863+
mappings: '',
864+
names: [],
865+
sources: ['a.js'],
866+
sourcesContent: ['content of a.js'],
867+
},
868+
},
869+
],
870+
},
871+
},
872+
],
873+
});
874+
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
875+
});
713876
});
714877
});
715878

0 commit comments

Comments
 (0)