Skip to content

Commit 34baa64

Browse files
committed
Throw error when empty regex in custom param types
Fixes #754
1 parent 0ca99cc commit 34baa64

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/validateConfig.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FormatOptions } from './FormatOptions.js';
22
import { ParamItems } from './formatter/Params.js';
3+
import { ParamTypes } from './lexer/TokenizerOptions.js';
34

45
export class ConfigError extends Error {}
56

@@ -29,10 +30,23 @@ export function validateConfig(cfg: FormatOptions): FormatOptions {
2930
console.warn('WARNING: All "params" option values should be strings.');
3031
}
3132

33+
if (cfg.paramTypes && !validateParamTypes(cfg.paramTypes)) {
34+
throw new ConfigError(
35+
'Empty regex given in custom paramTypes. That would result in matching infinite amount of parameters.'
36+
);
37+
}
38+
3239
return cfg;
3340
}
3441

3542
function validateParams(params: ParamItems | string[]): boolean {
3643
const paramValues = params instanceof Array ? params : Object.values(params);
3744
return paramValues.every(p => typeof p === 'string');
3845
}
46+
47+
function validateParamTypes(paramTypes: ParamTypes): boolean {
48+
if (paramTypes.custom && Array.isArray(paramTypes.custom)) {
49+
return paramTypes.custom.every(p => p.regex !== '');
50+
}
51+
return true;
52+
}

test/options/paramTypes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,15 @@ export default function supportsParamTypes(format: FormatFn) {
110110
{schema}.{table}
111111
`);
112112
});
113+
114+
it('does not enter infinite loop when empty regex given', () => {
115+
expect(() =>
116+
format('SELECT foo FROM bar', {
117+
paramTypes: { custom: [{ regex: '' }] },
118+
})
119+
).toThrow(
120+
'Empty regex given in custom paramTypes. That would result in matching infinite amount of parameters.'
121+
);
122+
});
113123
});
114124
}

0 commit comments

Comments
 (0)