rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
243 lines (242 loc) • 9.95 kB
TypeScript
import { SqlPrintToken, SqlPrintTokenContainerType } from "../models/SqlPrintToken";
import { IndentCharOption, NewlineOption } from "./LinePrinter";
import { BaseFormattingOptions, WithClauseStyle, CommentStyle } from "./SqlFormatter";
import { CommaBreakStyle as HelperCommaBreakStyle } from "./OnelineFormattingHelper";
import { CommentExportMode } from "../types/Formatting";
/**
* CommaBreakStyle determines how commas are placed in formatted SQL output.
* - 'none': No line break for commas
* - 'before': Line break before comma
* - 'after': Line break after comma
*/
export type CommaBreakStyle = HelperCommaBreakStyle;
/**
* AndBreakStyle determines how AND operators are placed in formatted SQL output.
* - 'none': No line break for AND
* - 'before': Line break before AND
* - 'after': Line break after AND
*/
export type AndBreakStyle = 'none' | 'before' | 'after';
/**
* OrBreakStyle determines how OR operators are placed in formatted SQL output.
* - 'none': No line break for OR
* - 'before': Line break before OR
* - 'after': Line break after OR
*/
export type OrBreakStyle = 'none' | 'before' | 'after';
/**
* Options for configuring SqlPrinter formatting behavior
*/
export interface SqlPrinterOptions extends BaseFormattingOptions {
/** Container types that should increase indentation level */
indentIncrementContainerTypes?: SqlPrintTokenContainerType[];
}
/**
* SqlPrinter formats a SqlPrintToken tree into a SQL string with flexible style options.
*
* This class provides various formatting options including:
* - Indentation control (character and size)
* - Line break styles for commas and AND operators
* - Keyword case transformation
* - Comment handling
* - WITH clause formatting styles
*
* @example
* const printer = new SqlPrinter({
* indentChar: ' ',
* indentSize: 1,
* keywordCase: 'upper',
* commaBreak: 'after',
* withClauseStyle: 'cte-oneline'
* });
* const formatted = printer.print(sqlToken);
*/
export declare class SqlPrinter {
/** Indent character (e.g., ' ' or '\\t') */
indentChar: IndentCharOption;
/** Indent size (number of indentChar repetitions per level) */
indentSize: number;
/** Newline character (e.g., '\\n' or '\\r\\n') */
newline: NewlineOption;
/** Comma break style: 'none', 'before', or 'after' */
commaBreak: CommaBreakStyle;
/** WITH clause comma break style (defaults to commaBreak) */
cteCommaBreak: CommaBreakStyle;
/** VALUES clause comma break style (defaults to commaBreak) */
valuesCommaBreak: CommaBreakStyle;
/** AND break style: 'none', 'before', or 'after' */
andBreak: AndBreakStyle;
/** OR break style: 'none', 'before', or 'after' */
orBreak: OrBreakStyle;
/** Keyword case style: 'none', 'upper' | 'lower' */
keywordCase: 'none' | 'upper' | 'lower';
/** Comment export mode controlling how comments are emitted */
commentExportMode: CommentExportMode;
/** WITH clause formatting style (default: 'standard') */
withClauseStyle: WithClauseStyle;
/** Comment formatting style (default: 'block') */
commentStyle: CommentStyle;
private linePrinter;
private indentIncrementContainers;
/** Track whether we are currently inside a WITH clause for full-oneline formatting */
private insideWithClause;
/** Whether to keep parentheses content on one line */
private parenthesesOneLine;
/** Whether to keep BETWEEN expressions on one line */
private betweenOneLine;
/** Whether to keep VALUES clause on one line */
private valuesOneLine;
/** Whether to keep JOIN conditions on one line */
private joinOneLine;
/** Whether to keep CASE expressions on one line */
private caseOneLine;
/** Whether to keep subqueries on one line */
private subqueryOneLine;
/** Whether to indent nested parentheses when boolean groups get expanded */
private indentNestedParentheses;
/** Whether to keep INSERT column lists on one line */
private insertColumnsOneLine;
/** Whether to keep MERGE WHEN predicates on a single line */
private whenOneLine;
/** Tracks nesting depth while formatting MERGE WHEN predicate segments */
private mergeWhenPredicateDepth;
/** Shared helper for oneline-specific formatting decisions */
private onelineHelper;
/** Pending line comment that needs a forced newline before next token */
private pendingLineCommentBreak;
/** Accumulates lines when reconstructing multi-line block comments inside CommentBlocks */
private smartCommentBlockBuilder;
/**
* @param options Optional style settings for pretty printing
*/
constructor(options?: SqlPrinterOptions);
/**
* Converts a SqlPrintToken tree to a formatted SQL string.
* @param token The root SqlPrintToken to format
* @param level Initial indentation level (default: 0)
* @returns Formatted SQL string
* @example
* const printer = new SqlPrinter({ indentChar: ' ', keywordCase: 'upper' });
* const formatted = printer.print(sqlToken);
*/
print(token: SqlPrintToken, level?: number): string;
private resolveCommentExportMode;
private rendersInlineComments;
private shouldRenderComment;
private appendToken;
private shouldAlignExplainStatementChild;
private isCaseContext;
/**
* Determines if a token should be skipped during printing.
* Tokens are skipped if they have no content and no inner tokens,
* except for special token types that have semantic meaning despite empty text.
*/
private shouldSkipToken;
private applyKeywordCase;
private handleKeywordToken;
private ensureSpaceBeforeKeyword;
private ensureTrailingSpace;
/**
* Normalizes INSERT column list token text when one-line formatting is active.
*/
private tryAppendInsertClauseTokenText;
private handleCommaToken;
private handleAndOperatorToken;
private handleParenthesisToken;
private handleOrOperatorToken;
/**
* Decide whether a parentheses group should increase indentation when inside nested structures.
* We only expand groups that contain further parentheses so simple comparisons stay compact.
*/
private shouldIndentNestedParentheses;
/**
* Recursively inspect descendants to find additional parentheses groups.
* Helps detect complex boolean groups like ((A) OR (B) OR (C)).
*/
private containsParenExpression;
private handleJoinClauseToken;
/**
* Decides whether the current container should collapse into a single line.
*/
private shouldFormatContainerAsOneline;
/**
* Detects an INSERT column list that must stay on a single line.
*/
private isInsertClauseOneline;
/**
* Handles space tokens with context-aware filtering.
* Skips spaces in CommentBlocks when in specific CTE modes to prevent duplication.
*/
private handleSpaceToken;
private findPreviousSignificantToken;
private shouldSkipSpaceBeforeParenthesis;
private shouldAlignCreateTableSelect;
private isCreateTableSpacingContext;
private isCreateTableNameToken;
private isCreateTableConstraintKeyword;
private isIdentifierAttachedToConstraint;
private printCommentToken;
private handleSmartCommentBlockToken;
private handleCommentBlockContainer;
private normalizeSmartBlockLine;
private extractLineCommentContent;
private flushSmartCommentBlockBuilder;
private collectCommentBlockLines;
private extractRawCommentBlockLines;
private normalizeCommentForSmart;
private buildBlockComment;
private getIndentString;
private sanitizeCommentLine;
private escapeCommentDelimiters;
private getCommentBaseIndentLevel;
private resolveCommentIndentLevel;
/**
* Handles commentNewline tokens with conditional newline behavior.
* In multiline mode (newline !== ' '), adds a newline after comments.
* In oneliner mode (newline === ' '), does nothing to keep comments on same line.
* Skips newlines in CTE modes (full-oneline, cte-oneline) to maintain one-line format.
*/
private handleCommentNewlineToken;
/**
* Determines whether to skip commentNewline tokens.
* Skips in CTE modes to maintain one-line formatting.
*/
private shouldSkipCommentNewline;
private shouldAddNewlineBeforeLeadingComments;
private getLeadingCommentIndentLevel;
/**
* Determines if the printer is in oneliner mode.
* Oneliner mode uses single spaces instead of actual newlines.
*/
private isOnelineMode;
/**
* Handles CTE tokens with one-liner formatting.
* Creates a nested SqlPrinter instance for proper CTE oneline formatting.
*/
private handleCteOnelineToken;
/**
* Creates a SqlPrinter instance configured for CTE oneline formatting.
*/
private createCteOnelinePrinter;
/**
* Handles tokens with oneline formatting (parentheses, BETWEEN, VALUES, JOIN, CASE, subqueries).
* Creates a unified oneline printer that formats everything as one line regardless of content type.
*/
private handleOnelineToken;
private getClauseBreakIndentLevel;
private isMergeActionContainer;
private shouldBreakAfterOpeningParen;
private shouldBreakBeforeClosingParen;
private shouldConvertSpaceToClauseBreak;
/**
* Creates a unified SqlPrinter instance configured for oneline formatting.
* Works for all oneline options: parentheses, BETWEEN, VALUES, JOIN, CASE, subqueries.
* Sets all oneline options to false to prevent recursion and uses newline=' ' for actual oneline effect.
*/
private createOnelinePrinter;
/**
* Removes duplicate consecutive spaces while preserving single spaces.
* Simple and safe space normalization for CTE oneline mode.
*/
private cleanDuplicateSpaces;
}