prettier-sql
Version:
Format whitespace in a SQL query to make it more readable
133 lines (107 loc) • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _token = require("./token");
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; }
/**
* Bookkeeper for inline blocks.
*
* Inline blocks are parenthesised expressions that are shorter than INLINE_MAX_LENGTH.
* These blocks are formatted on a single line, unlike longer parenthesised
* expressions where open-parenthesis causes newline and increase of indentation.
*/
var InlineBlock = /*#__PURE__*/function () {
function InlineBlock(lineWidth) {
_classCallCheck(this, InlineBlock);
_defineProperty(this, "level", void 0);
_defineProperty(this, "lineWidth", void 0);
this.level = 0;
this.lineWidth = lineWidth;
}
/**
* Begins inline block when lookahead through upcoming tokens determines
* that the block would be smaller than INLINE_MAX_LENGTH.
* @param {Token[]} 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
*/
}, {
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(`somecolumn`), 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;
if (this.isForbiddenToken(token)) {
return false;
} // Overran max length
if (length > this.lineWidth) {
return false;
} // CASE cannot start inline block
if (token.type === _token.TokenType.BLOCK_START && !_token.isToken.CASE(token)) {
level++;
} else if (token.type === _token.TokenType.BLOCK_END) {
level--;
if (level === 0) {
return true;
}
}
}
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 === _token.TokenType.RESERVED_COMMAND || type === _token.TokenType.RESERVED_LOGICAL_OPERATOR || // type === TokenType.LINE_COMMENT ||
type === _token.TokenType.BLOCK_COMMENT || value === ';' || _token.isToken.CASE({
type: type,
value: value
}) // CASE cannot have inline blocks
;
}
}]);
return InlineBlock;
}();
exports["default"] = InlineBlock;
module.exports = exports.default;
//# sourceMappingURL=InlineBlock.js.map