Skip to content

Commit 15e1188

Browse files
authored
Merge pull request #58 from rafaelrpinto/master
feat: support multiple statements
2 parents 0c81168 + 9ecd1b0 commit 15e1188

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/core/Formatter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ export default class Formatter {
7777
else if (token.value === ":") {
7878
formattedQuery = this.formatWithSpaceAfter(token, formattedQuery);
7979
}
80-
else if (token.value === "." || token.value === ";") {
80+
else if (token.value === ".") {
8181
formattedQuery = this.formatWithoutSpaces(token, formattedQuery);
8282
}
83+
else if (token.value === ";") {
84+
formattedQuery = this.formatQuerySeparator(token, formattedQuery);
85+
}
8386
else {
8487
formattedQuery = this.formatWithSpaces(token, formattedQuery);
8588
}
@@ -185,6 +188,10 @@ export default class Formatter {
185188
return query + token.value + " ";
186189
}
187190

191+
formatQuerySeparator(token, query) {
192+
return this.trimTrailingWhitespace(query) + token.value + "\n";
193+
}
194+
188195
addNewline(query) {
189196
return trimEnd(query) + "\n" + this.indentation.getIndent();
190197
}

test/behavesLikeSqlFormatter.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,24 @@ export default function behavesLikeSqlFormatter(language) {
445445
expect(format("foo !~* 'hello'")).toBe("foo !~* 'hello'");
446446
expect(format("foo !~~* 'hello'")).toBe("foo !~~* 'hello'");
447447
});
448+
449+
it("keeps separation between multiple statements", function() {
450+
expect(format("foo;bar;")).toBe("foo;\nbar;");
451+
expect(format("foo\n;bar;")).toBe("foo;\nbar;");
452+
expect(format("foo\n\n\n;bar;\n\n")).toBe("foo;\nbar;");
453+
454+
const result = format("SELECT count(*),Column1 FROM Table1;\nSELECT count(*),Column1 FROM Table2;");
455+
expect(result).toBe(
456+
"SELECT\n" +
457+
" count(*),\n" +
458+
" Column1\n" +
459+
"FROM\n" +
460+
" Table1;\n" +
461+
"SELECT\n" +
462+
" count(*),\n" +
463+
" Column1\n" +
464+
"FROM\n" +
465+
" Table2;"
466+
);
467+
});
448468
}

0 commit comments

Comments
 (0)