@@ -5,33 +5,21 @@ const { Source } = require("../types");
55let sourceMapConsumers = new Map ( ) ;
66let 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
6575function 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+
81103module . exports = {
82- setSourceMap,
83104 getOriginalSource,
105+ getGeneratedSourceLocation,
84106 createOriginalSources,
85107 isOriginal,
108+ isGenerated,
86109 getGeneratedSourceId
87110} ;
0 commit comments