File tree Expand file tree Collapse file tree 4 files changed +36
-1
lines changed
Expand file tree Collapse file tree 4 files changed +36
-1
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 ] + ) ? | 0 x [ 0 - 9 a - f A - F ] + | 0 b [ 0 1 ] + ) \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 }
Original file line number Diff line number Diff 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} ;
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments