diff --git a/querybook/webapp/components/QueryEditor/QueryEditor.tsx b/querybook/webapp/components/QueryEditor/QueryEditor.tsx index 3cb78be90..86ae84256 100644 --- a/querybook/webapp/components/QueryEditor/QueryEditor.tsx +++ b/querybook/webapp/components/QueryEditor/QueryEditor.tsx @@ -233,11 +233,11 @@ export const QueryEditor: React.FC< ); const formatQuery = useCallback( - ( - options: ISQLFormatOptions = { - silent: false, - } - ) => { + (options: ISQLFormatOptions) => { + options = { + silent: false, // default false to throw format errors + ...options, + }; if (editorRef.current) { const indentWithTabs = editorRef.current.getOption('indentWithTabs'); diff --git a/querybook/webapp/lib/sql-helper/sql-formatter.ts b/querybook/webapp/lib/sql-helper/sql-formatter.ts index c09f8cb58..96fefcb8e 100644 --- a/querybook/webapp/lib/sql-helper/sql-formatter.ts +++ b/querybook/webapp/lib/sql-helper/sql-formatter.ts @@ -153,23 +153,7 @@ function formatEachStatement( language: string, options: ISQLFormatOptions ) { - const safeSQLFormat = (text: string) => { - try { - return sqlFormat(text, { - tabWidth: options.tabWidth, - language: getLanguageForSqlFormatter(language), - useTabs: options.useTabs, - }); - } catch (e) { - if (options.silent) { - return text; - } else { - throw e; - } - } - }; - - const formattedStatements: string[] = statements.map( + return statements.map( ({ firstKeyWord, statementText, idToTemplateTag }) => { // Use standard formatter to format let formattedStatement = statementText; @@ -177,7 +161,11 @@ function formatEachStatement( firstKeyWord && allowedStatement.has(firstKeyWord.text.toLocaleLowerCase()) ) { - formattedStatement = safeSQLFormat(statementText); + formattedStatement = sqlFormat(statementText, { + tabWidth: options.tabWidth, + language: getLanguageForSqlFormatter(language), + useTabs: options.useTabs, + }); } for (const [id, templateTag] of Object.entries(idToTemplateTag)) { @@ -190,8 +178,6 @@ function formatEachStatement( return formattedStatement; } ); - - return formattedStatements; } export function format( @@ -210,22 +196,27 @@ export function format( ...options, }; - const { processedStatements, newLineBetweenStatement } = processStatements( - query, - language, - options - ); - const formattedStatements = formatEachStatement( - processedStatements, - language, - options - ); - - return formattedStatements.reduce( - (acc, statement, index) => - acc + '\n'.repeat(newLineBetweenStatement[index]) + statement, - '' - ); + try { + const { processedStatements, newLineBetweenStatement } = + processStatements(query, language, options); + const formattedStatements = formatEachStatement( + processedStatements, + language, + options + ); + + return formattedStatements.reduce( + (acc, statement, index) => + acc + '\n'.repeat(newLineBetweenStatement[index]) + statement, + '' + ); + } catch (e) { + if (options.silent) { + return query; + } else { + throw e; + } + } } // Override according to https://github.com/sql-formatter-org/sql-formatter/blob/master/docs/language.md