diff --git a/src/languages/bigquery.formatter.ts b/src/languages/bigquery.formatter.ts index 94d97ec94b..68445dd4ca 100644 --- a/src/languages/bigquery.formatter.ts +++ b/src/languages/bigquery.formatter.ts @@ -892,9 +892,9 @@ function combineParameterizedTypes(tokens: Token[]) { const endIndex = findClosingAngleBracketIndex(tokens, i + 1); const typeDefTokens = tokens.slice(i, endIndex + 1); processed.push({ - ...token, - value: typeDefTokens.map(t => t.value).join(''), - text: typeDefTokens.map(t => t.text).join(''), + type: TokenType.IDENT, + value: typeDefTokens.map(formatTypeDefToken('value')).join(''), + text: typeDefTokens.map(formatTypeDefToken('text')).join(''), }); i = endIndex; } else { @@ -904,6 +904,16 @@ function combineParameterizedTypes(tokens: Token[]) { return processed; } +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; for (let i = startIndex; i < tokens.length; i++) { diff --git a/test/bigquery.test.ts b/test/bigquery.test.ts index cb4ed3a99a..8f0161b48b 100644 --- a/test/bigquery.test.ts +++ b/test/bigquery.test.ts @@ -121,6 +121,35 @@ describe('BigQueryFormatter', () => { `); }); + // Issue #279 + it('supports parametric STRUCT with named fields', () => { + expect(format('SELECT STRUCT(1,"foo"), STRUCT>([1,2,3]);')) + .toBe(dedent` + SELECT + STRUCT(1, "foo"), + STRUCT>([1, 2, 3]); + `); + }); + + it('supports uppercasing of STRUCT', () => { + expect(format('select struct(1,"foo");', { keywordCase: 'upper' })) + .toBe(dedent` + SELECT + STRUCT(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(1,"foo");', { keywordCase: 'lower' })) + .toBe(dedent` + select + STRUCT(1, "foo"); + `); + }); + it('supports parameterised types', () => { const result = format( `