/* eslint max-len: 0 */ import repeat from "lodash/repeat"; import Buffer from "./buffer"; import * as n from "./node"; import * as t from "babel-types"; export default class Printer { constructor(format, map) { this.format = format || {}; this._buf = new Buffer(map); this.insideAux = false; this._printAuxAfterOnNextUserNode = false; this._printStack = []; this._printedCommentStarts = {}; this._parenPushNewlineState = null; this._indent = 0; this.inForStatementInitCounter = 0; } _printedCommentStarts: Object; _parenPushNewlineState: ?Object; /** * Get the current indent. */ _getIndent(): string { if (this.format.compact || this.format.concise) { return ""; } else { return repeat(this.format.indent.style, this._indent); } } /** * Increment indent size. */ indent(): void { this._indent++; } /** * Decrement indent size. */ dedent(): void { this._indent--; } /** * Add a semicolon to the buffer. */ semicolon(): void { this._append(";", true /* queue */); } /** * Add a right brace to the buffer. */ rightBrace(): void { if (!this.endsWith("\n")) this.newline(); if (this.format.minified && !this._lastPrintedIsEmptyStatement) { this._buf.removeLastSemicolon(); } this.token("}"); } /** * Add a keyword to the buffer. */ keyword(name: string): void { this.word(name); this.space(); } /** * Add a space to the buffer unless it is compact. */ space(force: boolean = false): void { if (this.format.compact) return; if ((this._buf.hasContent() && !this.endsWith(" ") && !this.endsWith("\n")) || force) { this._space(); } } /** * Writes a token that can't be safely parsed without taking whitespace into account. */ word(str: string): void { if (this._endsWithWord) this._space(); this._append(str); this._endsWithWord = true; } /** * Writes a simple token. */ token(str: string): void { const last = this._buf.getLast(); // space is mandatory to avoid outputting