Skip to content
Merged
Show file tree
Hide file tree
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
62 changes: 61 additions & 1 deletion packages/metro-source-map/src/Consumer/SectionsConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @oncall react_native
*/

import type {IndexMap} from '../source-map';
import type {BasicSourceMap, IndexMap} from '../source-map';
import type {
GeneratedOffset,
GeneratedPositionLookup,
Expand All @@ -21,6 +21,7 @@ import type {
import AbstractConsumer from './AbstractConsumer';
import {EMPTY_POSITION, FIRST_COLUMN, FIRST_LINE} from './constants';
import createConsumer from './createConsumer';
import normalizeSourcePath from './normalizeSourcePath';
import {subtractOffsetFromPosition} from './positionMath';
import {greatestLowerBound} from './search';
import {add, add0, get0, get1, sub, sub1} from 'ob1';
Expand All @@ -34,9 +35,13 @@ export default class SectionsConsumer
implements IConsumer
{
_consumers: ReadonlyArray<[GeneratedOffset, IConsumer]>;
_basicMaps: ?ReadonlyArray<BasicSourceMap>;
_contentBySource: ?ReadonlyMap<string, string>;

constructor(sourceMap: IndexMap) {
super(sourceMap);
this._basicMaps = getBasicMaps(sourceMap);
this._contentBySource = null;
this._consumers = sourceMap.sections.map((section, index) => {
const generatedOffset = {
lines: add0(section.offset.line),
Expand Down Expand Up @@ -113,6 +118,10 @@ export default class SectionsConsumer
}

sourceContentFor(source: string, nullOnMissing: true): ?string {
const contentBySource = this._buildAndCacheContentBySource();
if (contentBySource) {
return contentBySource.get(normalizeSourcePath(source, {})) ?? null;
}
for (const [_, consumer] of this._consumers) {
const content = consumer.sourceContentFor(source, nullOnMissing);
if (content != null) {
Expand All @@ -121,4 +130,55 @@ export default class SectionsConsumer
}
return null;
}

_buildAndCacheContentBySource(): ?ReadonlyMap<string, string> {
if (!this._contentBySource && this._basicMaps) {
this._contentBySource = buildContentBySource(this._basicMaps);
}
return this._contentBySource;
}
}

function getBasicMaps(
sourceMap: IndexMap,
basicMaps: Array<BasicSourceMap> = [],
): ?Array<BasicSourceMap> {
for (const {map} of sourceMap.sections) {
// eslint-disable-next-line lint/strictly-null
if (map.mappings === undefined) {
if (getBasicMaps(map, basicMaps) == null) {
return null;
}
} else if (map.sourceRoot != null) {
return null;
} else {
basicMaps.push(map);
}
}
return basicMaps;
}

function buildContentBySource(
basicMaps: ReadonlyArray<BasicSourceMap>,
): Map<string, string> {
const contentBySource = new Map<string, string>();
for (const map of basicMaps) {
const {sources, sourcesContent} = map;
if (sourcesContent == null) {
continue;
}
const seen = new Set<string>();
for (const [i, source] of sources.entries()) {
const normalized = normalizeSourcePath(source, map);
if (seen.has(normalized)) {
continue;
}
seen.add(normalized);
const content = sourcesContent[i];
if (content != null && !contentBySource.has(normalized)) {
contentBySource.set(normalized, content);
}
}
}
return contentBySource;
}
163 changes: 163 additions & 0 deletions packages/metro-source-map/src/__tests__/Consumer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,169 @@ describe('indexed (sectioned) maps', () => {
});
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
});

test('uses the first section with content', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js'],
sourcesContent: ['content in section 0'],
},
},
{
offset: {line: 1, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['./a.js'],
sourcesContent: ['content in section 1'],
},
},
],
});
expect(consumer.sourceContentFor('a.js', true)).toBe(
'content in section 0',
);
});

test('skips sections without sourcesContent', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {version: 3, mappings: '', names: [], sources: ['a.js']},
},
{
offset: {line: 1, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js'],
sourcesContent: ['content of a.js'],
},
},
],
});
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
expect(consumer.sourceContentFor('missing.js', true)).toBeNull();
});

test('uses the first entry for a source repeated within a section', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js', './a.js'],
sourcesContent: [null, 'second entry in section 1'],
},
},
{
offset: {line: 1, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js'],
sourcesContent: ['content of a.js'],
},
},
],
});
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
});

test('normalizes the requested source', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js'],
sourcesContent: ['content of a.js'],
},
},
],
});
expect(consumer.sourceContentFor('./a.js', true)).toBe('content of a.js');
});

test('found in a map with a sourceRoot section', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['b.js'],
sourcesContent: ['content of b.js'],
},
},
{
offset: {line: 1, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sourceRoot: '/root',
sources: ['/root/a.js'],
sourcesContent: ['content of a.js'],
},
},
],
});
expect(consumer.sourceContentFor('/root/a.js', true)).toBe(
'content of a.js',
);
expect(consumer.sourceContentFor('b.js', true)).toBe('content of b.js');
});

test('found in a nested section', () => {
const consumer = new Consumer({
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
sections: [
{
offset: {line: 0, column: 0},
map: {
version: 3,
mappings: '',
names: [],
sources: ['a.js'],
sourcesContent: ['content of a.js'],
},
},
],
},
},
],
});
expect(consumer.sourceContentFor('a.js', true)).toBe('content of a.js');
});
});
});

Expand Down
Loading