From 3680d10b6f5dcf28c67fba0e3d7f9fd2689ccc90 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sat, 2 Jul 2016 13:22:52 -0700 Subject: [PATCH] Centralize position tracking into buffer. --- packages/babel-generator/src/buffer.js | 24 +++++++++++++++--------- packages/babel-generator/src/index.js | 7 +------ packages/babel-generator/src/printer.js | 3 +-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/babel-generator/src/buffer.js b/packages/babel-generator/src/buffer.js index 750ecac11e..801042dfeb 100644 --- a/packages/babel-generator/src/buffer.js +++ b/packages/babel-generator/src/buffer.js @@ -1,4 +1,4 @@ -import type Position from "./position"; +import Position from "./position"; import repeat from "lodash/repeat"; import trimEnd from "lodash/trimEnd"; @@ -7,10 +7,10 @@ import trimEnd from "lodash/trimEnd"; */ export default class Buffer { - constructor(position: Position, format: Object) { + constructor(format: Object) { this.printedCommentStarts = {}; this.parenPushNewlineState = null; - this.position = position; + this._position = new Position(); this._indent = format.indent.base; this.format = format; this.buf = ""; @@ -44,7 +44,7 @@ export default class Buffer { catchUp(node: Object) { // catch up to this nodes newline if we're behind if (node.loc && this.format.retainLines && this.buf) { - while (this.position.line < node.loc.start.line) { + while (this.getCurrentLine() < node.loc.start.line) { this.push("\n"); } } @@ -164,7 +164,6 @@ export default class Buffer { this.push(str); } - /** * Remove the last character. */ @@ -178,7 +177,7 @@ export default class Buffer { if (!this.endsWith(cha)) return; this.buf = this.buf.slice(0, -1); this.last = this.buf[this.buf.length - 1]; - this.position.unshift(cha); + this._position.unshift(cha); } /** @@ -260,7 +259,7 @@ export default class Buffer { let toRemove = this.buf.slice(lastNewlineIndex + 1); this.buf = this.buf.substring(0, lastNewlineIndex + 1); this.last = "\n"; - this.position.unshift(toRemove); + this._position.unshift(toRemove); } } @@ -333,10 +332,10 @@ export default class Buffer { } // If there the line is ending, adding a new mapping marker is redundant - if (this.opts.sourceMaps && str[0] !== "\n") this.map.mark(this.position, this._sourcePosition); + if (this.opts.sourceMaps && str[0] !== "\n") this.map.mark(this._position, this._sourcePosition); // - this.position.push(str); + this._position.push(str); this.buf += str; this.last = str[str.length - 1]; @@ -358,4 +357,11 @@ export default class Buffer { } } + getCurrentColumn() { + return this._position.column; + } + + getCurrentLine() { + return this._position.line; + } } diff --git a/packages/babel-generator/src/index.js b/packages/babel-generator/src/index.js index f13bec1764..ffc9faca04 100644 --- a/packages/babel-generator/src/index.js +++ b/packages/babel-generator/src/index.js @@ -1,7 +1,6 @@ import detectIndent from "detect-indent"; import Whitespace from "./whitespace"; import SourceMap from "./source-map"; -import Position from "./position"; import * as messages from "babel-messages"; import Printer from "./printer"; @@ -18,12 +17,9 @@ class Generator extends Printer { let tokens = ast.tokens || []; let format = Generator.normalizeOptions(code, opts, tokens); - let position = new Position; - - super(position, format); + super(format); this.comments = comments; - this.position = position; this.tokens = tokens; this.format = format; this.opts = opts; @@ -54,7 +50,6 @@ class Generator extends Printer { auxiliaryCommentBefore: string; auxiliaryCommentAfter: string; whitespace: Whitespace; - position: Position; map: SourceMap; comments: Array; tokens: Array; diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 98690a3468..79fcb230fa 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -272,7 +272,6 @@ export default class Printer extends Buffer { if (!this.endsWith(["[", "{"])) this.space(); - let column = this.position.column; let val = this.generateComment(comment); // @@ -283,7 +282,7 @@ export default class Printer extends Buffer { val = val.replace(newlineRegex, "\n"); } - let indent = Math.max(this.indentSize(), column); + let indent = Math.max(this.indentSize(), this.getCurrentColumn()); val = val.replace(/\n/g, `\n${repeat(" ", indent)}`); }