Skip to content

Commit

Permalink
Improve upper- and lower-casing support
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jul 5, 2022
1 parent d77a21d commit 7e67cb2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/languages/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,9 @@ function combineParameterizedTypes(tokens: Token[]) {
const endIndex = findClosingAngleBracketIndex(tokens, i + 1);
const typeDefTokens = tokens.slice(i, endIndex + 1);
processed.push({
...token,
value: typeDefTokens.map(formatTypeDefToken).join(''),
text: typeDefTokens.map(formatTypeDefToken).join(''),
type: TokenType.IDENT,
value: typeDefTokens.map(formatTypeDefToken('value')).join(''),
text: typeDefTokens.map(formatTypeDefToken('text')).join(''),
});
i = endIndex;
} else {
Expand All @@ -907,13 +907,15 @@ function combineParameterizedTypes(tokens: Token[]) {
return processed;
}

function formatTypeDefToken({ type, value, text }: Token): string {
if (type === TokenType.IDENT || value === ',') {
return text + ' ';
} else {
return text;
}
}
const formatTypeDefToken =
(key: 'text' | 'value') =>
(token: Token): string => {
if (token.type === TokenType.IDENT || token.value === ',') {
return token[key] + ' ';
} else {
return token[key];
}
};

function findClosingAngleBracketIndex(tokens: Token[], startIndex: number): number {
let level = 0;
Expand Down
19 changes: 19 additions & 0 deletions test/bigquery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ describe('BigQueryFormatter', () => {
`);
});

it('supports uppercasing of STRUCT', () => {
expect(format('select struct<Nr int64, myName string>(1,"foo");', { keywordCase: 'upper' }))
.toBe(dedent`
SELECT
STRUCT<Nr INT64, myName STRING>(1, "foo");
`);
});

// XXX: This is hard to achieve with our current type-parameter processing hack.
// At least we're preserving the case of identifier names here,
// and lowercasing is luckily less used than uppercasing.
it('does not support lowercasing of STRUCT', () => {
expect(format('SELECT STRUCT<Nr INT64, myName STRING>(1,"foo");', { keywordCase: 'lower' }))
.toBe(dedent`
select
STRUCT<Nr INT64, myName STRING>(1, "foo");
`);
});

it('supports parameterised types', () => {
const result = format(
`
Expand Down

0 comments on commit 7e67cb2

Please sign in to comment.