@hokaccha/sql-formatter
Version:
Format whitespace in a SQL query to make it more readable
85 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPlaceholderRegex = exports.createParenRegex = exports.createStringPattern = exports.createStringRegex = exports.createWordRegex = exports.createReservedWordRegex = exports.createLineCommentRegex = exports.createOperatorRegex = void 0;
const utils_1 = require("../utils");
function createOperatorRegex(multiLetterOperators) {
return new RegExp(`^(${(0, utils_1.sortByLengthDesc)(multiLetterOperators)
.map(utils_1.escapeRegExp)
.join("|")}|.)`, "u");
}
exports.createOperatorRegex = createOperatorRegex;
function createLineCommentRegex(lineCommentTypes) {
return new RegExp(`^((?:${lineCommentTypes
.map((c) => (0, utils_1.escapeRegExp)(c))
.join("|")}).*?)(?:\r\n|\r|\n|$)`, "u");
}
exports.createLineCommentRegex = createLineCommentRegex;
function createReservedWordRegex(reservedWords) {
if (reservedWords.length === 0) {
return new RegExp(`^\b$`, "u");
}
const reservedWordsPattern = (0, utils_1.sortByLengthDesc)(reservedWords)
.join("|")
.replace(/ /gu, "\\s+");
return new RegExp(`^(${reservedWordsPattern})\\b`, "iu");
}
exports.createReservedWordRegex = createReservedWordRegex;
function createWordRegex(specialChars = []) {
return new RegExp(`^([\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}\\p{Connector_Punctuation}\\p{Join_Control}${specialChars.join("")}]+)`, "u");
}
exports.createWordRegex = createWordRegex;
function createStringRegex(stringTypes) {
return new RegExp("^(" + createStringPattern(stringTypes) + ")", "u");
}
exports.createStringRegex = createStringRegex;
// 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) {
const patterns = {
"``": "((`[^`]*($|`))+)",
"{}": "((\\{[^\\}]*($|\\}))+)",
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)",
'""': '(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)',
"''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
"N''": "((N'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
"U&''": "((U&'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
'U&""': '((U&"[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)',
"r''": "(([rR]'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
'r""': '(([rR]"[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)',
"b''": "(([bB]'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
'b""': '(([bB]"[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)',
$$: "((?<tag>\\$\\w*\\$)[\\s\\S]*?(?:\\k<tag>|$))",
};
return stringTypes.map((t) => patterns[t]).join("|");
}
exports.createStringPattern = createStringPattern;
function createParenRegex(parens) {
return new RegExp("^(" + parens.map(escapeParen).join("|") + ")", "iu");
}
exports.createParenRegex = createParenRegex;
function escapeParen(paren) {
if (paren.length === 1) {
// A single punctuation character
return (0, utils_1.escapeRegExp)(paren);
}
else {
// longer word
return "\\b" + paren + "\\b";
}
}
function createPlaceholderRegex(types, pattern) {
if ((0, utils_1.isEmpty)(types)) {
return null;
}
const typesRegex = types.map(utils_1.escapeRegExp).join("|");
return new RegExp(`^((?:${typesRegex})(?:${pattern}))`, "u");
}
exports.createPlaceholderRegex = createPlaceholderRegex;
//# sourceMappingURL=regexFactory.js.map