@@ -4,6 +4,12 @@ import { getSourceMap } from './getSourceMap';
4
4
import { getLinesAround } from './getLinesAround' ;
5
5
import path from 'path' ;
6
6
7
+ function count ( search : string , string : string ) : number {
8
+ let count = - 1 , index = 0 ;
9
+ for ( ; index !== - 1 ; count ++ , ( index = string . indexOf ( search , index + 1 ) ) ) ;
10
+ return count ;
11
+ }
12
+
7
13
/**
8
14
* Turns a set of mapped <code>StackFrame</code>s back into their generated code position and enhances them with code.
9
15
* @param {string } fileUri The URI of the <code>bundle.js</code> file.
@@ -39,28 +45,22 @@ async function unmap(
39
45
return frame ;
40
46
}
41
47
const fN : string = fileName ;
42
- const splitCache1 : any = { } , splitCache2 : any = { } , splitCache3 : any = { } ;
43
48
const source = map
44
49
. getSources ( )
45
- . map ( s => s . replace ( / [ \\ ] + / g, '/' ) )
46
- . filter ( s => {
47
- s = path . normalize ( s ) ;
48
- return s . indexOf ( fN ) === s . length - fN . length ;
49
- } )
50
- . sort ( ( a , b ) => {
51
- let a2 = splitCache1 [ a ] || ( splitCache1 [ a ] = a . split ( path . sep ) ) ,
52
- b2 = splitCache1 [ b ] || ( splitCache1 [ b ] = b . split ( path . sep ) ) ;
53
- return Math . sign ( a2 . length - b2 . length ) ;
54
- } )
55
- . sort ( ( a , b ) => {
56
- let a2 = splitCache2 [ a ] || ( splitCache2 [ a ] = a . split ( 'node_modules' ) ) ,
57
- b2 = splitCache2 [ b ] || ( splitCache2 [ b ] = b . split ( 'node_modules' ) ) ;
58
- return Math . sign ( a2 . length - b2 . length ) ;
50
+ . map ( s => path . normalize ( s . replace ( / [ \\ ] + / g, '/' ) ) )
51
+ . filter ( p => {
52
+ const i = p . lastIndexOf ( fN ) ;
53
+ return i !== - 1 && i === p . length - fN . length ;
59
54
} )
55
+ . map ( p => ( {
56
+ token : p ,
57
+ seps : count ( path . sep , p ) ,
58
+ penalties : count ( 'node_modules' , p ) + count ( '~' , p ) ,
59
+ } ) )
60
60
. sort ( ( a , b ) => {
61
- let a2 = splitCache3 [ a ] || ( splitCache3 [ a ] = a . split ( '~' ) ) ,
62
- b2 = splitCache3 [ b ] || ( splitCache3 [ b ] = b . split ( '~' ) ) ;
63
- return Math . sign ( a2 . length - b2 . length ) ;
61
+ const s = Math . sign ( a . seps - b . seps ) ;
62
+ if ( s !== 0 ) return s ;
63
+ return Math . sign ( a . penalties - b . penalties ) ;
64
64
} ) ;
65
65
if ( source . length < 1 || lineNumber == null ) {
66
66
return new StackFrame (
@@ -76,13 +76,14 @@ async function unmap(
76
76
null
77
77
) ;
78
78
}
79
+ const sourceT = source [ 0 ] . token ;
79
80
const { line, column } = map . getGeneratedPosition (
80
- source [ 0 ] ,
81
+ sourceT ,
81
82
lineNumber ,
82
83
// $FlowFixMe
83
84
columnNumber
84
85
) ;
85
- const originalSource = map . getSource ( source [ 0 ] ) ;
86
+ const originalSource = map . getSource ( sourceT ) ;
86
87
return new StackFrame (
87
88
functionName ,
88
89
fileUri ,
0 commit comments