diff --git a/packages/babel-generator/src/buffer.js b/packages/babel-generator/src/buffer.js index d4803d4a87..7e5e7c8970 100644 --- a/packages/babel-generator/src/buffer.js +++ b/packages/babel-generator/src/buffer.js @@ -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 { diff --git a/packages/babel-generator/src/position.js b/packages/babel-generator/src/position.js deleted file mode 100644 index 09283868d5..0000000000 --- a/packages/babel-generator/src/position.js +++ /dev/null @@ -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++; - } - } - } -} diff --git a/packages/babel-generator/src/source-map.js b/packages/babel-generator/src/source-map.js index 050c0f960c..4486bbf737 100644 --- a/packages/babel-generator/src/source-map.js +++ b/packages/babel-generator/src/source-map.js @@ -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 : {