Repository: sql-formatter-org/sql-formatter
Pull request #357 (2022-07-28 06:50:30)

Equalize whitespaces for keywordCase upper/lower
Fixes https://github.com/sql-formatter-org/sql-formatter/issues/356


Patch:
diff --git a/src/lexer/Tokenizer.ts b/src/lexer/Tokenizer.ts
index 9137fae5..c64ac89b 100644
--- a/src/lexer/Tokenizer.ts
+++ b/src/lexer/Tokenizer.ts
@@ -1,6 +1,7 @@
 import { Token, TokenType } from 'src/lexer/token';
 import * as regex from 'src/lexer/regexFactory';
 import * as regexTypes from 'src/lexer/regexTypes';
+import { equalizeWhitespace } from 'src/utils'
 
 import { escapeRegExp } from './regexUtil';
 import TokenizerEngine, { type TokenRule } from './TokenizerEngine';
@@ -76,36 +77,39 @@ export default class Tokenizer {
       },
       [TokenType.RESERVED_CASE_START]: {
         regex: /[Cc][Aa][Ss][Ee]\b/uy,
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
+      },
+      [TokenType.RESERVED_CASE_END]: {
+        regex: /[Ee][Nn][Dd]\b/uy,
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
-      [TokenType.RESERVED_CASE_END]: { regex: /[Ee][Nn][Dd]\b/uy, value: v => v.toUpperCase() },
       [TokenType.RESERVED_COMMAND]: {
         regex: regex.reservedWord(cfg.reservedCommands, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_SET_OPERATION]: {
         regex: regex.reservedWord(cfg.reservedSetOperations, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_DEPENDENT_CLAUSE]: {
         regex: regex.reservedWord(cfg.reservedDependentClauses, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_JOIN]: {
         regex: regex.reservedWord(cfg.reservedJoins, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_KEYWORD]: {
         regex: regex.reservedWord(cfg.reservedKeywords, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_FUNCTION_NAME]: {
         regex: regex.reservedWord(cfg.reservedFunctionNames, cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.RESERVED_LOGICAL_OPERATOR]: {
         regex: regex.reservedWord(cfg.reservedLogicalOperators ?? ['AND', 'OR'], cfg.identChars),
-        value: v => v.toUpperCase(),
+        value: v => equalizeWhitespace(v.toUpperCase()),
       },
       [TokenType.NAMED_PARAMETER]: {
         regex: regex.parameter(


Test patch:
diff --git a/test/options/keywordCase.ts b/test/options/keywordCase.ts
index 2d3558b6..e3f8a6f3 100644
--- a/test/options/keywordCase.ts
+++ b/test/options/keywordCase.ts
@@ -61,4 +61,47 @@ export default function supportsKeywordCase(format: FormatFn) {
         'distinct' AS foo
     `);
   });
+
+  // regression test for #356
+  it('formats multi-word reserved commands into single line', () => {
+    const input = `
+      insert
+      into
+      Customers (ID, MoneyBalance, Address, City) values (12,-123.4, 'Skagen 2111','Stv');`;
+    const expected = dedent`
+      INSERT INTO
+        Customers (ID, MoneyBalance, Address, City)
+      VALUES
+        (12, -123.4, 'Skagen 2111', 'Stv');
+    `;
+
+    expect(format(input, { keywordCase: 'upper' })).toBe(expected);
+
+    const inputSelect = `
+      select * from mytable
+      inner
+      join
+      mytable2 on mytable1.col1 = mytable2.col1
+      where mytable2.col1 = 5
+      group
+      bY mytable1.col2
+      order
+      by
+      mytable2.col3;`;
+    const expectedSelect = dedent`
+      SELECT
+        *
+      FROM
+        mytable
+        INNER JOIN mytable2 ON mytable1.col1 = mytable2.col1
+      WHERE
+        mytable2.col1 = 5
+      GROUP BY
+        mytable1.col2
+      ORDER BY
+        mytable2.col3;
+    `;
+
+    expect(format(inputSelect, { keywordCase: 'upper' })).toBe(expectedSelect);
+  });
 }