forked from babel/babel
/
credentials.ts
65 lines (59 loc) 路 1.65 KB
/
credentials.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
export const enum ParseErrorCode {
SyntaxError = "BABEL_PARSER_SYNTAX_ERROR",
SourceTypeModuleError = "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED",
}
export type SyntaxPlugin =
| "flow"
| "typescript"
| "jsx"
| "pipelineOperator"
| "placeholders";
export type ToMessage<ErrorDetails> = (self: ErrorDetails) => string;
export type ParseErrorCredentials<ErrorDetails> = {
code: ParseErrorCode;
reasonCode: string;
syntaxPlugin?: SyntaxPlugin;
toMessage: ToMessage<ErrorDetails>;
};
const reflect = (keys: string[], last = keys.length - 1) => ({
get(this: unknown): unknown {
return keys.reduce(
(object, key) =>
// @ts-expect-error key should index object
object[key],
this,
);
},
set(this: unknown, value: unknown) {
keys.reduce(
// @ts-expect-error key should index item
(item, key, i) => (i === last ? (item[key] = value) : item[key]),
this,
);
},
});
const instantiate = <T>(
constructor: new () => T,
properties: any,
descriptors: any,
) =>
Object.keys(descriptors)
.map(key => [key, descriptors[key]])
.filter(([, descriptor]) => !!descriptor)
.map(([key, descriptor]) => [
key,
typeof descriptor === "function"
? { value: descriptor, enumerable: false }
: typeof descriptor.reflect === "string"
? { ...descriptor, ...reflect(descriptor.reflect.split(".")) }
: descriptor,
])
.reduce(
(instance, [key, descriptor]) =>
Object.defineProperty(instance, key, {
configurable: true,
...descriptor,
}),
Object.assign(new constructor(), properties),
);
export { instantiate };