UNPKG

@gethue/sql-formatter

Version:

Format whitespace in a SQL query to make it more readable

92 lines (78 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createOperatorRegex = createOperatorRegex; exports.createLineCommentRegex = createLineCommentRegex; exports.createReservedWordRegex = createReservedWordRegex; exports.createWordRegex = createWordRegex; exports.createStringRegex = createStringRegex; exports.createStringPattern = createStringPattern; exports.createParenRegex = createParenRegex; exports.createPlaceholderRegex = createPlaceholderRegex; var _utils = require("../utils"); function createOperatorRegex(multiLetterOperators) { return new RegExp("^(".concat((0, _utils.sortByLengthDesc)(multiLetterOperators).map(_utils.escapeRegExp).join('|'), "|.)"), 'u'); } function createLineCommentRegex(lineCommentTypes) { return new RegExp("^((?:".concat(lineCommentTypes.map(function (c) { return (0, _utils.escapeRegExp)(c); }).join('|'), ").*?)(?:\r\n|\r|\n|$)"), 'u'); } function createReservedWordRegex(reservedWords) { if (reservedWords.length === 0) { return new RegExp("^\b$", 'u'); } var reservedWordsPattern = (0, _utils.sortByLengthDesc)(reservedWords).join('|').replace(/ /g, '\\s+'); return new RegExp("^(".concat(reservedWordsPattern, ")\\b"), 'iu'); } function createWordRegex() { var specialChars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return new RegExp("^([\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}\\p{Connector_Punctuation}\\p{Join_Control}".concat(specialChars.join(''), "]+)"), 'u'); } function createStringRegex(stringTypes) { return new RegExp('^(' + createStringPattern(stringTypes) + ')', 'u'); } // This enables the following string patterns: // 1. backtick quoted string using `` to escape // 2. square bracket quoted string (SQL Server) using ]] to escape // 3. double quoted string using "" or \" to escape // 4. single quoted string using '' or \' to escape // 5. national character quoted string using N'' or N\' to escape // 6. Unicode single-quoted string using \' to escape // 7. Unicode double-quoted string using \" to escape // 8. PostgreSQL dollar-quoted strings function createStringPattern(stringTypes) { var patterns = { '``': '((`[^`]*($|`))+)', '{}': '((\\{[^\\}]*($|\\}))+)', '[]': '((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)', '""': '(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)', "''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", "N''": "((N'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", "U&''": "((U&'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", 'U&""': '((U&"[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)', $$: '((?<tag>\\$\\w*\\$)[\\s\\S]*?(?:\\k<tag>|$))' }; return stringTypes.map(function (t) { return patterns[t]; }).join('|'); } function createParenRegex(parens) { return new RegExp('^(' + parens.map(escapeParen).join('|') + ')', 'iu'); } function escapeParen(paren) { if (paren.length === 1) { // A single punctuation character return (0, _utils.escapeRegExp)(paren); } else { // longer word return '\\b' + paren + '\\b'; } } function createPlaceholderRegex(types, pattern) { if ((0, _utils.isEmpty)(types)) { return false; } var typesRegex = types.map(_utils.escapeRegExp).join('|'); return new RegExp("^((?:".concat(typesRegex, ")(?:").concat(pattern, "))"), 'u'); }