UNPKG

@gethue/sql-formatter

Version:

Format whitespace in a SQL query to make it more readable

341 lines (305 loc) 15.8 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _tokenTypes = _interopRequireDefault(require("./tokenTypes")); var regexFactory = _interopRequireWildcard(require("./regexFactory")); var _utils = require("../utils"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 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 Tokenizer = /*#__PURE__*/function () { /** * @param {Object} cfg * @param {String[]} cfg.reservedWords Reserved words in SQL * @param {String[]} cfg.reservedTopLevelWords Words that are set to new line separately * @param {String[]} cfg.reservedNewlineWords Words that are set to newline * @param {String[]} cfg.reservedTopLevelWordsNoIndent Words that are top level but have no indentation * @param {String[]} cfg.stringTypes String types to enable: "", '', ``, [], N'' * @param {String[]} cfg.openParens Opening parentheses to enable, like (, [ * @param {String[]} cfg.closeParens Closing parentheses to enable, like ), ] * @param {String[]} cfg.indexedPlaceholderTypes Prefixes for indexed placeholders, like ? * @param {String[]} cfg.namedPlaceholderTypes Prefixes for named placeholders, like @ and : * @param {String[]} cfg.lineCommentTypes Line comments to enable, like # and -- * @param {String[]} cfg.specialWordChars Special chars that can be found inside of words, like @ and # * @param {String[]} [cfg.operator] Additional operators to recognize */ function Tokenizer(cfg) { _classCallCheck(this, Tokenizer); this.WHITESPACE_REGEX = /^([\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+)/; this.NUMBER_REGEX = /^((\x2D[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*)?[0-9]+(\.[0-9]+)?([Ee]\x2D?[0-9]+(\.[0-9]+)?)?|0x[0-9A-Fa-f]+|0b[01]+)\b/; this.OPERATOR_REGEX = regexFactory.createOperatorRegex(['<>', '<=', '>='].concat(_toConsumableArray(cfg.operators || []))); this.BLOCK_COMMENT_REGEX = /^(\/\*(?:(?![])[\s\S])*?(?:\*\/|$))/; this.LINE_COMMENT_REGEX = regexFactory.createLineCommentRegex(cfg.lineCommentTypes); this.RESERVED_TOP_LEVEL_REGEX = regexFactory.createReservedWordRegex(cfg.reservedTopLevelWords); this.RESERVED_TOP_LEVEL_NO_INDENT_REGEX = regexFactory.createReservedWordRegex(cfg.reservedTopLevelWordsNoIndent); this.RESERVED_NEWLINE_REGEX = regexFactory.createReservedWordRegex(cfg.reservedNewlineWords); this.RESERVED_PLAIN_REGEX = regexFactory.createReservedWordRegex(cfg.reservedWords); this.WORD_REGEX = regexFactory.createWordRegex(cfg.specialWordChars); this.STRING_REGEX = regexFactory.createStringRegex(cfg.stringTypes); this.OPEN_PAREN_REGEX = regexFactory.createParenRegex(cfg.openParens); this.CLOSE_PAREN_REGEX = regexFactory.createParenRegex(cfg.closeParens); this.INDEXED_PLACEHOLDER_REGEX = regexFactory.createPlaceholderRegex(cfg.indexedPlaceholderTypes, '[0-9]*'); this.IDENT_NAMED_PLACEHOLDER_REGEX = regexFactory.createPlaceholderRegex(cfg.namedPlaceholderTypes, '[a-zA-Z0-9._$]+'); this.STRING_NAMED_PLACEHOLDER_REGEX = regexFactory.createPlaceholderRegex(cfg.namedPlaceholderTypes, regexFactory.createStringPattern(cfg.stringTypes)); } /** * Takes a SQL string and breaks it into tokens. * Each token is an object with type and value. * * @param {String} input The SQL string * @return {Object[]} tokens An array of tokens. * @return {String} token.type * @return {String} token.value * @return {String} token.whitespaceBefore Preceding whitespace */ _createClass(Tokenizer, [{ key: "tokenize", value: function tokenize(input) { var tokens = []; var token; // Keep processing the string until it is empty while (input.length) { // grab any preceding whitespace var whitespaceBefore = this.getWhitespace(input); input = input.substring(whitespaceBefore.length); if (input.length) { // Get the next token and the token type token = this.getNextToken(input, token); // Advance the string input = input.substring(token.value.length); tokens.push(_objectSpread(_objectSpread({}, token), {}, { whitespaceBefore: whitespaceBefore })); } } return tokens; } }, { key: "getWhitespace", value: function getWhitespace(input) { var matches = input.match(this.WHITESPACE_REGEX); return matches ? matches[1] : ''; } }, { key: "getNextToken", value: function getNextToken(input, previousToken) { return this.getCommentToken(input) || this.getStringToken(input) || this.getOpenParenToken(input) || this.getCloseParenToken(input) || this.getPlaceholderToken(input) || this.getNumberToken(input) || this.getReservedWordToken(input, previousToken) || this.getWordToken(input) || this.getOperatorToken(input); } }, { key: "getCommentToken", value: function getCommentToken(input) { return this.getLineCommentToken(input) || this.getBlockCommentToken(input); } }, { key: "getLineCommentToken", value: function getLineCommentToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].LINE_COMMENT, regex: this.LINE_COMMENT_REGEX }); } }, { key: "getBlockCommentToken", value: function getBlockCommentToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].BLOCK_COMMENT, regex: this.BLOCK_COMMENT_REGEX }); } }, { key: "getStringToken", value: function getStringToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].STRING, regex: this.STRING_REGEX }); } }, { key: "getOpenParenToken", value: function getOpenParenToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].OPEN_PAREN, regex: this.OPEN_PAREN_REGEX }); } }, { key: "getCloseParenToken", value: function getCloseParenToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].CLOSE_PAREN, regex: this.CLOSE_PAREN_REGEX }); } }, { key: "getPlaceholderToken", value: function getPlaceholderToken(input) { return this.getIdentNamedPlaceholderToken(input) || this.getStringNamedPlaceholderToken(input) || this.getIndexedPlaceholderToken(input); } }, { key: "getIdentNamedPlaceholderToken", value: function getIdentNamedPlaceholderToken(input) { return this.getPlaceholderTokenWithKey({ input: input, regex: this.IDENT_NAMED_PLACEHOLDER_REGEX, parseKey: function parseKey(v) { return v.slice(1); } }); } }, { key: "getStringNamedPlaceholderToken", value: function getStringNamedPlaceholderToken(input) { var _this = this; return this.getPlaceholderTokenWithKey({ input: input, regex: this.STRING_NAMED_PLACEHOLDER_REGEX, parseKey: function parseKey(v) { return _this.getEscapedPlaceholderKey({ key: v.slice(2, -1), quoteChar: v.slice(-1) }); } }); } }, { key: "getIndexedPlaceholderToken", value: function getIndexedPlaceholderToken(input) { return this.getPlaceholderTokenWithKey({ input: input, regex: this.INDEXED_PLACEHOLDER_REGEX, parseKey: function parseKey(v) { return v.slice(1); } }); } }, { key: "getPlaceholderTokenWithKey", value: function getPlaceholderTokenWithKey(_ref) { var input = _ref.input, regex = _ref.regex, parseKey = _ref.parseKey; var token = this.getTokenOnFirstMatch({ input: input, regex: regex, type: _tokenTypes["default"].PLACEHOLDER }); if (token) { token.key = parseKey(token.value); } return token; } }, { key: "getEscapedPlaceholderKey", value: function getEscapedPlaceholderKey(_ref2) { var key = _ref2.key, quoteChar = _ref2.quoteChar; return key.replace(new RegExp((0, _utils.escapeRegExp)('\\' + quoteChar), 'gu'), quoteChar); } // Decimal, binary, or hex numbers }, { key: "getNumberToken", value: function getNumberToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].NUMBER, regex: this.NUMBER_REGEX }); } // Punctuation and symbols }, { key: "getOperatorToken", value: function getOperatorToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].OPERATOR, regex: this.OPERATOR_REGEX }); } }, { key: "getReservedWordToken", value: function getReservedWordToken(input, previousToken) { // A reserved word cannot be preceded by a "." // this makes it so in "mytable.from", "from" is not considered a reserved word if (previousToken && previousToken.value && previousToken.value === '.') { return undefined; } return this.getTopLevelReservedToken(input) || this.getNewlineReservedToken(input) || this.getTopLevelReservedTokenNoIndent(input) || this.getPlainReservedToken(input); } }, { key: "getTopLevelReservedToken", value: function getTopLevelReservedToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].RESERVED_TOP_LEVEL, regex: this.RESERVED_TOP_LEVEL_REGEX }); } }, { key: "getNewlineReservedToken", value: function getNewlineReservedToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].RESERVED_NEWLINE, regex: this.RESERVED_NEWLINE_REGEX }); } }, { key: "getTopLevelReservedTokenNoIndent", value: function getTopLevelReservedTokenNoIndent(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].RESERVED_TOP_LEVEL_NO_INDENT, regex: this.RESERVED_TOP_LEVEL_NO_INDENT_REGEX }); } }, { key: "getPlainReservedToken", value: function getPlainReservedToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].RESERVED, regex: this.RESERVED_PLAIN_REGEX }); } }, { key: "getWordToken", value: function getWordToken(input) { return this.getTokenOnFirstMatch({ input: input, type: _tokenTypes["default"].WORD, regex: this.WORD_REGEX }); } }, { key: "getTokenOnFirstMatch", value: function getTokenOnFirstMatch(_ref3) { var input = _ref3.input, type = _ref3.type, regex = _ref3.regex; var matches = input.match(regex); return matches ? { type: type, value: matches[1] } : undefined; } }]); return Tokenizer; }(); exports["default"] = Tokenizer; module.exports = exports.default;