-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
84 lines (71 loc) · 2.26 KB
/
types.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type {SimpleCallExpression, VariableDeclarator} from 'estree';
export type ArrayPluralValue = string[];
export type ObjectPluralValue = {
zero?: string;
one?: string;
two?: string;
few?: string;
many?: string;
other?: string;
};
export type KeyValue = string | ArrayPluralValue | ObjectPluralValue;
export type Keyset = Record<string, KeyValue>;
export type LocaleData = Record<string, KeyValue | Keyset>;
export type ImportResolver = (
source: string,
exportName: string,
identifierName: string,
module: string,
) =>
| {
resolved: boolean;
keyset?: string;
}
| undefined;
export type DeclarationResolver = (
node: VariableDeclarator,
module: string,
) =>
| {
functionName: string;
keyset?: string;
}
| undefined;
export interface ReplacerContext {
resolveKey(key: string, keyset?: string): KeyValue | undefined;
localeData: LocaleData;
}
export type ReplacerArgs = {
callNode: SimpleCallExpression;
callArguments: string[];
localeName: string;
key: string;
keyset?: string;
};
export type ReplacerResult = string | {value: string; key: string; keyset?: string};
export type Replacer = (this: ReplacerContext, args: ReplacerArgs) => ReplacerResult;
export type Options = {
locales: Record<string, LocaleData>;
importResolver?: ImportResolver;
declarationResolver?: DeclarationResolver;
replacer?: Replacer;
collectUnusedKeys?: boolean;
};
export function validateOptions(options: Options) {
if (!options) {
throw new Error('Options are required');
}
if (!options.locales) {
throw new Error('Locales are required');
}
if (Object.keys(options.locales).length === 0) {
throw new Error('locales must contain at least one locale');
}
const customFunctions = [options.declarationResolver, options.importResolver, options.replacer];
const enabledCustomFunctionsCount = customFunctions.filter(Boolean).length;
if (enabledCustomFunctionsCount && enabledCustomFunctionsCount !== customFunctions.length) {
throw new Error(
'If you use custom functions, you need to pass all these functions: declarationResolver, importResolver, replacer.',
);
}
}