File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1
1
import { FormatOptions } from './FormatOptions.js' ;
2
2
import { ParamItems } from './formatter/Params.js' ;
3
+ import { ParamTypes } from './lexer/TokenizerOptions.js' ;
3
4
4
5
export class ConfigError extends Error { }
5
6
@@ -29,10 +30,23 @@ export function validateConfig(cfg: FormatOptions): FormatOptions {
29
30
console . warn ( 'WARNING: All "params" option values should be strings.' ) ;
30
31
}
31
32
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
+
32
39
return cfg ;
33
40
}
34
41
35
42
function validateParams ( params : ParamItems | string [ ] ) : boolean {
36
43
const paramValues = params instanceof Array ? params : Object . values ( params ) ;
37
44
return paramValues . every ( p => typeof p === 'string' ) ;
38
45
}
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
+ }
Original file line number Diff line number Diff line change @@ -110,5 +110,15 @@ export default function supportsParamTypes(format: FormatFn) {
110
110
{schema}.{table}
111
111
` ) ;
112
112
} ) ;
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
+ } ) ;
113
123
} ) ;
114
124
}
You can’t perform that action at this time.
0 commit comments