Ditch the tiny Position class.

This commit is contained in:
Logan Smyth 2016-07-07 19:19:09 -07:00
parent 8379c21939
commit 475581dc66
3 changed files with 22 additions and 39 deletions

View File

@ -1,4 +1,3 @@
import Position from "./position";
import type SourceMap from "./source-map";
import trimEnd from "lodash/trimEnd";
@ -21,7 +20,10 @@ export default class Buffer {
_last: string = "";
_queue: Array = [];
_position: Position = new Position;
_position: Object = {
line: 1,
column: 0,
};
_sourcePosition: Object = {
line: null,
column: null,
@ -67,11 +69,21 @@ export default class Buffer {
_append(str: string, line: number, column: number, filename: ?string): void {
// If there the line is ending, adding a new mapping marker is redundant
if (this._map && str[0] !== "\n") this._map.mark(this._position, line, column, filename);
if (this._map && str[0] !== "\n") {
this._map.mark(this._position.line, this._position.column, line, column, filename);
}
this._buf += str;
this._last = str[str.length - 1];
this._position.push(str);
for (let i = 0; i < str.length; i++) {
if (str[i] === "\n") {
this._position.line++;
this._position.column = 0;
} else {
this._position.column++;
}
}
}
removeTrailingSpaces(): void {

View File

@ -1,28 +0,0 @@
/**
* Track current position in code generation.
*/
export default class Position {
column: number;
line: number;
constructor() {
this.line = 1;
this.column = 0;
}
/**
* Push a string to the current position, mantaining the current line and column.
*/
push(str: string): void {
for (let i = 0; i < str.length; i++) {
if (str[i] === "\n") {
this.line++;
this.column = 0;
} else {
this.column++;
}
}
}
}

View File

@ -1,5 +1,4 @@
import sourceMap from "source-map";
import type Position from "./position";
/**
* Build a sourcemap.
@ -35,25 +34,25 @@ export default class SourceMap {
* values to insert a mapping to nothing.
*/
mark(position: Position, line: number, column: number, filename: ?string) {
mark(generatedLine: number, generatedColumn: number, line: number, column: number, filename: ?string) {
// Adding an empty mapping at the start of a generated line just clutters the map.
if (this._lastGenLine !== position.line && line === null) return;
if (this._lastGenLine !== generatedLine && line === null) return;
// If this mapping points to the same source location as the last one, we can ignore it since
// the previous one covers it.
if (this._lastGenLine === position.line && this._lastSourceLine === line &&
if (this._lastGenLine === generatedLine && this._lastSourceLine === line &&
this._lastSourceColumn === column) {
return;
}
this._lastGenLine = position.line;
this._lastGenLine = generatedLine;
this._lastSourceLine = line;
this._lastSourceColumn = column;
this._map.addMapping({
generated: {
line: position.line,
column: position.column
line: generatedLine,
column: generatedColumn,
},
source: line == null ? null : filename || this._opts.sourceFileName,
original: line == null ? null : {