File tree Expand file tree Collapse file tree 1 file changed +40
-1
lines changed
apps/webapp/app/components/code Expand file tree Collapse file tree 1 file changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -338,11 +338,50 @@ export function TSQLEditor(opts: TSQLEditorProps) {
338338 ) ;
339339}
340340
341+ // SQL keywords that legitimately appear before parentheses with a space
342+ const SQL_KEYWORDS_BEFORE_PAREN = new Set ( [
343+ "IN" ,
344+ "NOT" ,
345+ "EXISTS" ,
346+ "OVER" ,
347+ "USING" ,
348+ "VALUES" ,
349+ "BETWEEN" ,
350+ "LIKE" ,
351+ "AND" ,
352+ "OR" ,
353+ "ON" ,
354+ "SET" ,
355+ "INTO" ,
356+ "TABLE" ,
357+ "CASE" ,
358+ "WHEN" ,
359+ "THEN" ,
360+ "ELSE" ,
361+ "AS" ,
362+ "FROM" ,
363+ "WHERE" ,
364+ "HAVING" ,
365+ "JOIN" ,
366+ "SELECT" ,
367+ ] ) ;
368+
341369export function autoFormatSQL ( sql : string ) {
342- return formatSQL ( sql , {
370+ let formatted = formatSQL ( sql , {
343371 language : "sql" ,
344372 keywordCase : "upper" ,
345373 indentStyle : "standard" ,
346374 linesBetweenQueries : 2 ,
347375 } ) ;
376+
377+ // sql-formatter adds a space before ( for unknown/custom functions (e.g. timeBucket ())
378+ // Remove that space for anything that isn't a SQL keyword
379+ formatted = formatted . replace ( / ( \b \w + ) \s + \( / g, ( match , name ) => {
380+ if ( SQL_KEYWORDS_BEFORE_PAREN . has ( name . toUpperCase ( ) ) ) {
381+ return match ;
382+ }
383+ return `${ name } (` ;
384+ } ) ;
385+
386+ return formatted ;
348387}
You can’t perform that action at this time.
0 commit comments