Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid referencing allDialects outside of function calls #517

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/sqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ import { createDialect, DialectOptions } from './dialect.js';
import Formatter from './formatter/Formatter.js';
import { ConfigError, validateConfig } from './validateConfig.js';

const formatters = {
...allDialects,
tsql: allDialects.transactsql, // alias for transactsql
const dialectNameMap: Record<string, keyof typeof allDialects> = {
bigquery: 'bigquery',
db2: 'db2',
hive: 'hive',
mariadb: 'mariadb',
mysql: 'mysql',
n1ql: 'n1ql',
plsql: 'plsql',
postgresql: 'postgresql',
redshift: 'redshift',
spark: 'spark',
sqlite: 'sqlite',
sql: 'sql',
trino: 'trino',
transactsql: 'transactsql',
tsql: 'transactsql', // alias for transactsq
singlestoredb: 'singlestoredb',
snowflake: 'snowflake',
};
export type SqlLanguage = keyof typeof formatters;
export const supportedDialects = Object.keys(formatters);

export const supportedDialects = Object.keys(dialectNameMap);
export type SqlLanguage = keyof typeof dialectNameMap;

export type FormatOptionsWithLanguage = Partial<FormatOptions> & {
language?: SqlLanguage;
Expand Down Expand Up @@ -46,9 +62,11 @@ export const format = (query: string, cfg: FormatOptionsWithLanguage = {}): stri
throw new ConfigError(`Unsupported SQL dialect: ${cfg.language}`);
}

const canonicalDialectName = dialectNameMap[cfg.language || 'sql'];

return formatDialect(query, {
...cfg,
dialect: formatters[cfg.language || 'sql'],
dialect: allDialects[canonicalDialectName],
});
};

Expand Down