-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathvalidateConfig.ts
More file actions
51 lines (43 loc) · 1.53 KB
/
validateConfig.ts
File metadata and controls
51 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { FormatOptions } from './FormatOptions.js';
import { ParamItems } from './formatter/Params.js';
import { ParamTypes } from './lexer/TokenizerOptions.js';
export class ConfigError extends Error {}
export function validateConfig(cfg: FormatOptions): FormatOptions {
const removedOptions = [
'multilineLists',
'newlineBeforeOpenParen',
'newlineBeforeCloseParen',
'aliasAs',
'tabulateAlias',
];
for (const optionName of removedOptions) {
if (optionName in cfg) {
throw new ConfigError(`${optionName} config is no more supported.`);
}
}
if (cfg.expressionWidth <= 0) {
throw new ConfigError(
`expressionWidth config must be positive number. Received ${cfg.expressionWidth} instead.`
);
}
if (cfg.params && !validateParams(cfg.params)) {
// eslint-disable-next-line no-console
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;
}