-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.ts
228 lines (209 loc) · 8.74 KB
/
plugins.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
export interface PluginRecord<
Args extends unknown[] = unknown[],
Info extends unknown = unknown,
Signature extends unknown = unknown
> {
/**
* Arguments to require when translating a key that matches this plugin
*
* Should be a named tuple
*/
args: Args;
/**
* Miscellanious information that the plugin uses
*
* This is a generic type that is then used as a type guard in PluginRegistry evaluation
*/
info?: Info;
/**
* This is displayed in a type hint when the user hovers over the translation function invocation
*
* Allows to display any important information (for example, original key signature) to the user
*/
signature?: Signature;
}
/**
* Opt-in global plugin registry,
* tracks all plugins included throughout the project to simplify type-checking
*/
export interface PluginRegistry<
LocaleDoc extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<LocaleDoc> = LocaleKey<LocaleDoc>,
PluginInfo = unknown,
ContextualPlugins extends Record<keyof PluginRegistry, Plugin> = Record<string, Plugin>
> {
[name: string]: PluginRecord;
}
/**
* An interface that other plugins use
* to reference another plugin in their code
*/
export interface PluginInterface<
LocaleDoc extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<LocaleDoc> = LocaleKey<LocaleDoc>,
Name extends keyof PluginRegistry<LocaleDoc> = keyof PluginRegistry<LocaleDoc>,
> {
translate(key: LocaleKey<LocaleDoc>, ...args: PluginRegistry<LocaleDoc, Key>[Name]['args']): string | undefined;
match(value: unknown, key: string, doc: Record<string, unknown>): boolean;
info: Exclude<PluginRegistry<LocaleDoc, Key>[Name]['info'], undefined>;
}
/**
* Context of the plugin's `translate` function
*/
export interface PluginContext<
Match = any,
LocaleDoc extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<LocaleDoc> = LocaleKey<LocaleDoc>,
Name extends keyof PluginRegistry = string,
> {
name: Name;
key: Key;
value: Match;
doc: LocaleDoc;
originalCallArgs: unknown[];
originalKey: LocaleKey<LocaleDoc>;
originalValue: unknown;
translate(key: LocaleKey<LocaleDoc>, ...args: unknown[]): string;
plugins: {
[name in keyof PluginRegistry<LocaleDoc, Key>]?: PluginInterface<LocaleDoc, Key, name>;
};
}
export interface Plugin<
Match = any,
Args extends any[] = any,
Name extends keyof PluginRegistry = string,
PluginInfo = unknown,
LocaleDoc extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<LocaleDoc> = LocaleKey<LocaleDoc>,
> {
name: Name;
info: PluginInfo;
match(value: unknown, key: Key, doc: LocaleDoc): value is Match;
translate(this: PluginContext<Match, LocaleDoc, Key, Name>, ...args: Args): string | undefined;
}
/**
* A plugin factory, mostly used for type-checking
*
* @param name A name for the plugin, will be used as a global plugin registry shortcut for other plugins,
* must match with the name used in the plugin registry definition
*
* @param match A {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates type predicate}
* that decides whether or not the plugin should be used on a specific key-value pair
*
* @param options Allows to define the functionality of a plugin. It can do 2 things:
* 1. Provide info and context to other plugins, using the `info` property
* 2. Provide additional ways of translating a key-value pair from a translation document, using the `translate` method
*
* @returns a ready-to-use plugin
*/
export const createPlugin: {
/**
* A plugin factory, mostly used for type-checking
*
* @param name A name for the plugin, will be used as a global plugin registry shortcut for other plugins,
* must match with the name used in the plugin registry definition
*
* @param match A {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates type predicate}
* that decides whether or not the plugin should be used on a specific key-value pair
*
* @param options Allows to define the functionality of a plugin. It can do 2 things:
* 1. Provide info and context to other plugins, using the `info` property
* 2. Provide additional ways of translating a key-value pair from a translation document, using the `translate` method
*
* @returns a ready-to-use plugin
*/
<Name extends keyof PluginRegistry, Match, PluginInfo, Args extends any[] = PluginRegistry[Name]['args']>(
name: Name,
match: (value: unknown) => value is Match,
options: {
info?: PluginInfo,
translate?: (this: PluginContext<Match>, ...args: Args) => string | undefined,
}
): Plugin<Match, Args, Name, PluginInfo>;
/**
* A plugin factory, mostly used for type-checking
*
* @param name A name for the plugin, will be used as a global plugin registry shortcut for other plugins,
* must match with the name used in the plugin registry definition
*
* @param match A {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates type predicate}
* that decides whether or not the plugin should be used on a specific key-value pair
*
* @param options Allows to define the functionality of a plugin.
* It can provide additional ways of translating a key-value pair
* from a translation document, using the `translate` method
*
* @returns a ready-to-use plugin
*/
<Match, Args extends any[]>(
name: string,
match: (value: unknown) => value is Match,
options: {
translate?: (this: PluginContext<Match>, ...args: Args) => string | undefined,
}
): Plugin<Match, Args>;
} = <Match, Args extends any[]>(
name: string,
match: (value: unknown) => value is Match,
options: {
info?: unknown,
translate?: (this: PluginContext<Match>, ...args: Args) => string | undefined,
}
): Plugin<Match, Args> => ({ name, match, translate: options.translate ?? (() => undefined), info: options.info });
export interface TranslationDocument {
[key: string]: unknown;
}
export type LocaleKey<LocaleDoc extends TranslationDocument> = Exclude<keyof LocaleDoc, '$schema'>;
export type PMatch<P extends readonly Plugin[]> = (
[] extends P ? never : P extends readonly Plugin<infer Match, any>[] ? Match : never
);
type NamePerPlugin<P extends readonly Plugin[]> = {
[key in keyof P]: P[key] extends Plugin<any, any, infer Name> ? Name : never;
};
export type MatchPerPlugin<P extends readonly Plugin[], Names extends Record<number, keyof PluginRegistry> = NamePerPlugin<P>> = {
[key in keyof Names & keyof P & `${number}` as Names[key]]: P[key] extends Plugin<infer Match, any> ? Match : never;
};
export type MatchPerPluginName<P extends Record<string, Plugin>> = {
[key in keyof P]: P[key] extends Plugin<infer Match, any> ? Match : never;
};
export type InfoPerPlugin<P extends readonly Plugin[], Names extends Record<number, keyof PluginRegistry> = NamePerPlugin<P>> = {
[key in keyof Names & keyof P & `${number}` as Names[key]]: P[key] extends Plugin<any, any, any, infer Info> ? Info : never;
};
export type PluginPerPlugin<P extends readonly Plugin[], Names extends Record<number, keyof PluginRegistry> = NamePerPlugin<P>> = {
[key in Extract<keyof Names & keyof P & `${number}`, keyof PluginRegistry> as Names[key]]: P[key] extends Plugin ? P[key] : never;
};
export type KeysOfType<O, T> = {
[K in keyof O]: T extends O[K] ? K : never
}[keyof O];
export type TypeOfKeys<O, T> = {
[K in keyof O]: O[K] extends T ? K : never
}[keyof O];
/**
* Gets a name of a plugin that processes a specific key
* from the initial plugin array
*/
export type GetPluginNameFromArray<
LocaleDoc extends Record<string, any>,
K extends LocaleKey<LocaleDoc>,
MatchPerP extends Record<keyof PluginRegistry, any>
> = KeysOfType<MatchPerP, LocaleDoc[K]> & string;
/**
* Gets information on a plugin that processes a specific key
* from the plugin registry
*/
export type GetPluginFromArray<
LocaleDoc extends Record<string, any>,
K extends LocaleKey<LocaleDoc>,
PluginKey extends keyof PluginRegistry,
PluginsInfo extends Record<keyof PluginRegistry, any>,
Plugins extends Record<keyof PluginRegistry, any>,
> = PluginRegistry<LocaleDoc, K, PluginsInfo[PluginKey], Plugins>[PluginKey] extends infer R extends PluginRecord ? R : never;
/**
* Gets a name of a plugin that processes a specific key
* from a plugin context
*/
export type GetPluginNameFromContext<
LocaleDoc extends Record<string, any>,
Key extends LocaleKey<LocaleDoc>,
ContextualPlugins extends Record<string, Plugin>
> = KeysOfType<MatchPerPluginName<ContextualPlugins>, LocaleDoc[Key]>;