Skip to content

Commit d2b0026

Browse files
committed
feat: support multiple statements
1 parent 0c81168 commit d2b0026

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/core/Formatter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export default class Formatter {
7171
else if (token.type === tokenTypes.PLACEHOLDER) {
7272
formattedQuery = this.formatPlaceholder(token, formattedQuery);
7373
}
74+
else if (token.type === tokenTypes.END_STATEMENT) {
75+
formattedQuery = this.formatWithoutSpaces(token, formattedQuery);
76+
}
7477
else if (token.value === ",") {
7578
formattedQuery = this.formatComma(token, formattedQuery);
7679
}

src/core/Tokenizer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class Tokenizer {
1818
*/
1919
constructor(cfg) {
2020
this.WHITESPACE_REGEX = /^(\s+)/;
21+
this.END_STATEMENT_REGEX = /^(;\n)/;
2122
this.NUMBER_REGEX = /^((-\s*)?[0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)\b/;
2223
this.OPERATOR_REGEX = /^(!=|<>|==|<=|>=|!<|!>|\|\||::|->>|->|~~\*|~~|!~~\*|!~~|~\*|!~\*|!~|.)/;
2324

@@ -133,6 +134,7 @@ export default class Tokenizer {
133134

134135
getNextToken(input, previousToken) {
135136
return this.getWhitespaceToken(input) ||
137+
this.getEndStatementToken(input) ||
136138
this.getCommentToken(input) ||
137139
this.getStringToken(input) ||
138140
this.getOpenParenToken(input) ||
@@ -152,6 +154,14 @@ export default class Tokenizer {
152154
});
153155
}
154156

157+
getEndStatementToken(input) {
158+
return this.getTokenOnFirstMatch({
159+
input,
160+
type: tokenTypes.END_STATEMENT,
161+
regex: this.END_STATEMENT_REGEX
162+
});
163+
}
164+
155165
getCommentToken(input) {
156166
return this.getLineCommentToken(input) || this.getBlockCommentToken(input);
157167
}

src/core/tokenTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ export default {
1414
LINE_COMMENT: "line-comment",
1515
BLOCK_COMMENT: "block-comment",
1616
NUMBER: "number",
17-
PLACEHOLDER: "placeholder"
17+
PLACEHOLDER: "placeholder",
18+
END_STATEMENT: "end-statement"
1819
};

test/behavesLikeSqlFormatter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,25 @@ 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;bar;");
451+
expect(format("foo\n;bar;")).toBe("foo;bar;");
452+
expect(format("foo;\nbar;\n\n\n")).toBe("foo;\nbar;");
453+
expect(format("foo;\n\n\nbar;\n\n\n")).toBe("foo;\nbar;");
454+
455+
const result = format("SELECT count(*),Column1 FROM Table1;\nSELECT count(*),Column1 FROM Table2;");
456+
expect(result).toBe(
457+
"SELECT\n" +
458+
" count(*),\n" +
459+
" Column1\n" +
460+
"FROM\n" +
461+
" Table1;\n" +
462+
"SELECT\n" +
463+
" count(*),\n" +
464+
" Column1\n" +
465+
"FROM\n" +
466+
" Table2;"
467+
);
468+
});
448469
}

0 commit comments

Comments
 (0)