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

Export language-specific format functions #484

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
export * from './sqlFormatter';
export {
format,
formatBigQuery,
formatDb2,
formatHive,
formatMariaDb,
formatMySql,
formatN1ql,
formatPlSql,
formatPostgreSql,
formatRedshift,
formatSpark,
formatSqlite,
formatStandardSQL,
formatTrino,
formatTransactSql,
formatSingleStoreDb,
formatSnowflake,
} from './sqlFormatter';
export type { SqlLanguage, FormatFn } from './sqlFormatter';
export type {
IndentStyle,
KeywordCase,
Expand Down
84 changes: 35 additions & 49 deletions src/sqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import TransactSqlFormatter from 'src/languages/transactsql/transactsql.formatte
import SingleStoreDbFormatter from './languages/singlestoredb/singlestoredb.formatter';
import SnowflakeFormatter from './languages/snowflake/snowflake.formatter';

import Formatter from './formatter/Formatter';
import { FormatOptions } from './FormatOptions';
import { ParamItems } from './formatter/Params';
import { ConfigError, validateConfig, validateQuery } from './validateConfigs';

export const formatters = {
const formatters = {
bigquery: BigQueryFormatter,
db2: Db2Formatter,
hive: HiveFormatter,
Expand Down Expand Up @@ -62,9 +63,11 @@ const defaultOptions: FormatOptions = {
* @param {FormatOptions} cfg Configuration options (see docs in README)
* @return {string} formatted query
*/
export const format = (query: string, cfg: Partial<FormatOptions> = {}): string => {
if (typeof query !== 'string') {
throw new Error('Invalid query argument. Expected string, instead got ' + typeof query);
export const format = (query: string, cfg: Partial<FormatOptions> = defaultOptions): string => {
validateQuery(query);

if (typeof cfg.language === 'string' && !supportedDialects.includes(cfg.language)) {
throw new ConfigError(`Unsupported SQL dialect: ${cfg.language}`);
}

const options = validateConfig({
Expand All @@ -78,49 +81,32 @@ export const format = (query: string, cfg: Partial<FormatOptions> = {}): string
return new FormatterCls(options).format(query);
};

export class ConfigError extends Error {}

function validateConfig(cfg: FormatOptions): FormatOptions {
if (typeof cfg.language === 'string' && !supportedDialects.includes(cfg.language)) {
throw new ConfigError(`Unsupported SQL dialect: ${cfg.language}`);
}

if ('multilineLists' in cfg) {
throw new ConfigError('multilineLists config is no more supported.');
}
if ('newlineBeforeOpenParen' in cfg) {
throw new ConfigError('newlineBeforeOpenParen config is no more supported.');
}
if ('newlineBeforeCloseParen' in cfg) {
throw new ConfigError('newlineBeforeCloseParen config is no more supported.');
}
if ('aliasAs' in cfg) {
throw new ConfigError('aliasAs config is no more supported.');
}

if (cfg.expressionWidth <= 0) {
throw new ConfigError(
`expressionWidth config must be positive number. Received ${cfg.expressionWidth} instead.`
);
}

if (cfg.commaPosition === 'before' && cfg.useTabs) {
throw new ConfigError(
'commaPosition: before does not work when tabs are used for indentation.'
);
}

if (cfg.params && !validateParams(cfg.params)) {
// eslint-disable-next-line no-console
console.warn('WARNING: All "params" option values should be strings.');
}

return cfg;
}

function validateParams(params: ParamItems | string[]): boolean {
const paramValues = params instanceof Array ? params : Object.values(params);
return paramValues.every(p => typeof p === 'string');
}
const languageFormat =
<Lang extends typeof Formatter>(FormatterCls: Lang) =>
(query: string, cfg: Partial<Exclude<FormatOptions, 'language'>> = {}) => {
validateQuery(query);
const options = validateConfig({
...defaultOptions,
...cfg,
});
return new FormatterCls(options).format(query);
};

export type FormatFn = typeof format;

export const formatBigQuery: FormatFn = languageFormat(BigQueryFormatter);
export const formatDb2: FormatFn = languageFormat(Db2Formatter);
export const formatHive: FormatFn = languageFormat(HiveFormatter);
export const formatMariaDb: FormatFn = languageFormat(MariaDbFormatter);
export const formatMySql: FormatFn = languageFormat(MySqlFormatter);
export const formatN1ql: FormatFn = languageFormat(N1qlFormatter);
export const formatPlSql: FormatFn = languageFormat(PlSqlFormatter);
export const formatPostgreSql: FormatFn = languageFormat(PostgreSqlFormatter);
export const formatRedshift: FormatFn = languageFormat(RedshiftFormatter);
export const formatSpark: FormatFn = languageFormat(SparkFormatter);
export const formatSqlite: FormatFn = languageFormat(SqliteFormatter);
export const formatStandardSQL: FormatFn = languageFormat(SqlFormatter);
export const formatTrino: FormatFn = languageFormat(TrinoFormatter);
export const formatTransactSql: FormatFn = languageFormat(TransactSqlFormatter);
export const formatSingleStoreDb: FormatFn = languageFormat(SingleStoreDbFormatter);
export const formatSnowflake: FormatFn = languageFormat(SnowflakeFormatter);
49 changes: 49 additions & 0 deletions src/validateConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FormatOptions } from './FormatOptions';
import { ParamItems } from './formatter/Params';

export class ConfigError extends Error {}

export const validateQuery = (query: string) => {
if (typeof query !== 'string') {
throw new Error('Invalid query argument. Expected string, instead got ' + typeof query);
}
};

export const validateConfig = (cfg: FormatOptions): FormatOptions => {
if ('multilineLists' in cfg) {
throw new ConfigError('multilineLists config is no more supported.');
}
if ('newlineBeforeOpenParen' in cfg) {
throw new ConfigError('newlineBeforeOpenParen config is no more supported.');
}
if ('newlineBeforeCloseParen' in cfg) {
throw new ConfigError('newlineBeforeCloseParen config is no more supported.');
}
if ('aliasAs' in cfg) {
throw new ConfigError('aliasAs config is no more supported.');
}

if (cfg.expressionWidth <= 0) {
throw new ConfigError(
`expressionWidth config must be positive number. Received ${cfg.expressionWidth} instead.`
);
}

if (cfg.commaPosition === 'before' && cfg.useTabs) {
throw new ConfigError(
'commaPosition: before does not work when tabs are used for indentation.'
);
}

if (cfg.params && !validateParams(cfg.params)) {
// eslint-disable-next-line no-console
console.warn('WARNING: All "params" option values should be strings.');
}

return cfg;
};

const validateParams = (params: ParamItems | string[]): boolean => {
const paramValues = params instanceof Array ? params : Object.values(params);
return paramValues.every(p => typeof p === 'string');
};