Skip to content

Commit

Permalink
Update SQLX formatting to use typeid-js instead of uuidv4 (#1542)
Browse files Browse the repository at this point in the history
* Update SQLX formatting to use typeid-js instead of uuidv4

* Try import order fix

* Fix tests failing due to UUID length

* Add some opinionated types
  • Loading branch information
Ekrekr authored Sep 19, 2023
1 parent a00b779 commit 899fc65
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"tslint-react": "^3.6.0",
"typed-css-modules": "^0.6.8",
"typedoc": "^0.19.2",
"typeid-js": "0.3.0",
"typescript": "^3.8.3",
"uglify-es": "^3.3.9",
"uglify-js": "^3.7.7",
Expand Down
1 change: 1 addition & 0 deletions packages/@dataform/cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ externals = [
"snowflake-sdk",
"sql-formatter",
"ssh2",
"typeid-js",
"vm2",
]

Expand Down
3 changes: 1 addition & 2 deletions sqlx/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ ts_library(
"//sqlx:sqlx",
"@npm//@types/js-beautify",
"@npm//@types/node",
"@npm//@types/uuid",
"@npm//js-beautify",
"@npm//sql-formatter",
"@npm//uuid",
"@npm//typeid-js",
],
)
28 changes: 18 additions & 10 deletions sqlx/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { promisify } from "util";
import { ErrorWithCause } from "df/common/errors/errors";
import { WarehouseType } from "df/core/adapters";
import { SyntaxTreeNode, SyntaxTreeNodeType } from "df/sqlx/lexer";
import { v4 as uuidv4 } from "uuid";
import { typeid } from "typeid-js";

const JS_BEAUTIFY_OPTIONS = {
indent_size: 2,
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function formatFile(
return formattedText;
}

function formatSqlx(node: SyntaxTreeNode, indent: string = "", warehouse: WarehouseType) {
function formatSqlx(node: SyntaxTreeNode, indent = "", warehouse: WarehouseType) {
const { sqlxStatements, javascriptBlocks, innerSqlBlocks } = separateSqlxIntoParts(
node.children()
);
Expand Down Expand Up @@ -149,6 +149,8 @@ function separateSqlxIntoParts(nodeContents: Array<string | SyntaxTreeNode>) {
case SyntaxTreeNodeType.SQL_STATEMENT_SEPARATOR:
sqlxStatements.push([]);
return;
default:
// Unknown parts are handled the same as strings.
}
}
sqlxStatements[sqlxStatements.length - 1].push(child);
Expand Down Expand Up @@ -196,9 +198,12 @@ function stripUnformattableText(
function generatePlaceholderId() {
// Add a leading character to ensure that the placeholder doesn't start with a number.
// Identifiers beginning with a number cause errors when formatting.
return '_' + uuidv4()
.replace(/-/g, "")
.substring(0, 16);
// A shortened UUID is used to facilitate same-line strings.
// The last chunk of the UUID is used as it is the most random.
const uuid = typeid("p")
.toString()
.replace(/-/g, "");
return "_" + uuid.substring(uuid.length - 16);
}

function replacePlaceholders(
Expand Down Expand Up @@ -241,7 +246,10 @@ function formatPlaceholderInSqlx(
sqlx: string
) {
const wholeLine = getWholeLineContainingPlaceholderId(placeholderId, sqlx);
const indent = " ".repeat(wholeLine.length - wholeLine.trimLeft().length);
if (!wholeLine) {
return sqlx;
}
const indent = " ".repeat(wholeLine.length - wholeLine.trimStart().length);
const formattedPlaceholder = formatSqlQueryPlaceholder(placeholderSyntaxNode, indent);

// Replace the placeholder entirely if (a) it fits on one line and (b) it isn't a comment.
Expand Down Expand Up @@ -277,9 +285,9 @@ function formatSqlQueryPlaceholder(node: SyntaxTreeNode, jsIndent: string): stri
return formatJavaScriptPlaceholder(node, jsIndent);
case SyntaxTreeNodeType.SQL_LITERAL_STRING:
case SyntaxTreeNodeType.SQL_COMMENT:
return formatEveryLine(node.concatenate(), line => `${jsIndent}${line.trimLeft()}`);
return formatEveryLine(node.concatenate(), line => `${jsIndent}${line.trimStart()}`);
case SyntaxTreeNodeType.SQL_LITERAL_MULTILINE_STRING:
return `${jsIndent}${node.concatenate().trimLeft()}`;
return `${jsIndent}${node.concatenate().trimStart()}`;
default:
throw new Error(`Unrecognized SyntaxTreeNodeType: ${node.type}`);
}
Expand Down Expand Up @@ -309,7 +317,7 @@ function getWholeLineContainingPlaceholderId(placeholderId: string, text: string
const regexpEscapedPlaceholderId = placeholderId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// This RegExp is safe because we only use a 'placeholderId' that this file has generated.
// tslint:disable-next-line: tsr-detect-non-literal-regexp
return text.match(new RegExp(".*" + regexpEscapedPlaceholderId + ".*"))[0];
return text.match(new RegExp(".*" + regexpEscapedPlaceholderId + ".*"))?.[0];
}

function postProcessFormattedSqlx(formattedSql: string) {
Expand All @@ -318,7 +326,7 @@ function postProcessFormattedSqlx(formattedSql: string) {
const lineHasContent = currentLine.trim().length > 0;
if (lineHasContent) {
previousLineHadContent = true;
return `${accumulatedSql}\n${currentLine.trimRight()}`;
return `${accumulatedSql}\n${currentLine.trimEnd()}`;
}
if (previousLineHadContent) {
previousLineHadContent = false;
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9273,6 +9273,13 @@ typedoc@^0.19.2:
shelljs "^0.8.4"
typedoc-default-themes "^0.11.4"

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/typeid-js/-/typeid-js-0.3.0.tgz#704dedde2382fcb4e5c15c54d285f0209d37f9cf"
integrity sha512-A1EmvIWG6xwYRfHuYUjPltHqteZ1EiDG+HOmbIYXeHUVztmnGrPIfU9KIK1QC30x59ko0r4JsMlwzsALCyiB3Q==
dependencies:
uuidv7 "^0.4.4"

typescript@^3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
Expand Down Expand Up @@ -9623,6 +9630,11 @@ uuid@^9.0.0:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5"
integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==

uuidv7@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/uuidv7/-/uuidv7-0.4.4.tgz#e7ffd7981f590c478fb8868eff4bb3bc55fa90e6"
integrity sha512-jjRGChg03uGp9f6wQYSO8qXkweJwRbA5WRuEQE8xLIiehIzIIi23qZSzsyvZPCPoFqkeLtZuz7Plt1LGukAInA==

validator@^13.7.0:
version "13.7.0"
resolved "https://registry.yarnpkg.com/validator/-/validator-13.7.0.tgz#4f9658ba13ba8f3d82ee881d3516489ea85c0857"
Expand Down

0 comments on commit 899fc65

Please sign in to comment.