1
- import { lineBreakG } from "acorn" ;
1
+ const lineBreakG = / \r \n ? | \n | \u2028 | \u2029 / g;
2
+
3
+ interface Edit {
4
+ start : number ;
5
+ end : number ;
6
+ value : string ;
7
+ }
8
+
9
+ export interface Position {
10
+ line : number ;
11
+ column : number ;
12
+ }
2
13
3
14
export class Sourcemap {
15
+ private readonly _input : string ;
16
+ private readonly _edits : Edit [ ] ;
4
17
constructor ( input = "" ) {
5
18
this . _input = input ;
6
19
this . _edits = [ ] ;
7
20
}
8
- _bisectLeft ( index ) {
21
+ private _bisectLeft ( index : number ) : number {
9
22
let lo = 0 ;
10
23
let hi = this . _edits . length ;
11
24
while ( lo < hi ) {
@@ -15,7 +28,7 @@ export class Sourcemap {
15
28
}
16
29
return lo ;
17
30
}
18
- _bisectRight ( index ) {
31
+ private _bisectRight ( index : number ) : number {
19
32
let lo = 0 ;
20
33
let hi = this . _edits . length ;
21
34
while ( lo < hi ) {
@@ -25,25 +38,25 @@ export class Sourcemap {
25
38
}
26
39
return lo ;
27
40
}
28
- insertLeft ( index , value ) {
41
+ insertLeft ( index : number , value : string ) : void {
29
42
this . replaceLeft ( index , index , value ) ;
30
43
}
31
- insertRight ( index , value ) {
44
+ insertRight ( index : number , value : string ) : void {
32
45
this . replaceRight ( index , index , value ) ;
33
46
}
34
- delete ( start , end ) {
47
+ delete ( start : number , end : number ) : void {
35
48
this . replaceRight ( start , end , "" ) ;
36
49
}
37
- replaceLeft ( start , end , value ) {
50
+ replaceLeft ( start : number , end : number , value : string ) : void {
38
51
this . _edits . splice ( this . _bisectLeft ( start ) , 0 , { start, end, value} ) ;
39
52
}
40
- replaceRight ( start , end , value ) {
53
+ replaceRight ( start : number , end : number , value : string ) : void {
41
54
this . _edits . splice ( this . _bisectRight ( start ) , 0 , { start, end, value} ) ;
42
55
}
43
- translate ( position ) {
56
+ translate ( position : Position ) : Position {
44
57
let index = 0 ;
45
- let ci = { line : 1 , column : 0 } ;
46
- let co = { line : 1 , column : 0 } ;
58
+ let ci : Position = { line : 1 , column : 0 } ;
59
+ let co : Position = { line : 1 , column : 0 } ;
47
60
for ( const { start, end, value} of this . _edits ) {
48
61
if ( start > index ) {
49
62
const l = positionLength ( this . _input , index , start ) ;
@@ -65,7 +78,7 @@ export class Sourcemap {
65
78
const l = positionSubtract ( position , co ) ;
66
79
return positionAdd ( ci , l ) ;
67
80
}
68
- toString ( ) {
81
+ toString ( ) : string {
69
82
let output = "" ;
70
83
let index = 0 ;
71
84
for ( const { start, end, value} of this . _edits ) {
@@ -78,13 +91,13 @@ export class Sourcemap {
78
91
}
79
92
}
80
93
81
- function positionCompare ( a , b ) {
94
+ function positionCompare ( a : Position , b : Position ) : number {
82
95
return a . line - b . line || a . column - b . column ;
83
96
}
84
97
85
- function positionLength ( input , start = 0 , end = input . length ) {
86
- let match ,
87
- line = 0 ;
98
+ function positionLength ( input : string , start = 0 , end = input . length ) : Position {
99
+ let match : RegExpExecArray | null ;
100
+ let line = 0 ;
88
101
lineBreakG . lastIndex = start ;
89
102
while ( ( match = lineBreakG . exec ( input ) ) && match . index < end ) {
90
103
++ line ;
@@ -93,10 +106,10 @@ function positionLength(input, start = 0, end = input.length) {
93
106
return { line, column : end - start } ;
94
107
}
95
108
96
- function positionSubtract ( b , a ) {
109
+ function positionSubtract ( b : Position , a : Position ) : Position {
97
110
return b . line === a . line ? { line : 0 , column : b . column - a . column } : { line : b . line - a . line , column : b . column } ;
98
111
}
99
112
100
- function positionAdd ( p , l ) {
113
+ function positionAdd ( p : Position , l : Position ) : Position {
101
114
return l . line === 0 ? { line : p . line , column : p . column + l . column } : { line : p . line + l . line , column : l . column } ;
102
115
}
0 commit comments