UNPKG

sql-formatter-plus

Version:

Formats whitespace in a SQL query to make it more readable

122 lines (99 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _tokenTypes = _interopRequireDefault(require("./tokenTypes")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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 INLINE_MAX_LENGTH = 50; /** * Bookkeeper for inline blocks. * * Inline blocks are parenthesized expressions that are shorter than INLINE_MAX_LENGTH. * These blocks are formatted on a single line, unlike longer parenthesized * expressions where open-parenthesis causes newline and increase of indentation. */ var InlineBlock = /*#__PURE__*/ function () { function InlineBlock() { _classCallCheck(this, InlineBlock); this.level = 0; } /** * Begins inline block when lookahead through upcoming tokens determines * that the block would be smaller than INLINE_MAX_LENGTH. * @param {Object[]} tokens Array of all tokens * @param {Number} index Current token position */ _createClass(InlineBlock, [{ key: "beginIfPossible", value: function beginIfPossible(tokens, index) { if (this.level === 0 && this.isInlineBlock(tokens, index)) { this.level = 1; } else if (this.level > 0) { this.level++; } else { this.level = 0; } } /** * Finishes current inline block. * There might be several nested ones. */ }, { key: "end", value: function end() { this.level--; } /** * True when inside an inline block * @return {Boolean} */ }, { key: "isActive", value: function isActive() { return this.level > 0; } // Check if this should be an inline parentheses block // Examples are "NOW()", "COUNT(*)", "int(10)", key(`some_column`), DECIMAL(7,2) }, { key: "isInlineBlock", value: function isInlineBlock(tokens, index) { var length = 0; var level = 0; for (var i = index; i < tokens.length; i++) { var token = tokens[i]; length += token.value.length; // Overran max length if (length > INLINE_MAX_LENGTH) { return false; } if (token.type === _tokenTypes["default"].OPEN_PAREN) { level++; } else if (token.type === _tokenTypes["default"].CLOSE_PAREN) { level--; if (level === 0) { return true; } } if (this.isForbiddenToken(token)) { return false; } } return false; } // Reserved words that cause newlines, comments and semicolons // are not allowed inside inline parentheses block }, { key: "isForbiddenToken", value: function isForbiddenToken(_ref) { var type = _ref.type, value = _ref.value; return type === _tokenTypes["default"].RESERVED_TOP_LEVEL || type === _tokenTypes["default"].RESERVED_NEWLINE || type === _tokenTypes["default"].COMMENT || type === _tokenTypes["default"].BLOCK_COMMENT || value === ';'; } }]); return InlineBlock; }(); exports["default"] = InlineBlock; module.exports = exports.default;