Skip to content

Commit dab8785

Browse files
committed
TypeScript Sourcemap
1 parent 2d45dd5 commit dab8785

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/sourcemap.js renamed to src/sourcemap.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
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+
}
213

314
export class Sourcemap {
15+
private readonly _input: string;
16+
private readonly _edits: Edit[];
417
constructor(input = "") {
518
this._input = input;
619
this._edits = [];
720
}
8-
_bisectLeft(index) {
21+
private _bisectLeft(index: number): number {
922
let lo = 0;
1023
let hi = this._edits.length;
1124
while (lo < hi) {
@@ -15,7 +28,7 @@ export class Sourcemap {
1528
}
1629
return lo;
1730
}
18-
_bisectRight(index) {
31+
private _bisectRight(index: number): number {
1932
let lo = 0;
2033
let hi = this._edits.length;
2134
while (lo < hi) {
@@ -25,25 +38,25 @@ export class Sourcemap {
2538
}
2639
return lo;
2740
}
28-
insertLeft(index, value) {
41+
insertLeft(index: number, value: string): void {
2942
this.replaceLeft(index, index, value);
3043
}
31-
insertRight(index, value) {
44+
insertRight(index: number, value: string): void {
3245
this.replaceRight(index, index, value);
3346
}
34-
delete(start, end) {
47+
delete(start: number, end: number): void {
3548
this.replaceRight(start, end, "");
3649
}
37-
replaceLeft(start, end, value) {
50+
replaceLeft(start: number, end: number, value: string): void {
3851
this._edits.splice(this._bisectLeft(start), 0, {start, end, value});
3952
}
40-
replaceRight(start, end, value) {
53+
replaceRight(start: number, end: number, value: string): void {
4154
this._edits.splice(this._bisectRight(start), 0, {start, end, value});
4255
}
43-
translate(position) {
56+
translate(position: Position): Position {
4457
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};
4760
for (const {start, end, value} of this._edits) {
4861
if (start > index) {
4962
const l = positionLength(this._input, index, start);
@@ -65,7 +78,7 @@ export class Sourcemap {
6578
const l = positionSubtract(position, co);
6679
return positionAdd(ci, l);
6780
}
68-
toString() {
81+
toString(): string {
6982
let output = "";
7083
let index = 0;
7184
for (const {start, end, value} of this._edits) {
@@ -78,13 +91,13 @@ export class Sourcemap {
7891
}
7992
}
8093

81-
function positionCompare(a, b) {
94+
function positionCompare(a: Position, b: Position): number {
8295
return a.line - b.line || a.column - b.column;
8396
}
8497

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;
88101
lineBreakG.lastIndex = start;
89102
while ((match = lineBreakG.exec(input)) && match.index < end) {
90103
++line;
@@ -93,10 +106,10 @@ function positionLength(input, start = 0, end = input.length) {
93106
return {line, column: end - start};
94107
}
95108

96-
function positionSubtract(b, a) {
109+
function positionSubtract(b: Position, a: Position): Position {
97110
return b.line === a.line ? {line: 0, column: b.column - a.column} : {line: b.line - a.line, column: b.column};
98111
}
99112

100-
function positionAdd(p, l) {
113+
function positionAdd(p: Position, l: Position): Position {
101114
return l.line === 0 ? {line: p.line, column: p.column + l.column} : {line: p.line + l.line, column: l.column};
102115
}

0 commit comments

Comments
 (0)