Skip to content

Commit

Permalink
fix: sql-formatter (#1152)
Browse files Browse the repository at this point in the history
* fix: sql-formmater

* remove originalStatementText

* try catch on whole query
  • Loading branch information
jczhong84 authored Feb 8, 2023
1 parent 4734e01 commit efdbd7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
10 changes: 5 additions & 5 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
80 changes: 29 additions & 51 deletions querybook/webapp/lib/sql-helper/sql-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ interface IProcessedStatement {
statementText: string;
idToTemplateTag: Record<string, string>;
firstKeyWord: IToken;
originalStatementText: string;
}

function tokenizeAndFormatQuery(
Expand Down Expand Up @@ -139,10 +138,6 @@ function processStatements(
statementText,
idToTemplateTag,
firstKeyWord,
originalStatementText: query.slice(
statementRange[0],
statementRange[1] + 1
),
};
}
);
Expand All @@ -158,39 +153,19 @@ function formatEachStatement(
language: string,
options: ISQLFormatOptions
) {
const safeSQLFormat = (text: string, unformattedText: string) => {
try {
return sqlFormat(text, {
tabWidth: options.tabWidth,
language: getLanguageForSqlFormatter(language),
useTabs: options.useTabs,
});
} catch (e) {
if (options.silent) {
return unformattedText;
} else {
throw e;
}
}
};

const formattedStatements: string[] = statements.map(
({
firstKeyWord,
statementText,
idToTemplateTag,
originalStatementText,
}) => {
return statements.map(
({ firstKeyWord, statementText, idToTemplateTag }) => {
// Use standard formatter to format
let formattedStatement = originalStatementText;
let formattedStatement = statementText;
if (
firstKeyWord &&
allowedStatement.has(firstKeyWord.text.toLocaleLowerCase())
) {
formattedStatement = safeSQLFormat(
statementText,
originalStatementText
);
formattedStatement = sqlFormat(statementText, {
tabWidth: options.tabWidth,
language: getLanguageForSqlFormatter(language),
useTabs: options.useTabs,
});
}

for (const [id, templateTag] of Object.entries(idToTemplateTag)) {
Expand All @@ -203,8 +178,6 @@ function formatEachStatement(
return formattedStatement;
}
);

return formattedStatements;
}

export function format(
Expand All @@ -223,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
Expand Down

0 comments on commit efdbd7b

Please sign in to comment.