forked from dataform-co/dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.ts
300 lines (271 loc) · 10.6 KB
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import * as crypto from "crypto";
import * as fs from "fs";
import * as jsBeautify from "js-beautify";
import * as sqlFormatter from "sql-formatter";
import { promisify } from "util";
import { ErrorWithCause } from "df/common/errors/errors";
import { SyntaxTreeNode, SyntaxTreeNodeType } from "df/sqlx/lexer";
import { v4 as uuidv4 } from "uuid";
const JS_BEAUTIFY_OPTIONS: JsBeautifyOptions = {
indent_size: 2,
preserve_newlines: true,
max_preserve_newlines: 2
};
const MAX_SQL_FORMAT_ATTEMPTS = 5;
export function format(text: string, fileExtension: string) {
try {
switch (fileExtension) {
case "sqlx":
return postProcessFormattedSqlx(formatSqlx(SyntaxTreeNode.create(text)));
case "js":
return `${formatJavaScript(text).trim()}\n`;
default:
return text;
}
} catch (e) {
throw new ErrorWithCause(`Unable to format "${text?.substring(0, 20)}...".`, e);
}
}
export async function formatFile(
filename: string,
options?: {
overwriteFile?: boolean;
}
) {
const fileExtension = filename.split(".").slice(-1)[0];
const originalFileContent = await promisify(fs.readFile)(filename, "utf8");
const formattedText = format(originalFileContent, fileExtension);
if (formattedText !== format(formattedText, fileExtension)) {
throw new Error("Formatter unable to determine final formatted form.");
}
const noWhiteSpaceFormatted = formattedText.replace(/\s/g, "");
const noWhiteSpaceOriginal = originalFileContent.replace(/\s/g, "");
if (noWhiteSpaceFormatted.length !== noWhiteSpaceOriginal.length) {
const isLonger = noWhiteSpaceFormatted.length > noWhiteSpaceOriginal.length;
throw new Error(`Formatter ${isLonger ? "added" : "removed"} non-whitespace characters`);
}
if (options && options.overwriteFile) {
await promisify(fs.writeFile)(filename, formattedText);
}
return formattedText;
}
function formatSqlx(node: SyntaxTreeNode, indent: string = "") {
const { sqlxStatements, javascriptBlocks, innerSqlBlocks } = separateSqlxIntoParts(
node.children()
);
// First, format the JS blocks (including the config block).
const formattedJsCodeBlocks = javascriptBlocks.map(jsCodeBlock =>
formatJavaScript(jsCodeBlock.concatenate())
);
// Second, format all the SQLX statements, replacing any placeholders with their formatted form.
const formattedSqlxStatements = sqlxStatements.map(sqlxStatement => {
const placeholders: {
[placeholderId: string]: SyntaxTreeNode | string;
} = {};
const unformattedPlaceholderSql = stripUnformattableText(sqlxStatement, placeholders).join("");
const formattedPlaceholderSql = formatSql(unformattedPlaceholderSql);
return formatEveryLine(
replacePlaceholders(formattedPlaceholderSql, placeholders),
line => `${indent}${line}`
);
});
// Third, format all "inner" SQL blocks, e.g. "pre_operations { ... }".
const formattedSqlCodeBlocks = innerSqlBlocks.map((sqlCodeBlock): string => {
// Strip out the declaration of this block, format the internals then add the declaration back.
const firstPart = sqlCodeBlock.children()[0] as string;
const upToFirstBrace = firstPart.slice(0, firstPart.indexOf("{") + 1);
const lastPart = sqlCodeBlock.children()[sqlCodeBlock.children().length - 1] as string;
const lastBraceOnwards = lastPart.slice(lastPart.lastIndexOf("}"));
const sqlCodeBlockWithoutOuterBraces =
sqlCodeBlock.children().length === 1
? new SyntaxTreeNode(SyntaxTreeNodeType.SQL, [
firstPart.slice(firstPart.indexOf("{") + 1, firstPart.lastIndexOf("}"))
])
: new SyntaxTreeNode(SyntaxTreeNodeType.SQL, [
firstPart.slice(firstPart.indexOf("{") + 1),
...sqlCodeBlock.children().slice(1, -1),
lastPart.slice(0, lastPart.lastIndexOf("}"))
]);
return `${upToFirstBrace}
${formatSqlx(sqlCodeBlockWithoutOuterBraces, " ")}
${lastBraceOnwards}`;
});
const finalText = `
${formattedJsCodeBlocks.join("\n\n")}
${formattedSqlxStatements.join(`\n\n${indent}---\n\n`)}
${formattedSqlCodeBlocks.join("\n\n")}
`;
return `${indent}${finalText.trim()}`;
}
function separateSqlxIntoParts(nodeContents: Array<string | SyntaxTreeNode>) {
const sqlxStatements: Array<Array<string | SyntaxTreeNode>> = [[]];
const javascriptBlocks: SyntaxTreeNode[] = [];
const innerSqlBlocks: SyntaxTreeNode[] = [];
nodeContents.forEach(child => {
if (typeof child !== "string") {
switch (child.type) {
case SyntaxTreeNodeType.JAVASCRIPT:
javascriptBlocks.push(child);
return;
case SyntaxTreeNodeType.SQL:
innerSqlBlocks.push(child);
return;
case SyntaxTreeNodeType.SQL_STATEMENT_SEPARATOR:
sqlxStatements.push([]);
return;
}
}
sqlxStatements[sqlxStatements.length - 1].push(child);
});
return {
sqlxStatements,
javascriptBlocks,
innerSqlBlocks
};
}
function stripUnformattableText(
sqlxStatementParts: Array<string | SyntaxTreeNode>,
placeholders: {
[placeholderId: string]: SyntaxTreeNode | string;
}
) {
return sqlxStatementParts.map(part => {
if (typeof part !== "string") {
const placeholderId = generatePlaceholderId();
switch (part.type) {
case SyntaxTreeNodeType.SQL_LITERAL_STRING:
case SyntaxTreeNodeType.JAVASCRIPT_TEMPLATE_STRING_PLACEHOLDER: {
placeholders[placeholderId] = part;
return placeholderId;
}
case SyntaxTreeNodeType.SQL_COMMENT: {
// sql-formatter knows how to format comments (as long as they keep to a single line);
// give it a hint.
const commentPlaceholderId = part.concatenate().startsWith("--")
? `--${placeholderId}`
: `/*${placeholderId}*/`;
placeholders[commentPlaceholderId] = part;
return commentPlaceholderId;
}
default:
throw new Error(`Misplaced SyntaxTreeNodeType inside SQLX: ${part.type}`);
}
}
return part;
});
}
function generatePlaceholderId() {
return uuidv4()
.replace(/-/g, "")
.substring(0, 16);
}
function replacePlaceholders(
formattedSql: string,
placeholders: {
[placeholderId: string]: SyntaxTreeNode | string;
}
) {
return Object.keys(placeholders).reduce((partiallyFormattedSql, placeholderId) => {
const placeholderValue = placeholders[placeholderId];
if (typeof placeholderValue === "string") {
return partiallyFormattedSql.replace(placeholderId, placeholderValue);
}
return formatPlaceholderInSqlx(placeholderId, placeholderValue, partiallyFormattedSql);
}, formattedSql);
}
function formatJavaScript(text: string) {
return jsBeautify.js(text, JS_BEAUTIFY_OPTIONS);
}
function formatSql(text: string) {
let formatted = sqlFormatter.format(text) as string;
// Unfortunately sql-formatter does not always produce final formatted output (even on plain SQL) in a single pass.
for (let attempts = 0; attempts < MAX_SQL_FORMAT_ATTEMPTS; attempts++) {
const newFormatted = sqlFormatter.format(formatted) as string;
if (newFormatted === formatted) {
return newFormatted;
}
formatted = newFormatted;
}
throw new Error(
`SQL formatter was unable to determine final formatted form within ${MAX_SQL_FORMAT_ATTEMPTS} attempts. Original text: ${text}`
);
}
function formatPlaceholderInSqlx(
placeholderId: string,
placeholderSyntaxNode: SyntaxTreeNode,
sqlx: string
) {
const wholeLine = getWholeLineContainingPlaceholderId(placeholderId, sqlx);
const indent = " ".repeat(wholeLine.length - wholeLine.trimLeft().length);
const formattedPlaceholder = formatSqlQueryPlaceholder(placeholderSyntaxNode, indent);
// Replace the placeholder entirely if (a) it fits on one line and (b) it isn't a comment.
// Otherwise, push the replacement onto its own line.
if (
placeholderSyntaxNode.type !== SyntaxTreeNodeType.SQL_COMMENT &&
!formattedPlaceholder.includes("\n")
) {
return sqlx.replace(placeholderId, () => formattedPlaceholder.trim());
}
// Push multi-line placeholders to their own lines, if they're not already on one.
const [textBeforePlaceholder, textAfterPlaceholder] = wholeLine.split(placeholderId);
const newLines: string[] = [];
if (textBeforePlaceholder.trim().length > 0) {
newLines.push(`${indent}${textBeforePlaceholder.trim()}`);
}
newLines.push(formattedPlaceholder);
if (textAfterPlaceholder.trim().length > 0) {
newLines.push(`${indent}${textAfterPlaceholder.trim()}`);
}
return sqlx.replace(wholeLine, newLines.join("\n"));
}
function formatSqlQueryPlaceholder(node: SyntaxTreeNode, jsIndent: string): string {
switch (node.type) {
case SyntaxTreeNodeType.JAVASCRIPT_TEMPLATE_STRING_PLACEHOLDER:
return formatJavaScriptPlaceholder(node, jsIndent);
case SyntaxTreeNodeType.SQL_LITERAL_STRING:
case SyntaxTreeNodeType.SQL_COMMENT:
return formatEveryLine(node.concatenate(), line => `${jsIndent}${line.trimLeft()}`);
default:
throw new Error(`Unrecognized SyntaxTreeNodeType: ${node.type}`);
}
}
function formatJavaScriptPlaceholder(node: SyntaxTreeNode, jsIndent: string) {
const formattedJs = formatJavaScript(node.concatenate());
const textInsideBraces = formattedJs.slice(
formattedJs.indexOf("{") + 1,
formattedJs.lastIndexOf("}")
);
// If the formatted JS is only a single line, trim all whitespace so that it stays a single line.
const finalJs = textInsideBraces.trim().includes("\n")
? `\${${textInsideBraces}}`
: `\${${textInsideBraces.trim()}}`;
return formatEveryLine(finalJs, line => `${jsIndent}${line}`);
}
function formatEveryLine(text: string, mapFn: (line: string) => string) {
return text
.split("\n")
.map(mapFn)
.join("\n");
}
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];
}
function postProcessFormattedSqlx(formattedSql: string) {
let previousLineHadContent = false;
formattedSql = formattedSql.split("\n").reduce((accumulatedSql, currentLine) => {
const lineHasContent = currentLine.trim().length > 0;
if (lineHasContent) {
previousLineHadContent = true;
return `${accumulatedSql}\n${currentLine.trimRight()}`;
}
if (previousLineHadContent) {
previousLineHadContent = false;
return `${accumulatedSql}\n`;
}
return accumulatedSql;
}, "");
return `${formattedSql.trim()}\n`;
}