-
Notifications
You must be signed in to change notification settings - Fork 0
/
trait.ts
363 lines (321 loc) · 14.1 KB
/
trait.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// #region Helper types
export type Ctor<T = Trait> = new (...args: any[]) => T;
type UnionToIntersection<U> =
(U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never;
type InstanceTypeIntersection<I extends Ctor[]> = UnionToIntersection<InstanceType<I[number]>>;
type TupleToIntersection<T extends any[]> = UnionToIntersection<T[number]>;
/**
* Fuse multiple class instance with the first one. The final constructor will always be from the first type.
*/
type MultipleClassInstance<T extends Ctor[]> = new (...args: any[]) => InstanceTypeIntersection<T>;
/**
* Combine multiple trait to a class. (with instance properties and static properties)
*/
type CombinedClass<Class extends Ctor, T extends Array<typeof Trait>> = MultipleClassInstance<[Class, ...T]> & Class & TupleToIntersection<T>;
/**
* The annotion return decorator function.
*/
type UseReturnType<T extends Array<typeof Trait>> = <C extends Ctor>(clazz: C) => CombinedClass<C, T>;
type _GetTraitName<T extends typeof Trait> = { [Name in keyof TraitsRegister]: TraitsRegister[Name] extends T ? T extends TraitsRegister[Name] ? Name : never : never}[keyof TraitsRegister];
type GetTraitName<T extends typeof Trait> = _GetTraitName<T> extends never ? string : _GetTraitName<T>;
/**
* Scopes are WIP
*/
export type Scope = 'public' | 'protected' | 'private';
export type StringKeys<T> = { [K in keyof T]: K extends string ? K extends "prototype" ? never : K : never}[keyof T];
type StaticName<T extends typeof Trait, N extends GetTraitName<T> = GetTraitName<T>> = `${N}::${StringKeys<T>}`; // eslint-disable-line prettier/prettier
type InstanceName<T extends typeof Trait, N extends GetTraitName<T> = GetTraitName<T>> = `${N}.${StringKeys<InstanceType<T>>}`;
type Identifier<Traits extends Array<typeof Trait>> = Traits extends Array<infer T> ? T extends typeof Trait ? StaticName<T> | InstanceName<T> : never : never;
type As<Traits extends Array<typeof Trait>, V extends string = string> =
Partial<Record<Identifier<Traits>, `${Scope} ${V}`>> // autocomplete "<class.prop>" and "<class.staticProp>" as key
| Partial<Record<string, `${Scope} ${V}`>> // accept any other string as key as well
| Partial<Record<string, `${Scope} `>>; // start autocomplete for optional scope
type InsteadOf<Traits extends Array<typeof Trait>, Ids extends Identifier<Traits> = Identifier<Traits>> = {
[K in Ids]?: K extends `${infer N}${'::'|'.'}${infer _}` ? N extends keyof TraitsRegister ? Array<Exclude<Traits[number], TraitsRegister[N]>> : never : never // in string key, if a trait name is found, exclude corresponding Trait from "insteadof" possibilities to avoid redundancy
} | Partial<Record<string, Array<Traits[number]>>>; // accept any other string a key (case when Trait is not registered)
// #endregion
/**
* Should be used with {{Use}}
*/
export class Trait {
private static __TRAITED: never;
}
/**
* Register to augment to have proper autocomplete for Traits.
*
* Usage:
* ```typescript
* class MyTrait extends Trait {}
*
* declare module "@bios21/tstrait" {
* interface TraitsRegister {
* "MyTrait": typeof MyTrait,
* }
* }
* // "MyTrait" is now available for autocomplete in any TraitConfig
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TraitsRegister {}
export interface TraitConfig<Traits extends Array<typeof Trait>> {
insteadOf?: InsteadOf<Traits>;
as?: As<Traits>;
}
export function isTraitConfig<Traits extends Array<typeof Trait>>(obj: unknown): obj is TraitConfig<Traits> {
return typeof obj === "object" && obj !== null && (
("insteadOf" in obj &&
typeof (obj as TraitConfig<Traits>).insteadOf === "object")
|| ("as" in obj &&
typeof (obj as TraitConfig<Traits>).as === "object")
);
}
/**
* Produce a token depending of the property/method selected.
* e.g.
* ```ts
* class Trait {
* public static STATIC_METHOD; // Will produce this token: `Trait::STATIC_METHOD`
* public instanceProp; // Will produce this token: `Trait.instanceProp`
* }
* ```
*
* @param trait The trait in which the member name will be selected
* @param name The member name itself. Intellisense will provide you a correct list to avoid mistakes
* @param staticMember If the member to select is static.
*/
export function traitSelector<
T extends typeof Trait,
NAME extends STATIC extends true ? keyof Omit<T, 'prototype'> : keyof InstanceType<T>,
STATIC extends boolean = false,
N extends GetTraitName<T> = GetTraitName<T>,
>(trait: T, name: NAME, staticMember?: STATIC): `${N}${STATIC extends true ? "::" : "."}${NAME & string}` {
const traitName = trait.name;
if (staticMember) {
if (((name as unknown) as string) in trait) return `${traitName}::${name}` as any;
throw new Error(
`Error on TraitSelector. Static member "${name}" was not found in trait "${traitName}", or is not instanciated, or is not public.`,
);
}
if (((name as unknown) as string) in trait.prototype) return `${traitName}.${name}` as any;
throw new Error(`Error on TraitSelector. Member "${name}" was not found in trait "${traitName}" or is not public.`);
}
interface AsRule {
rule: 'as';
klass: string;
oldName: string;
newName: string;
isStatic: boolean;
scope: Scope;
}
interface InsteadOfRule {
rule: 'insteadof';
klassFrom: string;
klassTo: string[];
name: string;
isStatic: boolean;
}
type TraitRules = Array<AsRule | InsteadOfRule>;
const SCOPES = ['public', 'protected', 'private'];
const NAME_REGEX = /^([a-zA-Z_]\w*)*$/i;
function isScope(scope: string): scope is Scope {
return SCOPES.includes(scope);
}
/**
* Validate and parse the TraitConfig.
*
* @internal
*/
function traitConfigParser<Traits extends Array<typeof Trait>, TC extends TraitConfig<Traits>>(config: TC, traits: Traits, target: Ctor) {
const ret: TraitRules = [];
function selectorParser(selector: string): [string, string, boolean] {
const isStatic = selector.includes('::');
const parts = selector.split(isStatic ? '::' : '.');
if (parts.length !== 2) {
throw new Error(
`The selector "${selector}" is malformed and should be either "<class>.<propOrMethod>" or "<class>::<staticPropOrMethod>".`,
);
}
const [klass, name] = parts;
const t = traits.filter(trait => trait.name === klass)[0];
if (isStatic && !t[name]) {
throw new Error(
`In selector "${selector}", the static property or method "${name}" was not found on the trait "${klass}".`,
);
} else if (!isStatic && !t.prototype[name]) {
throw new Error(
`In selector "${selector}", the property or method "${name}" was not found on the trait "${klass}" prototype.`,
);
}
return [klass, name, isStatic];
}
function changeAsParser(changeAs: string): [scope: Scope, name: string | null] {
const parts = changeAs.split(' ');
let scope: Scope = null!;
const part = parts[0];
let name: string | null = parts[1];
if (parts.length === 1) {
// if only one part is found, can be a scope or a name
if (isScope(part)) {
scope = part;
name = null;
} else {
name = part;
}
} else if (parts.length === 2) {
// if two parts are found, will be `<scope> <name>`
if (!isScope(part)) {
throw new Error(
`In the "as"-part "${changeAs}", the scope have to be one of these: ${SCOPES.join(', ')}`,
);
}
scope = part;
} else {
throw new Error(
`The "as"-part "${changeAs}" is not valid and should be like "<scope> <name>", or "<scope>", or "<name>" instead. (e.g. "public myProp")`,
);
}
if (name !== null && !NAME_REGEX.test(name)) {
throw new Error(
`In the "as"-part "${changeAs}", the name "${name}" is not a valid property not method name. It should respect the following regex: "${NAME_REGEX}"`,
);
}
return [scope, name];
}
// handle 'InsteadOf'
for (const [selector, ioTraits] of Object.entries(config.insteadOf ?? {}) as Array<[string, Traits]>) {
const [klassFrom, name, isStatic] = selectorParser(selector);
ret.push({
isStatic,
klassFrom,
name,
klassTo: ioTraits.map(t => t.name),
rule: 'insteadof',
});
}
// handle 'As'
for (const [selector, changeAs] of Object.entries(config.as ?? {}) as Array<[string, string]>) {
const [klass, oldName, isStatic] = selectorParser(selector);
const [scope, newName] = changeAsParser(changeAs);
if (newName === null) continue; // WIP Handle scope
const traitForName = traits.filter(t => t.name === klass)[0];
if (isStatic) {
if (typeof traitForName[newName] !== 'undefined') {
throw new Error(
`Collision on "as" trait rule. "${klass}.${newName}" (static) already exists and can't be overlap.`,
);
} else if (typeof target[newName] !== 'undefined') {
throw new Error(
`Collision on "as" trait rule. "${
target.name
}.${newName}" (static) already exists and can't be overlap.`,
);
}
} else {
if (typeof traitForName.prototype[newName] !== 'undefined') {
throw new Error(
`Collision on "as" trait rule. "${klass}.${newName}" already exists and can't be overlap.`,
);
} else if (typeof target.prototype[newName] !== 'undefined') {
throw new Error(
`Collision on "as" trait rule. "${target.name}.${newName}" already exists and can't be overlap.`,
);
}
}
ret.push({
isStatic,
klass,
newName,
oldName,
scope,
rule: 'as',
});
}
return ret;
}
/**
* Implements the "use" keyword from PHP with a `Trait`.
*
* @param traits The trait implementation to use or an array of trais followed by a trait config at the last element.
* @returns The class "traited"
*
* @annotation
*/
export function Use<T extends Array<typeof Trait>>(...traits: [...trait: T]): UseReturnType<T>;
// eslint-disable-next-line no-redeclare
export function Use<T extends Array<typeof Trait>>(...traits: [...trait: T, traitConfig?: TraitConfig<T>]): UseReturnType<T>;
// eslint-disable-next-line no-redeclare
export function Use<T extends Array<typeof Trait>>(...traits: [...trait: T] | [...trait: T, traitConfig?: TraitConfig<T>]): UseReturnType<T> {
return <Class extends Ctor>(clazz: Class) => {
let traitConfig = traits.pop() as TraitConfig<T>;
const restTraits = (traits as unknown as T);
if (!isTraitConfig<T>(traitConfig)) {
restTraits.push(traitConfig)
traitConfig = {}
}
const traitRules = traitConfigParser(traitConfig, restTraits, clazz);
const doNotUse: { [className: string]: Array<[string, boolean]> } = {}; // className [ [prop, isStatic] ]
const aliases: { [className: string]: Array<[string, string, boolean]> } = {}; // className [ [prop, alias, isStatic] ]
for (const rule of traitRules) {
if (rule.rule === 'insteadof') {
for (const to of rule.klassTo) {
if (!doNotUse[to]) {
doNotUse[to] = [];
}
doNotUse[to].push([rule.name, rule.isStatic]);
}
} else {
// TODO: handle scope
if (!aliases[rule.klass]) {
aliases[rule.klass] = [];
}
aliases[rule.klass].push([rule.oldName, rule.newName, rule.isStatic]);
}
}
for (const trait of restTraits) {
const filters = [copyProperties.DEFAULT_FILTER];
const filtersStatic = [copyProperties.DEFAULT_FILTER];
const dnu = doNotUse[trait.name] ?? [];
for (const [name, isStatic] of dnu) {
(isStatic ? filtersStatic : filters).push(new RegExp(name));
}
const traitClone = {};
const traitProtoClone = {};
copyProperties(traitClone, trait);
copyProperties(traitProtoClone, trait.prototype);
const al = aliases[trait.name] ?? [];
for (const [member, alias, isStatic] of al) {
if (isStatic) {
traitClone[alias] = traitClone[member];
delete traitClone[member];
} else {
traitProtoClone[alias] = traitProtoClone[member];
delete traitProtoClone[member];
}
}
copyProperties(clazz, al.length ? traitClone : trait, filtersStatic);
copyProperties(clazz.prototype, al.length ? traitProtoClone : trait.prototype, filters);
}
return clazz as CombinedClass<Class, T>;
};
}
/**
* Copy param from a `source` into the `target`.
*
* Used to properly mix class with prototypes and statics members.
*
* @param filters An array of regexp to omit some members to be copied. (/(prototype|name|constructor)/ by default).
*/
export function copyProperties<T1, T2>(target: T1, source: T2, filters = [copyProperties.DEFAULT_FILTER]) {
const ownPropertyNames = Object.getOwnPropertyNames(source);
ownPropertyNames
.filter(key => filters.every(f => !f.test(key)) && typeof target[key] === 'undefined')
.forEach(key => {
const desc = Object.getOwnPropertyDescriptor(source, key);
try {
Object.defineProperty(target, key, desc!);
} catch {
/* catch for IE11 - no op */
}
});
}
copyProperties.DEFAULT_FILTER = /(prototype|name|constructor)/;