diff --git a/src/validateConfig.ts b/src/validateConfig.ts index fe5efa496a..8789fe63d7 100644 --- a/src/validateConfig.ts +++ b/src/validateConfig.ts @@ -1,5 +1,6 @@ import { FormatOptions } from './FormatOptions.js'; import { ParamItems } from './formatter/Params.js'; +import { ParamTypes } from './lexer/TokenizerOptions.js'; export class ConfigError extends Error {} @@ -29,6 +30,12 @@ export function validateConfig(cfg: FormatOptions): FormatOptions { console.warn('WARNING: All "params" option values should be strings.'); } + if (cfg.paramTypes && !validateParamTypes(cfg.paramTypes)) { + throw new ConfigError( + 'Empty regex given in custom paramTypes. That would result in matching infinite amount of parameters.' + ); + } + return cfg; } @@ -36,3 +43,10 @@ function validateParams(params: ParamItems | string[]): boolean { const paramValues = params instanceof Array ? params : Object.values(params); return paramValues.every(p => typeof p === 'string'); } + +function validateParamTypes(paramTypes: ParamTypes): boolean { + if (paramTypes.custom && Array.isArray(paramTypes.custom)) { + return paramTypes.custom.every(p => p.regex !== ''); + } + return true; +} diff --git a/test/options/paramTypes.ts b/test/options/paramTypes.ts index dd1fde49b9..c5e89a9329 100644 --- a/test/options/paramTypes.ts +++ b/test/options/paramTypes.ts @@ -110,5 +110,15 @@ export default function supportsParamTypes(format: FormatFn) { {schema}.{table} `); }); + + it('does not enter infinite loop when empty regex given', () => { + expect(() => + format('SELECT foo FROM bar', { + paramTypes: { custom: [{ regex: '' }] }, + }) + ).toThrow( + 'Empty regex given in custom paramTypes. That would result in matching infinite amount of parameters.' + ); + }); }); }