UNPKG

rawsql-ts

Version:

[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

86 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrintLine = exports.LinePrinter = void 0; /** * SqlPrintHelper provides utility methods for SQL pretty printing. */ class LinePrinter { /** * @param indentChar Character used for indentation (default: ' ') // Updated comment to reflect options * @param indentSize Number of indentChar per level (default: 0) * @param newline Newline string (default: '\r\n') // Changed type and default value */ constructor(indentChar = ' ', indentSize = 0, newline = '\r\n') { this.indentChar = indentChar; this.indentSize = indentSize; this.newline = newline; this.lines = []; this.appendNewline(0); } print() { let result = ''; for (const line of this.lines) { if (line.text !== '') { // append indent and text result += this.indent(line.level) + line.text; } } return result.trimEnd(); } /** * Returns the indent string for a given level. * @param level Indentation level */ indent(level) { return this.indentChar.repeat(this.indentSize * level); } /** * Appends a newline token to the given tokens array if newline is set, or adds an empty line if tokens is empty. * @param tokens Array of token objects with 'level' and 'text' property * @param level Indentation level */ appendNewline(level) { if (this.lines.length > 0) { const current = this.lines[this.lines.length - 1]; if (current.text !== '') { current.text = current.text.trimEnd() + this.newline; } } this.lines.push(new PrintLine(level, '')); } /** * Appends text to the last element of tokens array. * @param tokens Array of token objects with 'text' property * @param text Text to append */ appendText(text) { if (this.lines.length > 0) { const workingIndex = this.lines.length - 1; const workLine = this.lines[workingIndex]; // Leading space is not needed if (!(text === ' ' && workLine.text === '')) { workLine.text += text; } } else { throw new Error('No tokens to append to.'); } } getCurrentLine() { if (this.lines.length > 0) { return this.lines[this.lines.length - 1]; } else { throw new Error('No tokens to get current line from.'); } } } exports.LinePrinter = LinePrinter; class PrintLine { constructor(level, text) { this.level = level; this.text = text; } } exports.PrintLine = PrintLine; //# sourceMappingURL=LinePrinter.js.map