UNPKG

@gethue/sql-formatter

Version:

Format whitespace in a SQL query to make it more readable

106 lines (89 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _utils = require("../utils"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var INDENT_TYPE_TOP_LEVEL = 'top-level'; var INDENT_TYPE_BLOCK_LEVEL = 'block-level'; /** * Manages indentation levels. * * There are two types of indentation levels: * * - BLOCK_LEVEL : increased by open-parenthesis * - TOP_LEVEL : increased by RESERVED_TOP_LEVEL words */ var Indentation = /*#__PURE__*/function () { /** * @param {String} indent Indent value, default is " " (2 spaces) */ function Indentation(indent) { _classCallCheck(this, Indentation); this.indent = indent || ' '; this.indentTypes = []; } /** * Returns current indentation string. * @return {String} */ _createClass(Indentation, [{ key: "getIndent", value: function getIndent() { return this.indent.repeat(this.indentTypes.length); } /** * Increases indentation by one top-level indent. */ }, { key: "increaseTopLevel", value: function increaseTopLevel() { this.indentTypes.push(INDENT_TYPE_TOP_LEVEL); } /** * Increases indentation by one block-level indent. */ }, { key: "increaseBlockLevel", value: function increaseBlockLevel() { this.indentTypes.push(INDENT_TYPE_BLOCK_LEVEL); } /** * Decreases indentation by one top-level indent. * Does nothing when the previous indent is not top-level. */ }, { key: "decreaseTopLevel", value: function decreaseTopLevel() { if (this.indentTypes.length > 0 && (0, _utils.last)(this.indentTypes) === INDENT_TYPE_TOP_LEVEL) { this.indentTypes.pop(); } } /** * Decreases indentation by one block-level indent. * If there are top-level indents within the block-level indent, * throws away these as well. */ }, { key: "decreaseBlockLevel", value: function decreaseBlockLevel() { while (this.indentTypes.length > 0) { var type = this.indentTypes.pop(); if (type !== INDENT_TYPE_TOP_LEVEL) { break; } } } }, { key: "resetIndentation", value: function resetIndentation() { this.indentTypes = []; } }]); return Indentation; }(); exports["default"] = Indentation; module.exports = exports.default;