/* 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; /** * Increment indent size. */ indent(): void { if (this.format.compact || this.format.concise) return; this._indent++; } /** * Decrement indent size. */ dedent(): void { if (this.format.compact || this.format.concise) return; this._indent--; } /** * Add a semicolon to the buffer. */ semicolon(force: boolean = false): void { this._append(";", !force /* queue */); } /** * Add a right brace to the buffer. */ rightBrace(): void { if (!this.endsWith("\n")) this.newline(); if (this.format.minified) { 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