// From packages\babel-generator\src\printer.ts import Buffer from "./buffer"; import type { Loc } from "./buffer"; import * as n from "./node"; import type * as t from "@babel/types"; import type { RecordAndTuplePluginOptions, PipelineOperatorPluginOptions, } from "@babel/parser"; import type { Opts as jsescOptions } from "jsesc"; import * as generatorFunctions from "./generators"; import type SourceMap from "./source-map"; import * as charCodes from "charcodes"; const SCIENTIFIC_NOTATION = /e/i; const ZERO_DECIMAL_INTEGER = /\.0+$/; const NON_DECIMAL_LITERAL = /^0[box]/; const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/; const { needsParens, needsWhitespaceAfter, needsWhitespaceBefore } = n; export type Format = { shouldPrintComment: (comment: string) => boolean; retainLines: boolean; retainFunctionParens: boolean; comments: boolean; auxiliaryCommentBefore: string; auxiliaryCommentAfter: string; compact: boolean | "auto"; minified: boolean; concise: boolean; indent: { adjustMultilineComment: boolean; style: string; }; recordAndTupleSyntaxType: RecordAndTuplePluginOptions["syntaxType"]; jsescOption: jsescOptions; jsonCompatibleStrings?: boolean; /** * For use with the Hack-style pipe operator. * Changes what token is used for pipe bodies’ topic references. */ topicToken?: PipelineOperatorPluginOptions["topicToken"]; /** * @deprecated Removed in Babel 8 */ decoratorsBeforeExport?: boolean; }; interface AddNewlinesOptions { addNewlines(leading: boolean, node: t.Node): number; } interface PrintSequenceOptions extends Partial { statement?: boolean; indent?: boolean; } interface PrintListOptions { separator?: (this: Printer) => void; statement?: boolean; indent?: boolean; } type PrintJoinOptions = PrintListOptions & PrintSequenceOptions & { iterator?: (node: t.Node, index: number) => void; }; class Printer { constructor(format: Format, map: SourceMap) { this.format = format; this._buf = new Buffer(map); this._indentChar = format.indent.style.charCodeAt(0); this._indentRepeat = format.indent.style.length; } declare format: Format; inForStatementInitCounter: number = 0; declare _buf: Buffer; _printStack: Array = []; _indent: number = 0; _indentChar: number = 0; _indentRepeat: number = 0; _insideAux: boolean = false; _parenPushNewlineState: { printed: boolean } | null = null; _noLineTerminator: boolean = false; _printAuxAfterOnNextUserNode: boolean = false; _printedComments = new Set(); _endsWithInteger = false; _endsWithWord = false; generate(ast: t.Node) { this.print(ast); this._maybeAddAuxComment(); return this._buf.get(); } /** * 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._maybeAddAuxComment(); if (force) { this._appendChar(charCodes.semicolon); } else { this._queue(charCodes.semicolon); } } /** * Add a right brace to the buffer. */ rightBrace(): void { if (this.format.minified) { this._buf.removeLastSemicolon(); } this.token("}"); } /** * Add a space to the buffer unless it is compact. */ space(force: boolean = false): void { if (this.format.compact) return; if (force) { this._space(); } else if (this._buf.hasContent()) { const lastCp = this.getLastChar(); if (lastCp !== charCodes.space && lastCp !== charCodes.lineFeed) { this._space(); } } } /** * Writes a token that can't be safely parsed without taking whitespace into account. */ word(str: string): void { // prevent concatenating words and creating // comment out of division and regex if ( this._endsWithWord || (str.charCodeAt(0) === charCodes.slash && this.endsWith(charCodes.slash)) ) { this._space(); } this._maybeAddAuxComment(); this._append(str, false); this._endsWithWord = true; } /** * Writes a number token so that we can validate if it is an integer. */ number(str: string): void { this.word(str); // Integer tokens need special handling because they cannot have '.'s inserted // immediately after them. this._endsWithInteger = Number.isInteger(+str) && !NON_DECIMAL_LITERAL.test(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== charCodes.dot; } /** * Writes a simple token. */ token(str: string, maybeNewline = false): void { // space is mandatory to avoid outputting