@@ -29,6 +29,11 @@ const { setupCoverageHooks } = require('internal/util');
2929const { tmpdir } = require ( 'os' ) ;
3030const { join, resolve, relative, matchesGlob } = require ( 'path' ) ;
3131const { fileURLToPath } = require ( 'internal/url' ) ;
32+ const {
33+ codes : {
34+ ERR_INVALID_SOURCE_MAP ,
35+ } ,
36+ } = require ( 'internal/errors' ) ;
3237const { kMappings, SourceMap } = require ( 'internal/source_map/source_map' ) ;
3338const kCoverageFileRegex = / ^ c o v e r a g e - ( \d + ) - ( \d { 13 } ) - ( \d + ) \. j s o n $ / ;
3439const kIgnoreRegex = / \/ \* n o d e : c o v e r a g e i g n o r e n e x t (?< count > \d + ) ? \* \/ / ;
@@ -347,67 +352,73 @@ class TestCoverage {
347352 newResult . set ( url , script ) ;
348353 continue ;
349354 }
350- const { data, lineLengths } = sourceMapCache [ url ] ;
351- let offset = 0 ;
352- const executedLines = ArrayPrototypeMap ( lineLengths , ( length , i ) => {
353- const coverageLine = new CoverageLine ( i + 1 , offset , null , length + 1 ) ;
354- offset += length + 1 ;
355- return coverageLine ;
356- } ) ;
357- if ( data . sourcesContent != null ) {
358- for ( let j = 0 ; j < data . sources . length ; ++ j ) {
359- this . getLines ( data . sources [ j ] , data . sourcesContent [ j ] ) ;
360- }
361- }
362- const sourceMap = new SourceMap ( data , { __proto__ : null , lineLengths } ) ;
363355
364- for ( let j = 0 ; j < functions . length ; ++ j ) {
365- const { ranges, functionName, isBlockCoverage } = functions [ j ] ;
366- if ( ranges == null ) {
367- continue ;
368- }
369- let newUrl ;
370- const newRanges = [ ] ;
371- for ( let k = 0 ; k < ranges . length ; ++ k ) {
372- const { startOffset, endOffset, count } = ranges [ k ] ;
373- const { lines } = mapRangeToLines ( ranges [ k ] , executedLines ) ;
374-
375- let startEntry = sourceMap
376- . findEntry ( lines [ 0 ] . line - 1 , MathMax ( 0 , startOffset - lines [ 0 ] . startOffset ) ) ;
377- const endEntry = sourceMap
378- . findEntry ( lines [ lines . length - 1 ] . line - 1 , ( endOffset - lines [ lines . length - 1 ] . startOffset ) - 1 ) ;
379- if ( ! startEntry . originalSource && endEntry . originalSource &&
380- lines [ 0 ] . line === 1 && startOffset === 0 && lines [ 0 ] . startOffset === 0 ) {
381- // Edge case when the first line is not mappable
382- const { 2 : originalSource , 3 : originalLine , 4 : originalColumn } = sourceMap [ kMappings ] [ 0 ] ;
383- startEntry = { __proto__ : null , originalSource, originalLine, originalColumn } ;
356+ try {
357+ const { data, lineLengths } = sourceMapCache [ url ] ;
358+ let offset = 0 ;
359+ const executedLines = ArrayPrototypeMap ( lineLengths , ( length , i ) => {
360+ const coverageLine = new CoverageLine ( i + 1 , offset , null , length + 1 ) ;
361+ offset += length + 1 ;
362+ return coverageLine ;
363+ } ) ;
364+ if ( data . sourcesContent != null ) {
365+ for ( let j = 0 ; j < data . sources . length ; ++ j ) {
366+ this . getLines ( data . sources [ j ] , data . sourcesContent [ j ] ) ;
384367 }
368+ }
369+ const sourceMap = new SourceMap ( data , { __proto__ : null , lineLengths } ) ;
385370
386- if ( ! startEntry . originalSource || startEntry . originalSource !== endEntry . originalSource ) {
387- // The range is not mappable. Skip it.
371+ for ( let j = 0 ; j < functions . length ; ++ j ) {
372+ const { ranges, functionName, isBlockCoverage } = functions [ j ] ;
373+ if ( ranges == null ) {
388374 continue ;
389375 }
376+ let newUrl ;
377+ const newRanges = [ ] ;
378+ for ( let k = 0 ; k < ranges . length ; ++ k ) {
379+ const { startOffset, endOffset, count } = ranges [ k ] ;
380+ const { lines } = mapRangeToLines ( ranges [ k ] , executedLines ) ;
381+
382+ let startEntry = sourceMap
383+ . findEntry ( lines [ 0 ] . line - 1 , MathMax ( 0 , startOffset - lines [ 0 ] . startOffset ) ) ;
384+ const endEntry = sourceMap
385+ . findEntry ( lines [ lines . length - 1 ] . line - 1 , ( endOffset - lines [ lines . length - 1 ] . startOffset ) - 1 ) ;
386+ if ( ! startEntry . originalSource && endEntry . originalSource &&
387+ lines [ 0 ] . line === 1 && startOffset === 0 && lines [ 0 ] . startOffset === 0 ) {
388+ // Edge case when the first line is not mappable
389+ const { 2 : originalSource , 3 : originalLine , 4 : originalColumn } = sourceMap [ kMappings ] [ 0 ] ;
390+ startEntry = { __proto__ : null , originalSource, originalLine, originalColumn } ;
391+ }
390392
391- newUrl ??= startEntry ?. originalSource ;
392- const mappedLines = this . getLines ( newUrl ) ;
393- const mappedStartOffset = this . entryToOffset ( startEntry , mappedLines ) ;
394- const mappedEndOffset = this . entryToOffset ( endEntry , mappedLines ) + 1 ;
395- for ( let l = startEntry . originalLine ; l <= endEntry . originalLine ; l ++ ) {
396- mappedLines [ l ] . count = count ;
397- }
393+ if ( ! startEntry . originalSource || startEntry . originalSource !== endEntry . originalSource ) {
394+ // The range is not mappable. Skip it.
395+ continue ;
396+ }
398397
399- ArrayPrototypePush ( newRanges , {
400- __proto__ : null , startOffset : mappedStartOffset , endOffset : mappedEndOffset , count,
401- } ) ;
402- }
398+ newUrl ??= startEntry ?. originalSource ;
399+ const mappedLines = this . getLines ( newUrl ) ;
400+ const mappedStartOffset = this . entryToOffset ( startEntry , mappedLines ) ;
401+ const mappedEndOffset = this . entryToOffset ( endEntry , mappedLines ) + 1 ;
402+ for ( let l = startEntry . originalLine ; l <= endEntry . originalLine ; l ++ ) {
403+ mappedLines [ l ] . count = count ;
404+ }
403405
404- if ( ! newUrl ) {
405- // No mappable ranges. Skip the function.
406- continue ;
406+ ArrayPrototypePush ( newRanges , {
407+ __proto__ : null , startOffset : mappedStartOffset , endOffset : mappedEndOffset , count,
408+ } ) ;
409+ }
410+
411+ if ( ! newUrl ) {
412+ // No mappable ranges. Skip the function.
413+ continue ;
414+ }
415+ const newScript = newResult . get ( newUrl ) ?? { __proto__ : null , url : newUrl , functions : [ ] } ;
416+ ArrayPrototypePush ( newScript . functions ,
417+ { __proto__ : null , functionName, ranges : newRanges , isBlockCoverage } ) ;
418+ newResult . set ( newUrl , newScript ) ;
407419 }
408- const newScript = newResult . get ( newUrl ) ?? { __proto__ : null , url : newUrl , functions : [ ] } ;
409- ArrayPrototypePush ( newScript . functions , { __proto__ : null , functionName, ranges : newRanges , isBlockCoverage } ) ;
410- newResult . set ( newUrl , newScript ) ;
420+ } catch {
421+ throw new ERR_INVALID_SOURCE_MAP ( url ) ;
411422 }
412423 }
413424
0 commit comments