@@ -32,7 +32,8 @@ const streamChunksOfSourceMapFull = (
3232 onName ,
3333) => {
3434 const lines = splitIntoLines ( source ) ;
35- if ( lines . length === 0 ) {
35+ const linesLength = lines . length ;
36+ if ( linesLength === 0 ) {
3637 return {
3738 generatedLine : 1 ,
3839 generatedColumn : 0 ,
@@ -52,9 +53,9 @@ const streamChunksOfSourceMapFull = (
5253 }
5354 }
5455
55- const lastLine = lines [ lines . length - 1 ] ;
56+ const lastLine = lines [ linesLength - 1 ] ;
5657 const lastNewLine = lastLine . endsWith ( "\n" ) ;
57- const finalLine = lastNewLine ? lines . length + 1 : lines . length ;
58+ const finalLine = lastNewLine ? linesLength + 1 : linesLength ;
5859 const finalColumn = lastNewLine ? 0 : lastLine . length ;
5960
6061 let currentGeneratedLine = 1 ;
@@ -83,7 +84,7 @@ const streamChunksOfSourceMapFull = (
8384 originalColumn ,
8485 nameIndex ,
8586 ) => {
86- if ( mappingActive && currentGeneratedLine <= lines . length ) {
87+ if ( mappingActive && currentGeneratedLine <= linesLength ) {
8788 let chunk ;
8889 const mappingLine = currentGeneratedLine ;
8990 const mappingColumn = currentGeneratedColumn ;
@@ -110,7 +111,7 @@ const streamChunksOfSourceMapFull = (
110111 mappingActive = false ;
111112 }
112113 if ( generatedLine > currentGeneratedLine && currentGeneratedColumn > 0 ) {
113- if ( currentGeneratedLine <= lines . length ) {
114+ if ( currentGeneratedLine <= linesLength ) {
114115 const chunk = lines [ currentGeneratedLine - 1 ] . slice (
115116 currentGeneratedColumn ,
116117 ) ;
@@ -127,22 +128,29 @@ const streamChunksOfSourceMapFull = (
127128 currentGeneratedLine ++ ;
128129 currentGeneratedColumn = 0 ;
129130 }
130- while ( generatedLine > currentGeneratedLine ) {
131- if ( currentGeneratedLine <= lines . length ) {
132- onChunk (
133- lines [ currentGeneratedLine - 1 ] ,
134- currentGeneratedLine ,
135- 0 ,
136- - 1 ,
137- - 1 ,
138- - 1 ,
139- - 1 ,
140- ) ;
141- }
131+ // Emit each fully-passed generated line. Once we move past the last
132+ // available line we stop emitting, but still need to advance the
133+ // counter to `generatedLine` so subsequent state matches the caller.
134+ while (
135+ generatedLine > currentGeneratedLine &&
136+ currentGeneratedLine <= linesLength
137+ ) {
138+ onChunk (
139+ lines [ currentGeneratedLine - 1 ] ,
140+ currentGeneratedLine ,
141+ 0 ,
142+ - 1 ,
143+ - 1 ,
144+ - 1 ,
145+ - 1 ,
146+ ) ;
142147 currentGeneratedLine ++ ;
143148 }
149+ if ( currentGeneratedLine < generatedLine ) {
150+ currentGeneratedLine = generatedLine ;
151+ }
144152 if ( generatedColumn > currentGeneratedColumn ) {
145- if ( currentGeneratedLine <= lines . length ) {
153+ if ( currentGeneratedLine <= linesLength ) {
146154 const chunk = lines [ currentGeneratedLine - 1 ] . slice (
147155 currentGeneratedColumn ,
148156 generatedColumn ,
@@ -195,7 +203,8 @@ const streamChunksOfSourceMapLinesFull = (
195203 _onName ,
196204) => {
197205 const lines = splitIntoLines ( source ) ;
198- if ( lines . length === 0 ) {
206+ const linesLength = lines . length ;
207+ if ( linesLength === 0 ) {
199208 return {
200209 generatedLine : 1 ,
201210 generatedColumn : 0 ,
@@ -232,39 +241,38 @@ const streamChunksOfSourceMapLinesFull = (
232241 if (
233242 sourceIndex < 0 ||
234243 generatedLine < currentGeneratedLine ||
235- generatedLine > lines . length
244+ generatedLine > linesLength
236245 ) {
237246 return ;
238247 }
248+ // `generatedLine <= linesLength` is guaranteed by the guard above, so
249+ // every line we iterate over is in bounds — no per-iteration length
250+ // check needed.
239251 while ( generatedLine > currentGeneratedLine ) {
240- if ( currentGeneratedLine <= lines . length ) {
241- onChunk (
242- lines [ currentGeneratedLine - 1 ] ,
243- currentGeneratedLine ,
244- 0 ,
245- - 1 ,
246- - 1 ,
247- - 1 ,
248- - 1 ,
249- ) ;
250- }
251- currentGeneratedLine ++ ;
252- }
253- if ( generatedLine <= lines . length ) {
254252 onChunk (
255- lines [ generatedLine - 1 ] ,
256- generatedLine ,
253+ lines [ currentGeneratedLine - 1 ] ,
254+ currentGeneratedLine ,
257255 0 ,
258- sourceIndex ,
259- originalLine ,
260- originalColumn ,
256+ - 1 ,
257+ - 1 ,
258+ - 1 ,
261259 - 1 ,
262260 ) ;
263261 currentGeneratedLine ++ ;
264262 }
263+ onChunk (
264+ lines [ generatedLine - 1 ] ,
265+ generatedLine ,
266+ 0 ,
267+ sourceIndex ,
268+ originalLine ,
269+ originalColumn ,
270+ - 1 ,
271+ ) ;
272+ currentGeneratedLine ++ ;
265273 } ;
266274 readMappings ( mappings , onMapping ) ;
267- for ( ; currentGeneratedLine <= lines . length ; currentGeneratedLine ++ ) {
275+ for ( ; currentGeneratedLine <= linesLength ; currentGeneratedLine ++ ) {
268276 onChunk (
269277 lines [ currentGeneratedLine - 1 ] ,
270278 currentGeneratedLine ,
@@ -276,10 +284,10 @@ const streamChunksOfSourceMapLinesFull = (
276284 ) ;
277285 }
278286
279- const lastLine = lines [ lines . length - 1 ] ;
287+ const lastLine = lines [ linesLength - 1 ] ;
280288 const lastNewLine = lastLine . endsWith ( "\n" ) ;
281289
282- const finalLine = lastNewLine ? lines . length + 1 : lines . length ;
290+ const finalLine = lastNewLine ? linesLength + 1 : linesLength ;
283291 const finalColumn = lastNewLine ? 0 : lastLine . length ;
284292
285293 return {
0 commit comments