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