Skip to content

Commit

Permalink
Throw error when empty regex in custom param types
Browse files Browse the repository at this point in the history
Fixes #754
  • Loading branch information
nene committed Jul 3, 2024
1 parent 0ca99cc commit 34baa64
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/validateConfig.ts
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down Expand Up @@ -29,10 +30,23 @@ 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;
}

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;
}
10 changes: 10 additions & 0 deletions test/options/paramTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
});
});
}

0 comments on commit 34baa64

Please sign in to comment.