Skip to content

Commit

Permalink
Consistently return mutable position objects from source map consumer
Browse files Browse the repository at this point in the history
Summary:
Previously, `SectionsConsumer` would reuse the frozen `EMPTY_POSITION` object when querying positions that don't belong to any section. With this diff we'll clone it instead.

There is an `originalPositionFor` call site in `metro-symbolicate` which relies on adding properties to the position object, which is possible in the `source-map` API we're emulating, so here we explicitly make this supported.

Reviewed By: MichaReiser

Differential Revision: D26166651

fbshipit-source-id: ee1e0aaefbbb59a0fd5f6bf24d94ffe11a3a84d6
  • Loading branch information
motiz88 authored and facebook-github-bot committed Feb 2, 2021
1 parent 2cc5aa8 commit fab9af9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/metro-source-map/src/Consumer/SectionsConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SectionsConsumer extends AbstractConsumer implements IConsumer {
const [generatedOffset, consumer] =
this._consumerForPosition(generatedPosition) || [];
if (!consumer) {
return EMPTY_POSITION;
return {...EMPTY_POSITION};
}
return consumer.originalPositionFor(
subtractOffsetFromPosition(generatedPosition, generatedOffset),
Expand Down
8 changes: 4 additions & 4 deletions packages/metro-source-map/src/Consumer/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import type {Number0, Number1} from 'ob1';
export type {IterationOrder, LookupBias};
export type GeneratedOffset = {|+lines: Number0, +columns: Number0|};
export type SourcePosition = {
+source: ?string,
+line: ?Number1,
+column: ?Number0,
+name: ?string,
source: ?string,
line: ?Number1,
column: ?Number0,
name: ?string,
...
};
export type GeneratedPosition = {
Expand Down
44 changes: 44 additions & 0 deletions packages/metro-source-map/src/__tests__/Consumer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ describe('basic maps', () => {
`);
});

test('empty position is a mutable object', () => {
const consumer = new Consumer({
version: 3,
mappings: '',
names: [],
sources: [],
});
const empty1 = consumer.originalPositionFor({
line: add1(0),
column: add0(0),
});
const empty2 = consumer.originalPositionFor({
line: add1(0),
column: add0(0),
});
expect(empty1).not.toBe(empty2);
expect(() => {
empty1.name = 'foo';
// $FlowIgnore[prop-missing]
empty1.someProp = 'bar';
}).not.toThrow();
});

test('single full mapping', () => {
const consumer = new Consumer({
version: 3,
Expand Down Expand Up @@ -259,6 +282,27 @@ describe('indexed (sectioned) maps', () => {
`);
});

test('empty position is a mutable object', () => {
const consumer = new Consumer({
version: 3,
sections: [],
});
const empty1 = consumer.originalPositionFor({
line: add1(0),
column: add0(0),
});
const empty2 = consumer.originalPositionFor({
line: add1(0),
column: add0(0),
});
expect(empty1).not.toBe(empty2);
expect(() => {
empty1.name = 'foo';
// $FlowIgnore[prop-missing]
empty1.someProp = 'bar';
}).not.toThrow();
});

test('section per column', () => {
const consumer = new Consumer({
version: 3,
Expand Down

0 comments on commit fab9af9

Please sign in to comment.