Skip to content

Commit 1f41cae

Browse files
authored
Merge pull request firefox-devtools#363 from jasonLaster/sm-ref2
Additional source-map util refactors
2 parents e4778a1 + ce099a8 commit 1f41cae

2 files changed

Lines changed: 54 additions & 32 deletions

File tree

public/js/actions/sources.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const constants = require("../constants");
1414
const Prefs = require("../prefs");
1515
const invariant = require("invariant");
1616
const { isEnabled } = require("../../../config/feature");
17-
const { setSourceMap, createOriginalSources, getGeneratedSourceId,
17+
const { createOriginalSources, getGeneratedSourceId,
1818
isOriginal, getOriginalSource } = require("../util/source-map");
1919

2020
/**
@@ -85,8 +85,7 @@ function loadSourceMap(generatedSource) {
8585
const sourceMapURL = getSourceMapURL(getState(), generatedSource);
8686
sourceMap = await networkRequest(sourceMapURL);
8787

88-
setSourceMap(generatedSource, sourceMap);
89-
createOriginalSources(generatedSource)
88+
createOriginalSources(generatedSource, sourceMap)
9089
.forEach(s => dispatch(newSource(s)));
9190

9291
return { sourceMap };

public/js/util/source-map.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,21 @@ const { Source } = require("../types");
55
let sourceMapConsumers = new Map();
66
let sourceNodes = new Map();
77

8-
function hasConsumer(sourceId) {
8+
function _hasConsumer(sourceId) {
99
return sourceMapConsumers.has(sourceId);
1010
}
1111

12-
function getConsumer(sourceId) {
12+
function _getConsumer(sourceId) {
1313
return sourceMapConsumers.get(sourceId);
1414
}
1515

16-
function getOriginalSources(sourceId) {
17-
const consumer = getConsumer(sourceId);
16+
function _getOriginalSources(sourceId) {
17+
const consumer = _getConsumer(sourceId);
1818
return consumer.sources;
1919
}
2020

21-
function isOriginal(originalSource) {
22-
return !!getGeneratedSourceId(originalSource);
23-
}
24-
25-
function getGeneratedSourceId(originalSource) {
26-
const match = [...sourceMapConsumers].find(
27-
([x, consumer]) => consumer.sources.includes(originalSource.url)
28-
);
29-
30-
return match ? match[0] : null;
31-
}
32-
33-
function setSourceMap(source, sourceMap) {
34-
if (hasConsumer(source.id)) {
21+
function _setConsumer(source, sourceMap) {
22+
if (_hasConsumer(source.id)) {
3523
return null;
3624
}
3725

@@ -40,32 +28,54 @@ function setSourceMap(source, sourceMap) {
4028
return consumer;
4129
}
4230

43-
function getSourceNode(generatedSourceId, text) {
31+
function _getSourceNode(generatedSourceId, text) {
4432
if (sourceNodes.has(generatedSourceId)) {
4533
return sourceNodes.get(generatedSourceId);
4634
}
4735

48-
const consumer = getConsumer(generatedSourceId);
36+
const consumer = _getConsumer(generatedSourceId);
4937
invariant(consumer, "source map must be present");
5038

5139
const sourceNode = SourceNode.fromStringWithSourceMap(text, consumer);
5240
sourceNodes.set(generatedSourceId, sourceNode);
5341
return sourceNode;
5442
}
5543

56-
function createOriginalSources(generatedSource) {
57-
return getOriginalSources(generatedSource.id)
58-
.map((source, index) => Source({
59-
url: source,
60-
id: generatedSource.id + "/" + index,
61-
isPrettyPrinted: false
62-
}));
44+
function isOriginal(originalSource) {
45+
return !!getGeneratedSourceId(originalSource);
46+
}
47+
48+
function isGenerated(source) {
49+
return [...sourceMapConsumers.keys()].includes(source.id);
50+
}
51+
52+
function getGeneratedSourceLocation(originalSource, originalLocation) {
53+
const generatedSourceId = getGeneratedSourceId(originalSource);
54+
const consumer = _getConsumer(generatedSourceId);
55+
const generatedLocation = consumer.generatedPositionFor({
56+
source: originalSource.url,
57+
line: originalLocation.line,
58+
column: 0
59+
});
60+
61+
return {
62+
sourceId: generatedSourceId,
63+
line: generatedLocation.line
64+
};
65+
}
66+
67+
function getGeneratedSourceId(originalSource) {
68+
const match = [...sourceMapConsumers].find(
69+
([x, consumer]) => consumer.sources.includes(originalSource.url)
70+
);
71+
72+
return match ? match[0] : null;
6373
}
6474

6575
function getOriginalSource(
6676
originalSource, generatedSource, generatedSourceText
6777
) {
68-
const sourceNode = getSourceNode(
78+
const sourceNode = _getSourceNode(
6979
generatedSource.id,
7080
generatedSourceText.text
7181
);
@@ -78,10 +88,23 @@ function getOriginalSource(
7888
};
7989
}
8090

91+
function createOriginalSources(generatedSource, sourceMap) {
92+
if (!_hasConsumer(generatedSource.id)) {
93+
_setConsumer(generatedSource, sourceMap);
94+
}
95+
return _getOriginalSources(generatedSource.id)
96+
.map((source, index) => Source({
97+
url: source,
98+
id: generatedSource.id + "/" + index,
99+
isPrettyPrinted: false
100+
}));
101+
}
102+
81103
module.exports = {
82-
setSourceMap,
83104
getOriginalSource,
105+
getGeneratedSourceLocation,
84106
createOriginalSources,
85107
isOriginal,
108+
isGenerated,
86109
getGeneratedSourceId
87110
};

0 commit comments

Comments
 (0)