UNPKG

prettier-sql

Version:

Format whitespace in a SQL query to make it more readable

117 lines (95 loc) 3.54 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; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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_COMMAND words */ var Indentation = /*#__PURE__*/function () { /** * @param {string} indent Indent value, default is " " (2 spaces) */ function Indentation() { var indent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ' '; _classCallCheck(this, Indentation); _defineProperty(this, "indent", void 0); _defineProperty(this, "indentTypes", void 0); this.indent = indent; this.indentTypes = []; } /** * Returns current indentation string. * @return {string} indentation string based on indentTypes */ _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; } } } /** Clears all indentation */ }, { key: "resetIndentation", value: function resetIndentation() { this.indentTypes = []; } }]); return Indentation; }(); exports["default"] = Indentation; module.exports = exports.default; //# sourceMappingURL=Indentation.js.map